SlideShare a Scribd company logo
1 of 52
Download to read offline
Orthogonal Functional
Architecture
Lambda Squared - Knoxville, TN
John A. De Goes — @jdegoes
http://degoes.net
Introduction
The Dysfunctional Nightmare abandon all
hope ye who
enter
Procedural Code
Ad hoc solutions constructed from large number of non-composable effects that
are reasoned about non-locally.
The Functional Dream bliss awaits all
ye who enter
Functional Code
Principled solutions constructed from a small number of composable building
blocks that are reasoned about locally.
Two Keys to Bliss
Orthogonality + Composability
Two Keys to Bliss
Composability makes functional
code powerful, and
orthogonality makes it
beautiful*
*i.e. modular and uncluttered by irrelevant details.
Two Keys to Bliss
Composable, orthogonal bases
tend to be powerful, but small
and simple to reason about,
permitting flexible, modular
solutions to many problems.
Composability
Composability
public interface Future<V> {
boolean cancel(boolean mayInterruptIfRunning);
boolean isDone();
V get() throws InterruptedException, ExecutionException;
V get(long timeout, TimeUnit unit) throws
InterruptedException, ExecutionException, TimeoutException;
}
Is this Java Future composable?
Composability
Composability measures the
extent to which values can be
combined with other values to
produce like values
Composability
1 + 1 = 2
Two integers combine to yield another integer.
1 – 1 = 0
Integers are composable with respect to addition/subtraction.
Composability
Non-Composable Composable
Composability
public interface Future<V> {
boolean cancel(boolean mayInterruptIfRunning);
boolean isDone();
V get() throws InterruptedException, ExecutionException;
V get(long timeout, TimeUnit unit) throws
InterruptedException, ExecutionException, TimeoutException;
}
Is this Java Future composable?
Orthogonality
Orthogonality
public interface Future<V> {
boolean cancel(boolean mayInterruptIfRunning);
boolean isDone();
V get() throws InterruptedException, ExecutionException;
V get(long timeout, TimeUnit unit) throws
InterruptedException, ExecutionException, TimeoutException;
}
Is this Java Future orthogonal?
Orthogonality
Orthogonality measures the
extent to which primitive
operations on values have
single, unique concerns
Orthogonality
Addition moves right
Addition/subtraction are orthogonal
Subtraction moves left
Orthogonality
A
A + B B
A
Non-Orthogonal Orthogonal
B
Orthogonality
public interface Future<V> {
boolean cancel(boolean mayInterruptIfRunning);
boolean isDone();
V get() throws InterruptedException, ExecutionException;
V get(long timeout, TimeUnit unit) throws
InterruptedException, ExecutionException, TimeoutException;
}
Is this orthogonal?
Orthogonality
public interface Future<V> {
boolean cancel(
boolean mayInterruptIfRunning);
boolean isDone();
V get() throws
InterruptedException,
ExecutionException;
V get(long timeout, TimeUnit unit)
throws
InterruptedException,
ExecutionException,
TimeoutException;
}
Is this orthogonal?
Timeout
Get
Timeout + Get
Orthogonality
The cardinal sin of
non-orthogonality is the
tangling of separate concerns,
which infects the code base to
destroy modularity.
Orthogonality
Process
Steps Toward Orthogonality
1. Make the system composable.
Steps Toward Orthogonality
2. Identify the primitive
operations.
Steps Toward Orthogonality
3. Identify the unique concerns.
Steps Toward Orthogonality
4. Refactor the primitive
operations until each has a
unique concern.
Worked Examples
Worked Examples
public interface Future<V> {
boolean cancel(boolean mayInterruptIfRunning);
boolean isDone();
V get() throws InterruptedException, ExecutionException;
V get(long timeout, TimeUnit unit) throws InterruptedException,ExecutionException,
TimeoutException;
}
Is this orthogonal?
Worked Examples
The curse of non-orthogonality!
// Would like to write:
<A> Future<A> retryUntilSuccess(Future<A> future, Interval spacing) {
// ???
}
// Forced to write:
A retryUntilSuccess(Future<A> future, Interval spacing,
long timeout, TimeUnit unit) {
// ...
}
Worked Examples
public interface Future<V> {
Future<Tuple2<Future<V>, Function<Boolean, Future<Unit>>>> fork();
V unsafePerformGet() throws InterruptedException, ExecutionException;
Future<V> timeout(long timeout, TimeUnit unit);
}
Future 2.0: Detangle ‘get’ and ‘timeout’
Worked Examples
Is this orthogonal?
static int compare(
String str1,
String str2,
boolean nullIsLess)
Worked Examples
Is this orthogonal?
NullIsLess +
Comparison
NullIsLess
Comparison
static int compare(
String str1,
String str2,
boolean nullIsLess)
Worked Examples
The curse of non-orthogonality!
List<String> myAlgorithm(List<String> list, boolean nullIsFirst) {
// ...
int c = compare(first, second, nullIsFirst);
// ...
}
enum Ordering { LT, EQ, GT }
interface Ord<A> {
Ordering compare(A l, A r);
}
Worked Examples
1. Define a unit of composition
class StringOrd extends Ord<String> {
public Ordering compare(String l, String r) {
// ...
}
}
Worked Examples
2. Define one dimension (string comparison)
class NullIsLess<A> {
private final Ord<A> ord;
public NullIsLess(Ord<A> ord) {
this.ord = ord;
}
public Ordering compare(A l, A r) {
if (l == null) { if (r == null) return EQ; else return LT; }
else if (r == null) return GT;
else return ord.compare(l, r);
}
}
Worked Examples
3. Define another dimension (null is first)
List<String> myAlgorithm(List<String> list, Ord<String> ord) {
// ...
Ordering c = ord.compare(first, second); // <- Beautiful!!!
// ...
}
Ord<String> ord = new NullIsLess<String>(new StringOrd());
List<String> list2 = myAlgorithm(list, ord);
Worked Examples
4. Compose orthogonal components
Worked Examples
Is this orthogonal?*
data MVar a
putMVar :: MVar a -> a -> IO ()
takeMVar :: MVar a -> IO a
*Thanks to Fabio the fabulous for this example.
Worked Examples
Is this orthogonal?
data MVar a
putMVar :: MVar a -> a -> IO ()
takeMVar :: MVar a -> IO a
Synchronization +
Concurrency
Synchronization
Concurrency
Worked Examples
data IORef a
newtype Expect a = Expect a
modify ::
IORef a -> (a -> a) -> IO Bool
data Promise a
newPromise :: IO (Promise a, a -> IO ())
awaitPromise :: Promise a -> IO a
Synchronization
(IORef)
Concurrency
(Promise)
Worked Examples
Is this orthogonal?*
*A tiny part of Apache String Utils.
Worked Examples
Is this orthogonal?
?
?
Worked Examples
Is this orthogonal?
data Parser a = Parser (String -> Either String (String, a))
char :: Parser Char
fail :: String -> Parser a
alt :: Parser a -> Parser a -> Parser a
seq :: Parser a -> (a -> Parser b) -> Parser b
pure :: a -> Parser a
map :: (a -> b) -> Parser a -> Parser b
Worked Examples
Is this orthogonal?*
data Parser a = Parser (String -> Either String (String, a))
char :: Parser Char
fail :: String -> Parser a
alt :: Parser a -> Parser a -> Parser a
seq :: Parser a -> (a -> Parser b) -> Parser b
pure :: a -> Parser a
map :: (a -> b) -> Parser a -> Parser b
*Trick question — or is it?
Worked Examples
Is this orthogonal?
data Parser a = Parser (String -> ...
char :: Parser Char
fail :: String -> Parser a
alt :: Parser a -> Parser a -> Parser a
seq :: Parser a -> (a -> Parser b) -> Parser b
pure :: a -> Parser a
map :: (a -> b) -> Parser a -> Parser b
SequencingMapping
Flattening
Worked Examples
Is this orthogonal?
data Parser a = Parser (String -> ...
char :: Parser Char
fail :: String -> Parser a
alt :: Parser a -> Parser a -> Parser a
join :: Parser (Parser a) -> Parser a
pure :: a -> Parser a
map :: (a -> b) -> Parser a -> Parser b
Mapping
Flattening
Wrap
Summary
1. Functional code is composable and orthogonal, allowing a small set of
principled building blocks to snap together to solve complex problems in
a predictable, reasonable way.
2. Composability measures the combinability of values.
3. Orthogonality measures the singular focus of primitive operations.
4. Refactor to orthogonality to obtain modular clode, uncluttered by
irrelevant details.
Thank You!
Special thanks to Reid, Cameron, Emily, & the wonderful Knoxville FP
community, and the generous sponsor ResultStack!
John A. De Goes — @jdegoes
http://degoes.net

More Related Content

What's hot

What's hot (20)

Linear differential equation of second order
Linear differential equation of second orderLinear differential equation of second order
Linear differential equation of second order
 
Control structures in C++ Programming Language
Control structures in C++ Programming LanguageControl structures in C++ Programming Language
Control structures in C++ Programming Language
 
Searching methodologies
Searching methodologiesSearching methodologies
Searching methodologies
 
TOPOLOGY and TYPES OF TOPOLOGY PowerPoint
TOPOLOGY and TYPES OF TOPOLOGY PowerPointTOPOLOGY and TYPES OF TOPOLOGY PowerPoint
TOPOLOGY and TYPES OF TOPOLOGY PowerPoint
 
Asymptotes and holes 97
Asymptotes and holes 97Asymptotes and holes 97
Asymptotes and holes 97
 
Calculus
CalculusCalculus
Calculus
 
Predicate Logic
Predicate LogicPredicate Logic
Predicate Logic
 
5.5 Injective and surjective functions. Dynamic slides.
5.5 Injective and surjective functions. Dynamic slides.5.5 Injective and surjective functions. Dynamic slides.
5.5 Injective and surjective functions. Dynamic slides.
 
Ai lecture 10(unit03)
Ai lecture  10(unit03)Ai lecture  10(unit03)
Ai lecture 10(unit03)
 
02 problem solving_search_control
02 problem solving_search_control02 problem solving_search_control
02 problem solving_search_control
 
Recursion
RecursionRecursion
Recursion
 
AI: Logic in AI
AI: Logic in AIAI: Logic in AI
AI: Logic in AI
 
Markov chain
Markov chainMarkov chain
Markov chain
 
Evaluating functions basic rules (day 2)
Evaluating functions   basic rules (day 2)Evaluating functions   basic rules (day 2)
Evaluating functions basic rules (day 2)
 
Newton's forward & backward interpolation
Newton's forward & backward interpolationNewton's forward & backward interpolation
Newton's forward & backward interpolation
 
Boolean algebra
Boolean algebraBoolean algebra
Boolean algebra
 
Algorithm
AlgorithmAlgorithm
Algorithm
 
Generating function
Generating functionGenerating function
Generating function
 
AI Greedy and A-STAR Search
AI Greedy and A-STAR SearchAI Greedy and A-STAR Search
AI Greedy and A-STAR Search
 
Mean Value Theorems
Mean Value TheoremsMean Value Theorems
Mean Value Theorems
 

Similar to Orthogonal Functional Architecture

Consider this code using the ArrayBag of Section 5.2 and the Locat.docx
Consider this code using the ArrayBag of Section 5.2 and the Locat.docxConsider this code using the ArrayBag of Section 5.2 and the Locat.docx
Consider this code using the ArrayBag of Section 5.2 and the Locat.docxmaxinesmith73660
 
FP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyondFP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyondMario Fusco
 
New Functional Features of Java 8
New Functional Features of Java 8New Functional Features of Java 8
New Functional Features of Java 8franciscoortin
 
If You Think You Can Stay Away from Functional Programming, You Are Wrong
If You Think You Can Stay Away from Functional Programming, You Are WrongIf You Think You Can Stay Away from Functional Programming, You Are Wrong
If You Think You Can Stay Away from Functional Programming, You Are WrongMario Fusco
 
The things we don't see – stories of Software, Scala and Akka
The things we don't see – stories of Software, Scala and AkkaThe things we don't see – stories of Software, Scala and Akka
The things we don't see – stories of Software, Scala and AkkaKonrad Malawski
 
Mario Fusco - Lazy Java - Codemotion Milan 2018
Mario Fusco - Lazy Java - Codemotion Milan 2018Mario Fusco - Lazy Java - Codemotion Milan 2018
Mario Fusco - Lazy Java - Codemotion Milan 2018Codemotion
 
Do I need tests when I have the compiler - Andrzej Jóźwiak - TomTom Dev Day 2020
Do I need tests when I have the compiler - Andrzej Jóźwiak - TomTom Dev Day 2020Do I need tests when I have the compiler - Andrzej Jóźwiak - TomTom Dev Day 2020
Do I need tests when I have the compiler - Andrzej Jóźwiak - TomTom Dev Day 2020Andrzej Jóźwiak
 
PythonStudyMaterialSTudyMaterial.pdf
PythonStudyMaterialSTudyMaterial.pdfPythonStudyMaterialSTudyMaterial.pdf
PythonStudyMaterialSTudyMaterial.pdfdata2businessinsight
 
C++ Interview Question And Answer
C++ Interview Question And AnswerC++ Interview Question And Answer
C++ Interview Question And AnswerJagan Mohan Bishoyi
 
C++ questions And Answer
C++ questions And AnswerC++ questions And Answer
C++ questions And Answerlavparmar007
 
Introduction to Client-Side Javascript
Introduction to Client-Side JavascriptIntroduction to Client-Side Javascript
Introduction to Client-Side JavascriptJulie Iskander
 
Os Vanrossum
Os VanrossumOs Vanrossum
Os Vanrossumoscon2007
 

Similar to Orthogonal Functional Architecture (20)

Consider this code using the ArrayBag of Section 5.2 and the Locat.docx
Consider this code using the ArrayBag of Section 5.2 and the Locat.docxConsider this code using the ArrayBag of Section 5.2 and the Locat.docx
Consider this code using the ArrayBag of Section 5.2 and the Locat.docx
 
FP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyondFP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyond
 
New Functional Features of Java 8
New Functional Features of Java 8New Functional Features of Java 8
New Functional Features of Java 8
 
Java 8 Workshop
Java 8 WorkshopJava 8 Workshop
Java 8 Workshop
 
If You Think You Can Stay Away from Functional Programming, You Are Wrong
If You Think You Can Stay Away from Functional Programming, You Are WrongIf You Think You Can Stay Away from Functional Programming, You Are Wrong
If You Think You Can Stay Away from Functional Programming, You Are Wrong
 
The things we don't see – stories of Software, Scala and Akka
The things we don't see – stories of Software, Scala and AkkaThe things we don't see – stories of Software, Scala and Akka
The things we don't see – stories of Software, Scala and Akka
 
Lambda Functions in Java 8
Lambda Functions in Java 8Lambda Functions in Java 8
Lambda Functions in Java 8
 
Mario Fusco - Lazy Java - Codemotion Milan 2018
Mario Fusco - Lazy Java - Codemotion Milan 2018Mario Fusco - Lazy Java - Codemotion Milan 2018
Mario Fusco - Lazy Java - Codemotion Milan 2018
 
Lazy java
Lazy javaLazy java
Lazy java
 
Lazy Java
Lazy JavaLazy Java
Lazy Java
 
Lazy Java
Lazy JavaLazy Java
Lazy Java
 
Do I need tests when I have the compiler - Andrzej Jóźwiak - TomTom Dev Day 2020
Do I need tests when I have the compiler - Andrzej Jóźwiak - TomTom Dev Day 2020Do I need tests when I have the compiler - Andrzej Jóźwiak - TomTom Dev Day 2020
Do I need tests when I have the compiler - Andrzej Jóźwiak - TomTom Dev Day 2020
 
Java gets a closure
Java gets a closureJava gets a closure
Java gets a closure
 
PythonStudyMaterialSTudyMaterial.pdf
PythonStudyMaterialSTudyMaterial.pdfPythonStudyMaterialSTudyMaterial.pdf
PythonStudyMaterialSTudyMaterial.pdf
 
Scala ntnu
Scala ntnuScala ntnu
Scala ntnu
 
C++ Interview Question And Answer
C++ Interview Question And AnswerC++ Interview Question And Answer
C++ Interview Question And Answer
 
C++ questions And Answer
C++ questions And AnswerC++ questions And Answer
C++ questions And Answer
 
Java
JavaJava
Java
 
Introduction to Client-Side Javascript
Introduction to Client-Side JavascriptIntroduction to Client-Side Javascript
Introduction to Client-Side Javascript
 
Os Vanrossum
Os VanrossumOs Vanrossum
Os Vanrossum
 

More from John De Goes

Refactoring Functional Type Classes
Refactoring Functional Type ClassesRefactoring Functional Type Classes
Refactoring Functional Type ClassesJohn De Goes
 
One Monad to Rule Them All
One Monad to Rule Them AllOne Monad to Rule Them All
One Monad to Rule Them AllJohn De Goes
 
Error Management: Future vs ZIO
Error Management: Future vs ZIOError Management: Future vs ZIO
Error Management: Future vs ZIOJohn De Goes
 
Atomically { Delete Your Actors }
Atomically { Delete Your Actors }Atomically { Delete Your Actors }
Atomically { Delete Your Actors }John De Goes
 
The Death of Final Tagless
The Death of Final TaglessThe Death of Final Tagless
The Death of Final TaglessJohn De Goes
 
Scalaz Stream: Rebirth
Scalaz Stream: RebirthScalaz Stream: Rebirth
Scalaz Stream: RebirthJohn De Goes
 
Scalaz Stream: Rebirth
Scalaz Stream: RebirthScalaz Stream: Rebirth
Scalaz Stream: RebirthJohn De Goes
 
ZIO Schedule: Conquering Flakiness & Recurrence with Pure Functional Programming
ZIO Schedule: Conquering Flakiness & Recurrence with Pure Functional ProgrammingZIO Schedule: Conquering Flakiness & Recurrence with Pure Functional Programming
ZIO Schedule: Conquering Flakiness & Recurrence with Pure Functional ProgrammingJohn De Goes
 
Blazing Fast, Pure Effects without Monads — LambdaConf 2018
Blazing Fast, Pure Effects without Monads — LambdaConf 2018Blazing Fast, Pure Effects without Monads — LambdaConf 2018
Blazing Fast, Pure Effects without Monads — LambdaConf 2018John De Goes
 
Scalaz 8: A Whole New Game
Scalaz 8: A Whole New GameScalaz 8: A Whole New Game
Scalaz 8: A Whole New GameJohn De Goes
 
Scalaz 8 vs Akka Actors
Scalaz 8 vs Akka ActorsScalaz 8 vs Akka Actors
Scalaz 8 vs Akka ActorsJohn De Goes
 
The Design of the Scalaz 8 Effect System
The Design of the Scalaz 8 Effect SystemThe Design of the Scalaz 8 Effect System
The Design of the Scalaz 8 Effect SystemJohn De Goes
 
Quark: A Purely-Functional Scala DSL for Data Processing & Analytics
Quark: A Purely-Functional Scala DSL for Data Processing & AnalyticsQuark: A Purely-Functional Scala DSL for Data Processing & Analytics
Quark: A Purely-Functional Scala DSL for Data Processing & AnalyticsJohn De Goes
 
Post-Free: Life After Free Monads
Post-Free: Life After Free MonadsPost-Free: Life After Free Monads
Post-Free: Life After Free MonadsJohn De Goes
 
Streams for (Co)Free!
Streams for (Co)Free!Streams for (Co)Free!
Streams for (Co)Free!John De Goes
 
The Easy-Peasy-Lemon-Squeezy, Statically-Typed, Purely Functional Programming...
The Easy-Peasy-Lemon-Squeezy, Statically-Typed, Purely Functional Programming...The Easy-Peasy-Lemon-Squeezy, Statically-Typed, Purely Functional Programming...
The Easy-Peasy-Lemon-Squeezy, Statically-Typed, Purely Functional Programming...John De Goes
 
Halogen: Past, Present, and Future
Halogen: Past, Present, and FutureHalogen: Past, Present, and Future
Halogen: Past, Present, and FutureJohn De Goes
 
All Aboard The Scala-to-PureScript Express!
All Aboard The Scala-to-PureScript Express!All Aboard The Scala-to-PureScript Express!
All Aboard The Scala-to-PureScript Express!John De Goes
 

More from John De Goes (20)

Refactoring Functional Type Classes
Refactoring Functional Type ClassesRefactoring Functional Type Classes
Refactoring Functional Type Classes
 
One Monad to Rule Them All
One Monad to Rule Them AllOne Monad to Rule Them All
One Monad to Rule Them All
 
Error Management: Future vs ZIO
Error Management: Future vs ZIOError Management: Future vs ZIO
Error Management: Future vs ZIO
 
Atomically { Delete Your Actors }
Atomically { Delete Your Actors }Atomically { Delete Your Actors }
Atomically { Delete Your Actors }
 
The Death of Final Tagless
The Death of Final TaglessThe Death of Final Tagless
The Death of Final Tagless
 
Scalaz Stream: Rebirth
Scalaz Stream: RebirthScalaz Stream: Rebirth
Scalaz Stream: Rebirth
 
Scalaz Stream: Rebirth
Scalaz Stream: RebirthScalaz Stream: Rebirth
Scalaz Stream: Rebirth
 
ZIO Schedule: Conquering Flakiness & Recurrence with Pure Functional Programming
ZIO Schedule: Conquering Flakiness & Recurrence with Pure Functional ProgrammingZIO Schedule: Conquering Flakiness & Recurrence with Pure Functional Programming
ZIO Schedule: Conquering Flakiness & Recurrence with Pure Functional Programming
 
ZIO Queue
ZIO QueueZIO Queue
ZIO Queue
 
Blazing Fast, Pure Effects without Monads — LambdaConf 2018
Blazing Fast, Pure Effects without Monads — LambdaConf 2018Blazing Fast, Pure Effects without Monads — LambdaConf 2018
Blazing Fast, Pure Effects without Monads — LambdaConf 2018
 
Scalaz 8: A Whole New Game
Scalaz 8: A Whole New GameScalaz 8: A Whole New Game
Scalaz 8: A Whole New Game
 
Scalaz 8 vs Akka Actors
Scalaz 8 vs Akka ActorsScalaz 8 vs Akka Actors
Scalaz 8 vs Akka Actors
 
The Design of the Scalaz 8 Effect System
The Design of the Scalaz 8 Effect SystemThe Design of the Scalaz 8 Effect System
The Design of the Scalaz 8 Effect System
 
Quark: A Purely-Functional Scala DSL for Data Processing & Analytics
Quark: A Purely-Functional Scala DSL for Data Processing & AnalyticsQuark: A Purely-Functional Scala DSL for Data Processing & Analytics
Quark: A Purely-Functional Scala DSL for Data Processing & Analytics
 
Post-Free: Life After Free Monads
Post-Free: Life After Free MonadsPost-Free: Life After Free Monads
Post-Free: Life After Free Monads
 
Streams for (Co)Free!
Streams for (Co)Free!Streams for (Co)Free!
Streams for (Co)Free!
 
MTL Versus Free
MTL Versus FreeMTL Versus Free
MTL Versus Free
 
The Easy-Peasy-Lemon-Squeezy, Statically-Typed, Purely Functional Programming...
The Easy-Peasy-Lemon-Squeezy, Statically-Typed, Purely Functional Programming...The Easy-Peasy-Lemon-Squeezy, Statically-Typed, Purely Functional Programming...
The Easy-Peasy-Lemon-Squeezy, Statically-Typed, Purely Functional Programming...
 
Halogen: Past, Present, and Future
Halogen: Past, Present, and FutureHalogen: Past, Present, and Future
Halogen: Past, Present, and Future
 
All Aboard The Scala-to-PureScript Express!
All Aboard The Scala-to-PureScript Express!All Aboard The Scala-to-PureScript Express!
All Aboard The Scala-to-PureScript Express!
 

Recently uploaded

Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentationphoebematthew05
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 

Recently uploaded (20)

Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentation
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 

Orthogonal Functional Architecture

  • 1. Orthogonal Functional Architecture Lambda Squared - Knoxville, TN John A. De Goes — @jdegoes http://degoes.net
  • 3. The Dysfunctional Nightmare abandon all hope ye who enter
  • 4. Procedural Code Ad hoc solutions constructed from large number of non-composable effects that are reasoned about non-locally.
  • 5. The Functional Dream bliss awaits all ye who enter
  • 6. Functional Code Principled solutions constructed from a small number of composable building blocks that are reasoned about locally.
  • 7. Two Keys to Bliss Orthogonality + Composability
  • 8. Two Keys to Bliss Composability makes functional code powerful, and orthogonality makes it beautiful* *i.e. modular and uncluttered by irrelevant details.
  • 9. Two Keys to Bliss Composable, orthogonal bases tend to be powerful, but small and simple to reason about, permitting flexible, modular solutions to many problems.
  • 11. Composability public interface Future<V> { boolean cancel(boolean mayInterruptIfRunning); boolean isDone(); V get() throws InterruptedException, ExecutionException; V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException; } Is this Java Future composable?
  • 12. Composability Composability measures the extent to which values can be combined with other values to produce like values
  • 13. Composability 1 + 1 = 2 Two integers combine to yield another integer. 1 – 1 = 0 Integers are composable with respect to addition/subtraction.
  • 15. Composability public interface Future<V> { boolean cancel(boolean mayInterruptIfRunning); boolean isDone(); V get() throws InterruptedException, ExecutionException; V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException; } Is this Java Future composable?
  • 17. Orthogonality public interface Future<V> { boolean cancel(boolean mayInterruptIfRunning); boolean isDone(); V get() throws InterruptedException, ExecutionException; V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException; } Is this Java Future orthogonal?
  • 18. Orthogonality Orthogonality measures the extent to which primitive operations on values have single, unique concerns
  • 19. Orthogonality Addition moves right Addition/subtraction are orthogonal Subtraction moves left
  • 20. Orthogonality A A + B B A Non-Orthogonal Orthogonal B
  • 21. Orthogonality public interface Future<V> { boolean cancel(boolean mayInterruptIfRunning); boolean isDone(); V get() throws InterruptedException, ExecutionException; V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException; } Is this orthogonal?
  • 22. Orthogonality public interface Future<V> { boolean cancel( boolean mayInterruptIfRunning); boolean isDone(); V get() throws InterruptedException, ExecutionException; V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException; } Is this orthogonal? Timeout Get Timeout + Get
  • 23. Orthogonality The cardinal sin of non-orthogonality is the tangling of separate concerns, which infects the code base to destroy modularity.
  • 26. Steps Toward Orthogonality 1. Make the system composable.
  • 27. Steps Toward Orthogonality 2. Identify the primitive operations.
  • 28. Steps Toward Orthogonality 3. Identify the unique concerns.
  • 29. Steps Toward Orthogonality 4. Refactor the primitive operations until each has a unique concern.
  • 31. Worked Examples public interface Future<V> { boolean cancel(boolean mayInterruptIfRunning); boolean isDone(); V get() throws InterruptedException, ExecutionException; V get(long timeout, TimeUnit unit) throws InterruptedException,ExecutionException, TimeoutException; } Is this orthogonal?
  • 32. Worked Examples The curse of non-orthogonality! // Would like to write: <A> Future<A> retryUntilSuccess(Future<A> future, Interval spacing) { // ??? } // Forced to write: A retryUntilSuccess(Future<A> future, Interval spacing, long timeout, TimeUnit unit) { // ... }
  • 33. Worked Examples public interface Future<V> { Future<Tuple2<Future<V>, Function<Boolean, Future<Unit>>>> fork(); V unsafePerformGet() throws InterruptedException, ExecutionException; Future<V> timeout(long timeout, TimeUnit unit); } Future 2.0: Detangle ‘get’ and ‘timeout’
  • 34. Worked Examples Is this orthogonal? static int compare( String str1, String str2, boolean nullIsLess)
  • 35. Worked Examples Is this orthogonal? NullIsLess + Comparison NullIsLess Comparison static int compare( String str1, String str2, boolean nullIsLess)
  • 36. Worked Examples The curse of non-orthogonality! List<String> myAlgorithm(List<String> list, boolean nullIsFirst) { // ... int c = compare(first, second, nullIsFirst); // ... }
  • 37. enum Ordering { LT, EQ, GT } interface Ord<A> { Ordering compare(A l, A r); } Worked Examples 1. Define a unit of composition
  • 38. class StringOrd extends Ord<String> { public Ordering compare(String l, String r) { // ... } } Worked Examples 2. Define one dimension (string comparison)
  • 39. class NullIsLess<A> { private final Ord<A> ord; public NullIsLess(Ord<A> ord) { this.ord = ord; } public Ordering compare(A l, A r) { if (l == null) { if (r == null) return EQ; else return LT; } else if (r == null) return GT; else return ord.compare(l, r); } } Worked Examples 3. Define another dimension (null is first)
  • 40. List<String> myAlgorithm(List<String> list, Ord<String> ord) { // ... Ordering c = ord.compare(first, second); // <- Beautiful!!! // ... } Ord<String> ord = new NullIsLess<String>(new StringOrd()); List<String> list2 = myAlgorithm(list, ord); Worked Examples 4. Compose orthogonal components
  • 41. Worked Examples Is this orthogonal?* data MVar a putMVar :: MVar a -> a -> IO () takeMVar :: MVar a -> IO a *Thanks to Fabio the fabulous for this example.
  • 42. Worked Examples Is this orthogonal? data MVar a putMVar :: MVar a -> a -> IO () takeMVar :: MVar a -> IO a Synchronization + Concurrency Synchronization Concurrency
  • 43. Worked Examples data IORef a newtype Expect a = Expect a modify :: IORef a -> (a -> a) -> IO Bool data Promise a newPromise :: IO (Promise a, a -> IO ()) awaitPromise :: Promise a -> IO a Synchronization (IORef) Concurrency (Promise)
  • 44. Worked Examples Is this orthogonal?* *A tiny part of Apache String Utils.
  • 45. Worked Examples Is this orthogonal? ? ?
  • 46. Worked Examples Is this orthogonal? data Parser a = Parser (String -> Either String (String, a)) char :: Parser Char fail :: String -> Parser a alt :: Parser a -> Parser a -> Parser a seq :: Parser a -> (a -> Parser b) -> Parser b pure :: a -> Parser a map :: (a -> b) -> Parser a -> Parser b
  • 47. Worked Examples Is this orthogonal?* data Parser a = Parser (String -> Either String (String, a)) char :: Parser Char fail :: String -> Parser a alt :: Parser a -> Parser a -> Parser a seq :: Parser a -> (a -> Parser b) -> Parser b pure :: a -> Parser a map :: (a -> b) -> Parser a -> Parser b *Trick question — or is it?
  • 48. Worked Examples Is this orthogonal? data Parser a = Parser (String -> ... char :: Parser Char fail :: String -> Parser a alt :: Parser a -> Parser a -> Parser a seq :: Parser a -> (a -> Parser b) -> Parser b pure :: a -> Parser a map :: (a -> b) -> Parser a -> Parser b SequencingMapping Flattening
  • 49. Worked Examples Is this orthogonal? data Parser a = Parser (String -> ... char :: Parser Char fail :: String -> Parser a alt :: Parser a -> Parser a -> Parser a join :: Parser (Parser a) -> Parser a pure :: a -> Parser a map :: (a -> b) -> Parser a -> Parser b Mapping Flattening
  • 50. Wrap
  • 51. Summary 1. Functional code is composable and orthogonal, allowing a small set of principled building blocks to snap together to solve complex problems in a predictable, reasonable way. 2. Composability measures the combinability of values. 3. Orthogonality measures the singular focus of primitive operations. 4. Refactor to orthogonality to obtain modular clode, uncluttered by irrelevant details.
  • 52. Thank You! Special thanks to Reid, Cameron, Emily, & the wonderful Knoxville FP community, and the generous sponsor ResultStack! John A. De Goes — @jdegoes http://degoes.net