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

Kernels and Support Vector Machines
Kernels and Support Vector  MachinesKernels and Support Vector  Machines
Kernels and Support Vector MachinesEdgar Marca
 
Linear differential equation
Linear differential equationLinear differential equation
Linear differential equationPratik Sudra
 
Liner algebra-vector space-1 introduction to vector space and subspace
Liner algebra-vector space-1   introduction to vector space and subspace Liner algebra-vector space-1   introduction to vector space and subspace
Liner algebra-vector space-1 introduction to vector space and subspace Manikanta satyala
 
Lesson 27: Integration by Substitution (slides)
Lesson 27: Integration by Substitution (slides)Lesson 27: Integration by Substitution (slides)
Lesson 27: Integration by Substitution (slides)Matthew Leingang
 
Fuzzy logic-introduction
Fuzzy logic-introductionFuzzy logic-introduction
Fuzzy logic-introductionWBUTTUTORIALS
 
Decision tree, softmax regression and ensemble methods in machine learning
Decision tree, softmax regression and ensemble methods in machine learningDecision tree, softmax regression and ensemble methods in machine learning
Decision tree, softmax regression and ensemble methods in machine learningAbhishek Vijayvargia
 
Logistic regression in Machine Learning
Logistic regression in Machine LearningLogistic regression in Machine Learning
Logistic regression in Machine LearningKuppusamy P
 
Ensemble learning Techniques
Ensemble learning TechniquesEnsemble learning Techniques
Ensemble learning TechniquesBabu Priyavrat
 
5. Destructuring | ES6 | Assignment
5. Destructuring | ES6 | Assignment 5. Destructuring | ES6 | Assignment
5. Destructuring | ES6 | Assignment pcnmtutorials
 
Interpolation with Finite differences
Interpolation with Finite differencesInterpolation with Finite differences
Interpolation with Finite differencesDr. Nirav Vyas
 
Differential equations
Differential equationsDifferential equations
Differential equationsCharan Kumar
 
Lesson 32: The Fundamental Theorem Of Calculus
Lesson 32: The Fundamental Theorem Of CalculusLesson 32: The Fundamental Theorem Of Calculus
Lesson 32: The Fundamental Theorem Of CalculusMatthew Leingang
 
Contour integration and Mittag Leffler theorem
Contour integration and Mittag Leffler theoremContour integration and Mittag Leffler theorem
Contour integration and Mittag Leffler theoremAmenahGondal1
 
Bessel’s equation
Bessel’s equationBessel’s equation
Bessel’s equationkishor pokar
 

What's hot (20)

Kernels and Support Vector Machines
Kernels and Support Vector  MachinesKernels and Support Vector  Machines
Kernels and Support Vector Machines
 
Linear differential equation
Linear differential equationLinear differential equation
Linear differential equation
 
Lec3 dqn
Lec3 dqnLec3 dqn
Lec3 dqn
 
Liner algebra-vector space-1 introduction to vector space and subspace
Liner algebra-vector space-1   introduction to vector space and subspace Liner algebra-vector space-1   introduction to vector space and subspace
Liner algebra-vector space-1 introduction to vector space and subspace
 
Double Integrals
Double IntegralsDouble Integrals
Double Integrals
 
Context free grammar
Context free grammarContext free grammar
Context free grammar
 
Lesson 27: Integration by Substitution (slides)
Lesson 27: Integration by Substitution (slides)Lesson 27: Integration by Substitution (slides)
Lesson 27: Integration by Substitution (slides)
 
Fuzzy logic-introduction
Fuzzy logic-introductionFuzzy logic-introduction
Fuzzy logic-introduction
 
Decision tree, softmax regression and ensemble methods in machine learning
Decision tree, softmax regression and ensemble methods in machine learningDecision tree, softmax regression and ensemble methods in machine learning
Decision tree, softmax regression and ensemble methods in machine learning
 
Logistic regression in Machine Learning
Logistic regression in Machine LearningLogistic regression in Machine Learning
Logistic regression in Machine Learning
 
Ensemble learning Techniques
Ensemble learning TechniquesEnsemble learning Techniques
Ensemble learning Techniques
 
enums
enumsenums
enums
 
5. Destructuring | ES6 | Assignment
5. Destructuring | ES6 | Assignment 5. Destructuring | ES6 | Assignment
5. Destructuring | ES6 | Assignment
 
Interpolation with Finite differences
Interpolation with Finite differencesInterpolation with Finite differences
Interpolation with Finite differences
 
Kruskal's algorithm
Kruskal's algorithmKruskal's algorithm
Kruskal's algorithm
 
Differential equations
Differential equationsDifferential equations
Differential equations
 
Lesson 32: The Fundamental Theorem Of Calculus
Lesson 32: The Fundamental Theorem Of CalculusLesson 32: The Fundamental Theorem Of Calculus
Lesson 32: The Fundamental Theorem Of Calculus
 
Contour integration and Mittag Leffler theorem
Contour integration and Mittag Leffler theoremContour integration and Mittag Leffler theorem
Contour integration and Mittag Leffler theorem
 
Vector spaces
Vector spaces Vector spaces
Vector spaces
 
Bessel’s equation
Bessel’s equationBessel’s equation
Bessel’s equation
 

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
 
Lazy Java
Lazy JavaLazy Java
Lazy Java
 
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
 
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

08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 

Recently uploaded (20)

08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 

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