SlideShare une entreprise Scribd logo
1  sur  38
Télécharger pour lire hors ligne
Functional Programming

beyond map/filter/reduce
JFokus 2018
Prof. Dierk König
canoo
mittie
Monads

are just

monoids in the category
of endofunctors.
Explanation Path
Monoid it's simple
Functor you already know it
Endo- cool idea
Monoid of endofunctors = monad
Integers (+)
2 + 3 == 5 

2 + 0 == 2 

0 + 3 == 3 

( 1+2 )+3 == 1+( 2+3 )
Integers (*)
2 * 3 == 6 

2 * 1 == 2 

1 * 3 == 3 

( 2*3 )*4 == 2*( 3*4 )
Boolean (&&)
a && b == c 

a && true == a 

true && b == b 

( a && b ) && c == a &&( b && c )
Lists, Arrays (+)
[1] + [2] == [1, 2]

[1] + [ ] == [1]

[ ] + [2] == [2]

([1]+[2])+[3] == [1]+([2]+[3])
Strings (+)
"1" + "2" == "12"

"1" + "" == "1"

"" + "2" == "2"

( "Hi"+"," )+"JFokus" == 

"Hi"+( ","+"JFokus" )
Extract
type <> type => type
neutral element
left & right identity
associative
Abstract
We call this concept "Monoid"

short for "that thing, you know, that you can combine
with another thing of the same type to produce a new
thing of the type of the other two as long as there is a
value of that type that when combined left or right does
not change the value you combined that special value
with and any such combination must yield the same
result no matter which two you combine first."
LEGO Monoid
LEGO Monoid
LEGO Monoid
LEGO Neutral Brick
LEGO Left & Right Identity
Generic Fold Right
Generic Fold Right
Generic Fold Right
Generic Fold Right
Generic Optimized Fold
Generic Optimized Fold
Generic Optimized Fold
Crazy Monoids
(a -> b) <> (b -> c) == (a -> c)
Can a function type (a->b) be a monoid?
What is the operation?
What is the neutral element?
Is it associative?
Function Composition
co(f,g) = x => f(g(x));
id(x) = x;
co(id,g) == x => id(g(x)) // definition co

== x => g(x) // apply id

== g // qed
Functors map
[1, 2, 3].map( x=> x+2 ) == [3, 4, 5]
(Just 1).map( x=> x+2 ) == Just 3
Nothing.map( whatever ) == Nothing
List, Tree, Stream, Optional, Pipeline, Error,
Validation, Observable, Future, Promise,..
Functors compose
[1, 2, 3].map( x => x + 2 )

.map( x => 3 * x ) 

==

[1, 2, 3].map( x => 3 * (x+2) )
co( functor.map(f), functor.map(g) ) 

== functor.map( co(f, g) )
functor.map(id) == id // only for completeness
Functors are not Monoids
[1, 2, 3] .map( x => x.toString() ) == ["1","2","3"]
[Int] .map( Int -> String ) -> [String]
functor a .map( a -> b) -> functor b
Clever Idea:
instead of (a->b)

provide a special mapping with (a -> functor b),

which is essentially a constructor for functors.


functor a <> functor b => functor b

functor a .endo(a->functor b) => functor b
Clever Idea in Action
[1, 2, 3].endo( x => replicate(x, x.toString()) ) ==

[ ["1"], ["2","2"], ["3","3","3"] ]
then flatten => ["1", "2", "2", "3", "3", "3"] //aka "join"
funcA.flatMap(f) = funcB.flatten(funcA.endo(f))
// aka "bind" or ">>="
Finalising the Monoid
functor a .flatMap(a->functor b) => functor b



(a->functor a) <> (a->functor b) => (a->functor b)
We need an (a-> functor a) ctor and flatMap (we
already have map, so we only need flatten). If the
functor is monoidal with flatMap as <> and ctor as
neutal element, then we call it a Monad.
Wrapping up
Let (m a) be a given monad over type a.

(m a) has a ctor (a -> m a). // aka "return" or "pure"

(m a) is a functor over a. 

(m (m a)) can be flattened to (m a).

(a-> m a).flatMap( a -> m b) is a monoidal operation.
Alternative way of writing in pure FP style

(>>=) :: Monad m => m a -> (a -> m b) -> m b
Takeaways
Associativity (monoid) requires pure functions.
Monoids can fold but they cannot escape.
Monads are the most versatile functors (map, filter,
expand, reduce) that composes and folds without
escaping.
Use Cases
Purely functional state threads

List comprehensions, Streams (possibly reactive)

CompletableFuture, Promises, Continuations

LINQ-style database access

Either, Validation, Exception handling

Optionality, Indeterminism, Parsers, Search trees

Sequencing IO actions, isolating UI actions

STM transactions, …
No IO in Transactions!
Type inference FTW
Less	tricky

errors
There is a world…
… where logical reasoning rules and structure
arises from consistency and a rich set of relations.
Exploring this world is like programming without
implementation.
Purely functional programming opens the door.

Consider Haskell, Frege, Purescript, Idris.
mittie
Please give feedback!
Prof. Dierk König
canoo

Contenu connexe

Tendances

2.4 operations on functions
2.4 operations on functions2.4 operations on functions
2.4 operations on functions
hisema01
 
Operations With Functions May 25 2009
Operations With Functions May 25 2009Operations With Functions May 25 2009
Operations With Functions May 25 2009
ingroy
 
Day 5 examples u6w14
Day 5 examples u6w14Day 5 examples u6w14
Day 5 examples u6w14
jchartiersjsd
 
Exercise 2
Exercise 2Exercise 2
Exercise 2
math126
 
4 2 operations on functions
4 2 operations on functions4 2 operations on functions
4 2 operations on functions
hisema01
 
119 Powerpoint 2.6
119 Powerpoint 2.6119 Powerpoint 2.6
119 Powerpoint 2.6
Jeneva Clark
 

Tendances (20)

Composite functions
Composite functionsComposite functions
Composite functions
 
2.4 operations on functions
2.4 operations on functions2.4 operations on functions
2.4 operations on functions
 
Operations With Functions May 25 2009
Operations With Functions May 25 2009Operations With Functions May 25 2009
Operations With Functions May 25 2009
 
Gentle Introduction to Functional Programming
Gentle Introduction to Functional ProgrammingGentle Introduction to Functional Programming
Gentle Introduction to Functional Programming
 
Day 4b iteration and functions for-loops.pptx
Day 4b   iteration and functions  for-loops.pptxDay 4b   iteration and functions  for-loops.pptx
Day 4b iteration and functions for-loops.pptx
 
Day 5 examples u6w14
Day 5 examples u6w14Day 5 examples u6w14
Day 5 examples u6w14
 
2.1 the basic language of functions t
2.1 the basic language of functions  t2.1 the basic language of functions  t
2.1 the basic language of functions t
 
Day 4a iteration and functions.pptx
Day 4a   iteration and functions.pptxDay 4a   iteration and functions.pptx
Day 4a iteration and functions.pptx
 
Exercise 2
Exercise 2Exercise 2
Exercise 2
 
Day 5 u8f13
Day 5 u8f13Day 5 u8f13
Day 5 u8f13
 
Translating Linear Functions "I AM" Answer Key!
Translating Linear Functions "I AM" Answer Key!Translating Linear Functions "I AM" Answer Key!
Translating Linear Functions "I AM" Answer Key!
 
4 2 operations on functions
4 2 operations on functions4 2 operations on functions
4 2 operations on functions
 
Array and pointer
Array and pointerArray and pointer
Array and pointer
 
119 Powerpoint 2.6
119 Powerpoint 2.6119 Powerpoint 2.6
119 Powerpoint 2.6
 
Admission for b.tech
Admission for b.techAdmission for b.tech
Admission for b.tech
 
Math - Operations on Functions, Kinds of Functions
Math - Operations on Functions, Kinds of FunctionsMath - Operations on Functions, Kinds of Functions
Math - Operations on Functions, Kinds of Functions
 
Lesson 3 Operation on Functions
Lesson 3 Operation on FunctionsLesson 3 Operation on Functions
Lesson 3 Operation on Functions
 
Function : Introduction
Function : IntroductionFunction : Introduction
Function : Introduction
 
Quadratic functions
Quadratic functionsQuadratic functions
Quadratic functions
 
Ch18 18
Ch18 18Ch18 18
Ch18 18
 

Similaire à Monads from Definition

Introduction to Monads in Scala (2)
Introduction to Monads in Scala (2)Introduction to Monads in Scala (2)
Introduction to Monads in Scala (2)
stasimus
 
Monad Transformers In The Wild
Monad Transformers In The WildMonad Transformers In The Wild
Monad Transformers In The Wild
StackMob Inc
 
Unit 8(rational expressions) week 22 - simplifying rational expressions
Unit 8(rational expressions)   week 22 - simplifying rational expressionsUnit 8(rational expressions)   week 22 - simplifying rational expressions
Unit 8(rational expressions) week 22 - simplifying rational expressions
Jason Morgan
 

Similaire à Monads from Definition (20)

하스켈 프로그래밍 입문 4
하스켈 프로그래밍 입문 4하스켈 프로그래밍 입문 4
하스켈 프로그래밍 입문 4
 
Grokking Monads in Scala
Grokking Monads in ScalaGrokking Monads in Scala
Grokking Monads in Scala
 
Functors, applicatives, monads
Functors, applicatives, monadsFunctors, applicatives, monads
Functors, applicatives, monads
 
Fp in scala with adts
Fp in scala with adtsFp in scala with adts
Fp in scala with adts
 
Introduction to Monads in Scala (2)
Introduction to Monads in Scala (2)Introduction to Monads in Scala (2)
Introduction to Monads in Scala (2)
 
MP in Clojure
MP in ClojureMP in Clojure
MP in Clojure
 
Monoids, monoids, monoids
Monoids, monoids, monoidsMonoids, monoids, monoids
Monoids, monoids, monoids
 
Functional programming from its fundamentals
Functional programming from its fundamentalsFunctional programming from its fundamentals
Functional programming from its fundamentals
 
The Essence of the Iterator Pattern
The Essence of the Iterator PatternThe Essence of the Iterator Pattern
The Essence of the Iterator Pattern
 
The Essence of the Iterator Pattern (pdf)
The Essence of the Iterator Pattern (pdf)The Essence of the Iterator Pattern (pdf)
The Essence of the Iterator Pattern (pdf)
 
Introduction to MATLAB
Introduction to MATLABIntroduction to MATLAB
Introduction to MATLAB
 
Class XII CBSE Mathematics Sample question paper with solution
Class XII CBSE Mathematics Sample question paper with solutionClass XII CBSE Mathematics Sample question paper with solution
Class XII CBSE Mathematics Sample question paper with solution
 
Christian Gill ''Functional programming for the people''
Christian Gill ''Functional programming for the people''Christian Gill ''Functional programming for the people''
Christian Gill ''Functional programming for the people''
 
Monad Transformers In The Wild
Monad Transformers In The WildMonad Transformers In The Wild
Monad Transformers In The Wild
 
JSDC 2014 - functional java script, why or why not
JSDC 2014 - functional java script, why or why notJSDC 2014 - functional java script, why or why not
JSDC 2014 - functional java script, why or why not
 
Use Applicative where applicable!
Use Applicative where applicable!Use Applicative where applicable!
Use Applicative where applicable!
 
Efoom 2020
Efoom 2020Efoom 2020
Efoom 2020
 
Unit 8(rational expressions) week 22 - simplifying rational expressions
Unit 8(rational expressions)   week 22 - simplifying rational expressionsUnit 8(rational expressions)   week 22 - simplifying rational expressions
Unit 8(rational expressions) week 22 - simplifying rational expressions
 
Fp in scala part 2
Fp in scala part 2Fp in scala part 2
Fp in scala part 2
 
Monad Fact #4
Monad Fact #4Monad Fact #4
Monad Fact #4
 

Plus de Dierk König

UI Engineer - the missing profession, devoxx 2013
UI Engineer - the missing profession, devoxx 2013UI Engineer - the missing profession, devoxx 2013
UI Engineer - the missing profession, devoxx 2013
Dierk König
 
OpenDolphin: Enterprise Apps for collaboration on Desktop, Web, and Mobile
OpenDolphin: Enterprise Apps for collaboration on Desktop, Web, and MobileOpenDolphin: Enterprise Apps for collaboration on Desktop, Web, and Mobile
OpenDolphin: Enterprise Apps for collaboration on Desktop, Web, and Mobile
Dierk König
 

Plus de Dierk König (12)

OpenDolphin with GroovyFX Workshop at GreachConf, Madrid
OpenDolphin with GroovyFX Workshop at GreachConf, MadridOpenDolphin with GroovyFX Workshop at GreachConf, Madrid
OpenDolphin with GroovyFX Workshop at GreachConf, Madrid
 
Greach, GroovyFx Workshop
Greach, GroovyFx WorkshopGreach, GroovyFx Workshop
Greach, GroovyFx Workshop
 
FregeFX - JavaFX with Frege, a Haskell for the JVM
FregeFX - JavaFX with Frege, a Haskell for the JVMFregeFX - JavaFX with Frege, a Haskell for the JVM
FregeFX - JavaFX with Frege, a Haskell for the JVM
 
Software Transactional Memory (STM) in Frege
Software Transactional Memory (STM) in Frege Software Transactional Memory (STM) in Frege
Software Transactional Memory (STM) in Frege
 
Quick into to Software Transactional Memory in Frege
Quick into to Software Transactional Memory in FregeQuick into to Software Transactional Memory in Frege
Quick into to Software Transactional Memory in Frege
 
Frege Tutorial at JavaOne 2015
Frege Tutorial at JavaOne 2015Frege Tutorial at JavaOne 2015
Frege Tutorial at JavaOne 2015
 
Frege - consequently functional programming for the JVM
Frege - consequently functional programming for the JVMFrege - consequently functional programming for the JVM
Frege - consequently functional programming for the JVM
 
FregeDay: Parallelism in Frege compared to GHC Haskell (Volker Steiss)
FregeDay: Parallelism in Frege compared to GHC Haskell (Volker Steiss) FregeDay: Parallelism in Frege compared to GHC Haskell (Volker Steiss)
FregeDay: Parallelism in Frege compared to GHC Haskell (Volker Steiss)
 
FregeDay: Design and Implementation of the language (Ingo Wechsung)
FregeDay: Design and Implementation of the language (Ingo Wechsung)FregeDay: Design and Implementation of the language (Ingo Wechsung)
FregeDay: Design and Implementation of the language (Ingo Wechsung)
 
FregeDay: Roadmap for resolving differences between Haskell and Frege (Ingo W...
FregeDay: Roadmap for resolving differences between Haskell and Frege (Ingo W...FregeDay: Roadmap for resolving differences between Haskell and Frege (Ingo W...
FregeDay: Roadmap for resolving differences between Haskell and Frege (Ingo W...
 
UI Engineer - the missing profession, devoxx 2013
UI Engineer - the missing profession, devoxx 2013UI Engineer - the missing profession, devoxx 2013
UI Engineer - the missing profession, devoxx 2013
 
OpenDolphin: Enterprise Apps for collaboration on Desktop, Web, and Mobile
OpenDolphin: Enterprise Apps for collaboration on Desktop, Web, and MobileOpenDolphin: Enterprise Apps for collaboration on Desktop, Web, and Mobile
OpenDolphin: Enterprise Apps for collaboration on Desktop, Web, and Mobile
 

Dernier

Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
chiefasafspells
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
masabamasaba
 

Dernier (20)

WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
 
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security Program
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
WSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - Keynote
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 

Monads from Definition

  • 3. Monads
 are just
 monoids in the category of endofunctors.
  • 4. Explanation Path Monoid it's simple Functor you already know it Endo- cool idea Monoid of endofunctors = monad
  • 5. Integers (+) 2 + 3 == 5 
 2 + 0 == 2 
 0 + 3 == 3 
 ( 1+2 )+3 == 1+( 2+3 )
  • 6. Integers (*) 2 * 3 == 6 
 2 * 1 == 2 
 1 * 3 == 3 
 ( 2*3 )*4 == 2*( 3*4 )
  • 7. Boolean (&&) a && b == c 
 a && true == a 
 true && b == b 
 ( a && b ) && c == a &&( b && c )
  • 8. Lists, Arrays (+) [1] + [2] == [1, 2]
 [1] + [ ] == [1]
 [ ] + [2] == [2]
 ([1]+[2])+[3] == [1]+([2]+[3])
  • 9. Strings (+) "1" + "2" == "12"
 "1" + "" == "1"
 "" + "2" == "2"
 ( "Hi"+"," )+"JFokus" == 
 "Hi"+( ","+"JFokus" )
  • 10. Extract type <> type => type neutral element left & right identity associative
  • 11. Abstract We call this concept "Monoid"
 short for "that thing, you know, that you can combine with another thing of the same type to produce a new thing of the type of the other two as long as there is a value of that type that when combined left or right does not change the value you combined that special value with and any such combination must yield the same result no matter which two you combine first."
  • 16. LEGO Left & Right Identity
  • 24. Crazy Monoids (a -> b) <> (b -> c) == (a -> c) Can a function type (a->b) be a monoid? What is the operation? What is the neutral element? Is it associative?
  • 25. Function Composition co(f,g) = x => f(g(x)); id(x) = x; co(id,g) == x => id(g(x)) // definition co
 == x => g(x) // apply id
 == g // qed
  • 26. Functors map [1, 2, 3].map( x=> x+2 ) == [3, 4, 5] (Just 1).map( x=> x+2 ) == Just 3 Nothing.map( whatever ) == Nothing List, Tree, Stream, Optional, Pipeline, Error, Validation, Observable, Future, Promise,..
  • 27. Functors compose [1, 2, 3].map( x => x + 2 )
 .map( x => 3 * x ) 
 ==
 [1, 2, 3].map( x => 3 * (x+2) ) co( functor.map(f), functor.map(g) ) 
 == functor.map( co(f, g) ) functor.map(id) == id // only for completeness
  • 28. Functors are not Monoids [1, 2, 3] .map( x => x.toString() ) == ["1","2","3"] [Int] .map( Int -> String ) -> [String] functor a .map( a -> b) -> functor b
  • 29. Clever Idea: instead of (a->b)
 provide a special mapping with (a -> functor b),
 which is essentially a constructor for functors. 
 functor a <> functor b => functor b
 functor a .endo(a->functor b) => functor b
  • 30. Clever Idea in Action [1, 2, 3].endo( x => replicate(x, x.toString()) ) ==
 [ ["1"], ["2","2"], ["3","3","3"] ] then flatten => ["1", "2", "2", "3", "3", "3"] //aka "join" funcA.flatMap(f) = funcB.flatten(funcA.endo(f)) // aka "bind" or ">>="
  • 31. Finalising the Monoid functor a .flatMap(a->functor b) => functor b
 
 (a->functor a) <> (a->functor b) => (a->functor b) We need an (a-> functor a) ctor and flatMap (we already have map, so we only need flatten). If the functor is monoidal with flatMap as <> and ctor as neutal element, then we call it a Monad.
  • 32. Wrapping up Let (m a) be a given monad over type a.
 (m a) has a ctor (a -> m a). // aka "return" or "pure"
 (m a) is a functor over a. 
 (m (m a)) can be flattened to (m a).
 (a-> m a).flatMap( a -> m b) is a monoidal operation. Alternative way of writing in pure FP style
 (>>=) :: Monad m => m a -> (a -> m b) -> m b
  • 33. Takeaways Associativity (monoid) requires pure functions. Monoids can fold but they cannot escape. Monads are the most versatile functors (map, filter, expand, reduce) that composes and folds without escaping.
  • 34. Use Cases Purely functional state threads
 List comprehensions, Streams (possibly reactive)
 CompletableFuture, Promises, Continuations
 LINQ-style database access
 Either, Validation, Exception handling
 Optionality, Indeterminism, Parsers, Search trees
 Sequencing IO actions, isolating UI actions
 STM transactions, …
  • 35. No IO in Transactions!
  • 37. There is a world… … where logical reasoning rules and structure arises from consistency and a rich set of relations. Exploring this world is like programming without implementation. Purely functional programming opens the door.
 Consider Haskell, Frege, Purescript, Idris.