SlideShare uma empresa Scribd logo
1 de 32
MONADIC FUNCTIONAL
PROGRAMMING
Dean Hallman - August 2019
Twitter: @rdhallman,
Email: rdhallman@gmail.com
LinkedIn: www.linkedin.com/in/dean-hallman
F1() F2() F3() F4()
Function Composition
a b c d e
Higher-Order
Itemizer Functions
I2I1
F1 F2 F3 F4 F5 F6 F7 F8
V1 V2 V3 V4 V5 V6 V7
Vi
Vo
Functional Programming
F1 F2 F3 F4 F5 F6 F7 F8
V1 V2 V3 V4 V5 V6 V7
Vi
Vo
Functional Programming
Infix Notation:
let F = Compose(f1,f2,f3,f4)
F(a)
=> e
Prefix Notation:
(f4 (f3 (f2 (f1 a) ) ) )
=> e
Dot Notation??
f1(a).f2.f3.f4
=> e
F1 F2 F3 F4 F5 F6 F7 F8
V1 V2 V3 V4 V5 V6 V7
Vi
Vo
Monadic Functional Programming
M M M M M M M
M
Dot Chaining
Array
.of(1,2,3,4,5) # [1,2,3,4,5]
.filter((v) => v % 2 == 0) # [2, 4]
.map((v) => v * 3) # [6, 12]
.filter((v) => v % 4 == 0) # [12]
.length()
=> 1
TSA Scanner as Monad
F1 F2 F3 F4 F5 F6 F7 F8
V1 V2 V3 V4 V5 V6 V7
Vi
Vo
M M M M M M M
M
Cross-Functional State
NULL
Monadic Functional Programming:
The “Maybe Monad”
X X X
Maybe Monad
function getUserBanner(user)
{
return Maybe
.of(user)
.map(prop('accountDetails’))
.map(prop('address’))
.map(prop('province’))
.map(getProvinceBanner)
.default(“Property Undefined”);
}
Source: https://jrsinclair.com/articles/2016/marvellously-mysterious-javascript-maybe-monad/
The Rise of Inversion Of Control:
MTS, EJB & Transactional COM+
Inversion of Control &
Aspect-Oriented Programming
Main() Library
Main()
IoC Container
Before 2000 After 2000
Your Code Your CodeLibrary
PRE
POST
Event
Inversion of Control &
Aspect-Oriented Programming
Main() Library
Main()
IoC Container
Before 2000 After 2000
Your Code Your CodeLibrary
PRE
POST
Event
Inversion of Control &
Aspect-Oriented Programming
Main() Library
Main()
IoC Container
Before 2000 After 2000
Your Code Your CodeLibrary
PRE
POST
Event
F1F2F3F4F8
V1V2V3V4
ViVo
f f f f f f f f
V1 V2 V3 V4 V5 V6 V7
Vi
Vo
Super-Monadic Functional Programming
Run Time
Declaration
Time
.g1 .g2 .g3 .g4 .g5 .g6 .g7 .g8
Mf
Pipeline
Describe
Mf Mf Mf Mf Mf Mf Mf
F1 F2 F3 F4 F5 F6 F7 F8
V1 V2 V3 V4 V5 V6 V7
Vi
Vo
Monadic Functional Programming
M M M M M M M
M
The Monad Laws & Interface
• Left identity: return a >>= f ≡ f a
• Right identity: m >>= return ≡ m
if we take a value, put it in a default context with return and
then feed it to a function by using >>=, it’s the same as
just taking the value and applying the function to it.
if we have a monadic value and we use >>= to feed it
to return, the result is our original monadic value.
• Associativity: (m >>= f) >>= g ≡ m >>= (x -> f x >>= g)
when we have a chain of monadic function applications
with >>=, it shouldn’t matter how they’re nested.
• Required:
• unit
• bind
• Optional:
• of
• flatMap
• flatten, join
Fantasy Land Specification
Category Theory
a b c
f g
Category Theory
a b c
f g
a b c
f g
==
Category Theory
a b c
f g
M(a) M(b) M(c)
M(f) M(g)
==
Category Theory
a b c
f g
M(a) M(b) M(c)
M(f) M(g)
Category Theory
a b c
f g
M(a) M(b) M(c)
M(f) M(g)
unit, of
map, chain
flatten, join
map, chain
Category
Theory
a b c
f g
M(a) M(b) M(c)
M(f) M(g)
M(M(b)) M(M(c))
M(M(g))
map
flat, join
map
flat, join
Category
Theory
a b c
f g
M(a) M(b) M(c)
M(f) M(g)
M(M(b)) M(M(c))
M(M(g))
map
flat, join
map
M(M(d))
M(M(h))
map
d
M(d)
h
M(h)
Promise/A+ vs Monadic Promises
doSomethingAsync()
.then(doSomethingElseAsync())
.then(doAnotherThingAsync())
.then(doThirdThingAsync())
.catch(err => {
console.log(“Error: “ + err);
});
Monadic FP Complexity
◦ Learning Curve for the more advanced concepts
◦ Monadic lensing etc.
compose
F(x) F(x) F(x) F(x) F(x)
d
estack f
g
h
i
j
a
b
c
input
stack
stack
stack stack
stack
k
l
output
B, doc
Parameter Hiding & Monadic Lensing
F1 F2 F3 F4 F5 F6 F7 F8
V1 V2 V3 V4 V5 V6 V7
Vi
Vo
Monadic Functional Programming
M M M M M M M
M
Cross-Functional State
Surender
Thread
Surender
Thread
Railway-Oriented
Programming
◦ Extracting failure capture logic
◦ Either Monad
◦ Happy path
◦ Failure path
F1()
F2()
F3()
F4()
Recourse
Promise Waterfall with Recourse
a
b
c
d
e
Rejected
Promise Waterfall with Recourse
F1()
F2()
F3()
F4()
Recourse
a
b
c
d
e
Rejected
F1()
F2()
F3()
F4()
Recourse
f
g
h
i
Rejected
Recourse
Rejected
Recommendations & Questions
◦ “Oh Composable World”, by Brian Lansdorf

Mais conteúdo relacionado

Mais de All Things Open

Scaling Web Applications with Background
Scaling Web Applications with BackgroundScaling Web Applications with Background
Scaling Web Applications with Background
All Things Open
 
Build Developer Experience Teams for Open Source
Build Developer Experience Teams for Open SourceBuild Developer Experience Teams for Open Source
Build Developer Experience Teams for Open Source
All Things Open
 
Sudo – Giving access while staying in control
Sudo – Giving access while staying in controlSudo – Giving access while staying in control
Sudo – Giving access while staying in control
All Things Open
 
Fortifying the Future: Tackling Security Challenges in AI/ML Applications
Fortifying the Future: Tackling Security Challenges in AI/ML ApplicationsFortifying the Future: Tackling Security Challenges in AI/ML Applications
Fortifying the Future: Tackling Security Challenges in AI/ML Applications
All Things Open
 
Securing Cloud Resources Deployed with Control Planes on Kubernetes using Gov...
Securing Cloud Resources Deployed with Control Planes on Kubernetes using Gov...Securing Cloud Resources Deployed with Control Planes on Kubernetes using Gov...
Securing Cloud Resources Deployed with Control Planes on Kubernetes using Gov...
All Things Open
 
System Design on Easy Mode
System Design on Easy ModeSystem Design on Easy Mode
System Design on Easy Mode
All Things Open
 
Linux Distribution Collaboration …on a Mainframe!
Linux Distribution Collaboration …on a Mainframe!Linux Distribution Collaboration …on a Mainframe!
Linux Distribution Collaboration …on a Mainframe!
All Things Open
 
Imagined Dragons: Building an Imagination-Powered Music Recommendation Platform
Imagined Dragons: Building an Imagination-Powered Music Recommendation PlatformImagined Dragons: Building an Imagination-Powered Music Recommendation Platform
Imagined Dragons: Building an Imagination-Powered Music Recommendation Platform
All Things Open
 

Mais de All Things Open (20)

Spinning Your Drones with Cadence Workflows, Apache Kafka and TensorFlow
 Spinning Your Drones with Cadence Workflows, Apache Kafka and TensorFlow Spinning Your Drones with Cadence Workflows, Apache Kafka and TensorFlow
Spinning Your Drones with Cadence Workflows, Apache Kafka and TensorFlow
 
DEI Challenges and Success
DEI Challenges and SuccessDEI Challenges and Success
DEI Challenges and Success
 
Scaling Web Applications with Background
Scaling Web Applications with BackgroundScaling Web Applications with Background
Scaling Web Applications with Background
 
Supercharging tutorials with WebAssembly
Supercharging tutorials with WebAssemblySupercharging tutorials with WebAssembly
Supercharging tutorials with WebAssembly
 
Using SQL to Find Needles in Haystacks
Using SQL to Find Needles in HaystacksUsing SQL to Find Needles in Haystacks
Using SQL to Find Needles in Haystacks
 
Configuration Security as a Game of Pursuit Intercept
Configuration Security as a Game of Pursuit InterceptConfiguration Security as a Game of Pursuit Intercept
Configuration Security as a Game of Pursuit Intercept
 
Scaling an Open Source Sponsorship Program
Scaling an Open Source Sponsorship ProgramScaling an Open Source Sponsorship Program
Scaling an Open Source Sponsorship Program
 
Build Developer Experience Teams for Open Source
Build Developer Experience Teams for Open SourceBuild Developer Experience Teams for Open Source
Build Developer Experience Teams for Open Source
 
Deploying Models at Scale with Apache Beam
Deploying Models at Scale with Apache BeamDeploying Models at Scale with Apache Beam
Deploying Models at Scale with Apache Beam
 
Sudo – Giving access while staying in control
Sudo – Giving access while staying in controlSudo – Giving access while staying in control
Sudo – Giving access while staying in control
 
Fortifying the Future: Tackling Security Challenges in AI/ML Applications
Fortifying the Future: Tackling Security Challenges in AI/ML ApplicationsFortifying the Future: Tackling Security Challenges in AI/ML Applications
Fortifying the Future: Tackling Security Challenges in AI/ML Applications
 
Securing Cloud Resources Deployed with Control Planes on Kubernetes using Gov...
Securing Cloud Resources Deployed with Control Planes on Kubernetes using Gov...Securing Cloud Resources Deployed with Control Planes on Kubernetes using Gov...
Securing Cloud Resources Deployed with Control Planes on Kubernetes using Gov...
 
Building AlmaLinux OS without RHEL sources code
Building AlmaLinux OS without RHEL sources codeBuilding AlmaLinux OS without RHEL sources code
Building AlmaLinux OS without RHEL sources code
 
Open Source evaluation: A comprehensive guide on what you are using
Open Source evaluation: A comprehensive guide on what you are usingOpen Source evaluation: A comprehensive guide on what you are using
Open Source evaluation: A comprehensive guide on what you are using
 
System Design on Easy Mode
System Design on Easy ModeSystem Design on Easy Mode
System Design on Easy Mode
 
Introduction to GitHub Copilot
Introduction to GitHub CopilotIntroduction to GitHub Copilot
Introduction to GitHub Copilot
 
Linux Distribution Collaboration …on a Mainframe!
Linux Distribution Collaboration …on a Mainframe!Linux Distribution Collaboration …on a Mainframe!
Linux Distribution Collaboration …on a Mainframe!
 
Know Your Data: The stats behind your alerts
Know Your Data: The stats behind your alertsKnow Your Data: The stats behind your alerts
Know Your Data: The stats behind your alerts
 
Imagined Dragons: Building an Imagination-Powered Music Recommendation Platform
Imagined Dragons: Building an Imagination-Powered Music Recommendation PlatformImagined Dragons: Building an Imagination-Powered Music Recommendation Platform
Imagined Dragons: Building an Imagination-Powered Music Recommendation Platform
 
The Path to Real-time Data Integration with Open Source
The Path to Real-time Data Integration with Open SourceThe Path to Real-time Data Integration with Open Source
The Path to Real-time Data Integration with Open Source
 

Último

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 

Último (20)

Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Cyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfCyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdf
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
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
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 

Intermediate Topics in Functional Programming

  • 1. MONADIC FUNCTIONAL PROGRAMMING Dean Hallman - August 2019 Twitter: @rdhallman, Email: rdhallman@gmail.com LinkedIn: www.linkedin.com/in/dean-hallman
  • 2. F1() F2() F3() F4() Function Composition a b c d e Higher-Order Itemizer Functions I2I1
  • 3. F1 F2 F3 F4 F5 F6 F7 F8 V1 V2 V3 V4 V5 V6 V7 Vi Vo Functional Programming
  • 4. F1 F2 F3 F4 F5 F6 F7 F8 V1 V2 V3 V4 V5 V6 V7 Vi Vo Functional Programming Infix Notation: let F = Compose(f1,f2,f3,f4) F(a) => e Prefix Notation: (f4 (f3 (f2 (f1 a) ) ) ) => e Dot Notation?? f1(a).f2.f3.f4 => e
  • 5. F1 F2 F3 F4 F5 F6 F7 F8 V1 V2 V3 V4 V5 V6 V7 Vi Vo Monadic Functional Programming M M M M M M M M
  • 6. Dot Chaining Array .of(1,2,3,4,5) # [1,2,3,4,5] .filter((v) => v % 2 == 0) # [2, 4] .map((v) => v * 3) # [6, 12] .filter((v) => v % 4 == 0) # [12] .length() => 1
  • 8. F1 F2 F3 F4 F5 F6 F7 F8 V1 V2 V3 V4 V5 V6 V7 Vi Vo M M M M M M M M Cross-Functional State NULL Monadic Functional Programming: The “Maybe Monad” X X X
  • 9. Maybe Monad function getUserBanner(user) { return Maybe .of(user) .map(prop('accountDetails’)) .map(prop('address’)) .map(prop('province’)) .map(getProvinceBanner) .default(“Property Undefined”); } Source: https://jrsinclair.com/articles/2016/marvellously-mysterious-javascript-maybe-monad/
  • 10. The Rise of Inversion Of Control: MTS, EJB & Transactional COM+
  • 11. Inversion of Control & Aspect-Oriented Programming Main() Library Main() IoC Container Before 2000 After 2000 Your Code Your CodeLibrary PRE POST Event
  • 12. Inversion of Control & Aspect-Oriented Programming Main() Library Main() IoC Container Before 2000 After 2000 Your Code Your CodeLibrary PRE POST Event
  • 13. Inversion of Control & Aspect-Oriented Programming Main() Library Main() IoC Container Before 2000 After 2000 Your Code Your CodeLibrary PRE POST Event F1F2F3F4F8 V1V2V3V4 ViVo
  • 14. f f f f f f f f V1 V2 V3 V4 V5 V6 V7 Vi Vo Super-Monadic Functional Programming Run Time Declaration Time .g1 .g2 .g3 .g4 .g5 .g6 .g7 .g8 Mf Pipeline Describe Mf Mf Mf Mf Mf Mf Mf
  • 15. F1 F2 F3 F4 F5 F6 F7 F8 V1 V2 V3 V4 V5 V6 V7 Vi Vo Monadic Functional Programming M M M M M M M M
  • 16. The Monad Laws & Interface • Left identity: return a >>= f ≡ f a • Right identity: m >>= return ≡ m if we take a value, put it in a default context with return and then feed it to a function by using >>=, it’s the same as just taking the value and applying the function to it. if we have a monadic value and we use >>= to feed it to return, the result is our original monadic value. • Associativity: (m >>= f) >>= g ≡ m >>= (x -> f x >>= g) when we have a chain of monadic function applications with >>=, it shouldn’t matter how they’re nested. • Required: • unit • bind • Optional: • of • flatMap • flatten, join
  • 19. Category Theory a b c f g a b c f g ==
  • 20. Category Theory a b c f g M(a) M(b) M(c) M(f) M(g) ==
  • 21. Category Theory a b c f g M(a) M(b) M(c) M(f) M(g)
  • 22. Category Theory a b c f g M(a) M(b) M(c) M(f) M(g) unit, of map, chain flatten, join map, chain
  • 23. Category Theory a b c f g M(a) M(b) M(c) M(f) M(g) M(M(b)) M(M(c)) M(M(g)) map flat, join map flat, join
  • 24. Category Theory a b c f g M(a) M(b) M(c) M(f) M(g) M(M(b)) M(M(c)) M(M(g)) map flat, join map M(M(d)) M(M(h)) map d M(d) h M(h)
  • 25. Promise/A+ vs Monadic Promises doSomethingAsync() .then(doSomethingElseAsync()) .then(doAnotherThingAsync()) .then(doThirdThingAsync()) .catch(err => { console.log(“Error: “ + err); });
  • 26. Monadic FP Complexity ◦ Learning Curve for the more advanced concepts ◦ Monadic lensing etc.
  • 27. compose F(x) F(x) F(x) F(x) F(x) d estack f g h i j a b c input stack stack stack stack stack k l output B, doc Parameter Hiding & Monadic Lensing
  • 28. F1 F2 F3 F4 F5 F6 F7 F8 V1 V2 V3 V4 V5 V6 V7 Vi Vo Monadic Functional Programming M M M M M M M M Cross-Functional State Surender Thread Surender Thread
  • 29. Railway-Oriented Programming ◦ Extracting failure capture logic ◦ Either Monad ◦ Happy path ◦ Failure path
  • 31. Promise Waterfall with Recourse F1() F2() F3() F4() Recourse a b c d e Rejected F1() F2() F3() F4() Recourse f g h i Rejected Recourse Rejected
  • 32. Recommendations & Questions ◦ “Oh Composable World”, by Brian Lansdorf

Notas do Editor

  1. By-Value / Containment Monads Co-propagation Utilizing OO and FP
  2. Returning this
  3. By-Value / Containment Monads Value VS State Co-propagation of Value & State Monads are a way of avoiding side-effects, by converting cross-functional state into return values
  4. ? Syntax in Kotlin and C#
  5. Kubernetes & Istio
  6. Kubernetes & Istio
  7. Kubernetes & Istio ETL & Node
  8. By-Reference / Generated Monads
  9. By-Value / Containment Monads
  10. RxJava, RxJs, Bacon etc.
  11. Like in math, multiply both sides of =
  12. Chain, join Extending Subscribing Fluture
  13. By-Value / Containment Monads