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

Composite functions
Composite functionsComposite functions
Composite functionsShaun Wilson
 
2.4 operations on functions
2.4 operations on functions2.4 operations on functions
2.4 operations on functionshisema01
 
Operations With Functions May 25 2009
Operations With Functions May 25 2009Operations With Functions May 25 2009
Operations With Functions May 25 2009ingroy
 
Gentle Introduction to Functional Programming
Gentle Introduction to Functional ProgrammingGentle Introduction to Functional Programming
Gentle Introduction to Functional ProgrammingSaurabh Singh
 
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.pptxAdrien Melquiond
 
Day 5 examples u6w14
Day 5 examples u6w14Day 5 examples u6w14
Day 5 examples u6w14jchartiersjsd
 
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 tmath260
 
Day 4a iteration and functions.pptx
Day 4a   iteration and functions.pptxDay 4a   iteration and functions.pptx
Day 4a iteration and functions.pptxAdrien Melquiond
 
Exercise 2
Exercise 2Exercise 2
Exercise 2math126
 
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!Deborah_Johnson
 
4 2 operations on functions
4 2 operations on functions4 2 operations on functions
4 2 operations on functionshisema01
 
119 Powerpoint 2.6
119 Powerpoint 2.6119 Powerpoint 2.6
119 Powerpoint 2.6Jeneva Clark
 
Admission for b.tech
Admission for b.techAdmission for b.tech
Admission for b.techEdhole.com
 
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 FunctionsChuckie Balbuena
 
Quadratic functions
Quadratic functionsQuadratic functions
Quadratic functionsIvy Estrella
 

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

하스켈 프로그래밍 입문 4
하스켈 프로그래밍 입문 4하스켈 프로그래밍 입문 4
하스켈 프로그래밍 입문 4Kwang Yul Seo
 
Grokking Monads in Scala
Grokking Monads in ScalaGrokking Monads in Scala
Grokking Monads in ScalaTim Dalton
 
Functors, applicatives, monads
Functors, applicatives, monadsFunctors, applicatives, monads
Functors, applicatives, monadsrkaippully
 
Fp in scala with adts
Fp in scala with adtsFp in scala with adts
Fp in scala with adtsHang Zhao
 
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
 
Monoids, monoids, monoids
Monoids, monoids, monoidsMonoids, monoids, monoids
Monoids, monoids, monoidsLuka Jacobowitz
 
Functional programming from its fundamentals
Functional programming from its fundamentalsFunctional programming from its fundamentals
Functional programming from its fundamentalsMauro Palsgraaf
 
The Essence of the Iterator Pattern
The Essence of the Iterator PatternThe Essence of the Iterator Pattern
The Essence of the Iterator PatternEric Torreborre
 
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)Eric Torreborre
 
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''OdessaJS Conf
 
Monad Transformers In The Wild
Monad Transformers In The WildMonad Transformers In The Wild
Monad Transformers In The WildStackMob Inc
 
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 notChengHui Weng
 
Use Applicative where applicable!
Use Applicative where applicable!Use Applicative where applicable!
Use Applicative where applicable!Hermann Hueck
 
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 expressionsJason Morgan
 
Fp in scala part 2
Fp in scala part 2Fp in scala part 2
Fp in scala part 2Hang Zhao
 

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

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, MadridDierk König
 
Greach, GroovyFx Workshop
Greach, GroovyFx WorkshopGreach, GroovyFx Workshop
Greach, GroovyFx WorkshopDierk König
 
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 JVMDierk König
 
Software Transactional Memory (STM) in Frege
Software Transactional Memory (STM) in Frege Software Transactional Memory (STM) in Frege
Software Transactional Memory (STM) in Frege Dierk König
 
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 FregeDierk König
 
Frege Tutorial at JavaOne 2015
Frege Tutorial at JavaOne 2015Frege Tutorial at JavaOne 2015
Frege Tutorial at JavaOne 2015Dierk König
 
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 JVMDierk König
 
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) Dierk König
 
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)Dierk König
 
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...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 2013Dierk 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 MobileDierk 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

Revolutionize Your Video Editing with InVideo.io: A Comprehensive Review
Revolutionize Your Video Editing with InVideo.io: A Comprehensive ReviewRevolutionize Your Video Editing with InVideo.io: A Comprehensive Review
Revolutionize Your Video Editing with InVideo.io: A Comprehensive Reviewjw364beach
 
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...OnePlan Solutions
 
Transform your Corporate Strategy Office - Harness OnePlan’s Strategic Portfo...
Transform your Corporate Strategy Office - Harness OnePlan’s Strategic Portfo...Transform your Corporate Strategy Office - Harness OnePlan’s Strategic Portfo...
Transform your Corporate Strategy Office - Harness OnePlan’s Strategic Portfo...OnePlan Solutions
 
Ronisha Informatics Private Limited Catalogue
Ronisha Informatics Private Limited CatalogueRonisha Informatics Private Limited Catalogue
Ronisha Informatics Private Limited Catalogueitservices996
 
Zer0con 2024 final share short version.pdf
Zer0con 2024 final share short version.pdfZer0con 2024 final share short version.pdf
Zer0con 2024 final share short version.pdfmaor17
 
The Ultimate Guide to Performance Testing in Low-Code, No-Code Environments (...
The Ultimate Guide to Performance Testing in Low-Code, No-Code Environments (...The Ultimate Guide to Performance Testing in Low-Code, No-Code Environments (...
The Ultimate Guide to Performance Testing in Low-Code, No-Code Environments (...kalichargn70th171
 
GraphSummit Madrid - Product Vision and Roadmap - Luis Salvador Neo4j
GraphSummit Madrid - Product Vision and Roadmap - Luis Salvador Neo4jGraphSummit Madrid - Product Vision and Roadmap - Luis Salvador Neo4j
GraphSummit Madrid - Product Vision and Roadmap - Luis Salvador Neo4jNeo4j
 
oracle 23c new features for developer and dba
oracle 23c new features for developer and dbaoracle 23c new features for developer and dba
oracle 23c new features for developer and dbaRemote DBA Services
 
OpenMetadata Community Meeting - 4th April, 2024
OpenMetadata Community Meeting - 4th April, 2024OpenMetadata Community Meeting - 4th April, 2024
OpenMetadata Community Meeting - 4th April, 2024OpenMetadata
 
Pros and Cons of Selenium In Automation Testing_ A Comprehensive Assessment.pdf
Pros and Cons of Selenium In Automation Testing_ A Comprehensive Assessment.pdfPros and Cons of Selenium In Automation Testing_ A Comprehensive Assessment.pdf
Pros and Cons of Selenium In Automation Testing_ A Comprehensive Assessment.pdfkalichargn70th171
 
Effectively Troubleshoot 9 Types of OutOfMemoryError
Effectively Troubleshoot 9 Types of OutOfMemoryErrorEffectively Troubleshoot 9 Types of OutOfMemoryError
Effectively Troubleshoot 9 Types of OutOfMemoryErrorTier1 app
 
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full RecordingOpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full RecordingShane Coughlan
 
[ CNCF Q1 2024 ] Intro to Continuous Profiling and Grafana Pyroscope.pdf
[ CNCF Q1 2024 ] Intro to Continuous Profiling and Grafana Pyroscope.pdf[ CNCF Q1 2024 ] Intro to Continuous Profiling and Grafana Pyroscope.pdf
[ CNCF Q1 2024 ] Intro to Continuous Profiling and Grafana Pyroscope.pdfSteve Caron
 
SAM Training Session - How to use EXCEL ?
SAM Training Session - How to use EXCEL ?SAM Training Session - How to use EXCEL ?
SAM Training Session - How to use EXCEL ?Alexandre Beguel
 
Mastering Project Planning with Microsoft Project 2016.pptx
Mastering Project Planning with Microsoft Project 2016.pptxMastering Project Planning with Microsoft Project 2016.pptx
Mastering Project Planning with Microsoft Project 2016.pptxAS Design & AST.
 
full course of software engineering mid term.pdf
full course of software engineering mid term.pdffull course of software engineering mid term.pdf
full course of software engineering mid term.pdfAbdul salam
 
Keeping your build tool updated in a multi repository world
Keeping your build tool updated in a multi repository worldKeeping your build tool updated in a multi repository world
Keeping your build tool updated in a multi repository worldRoberto Pérez Alcolea
 
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...OnePlan Solutions
 
What is Mendix and the concept of low-code development.docx
What is Mendix and the concept of low-code development.docxWhat is Mendix and the concept of low-code development.docx
What is Mendix and the concept of low-code development.docxTechnogeeks
 
Introduction to Firebase Workshop Slides
Introduction to Firebase Workshop SlidesIntroduction to Firebase Workshop Slides
Introduction to Firebase Workshop Slidesvaideheekore1
 

Dernier (20)

Revolutionize Your Video Editing with InVideo.io: A Comprehensive Review
Revolutionize Your Video Editing with InVideo.io: A Comprehensive ReviewRevolutionize Your Video Editing with InVideo.io: A Comprehensive Review
Revolutionize Your Video Editing with InVideo.io: A Comprehensive Review
 
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
 
Transform your Corporate Strategy Office - Harness OnePlan’s Strategic Portfo...
Transform your Corporate Strategy Office - Harness OnePlan’s Strategic Portfo...Transform your Corporate Strategy Office - Harness OnePlan’s Strategic Portfo...
Transform your Corporate Strategy Office - Harness OnePlan’s Strategic Portfo...
 
Ronisha Informatics Private Limited Catalogue
Ronisha Informatics Private Limited CatalogueRonisha Informatics Private Limited Catalogue
Ronisha Informatics Private Limited Catalogue
 
Zer0con 2024 final share short version.pdf
Zer0con 2024 final share short version.pdfZer0con 2024 final share short version.pdf
Zer0con 2024 final share short version.pdf
 
The Ultimate Guide to Performance Testing in Low-Code, No-Code Environments (...
The Ultimate Guide to Performance Testing in Low-Code, No-Code Environments (...The Ultimate Guide to Performance Testing in Low-Code, No-Code Environments (...
The Ultimate Guide to Performance Testing in Low-Code, No-Code Environments (...
 
GraphSummit Madrid - Product Vision and Roadmap - Luis Salvador Neo4j
GraphSummit Madrid - Product Vision and Roadmap - Luis Salvador Neo4jGraphSummit Madrid - Product Vision and Roadmap - Luis Salvador Neo4j
GraphSummit Madrid - Product Vision and Roadmap - Luis Salvador Neo4j
 
oracle 23c new features for developer and dba
oracle 23c new features for developer and dbaoracle 23c new features for developer and dba
oracle 23c new features for developer and dba
 
OpenMetadata Community Meeting - 4th April, 2024
OpenMetadata Community Meeting - 4th April, 2024OpenMetadata Community Meeting - 4th April, 2024
OpenMetadata Community Meeting - 4th April, 2024
 
Pros and Cons of Selenium In Automation Testing_ A Comprehensive Assessment.pdf
Pros and Cons of Selenium In Automation Testing_ A Comprehensive Assessment.pdfPros and Cons of Selenium In Automation Testing_ A Comprehensive Assessment.pdf
Pros and Cons of Selenium In Automation Testing_ A Comprehensive Assessment.pdf
 
Effectively Troubleshoot 9 Types of OutOfMemoryError
Effectively Troubleshoot 9 Types of OutOfMemoryErrorEffectively Troubleshoot 9 Types of OutOfMemoryError
Effectively Troubleshoot 9 Types of OutOfMemoryError
 
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full RecordingOpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
 
[ CNCF Q1 2024 ] Intro to Continuous Profiling and Grafana Pyroscope.pdf
[ CNCF Q1 2024 ] Intro to Continuous Profiling and Grafana Pyroscope.pdf[ CNCF Q1 2024 ] Intro to Continuous Profiling and Grafana Pyroscope.pdf
[ CNCF Q1 2024 ] Intro to Continuous Profiling and Grafana Pyroscope.pdf
 
SAM Training Session - How to use EXCEL ?
SAM Training Session - How to use EXCEL ?SAM Training Session - How to use EXCEL ?
SAM Training Session - How to use EXCEL ?
 
Mastering Project Planning with Microsoft Project 2016.pptx
Mastering Project Planning with Microsoft Project 2016.pptxMastering Project Planning with Microsoft Project 2016.pptx
Mastering Project Planning with Microsoft Project 2016.pptx
 
full course of software engineering mid term.pdf
full course of software engineering mid term.pdffull course of software engineering mid term.pdf
full course of software engineering mid term.pdf
 
Keeping your build tool updated in a multi repository world
Keeping your build tool updated in a multi repository worldKeeping your build tool updated in a multi repository world
Keeping your build tool updated in a multi repository world
 
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
 
What is Mendix and the concept of low-code development.docx
What is Mendix and the concept of low-code development.docxWhat is Mendix and the concept of low-code development.docx
What is Mendix and the concept of low-code development.docx
 
Introduction to Firebase Workshop Slides
Introduction to Firebase Workshop SlidesIntroduction to Firebase Workshop Slides
Introduction to Firebase Workshop Slides
 

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.