SlideShare a Scribd company logo
1 of 59
Download to read offline
Applicative style
programming
jlgarhdez HaskellMADpepegar
Pepe García
Scala Engineer @ Fidesmo
Co-organizer @ HaskellMad & FP-MAD
Scala fan
Haskell Acolyth
Functional Programming Devotee
jlgarhdez HaskellMADpepegar
About this talk
Slides are available here:
http://es.slideshare.net/JosLuisGarcaHernndez/applicative-style-programming
Code is available here:
https://github.com/pepegar/Control.Applicative.Talk
jlgarhdez HaskellMADpepegar
What are we learning today?
● What are applicatives?
● When do we use them?
● Who is using them?
● How to use them?
jlgarhdez HaskellMADpepegar
What are applicatives?
Applicatives are an abstraction between functions and monads. And they open the
doors for the so called applicative style!
jlgarhdez HaskellMADpepegar
What are applicatives?
@jlgarhdez
class Functor f => Applicative f where
pure :: a -> f a
(<*>) :: f (a -> b) -> f a -> f b
jlgarhdez HaskellMADpepegar
What are applicatives?
Let’s understand the typeclass:
It exposes a `pure` function, that puts a value inside the applicative context
And a `<*>` function, that applies a function inside an applicative context to the
content of another value with the same applicative context
jlgarhdez HaskellMADpepegar
What are applicatives?
Do you remember fmap from functors? Try to compare it with <*>
fmap :: (a -> b) -> f a -> f b
(<*>) :: f (a -> b) -> f a -> f b
jlgarhdez HaskellMADpepegar
What are applicatives?
Basically an Applicative is a Functor that can also apply a function contained in a
container to other container of the same type.
Prelude Control.Monad> let fn1 =  x-> x * x
Prelude Control.Monad> let fn2 =  x -> x + 33
Prelude Control.Monad> let fns = [fn1, fn2]
Prelude Control.Monad> :t fns
fns :: Num a => [a -> a]
Prelude Control.Monad> let nums = [1,2,3,4]
Prelude Control.Monad> :t nums
nums :: Num t => [t]
Prelude Control.Monad> fns <*> nums
jlgarhdez HaskellMADpepegar
A small example
As an example of Applicative Programming, let’s see how Applicative and
Monadic styles compare:
jlgarhdez HaskellMADpepegar
A small example
data Person = Person {
name :: String,
lastName :: String
} deriving Show
data Err = String
validateName :: String -> Either Err String
validateName n = if n == "Pepe" then Right n
else Left "name is not Pepe"
validateLastName :: String -> Either Err String
validateLastName l = if l == "García" then Right l
else Left "last name is not García"
jlgarhdez HaskellMADpepegar
A small example. Monadic
validatePersonM :: String -> String -> Either String Person
validatePersonM n l = do
vName <- validateName n
vLast <- validateLastName l
return $ Person vName vLast
jlgarhdez HaskellMADpepegar
A small example. Applicative
validatePersonA :: String -> String -> Either String Person
validatePersonA n l = Person <$> validateName n <*> validateLastName l
jlgarhdez HaskellMADpepegar
A small example. Applicative (2)
Why does the previous example work? Let’s dig a bit deeper on it:
Prelude> :t Person
Person :: String -> String -> Person
Prelude> :t Person <$> validateName "pepe"
Person <$> validateName "pepe" :: Either String (String -> Person)
Prelude> :t Person <$> validateName "pepe" <*> validateLastName "Garcia"
Person <$> validateName "pepe" <*> validateLastName "Garcia" :: Either String Person
jlgarhdez HaskellMADpepegar
A small example. Recap
So, what do we learn from our small example?
Monads are not a silver bullet, and can be replaced easily by Applicative Functors
when ordering is not important.
jlgarhdez HaskellMADpepegar
When to use applicatives?
When we need more than a functor, and less than a monad…
Sounds appealing, but WTF is this!?
jlgarhdez HaskellMADpepegar
When to use applicatives? Functor
Functor defines containers that can be mapped over.
class Functor f where
fmap :: (a -> b) -> f a -> f b
jlgarhdez HaskellMADpepegar
When to use applicatives? Monad
Monad defines containers allow operation sequencing!
class Monad m where
(>>=) :: m a -> (a -> m b) -> m b
(>>) :: m a -> m b -> m b
return :: a -> m a
fail :: String -> m a
jlgarhdez HaskellMADpepegar
When to use applicatives?
Some rule-o-thumbs I use for detecting Monad overkill
● import Control.Applicative
● replace return with pure, liftM with (<$>) (or fmap or liftA), liftM2 with liftA2,
etc, and ap with (<*>)
● If your function signature was Monad m => ..., change to Applicative m => ...
(and maybe rename m to f or whatever).
jlgarhdez HaskellMADpepegar
When your do block can be substituted by liftM2/3/4...
When to use applicatives?
validatePersonM :: String -> String -> Either String Person
validatePersonM n l = do
vName <- validateName n
vLast <- validateLastName l
return $ Person vName vLast
becomes
validatePersonA :: String -> String -> Either String Person
validatePersonA n l = liftM2 Person $ validateName n $ validateLastName l
jlgarhdez HaskellMADpepegar
When your operations does not depend on each other
When to use applicatives?
validatePersonM :: String -> String -> Either String Person
validatePersonM n l = do
vName <- validateName n
vLast <- validateLastName l
return $ Person vName vLast
becomes
validatePersonA :: String -> String -> Either String Person
validatePersonA n l = Person <$> validateName n <*> validateLastName l
jlgarhdez HaskellMADpepegar
Who uses Applicative?
Applicative is being used all over the place nowadays. Some interesting examples
of its use are:
● HAXL. Efficient data access
● CmdTheLine. Command line argument parsing
● Validation. Data validation library
● Attoparsec. ByteString parsing library
jlgarhdez HaskellMADpepegar
How to use Applicatives?
@jlgarhdez
Applicatives are really easy to use. You just need to provide an instance of the
typeclass for your data type.
data Maybe a = Just a
| Nothing
instance Applicative Maybe where
pure = Just
Just f <*> m = fmap f m
Nothing <*> _m = Nothing
jlgarhdez HaskellMADpepegar
Or, you can go Free!
jlgarhdez HaskellMADpepegar
Free Applicatives
Create an applicative from any Functor
jlgarhdez HaskellMADpepegar
Free Applicatives.
data Ap f a where
Pure :: a -> Ap f a
Ap :: f a -> Ap f (a -> b) -> Ap f b
@jlgarhdez
jlgarhdez HaskellMADpepegar
Free Applicatives.
instance Functor (Ap f) where
fmap f (Pure a) = Pure (f a)
fmap f (Ap x y) = Ap x ((f .) <$> y)
instance Apply (Ap f) where
Pure f <.> y = fmap f y
Ap x y <.> z = Ap x (flip <$> y <.> z)
instance Applicative (Ap f) where
pure = Pure
Pure f <*> y = fmap f y
Ap x y <*> z = Ap x (flip <$> y <*> z)
@jlgarhdez
jlgarhdez HaskellMADpepegar
Free applicatives
@jlgarhdez
Now let’s create a small ADT and create a Free Applicative out of it!
import Control.Applicative.Free
import Control.Applicative
type Author = String
type Post = String
type Id = String
data BlogF a where
GetPost :: Id -> BlogF Post
GetAuthor :: Id -> BlogF Author
type Blog a = Ap BlogF a
jlgarhdez HaskellMADpepegar
And a bit of syntax…
getPost :: Id -> Blog Post
getPost id = liftAp $ GetPost id
getAuthor :: Id -> Blog Author
getAuthor id = liftAp $ GetAuthor id
Free applicatives
@jlgarhdez
jlgarhdez HaskellMADpepegar
Free applicatives
@jlgarhdez
Now, we might want to render a page of our blog:
data Page = Page {
post :: Post,
author :: Author
} deriving Show
jlgarhdez HaskellMADpepegar
Now, let’s do Applicative magic!
jlgarhdez HaskellMADpepegar
Free applicatives
@jlgarhdez
With all we have already learned, how would you implement the following?
renderPage :: Id -> Id -> Blog Page
jlgarhdez HaskellMADpepegar
Free applicatives
@jlgarhdez
With all we have already learned, how would you implement the following?
renderPage :: Id -> Id -> Blog Page
renderPage postId authorId = Page <$> getPost postId
<*> getAuthor authorId
jlgarhdez HaskellMADpepegar
jlgarhdez HaskellMADpepegar
But we are not doing anything in that
renderPage function
jlgarhdez HaskellMADpepegar
Let’s do, let’s interpret!
jlgarhdez HaskellMADpepegar
Free applicatives
@jlgarhdez
First, we need an interpreter. This interpreter is called Natural Transformation in
Category Theory, and transforms a value `f a` into a `g a`.
interpIO :: BlogF a -> IO a
interpIO (GetPost id) = putStrLn ("getting post " ++ show id ++ " from DB") *> pure "this is the post"
interpIO (GetAuthor id) = putStrLn ("getting author " ++ show id ++ " from DB") *> pure "Pepe García"
jlgarhdez HaskellMADpepegar
Free applicatives
@jlgarhdez
And, last but not least, wire everything together:
main :: IO ()
main = do
page <- runAp interpIO $ renderPage 1 1
print page
jlgarhdez HaskellMADpepegar
Abstract Syntax Trees
As you know, the most important part of functional programming is referential
transparency. This means creating values, not executing effects.
That’s what our free applicatives does, they create little programs, or Abstract
Syntax Trees
jlgarhdez HaskellMADpepegar
Applicatives AST
As you imagine from our
implementation, this Blog does
nothing, just create values. It does
not go to the DB to fetch
users/posts/whatever.
It just creates what is called an
Abstract Syntax Tree
jlgarhdez HaskellMADpepegar
Monadic AST
And, if we compare this AST with the
one created by its monadic
counterpart, implemented in terms of
monadic bind, we can see something
interesting.
We need to evaluate getPost 1 in
order to evaluate getAuthor 1
jlgarhdez HaskellMADpepegar
Applicative AST. Static Analysis
@jlgarhdez
Applicative functors’ ASTs allow you to explore them from the top down without
evaluating a single line of code! This technique is called static analysis, and is
awesome for:
● Optimizing performance (deduplicate requests, in a series of HTTP requests)
● Calculate dependencies automatically
● Lint your code
jlgarhdez HaskellMADpepegar
Static Analysis
Analysis of a program that does not evaluate the program.
It is possible with our Applicatives!
jlgarhdez HaskellMADpepegar
Static Analysis
@jlgarhdez
To demonstrate static analysis, let’s keep with our blog example:
data BlogF a where
GetPost :: Id -> BlogF Post
GetAuthor :: Id -> BlogF Author
GetComments :: Id -> BlogF [(Comment, Author)]
type Blog a = Ap BlogF a
jlgarhdez HaskellMADpepegar
Static Analysis
@jlgarhdez
Also some convenience functions:
getPost :: Id -> Blog Post
getPost id = liftAp $ GetPost id
getAuthor :: Id -> Blog Author
getAuthor id = liftAp $ GetAuthor id
getComments :: Id -> Blog [(Comment, Author)]
getComments id = liftAp $ GetComments id
jlgarhdez HaskellMADpepegar
Static Analysis
@jlgarhdez
Then, our renderPage would look like this:
renderPage :: Id -> Id -> Blog Page
renderPage post author = Page <$> getPost post
<*> getAuthor author
<*> getComments post
jlgarhdez HaskellMADpepegar
Static Analysis
@jlgarhdez
And, as explained before, we will need to interpret the AST
interpIO :: BlogF a -> IO a
interpIO (GetPost id) = putStrLn ("getting post " ++ show id ++ " from DB") *> pure "this is the
post"
interpIO (GetAuthor id) = putStrLn ("getting author " ++ show id ++ " from DB") *> pure "@pepe"
interpIO (GetComments id) = putStrLn ("getting comments for post " ++ show id ++ " from DB")
*> pure [
("this post rocks" , "@anler"),
("you're right, @anler" , "@lorenzo"),
("Oh boy, I love haskell so bad!" , "@dani"),
("Indeed, Haskell is better than Erlang!" , "@joseluis" )
]
jlgarhdez HaskellMADpepegar
Static Analysis
@jlgarhdez
But, the most amazing thing is that we can calculate the number of operations that
our renderProgram does, for example:
instance Monoid Int where
mempty = 0
mappend = (+)
countInstructions :: BlogF a -> Int
countInstructions _ = 1
jlgarhdez HaskellMADpepegar
Static Analysis
@jlgarhdez
But, the most amazing thing is that we can calculate the number of operations that
our renderProgram does, for example:
main :: IO ()
main = do
putStrLn "NUMBER OF REQUESTS TO THE DB:"
print instructions
putStrLn ""
putStrLn "PAGE RENDERING:"
page <- runAp interpIO page
print page
where instructions = runAp_ countInstructions page
page = renderPage 1 1
jlgarhdez HaskellMADpepegar
Composing Applicative
Functors
Unlike Monads, our Applicatives are closed on composition. This means that you
can provide an instance of Applicative for the product of any other two
applicatives!
jlgarhdez HaskellMADpepegar
Composing Applicative Functors
@jlgarhdez
data Product m n a = Product {
first :: m a,
second :: n a
}
instance (Functor m, Functor n) => Functor (Product m n) where
fmap f fa = Product (f <$> first fa) (f <$> second fa)
instance (Applicative m, Applicative n) => Applicative (Product m n) where
pure x = Product (pure x) (pure x)
mf <*> mx = Product (first mf <*> first mx) (second mf <*> second mx)
jlgarhdez HaskellMADpepegar
Applicative Do Notation
Created at Facebook, ApplicativeDo extension allows you to write Applicative
Code in a more familiar fashion, if you come from imperative programming.
jlgarhdez HaskellMADpepegar
{-# LANGUAGE ApplicativeDo #-}
renderPage :: Id -> Id -> Blog Page
renderPage postId authorId = do
post <- getPost postId
author <- getAuthor authorId
return $ Page post author
ApplicativeDo
@jlgarhdez
jlgarhdez HaskellMADpepegar
Recap
jlgarhdez HaskellMADpepegar
Recap
● Use more applicatives, they DO ROCK!
● Always separate domain modelling from interpretation (with free applicatives)
● Eliminate boilerplate with meta-programming (with static analysis)
● Keep writing your code in do-block style (with ApplicativeDo)
● Have fun!
● Bonus point: Convince your boss to use Haskell!
jlgarhdez HaskellMADpepegar
Bibliography
● Idioms: applicative programming with effects. McBride, Paterson
● The Essence of the Iterator Pattern. Gibbons, Oliveira
● Origami programming. Gibbons
● Static Analysis with Applicatives. Gergő Érdi
jlgarhdez HaskellMADpepegar
Applicative style programming
Applicative style programming

More Related Content

What's hot

Ad hoc Polymorphism using Type Classes and Cats
Ad hoc Polymorphism using Type Classes and CatsAd hoc Polymorphism using Type Classes and Cats
Ad hoc Polymorphism using Type Classes and CatsPhilip Schwarz
 
The aggregate function - from sequential and parallel folds to parallel aggre...
The aggregate function - from sequential and parallel folds to parallel aggre...The aggregate function - from sequential and parallel folds to parallel aggre...
The aggregate function - from sequential and parallel folds to parallel aggre...Philip Schwarz
 
ZIO-Direct - Functional Scala 2022
ZIO-Direct - Functional Scala 2022ZIO-Direct - Functional Scala 2022
ZIO-Direct - Functional Scala 2022Alexander Ioffe
 
Taking your side effects aside
Taking your side effects asideTaking your side effects aside
Taking your side effects aside💡 Tomasz Kogut
 
A Prelude of Purity: Scaling Back ZIO
A Prelude of Purity: Scaling Back ZIOA Prelude of Purity: Scaling Back ZIO
A Prelude of Purity: Scaling Back ZIOJorge Vásquez
 
Sum and Product Types - The Fruit Salad & Fruit Snack Example - From F# to Ha...
Sum and Product Types -The Fruit Salad & Fruit Snack Example - From F# to Ha...Sum and Product Types -The Fruit Salad & Fruit Snack Example - From F# to Ha...
Sum and Product Types - The Fruit Salad & Fruit Snack Example - From F# to Ha...Philip Schwarz
 
Monoids - Part 1 - with examples using Scalaz and Cats
Monoids - Part 1 - with examples using Scalaz and CatsMonoids - Part 1 - with examples using Scalaz and Cats
Monoids - Part 1 - with examples using Scalaz and CatsPhilip Schwarz
 
Railway Oriented Programming
Railway Oriented ProgrammingRailway Oriented Programming
Railway Oriented ProgrammingScott Wlaschin
 
Peeking inside the engine of ZIO SQL.pdf
Peeking inside the engine of ZIO SQL.pdfPeeking inside the engine of ZIO SQL.pdf
Peeking inside the engine of ZIO SQL.pdfJaroslavRegec1
 
Quill vs Slick Smackdown
Quill vs Slick SmackdownQuill vs Slick Smackdown
Quill vs Slick SmackdownAlexander Ioffe
 
Be Smart, Constrain Your Types to Free Your Brain!
Be Smart, Constrain Your Types to Free Your Brain!Be Smart, Constrain Your Types to Free Your Brain!
Be Smart, Constrain Your Types to Free Your Brain!Jorge Vásquez
 
Object Calisthenics Applied to PHP
Object Calisthenics Applied to PHPObject Calisthenics Applied to PHP
Object Calisthenics Applied to PHPGuilherme Blanco
 
Functional Programming Patterns (BuildStuff '14)
Functional Programming Patterns (BuildStuff '14)Functional Programming Patterns (BuildStuff '14)
Functional Programming Patterns (BuildStuff '14)Scott Wlaschin
 
Implementing the IO Monad in Scala
Implementing the IO Monad in ScalaImplementing the IO Monad in Scala
Implementing the IO Monad in ScalaHermann Hueck
 
Boost your productivity with Scala tooling!
Boost your productivity  with Scala tooling!Boost your productivity  with Scala tooling!
Boost your productivity with Scala tooling!MeriamLachkar1
 
Parboiled explained
Parboiled explainedParboiled explained
Parboiled explainedPaul Popoff
 
The Power of Composition (NDC Oslo 2020)
The Power of Composition (NDC Oslo 2020)The Power of Composition (NDC Oslo 2020)
The Power of Composition (NDC Oslo 2020)Scott Wlaschin
 
Scala 3 by Example - Algebraic Data Types for Domain Driven Design - Part 2
Scala 3 by Example - Algebraic Data Types for Domain Driven Design - Part 2Scala 3 by Example - Algebraic Data Types for Domain Driven Design - Part 2
Scala 3 by Example - Algebraic Data Types for Domain Driven Design - Part 2Philip Schwarz
 
The lazy programmer's guide to writing thousands of tests
The lazy programmer's guide to writing thousands of testsThe lazy programmer's guide to writing thousands of tests
The lazy programmer's guide to writing thousands of testsScott Wlaschin
 

What's hot (20)

Ad hoc Polymorphism using Type Classes and Cats
Ad hoc Polymorphism using Type Classes and CatsAd hoc Polymorphism using Type Classes and Cats
Ad hoc Polymorphism using Type Classes and Cats
 
The aggregate function - from sequential and parallel folds to parallel aggre...
The aggregate function - from sequential and parallel folds to parallel aggre...The aggregate function - from sequential and parallel folds to parallel aggre...
The aggregate function - from sequential and parallel folds to parallel aggre...
 
ZIO-Direct - Functional Scala 2022
ZIO-Direct - Functional Scala 2022ZIO-Direct - Functional Scala 2022
ZIO-Direct - Functional Scala 2022
 
Taking your side effects aside
Taking your side effects asideTaking your side effects aside
Taking your side effects aside
 
A Prelude of Purity: Scaling Back ZIO
A Prelude of Purity: Scaling Back ZIOA Prelude of Purity: Scaling Back ZIO
A Prelude of Purity: Scaling Back ZIO
 
Sum and Product Types - The Fruit Salad & Fruit Snack Example - From F# to Ha...
Sum and Product Types -The Fruit Salad & Fruit Snack Example - From F# to Ha...Sum and Product Types -The Fruit Salad & Fruit Snack Example - From F# to Ha...
Sum and Product Types - The Fruit Salad & Fruit Snack Example - From F# to Ha...
 
Monoids - Part 1 - with examples using Scalaz and Cats
Monoids - Part 1 - with examples using Scalaz and CatsMonoids - Part 1 - with examples using Scalaz and Cats
Monoids - Part 1 - with examples using Scalaz and Cats
 
Railway Oriented Programming
Railway Oriented ProgrammingRailway Oriented Programming
Railway Oriented Programming
 
Peeking inside the engine of ZIO SQL.pdf
Peeking inside the engine of ZIO SQL.pdfPeeking inside the engine of ZIO SQL.pdf
Peeking inside the engine of ZIO SQL.pdf
 
Quill vs Slick Smackdown
Quill vs Slick SmackdownQuill vs Slick Smackdown
Quill vs Slick Smackdown
 
Be Smart, Constrain Your Types to Free Your Brain!
Be Smart, Constrain Your Types to Free Your Brain!Be Smart, Constrain Your Types to Free Your Brain!
Be Smart, Constrain Your Types to Free Your Brain!
 
Object Calisthenics Applied to PHP
Object Calisthenics Applied to PHPObject Calisthenics Applied to PHP
Object Calisthenics Applied to PHP
 
Functional Programming Patterns (BuildStuff '14)
Functional Programming Patterns (BuildStuff '14)Functional Programming Patterns (BuildStuff '14)
Functional Programming Patterns (BuildStuff '14)
 
Applicative Functor
Applicative FunctorApplicative Functor
Applicative Functor
 
Implementing the IO Monad in Scala
Implementing the IO Monad in ScalaImplementing the IO Monad in Scala
Implementing the IO Monad in Scala
 
Boost your productivity with Scala tooling!
Boost your productivity  with Scala tooling!Boost your productivity  with Scala tooling!
Boost your productivity with Scala tooling!
 
Parboiled explained
Parboiled explainedParboiled explained
Parboiled explained
 
The Power of Composition (NDC Oslo 2020)
The Power of Composition (NDC Oslo 2020)The Power of Composition (NDC Oslo 2020)
The Power of Composition (NDC Oslo 2020)
 
Scala 3 by Example - Algebraic Data Types for Domain Driven Design - Part 2
Scala 3 by Example - Algebraic Data Types for Domain Driven Design - Part 2Scala 3 by Example - Algebraic Data Types for Domain Driven Design - Part 2
Scala 3 by Example - Algebraic Data Types for Domain Driven Design - Part 2
 
The lazy programmer's guide to writing thousands of tests
The lazy programmer's guide to writing thousands of testsThe lazy programmer's guide to writing thousands of tests
The lazy programmer's guide to writing thousands of tests
 

Viewers also liked

Social media (ICT Month)
Social media (ICT Month)Social media (ICT Month)
Social media (ICT Month)Ruben Canlas
 
2ª Sesión Aprende a Cuidarte: Doctor estoy infoxicado, ¿es grave? Prevención,...
2ª Sesión Aprende a Cuidarte: Doctor estoy infoxicado, ¿es grave? Prevención,...2ª Sesión Aprende a Cuidarte: Doctor estoy infoxicado, ¿es grave? Prevención,...
2ª Sesión Aprende a Cuidarte: Doctor estoy infoxicado, ¿es grave? Prevención,...Kronikoen Sarea La red vasca de pacientes
 
Weblogs
WeblogsWeblogs
Weblogsjuancm
 
Bitcoin: salto tecnológico en los sistemas de pago
Bitcoin: salto tecnológico en los sistemas de pagoBitcoin: salto tecnológico en los sistemas de pago
Bitcoin: salto tecnológico en los sistemas de pagoDaniel Vicent Lluesa
 
Presentación t2.pptx a 1per audi
Presentación t2.pptx a 1per audiPresentación t2.pptx a 1per audi
Presentación t2.pptx a 1per audiMarc Macia Sans
 
Machine Learning Concepts for Software Monitoring - Lior Redlus, Coralogix - ...
Machine Learning Concepts for Software Monitoring - Lior Redlus, Coralogix - ...Machine Learning Concepts for Software Monitoring - Lior Redlus, Coralogix - ...
Machine Learning Concepts for Software Monitoring - Lior Redlus, Coralogix - ...DevOpsDays Tel Aviv
 
Returns basedstyleanalysisinexcel mcdermott
Returns basedstyleanalysisinexcel mcdermottReturns basedstyleanalysisinexcel mcdermott
Returns basedstyleanalysisinexcel mcdermottbfmresearch
 
The 2012 Channel Marketing Preference Survey
The 2012 Channel Marketing Preference SurveyThe 2012 Channel Marketing Preference Survey
The 2012 Channel Marketing Preference SurveyRyan Bonnici
 
Software Libre/Código Abierto - Enunciado
Software Libre/Código Abierto - EnunciadoSoftware Libre/Código Abierto - Enunciado
Software Libre/Código Abierto - Enunciadomartinp
 
SEO Audit Workshop: Framework, Techniques And Tools
SEO Audit Workshop: Framework, Techniques And ToolsSEO Audit Workshop: Framework, Techniques And Tools
SEO Audit Workshop: Framework, Techniques And ToolsDigital Vidya
 
Criterio y funcionamiento de un sniffer (cain & abel) (www.dragon jar.us)
Criterio y funcionamiento de un sniffer (cain & abel)  (www.dragon jar.us)Criterio y funcionamiento de un sniffer (cain & abel)  (www.dragon jar.us)
Criterio y funcionamiento de un sniffer (cain & abel) (www.dragon jar.us)Gabriel Romero Pastrana
 

Viewers also liked (20)

Social media (ICT Month)
Social media (ICT Month)Social media (ICT Month)
Social media (ICT Month)
 
Instal·lació solar aïllada
Instal·lació solar aïlladaInstal·lació solar aïllada
Instal·lació solar aïllada
 
Sol Talks n.7
Sol Talks n.7Sol Talks n.7
Sol Talks n.7
 
2ª Sesión Aprende a Cuidarte: Doctor estoy infoxicado, ¿es grave? Prevención,...
2ª Sesión Aprende a Cuidarte: Doctor estoy infoxicado, ¿es grave? Prevención,...2ª Sesión Aprende a Cuidarte: Doctor estoy infoxicado, ¿es grave? Prevención,...
2ª Sesión Aprende a Cuidarte: Doctor estoy infoxicado, ¿es grave? Prevención,...
 
Grow digital
Grow digitalGrow digital
Grow digital
 
Weblogs
WeblogsWeblogs
Weblogs
 
Real Photography
Real PhotographyReal Photography
Real Photography
 
Bitcoin: salto tecnológico en los sistemas de pago
Bitcoin: salto tecnológico en los sistemas de pagoBitcoin: salto tecnológico en los sistemas de pago
Bitcoin: salto tecnológico en los sistemas de pago
 
Enfermedad de cutting
Enfermedad de cuttingEnfermedad de cutting
Enfermedad de cutting
 
Presentación t2.pptx a 1per audi
Presentación t2.pptx a 1per audiPresentación t2.pptx a 1per audi
Presentación t2.pptx a 1per audi
 
Machine Learning Concepts for Software Monitoring - Lior Redlus, Coralogix - ...
Machine Learning Concepts for Software Monitoring - Lior Redlus, Coralogix - ...Machine Learning Concepts for Software Monitoring - Lior Redlus, Coralogix - ...
Machine Learning Concepts for Software Monitoring - Lior Redlus, Coralogix - ...
 
Solinex
SolinexSolinex
Solinex
 
ELIX Viviendas con encanto
ELIX Viviendas con encantoELIX Viviendas con encanto
ELIX Viviendas con encanto
 
Returns basedstyleanalysisinexcel mcdermott
Returns basedstyleanalysisinexcel mcdermottReturns basedstyleanalysisinexcel mcdermott
Returns basedstyleanalysisinexcel mcdermott
 
The 2012 Channel Marketing Preference Survey
The 2012 Channel Marketing Preference SurveyThe 2012 Channel Marketing Preference Survey
The 2012 Channel Marketing Preference Survey
 
Trabajo y economia humana
Trabajo y economia humanaTrabajo y economia humana
Trabajo y economia humana
 
Software Libre/Código Abierto - Enunciado
Software Libre/Código Abierto - EnunciadoSoftware Libre/Código Abierto - Enunciado
Software Libre/Código Abierto - Enunciado
 
SEO Audit Workshop: Framework, Techniques And Tools
SEO Audit Workshop: Framework, Techniques And ToolsSEO Audit Workshop: Framework, Techniques And Tools
SEO Audit Workshop: Framework, Techniques And Tools
 
Criterio y funcionamiento de un sniffer (cain & abel) (www.dragon jar.us)
Criterio y funcionamiento de un sniffer (cain & abel)  (www.dragon jar.us)Criterio y funcionamiento de un sniffer (cain & abel)  (www.dragon jar.us)
Criterio y funcionamiento de un sniffer (cain & abel) (www.dragon jar.us)
 
Ha 2000 Anos
Ha 2000 AnosHa 2000 Anos
Ha 2000 Anos
 

Similar to Applicative style programming

Logstash-Elasticsearch-Kibana
Logstash-Elasticsearch-KibanaLogstash-Elasticsearch-Kibana
Logstash-Elasticsearch-Kibanadknx01
 
Writing a REST Interconnection Library in Swift
Writing a REST Interconnection Library in SwiftWriting a REST Interconnection Library in Swift
Writing a REST Interconnection Library in SwiftPablo Villar
 
DEFUN 2008 - Real World Haskell
DEFUN 2008 - Real World HaskellDEFUN 2008 - Real World Haskell
DEFUN 2008 - Real World HaskellBryan O'Sullivan
 
course slides -- powerpoint
course slides -- powerpointcourse slides -- powerpoint
course slides -- powerpointwebhostingguy
 
The... Wonderful? World of Lambdas
The... Wonderful? World of LambdasThe... Wonderful? World of Lambdas
The... Wonderful? World of LambdasEsther Lozano
 
Reactive Access to MongoDB from Scala
Reactive Access to MongoDB from ScalaReactive Access to MongoDB from Scala
Reactive Access to MongoDB from ScalaHermann Hueck
 
PHP 5.3 Part 2 - Lambda Functions & Closures
PHP 5.3 Part 2 - Lambda Functions & ClosuresPHP 5.3 Part 2 - Lambda Functions & Closures
PHP 5.3 Part 2 - Lambda Functions & Closuresmelechi
 
Phpをいじり倒す10の方法
Phpをいじり倒す10の方法Phpをいじり倒す10の方法
Phpをいじり倒す10の方法Moriyoshi Koizumi
 
TWINS: OOP and FP - Warburton
TWINS: OOP and FP - WarburtonTWINS: OOP and FP - Warburton
TWINS: OOP and FP - WarburtonCodemotion
 
Twins: Object Oriented Programming and Functional Programming
Twins: Object Oriented Programming and Functional ProgrammingTwins: Object Oriented Programming and Functional Programming
Twins: Object Oriented Programming and Functional ProgrammingRichardWarburton
 
Real World Haskell: Lecture 7
Real World Haskell: Lecture 7Real World Haskell: Lecture 7
Real World Haskell: Lecture 7Bryan O'Sullivan
 
Practical catalyst
Practical catalystPractical catalyst
Practical catalystdwm042
 
Data Summer Conf 2018, “Mist – Serverless proxy for Apache Spark (RUS)” — Vad...
Data Summer Conf 2018, “Mist – Serverless proxy for Apache Spark (RUS)” — Vad...Data Summer Conf 2018, “Mist – Serverless proxy for Apache Spark (RUS)” — Vad...
Data Summer Conf 2018, “Mist – Serverless proxy for Apache Spark (RUS)” — Vad...Provectus
 

Similar to Applicative style programming (20)

Free your lambdas
Free your lambdasFree your lambdas
Free your lambdas
 
Logstash-Elasticsearch-Kibana
Logstash-Elasticsearch-KibanaLogstash-Elasticsearch-Kibana
Logstash-Elasticsearch-Kibana
 
Free your lambdas
Free your lambdasFree your lambdas
Free your lambdas
 
Writing a REST Interconnection Library in Swift
Writing a REST Interconnection Library in SwiftWriting a REST Interconnection Library in Swift
Writing a REST Interconnection Library in Swift
 
DEFUN 2008 - Real World Haskell
DEFUN 2008 - Real World HaskellDEFUN 2008 - Real World Haskell
DEFUN 2008 - Real World Haskell
 
Easy R
Easy REasy R
Easy R
 
course slides -- powerpoint
course slides -- powerpointcourse slides -- powerpoint
course slides -- powerpoint
 
Lambdas puzzler - Peter Lawrey
Lambdas puzzler - Peter LawreyLambdas puzzler - Peter Lawrey
Lambdas puzzler - Peter Lawrey
 
The... Wonderful? World of Lambdas
The... Wonderful? World of LambdasThe... Wonderful? World of Lambdas
The... Wonderful? World of Lambdas
 
Reactive Access to MongoDB from Scala
Reactive Access to MongoDB from ScalaReactive Access to MongoDB from Scala
Reactive Access to MongoDB from Scala
 
PHP 5.3 Part 2 - Lambda Functions & Closures
PHP 5.3 Part 2 - Lambda Functions & ClosuresPHP 5.3 Part 2 - Lambda Functions & Closures
PHP 5.3 Part 2 - Lambda Functions & Closures
 
Phpをいじり倒す10の方法
Phpをいじり倒す10の方法Phpをいじり倒す10の方法
Phpをいじり倒す10の方法
 
TWINS: OOP and FP - Warburton
TWINS: OOP and FP - WarburtonTWINS: OOP and FP - Warburton
TWINS: OOP and FP - Warburton
 
Twins: Object Oriented Programming and Functional Programming
Twins: Object Oriented Programming and Functional ProgrammingTwins: Object Oriented Programming and Functional Programming
Twins: Object Oriented Programming and Functional Programming
 
Real World Haskell: Lecture 7
Real World Haskell: Lecture 7Real World Haskell: Lecture 7
Real World Haskell: Lecture 7
 
Practical catalyst
Practical catalystPractical catalyst
Practical catalyst
 
Mist - Serverless proxy to Apache Spark
Mist - Serverless proxy to Apache SparkMist - Serverless proxy to Apache Spark
Mist - Serverless proxy to Apache Spark
 
Data Summer Conf 2018, “Mist – Serverless proxy for Apache Spark (RUS)” — Vad...
Data Summer Conf 2018, “Mist – Serverless proxy for Apache Spark (RUS)” — Vad...Data Summer Conf 2018, “Mist – Serverless proxy for Apache Spark (RUS)” — Vad...
Data Summer Conf 2018, “Mist – Serverless proxy for Apache Spark (RUS)” — Vad...
 
Effective PHP. Part 5
Effective PHP. Part 5Effective PHP. Part 5
Effective PHP. Part 5
 
Java 8
Java 8Java 8
Java 8
 

Recently uploaded

The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AIABDERRAOUF MEHENNI
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceanilsa9823
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 

Recently uploaded (20)

The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 

Applicative style programming

  • 2. Pepe García Scala Engineer @ Fidesmo Co-organizer @ HaskellMad & FP-MAD Scala fan Haskell Acolyth Functional Programming Devotee jlgarhdez HaskellMADpepegar
  • 3. About this talk Slides are available here: http://es.slideshare.net/JosLuisGarcaHernndez/applicative-style-programming Code is available here: https://github.com/pepegar/Control.Applicative.Talk jlgarhdez HaskellMADpepegar
  • 4. What are we learning today? ● What are applicatives? ● When do we use them? ● Who is using them? ● How to use them? jlgarhdez HaskellMADpepegar
  • 5.
  • 6. What are applicatives? Applicatives are an abstraction between functions and monads. And they open the doors for the so called applicative style! jlgarhdez HaskellMADpepegar
  • 7. What are applicatives? @jlgarhdez class Functor f => Applicative f where pure :: a -> f a (<*>) :: f (a -> b) -> f a -> f b jlgarhdez HaskellMADpepegar
  • 8. What are applicatives? Let’s understand the typeclass: It exposes a `pure` function, that puts a value inside the applicative context And a `<*>` function, that applies a function inside an applicative context to the content of another value with the same applicative context jlgarhdez HaskellMADpepegar
  • 9. What are applicatives? Do you remember fmap from functors? Try to compare it with <*> fmap :: (a -> b) -> f a -> f b (<*>) :: f (a -> b) -> f a -> f b jlgarhdez HaskellMADpepegar
  • 10. What are applicatives? Basically an Applicative is a Functor that can also apply a function contained in a container to other container of the same type. Prelude Control.Monad> let fn1 = x-> x * x Prelude Control.Monad> let fn2 = x -> x + 33 Prelude Control.Monad> let fns = [fn1, fn2] Prelude Control.Monad> :t fns fns :: Num a => [a -> a] Prelude Control.Monad> let nums = [1,2,3,4] Prelude Control.Monad> :t nums nums :: Num t => [t] Prelude Control.Monad> fns <*> nums jlgarhdez HaskellMADpepegar
  • 11. A small example As an example of Applicative Programming, let’s see how Applicative and Monadic styles compare: jlgarhdez HaskellMADpepegar
  • 12. A small example data Person = Person { name :: String, lastName :: String } deriving Show data Err = String validateName :: String -> Either Err String validateName n = if n == "Pepe" then Right n else Left "name is not Pepe" validateLastName :: String -> Either Err String validateLastName l = if l == "García" then Right l else Left "last name is not García" jlgarhdez HaskellMADpepegar
  • 13. A small example. Monadic validatePersonM :: String -> String -> Either String Person validatePersonM n l = do vName <- validateName n vLast <- validateLastName l return $ Person vName vLast jlgarhdez HaskellMADpepegar
  • 14. A small example. Applicative validatePersonA :: String -> String -> Either String Person validatePersonA n l = Person <$> validateName n <*> validateLastName l jlgarhdez HaskellMADpepegar
  • 15. A small example. Applicative (2) Why does the previous example work? Let’s dig a bit deeper on it: Prelude> :t Person Person :: String -> String -> Person Prelude> :t Person <$> validateName "pepe" Person <$> validateName "pepe" :: Either String (String -> Person) Prelude> :t Person <$> validateName "pepe" <*> validateLastName "Garcia" Person <$> validateName "pepe" <*> validateLastName "Garcia" :: Either String Person jlgarhdez HaskellMADpepegar
  • 16. A small example. Recap So, what do we learn from our small example? Monads are not a silver bullet, and can be replaced easily by Applicative Functors when ordering is not important. jlgarhdez HaskellMADpepegar
  • 17. When to use applicatives? When we need more than a functor, and less than a monad… Sounds appealing, but WTF is this!? jlgarhdez HaskellMADpepegar
  • 18. When to use applicatives? Functor Functor defines containers that can be mapped over. class Functor f where fmap :: (a -> b) -> f a -> f b jlgarhdez HaskellMADpepegar
  • 19. When to use applicatives? Monad Monad defines containers allow operation sequencing! class Monad m where (>>=) :: m a -> (a -> m b) -> m b (>>) :: m a -> m b -> m b return :: a -> m a fail :: String -> m a jlgarhdez HaskellMADpepegar
  • 20. When to use applicatives? Some rule-o-thumbs I use for detecting Monad overkill ● import Control.Applicative ● replace return with pure, liftM with (<$>) (or fmap or liftA), liftM2 with liftA2, etc, and ap with (<*>) ● If your function signature was Monad m => ..., change to Applicative m => ... (and maybe rename m to f or whatever). jlgarhdez HaskellMADpepegar
  • 21. When your do block can be substituted by liftM2/3/4... When to use applicatives? validatePersonM :: String -> String -> Either String Person validatePersonM n l = do vName <- validateName n vLast <- validateLastName l return $ Person vName vLast becomes validatePersonA :: String -> String -> Either String Person validatePersonA n l = liftM2 Person $ validateName n $ validateLastName l jlgarhdez HaskellMADpepegar
  • 22. When your operations does not depend on each other When to use applicatives? validatePersonM :: String -> String -> Either String Person validatePersonM n l = do vName <- validateName n vLast <- validateLastName l return $ Person vName vLast becomes validatePersonA :: String -> String -> Either String Person validatePersonA n l = Person <$> validateName n <*> validateLastName l jlgarhdez HaskellMADpepegar
  • 23. Who uses Applicative? Applicative is being used all over the place nowadays. Some interesting examples of its use are: ● HAXL. Efficient data access ● CmdTheLine. Command line argument parsing ● Validation. Data validation library ● Attoparsec. ByteString parsing library jlgarhdez HaskellMADpepegar
  • 24. How to use Applicatives? @jlgarhdez Applicatives are really easy to use. You just need to provide an instance of the typeclass for your data type. data Maybe a = Just a | Nothing instance Applicative Maybe where pure = Just Just f <*> m = fmap f m Nothing <*> _m = Nothing jlgarhdez HaskellMADpepegar
  • 25. Or, you can go Free! jlgarhdez HaskellMADpepegar
  • 26. Free Applicatives Create an applicative from any Functor jlgarhdez HaskellMADpepegar
  • 27. Free Applicatives. data Ap f a where Pure :: a -> Ap f a Ap :: f a -> Ap f (a -> b) -> Ap f b @jlgarhdez jlgarhdez HaskellMADpepegar
  • 28. Free Applicatives. instance Functor (Ap f) where fmap f (Pure a) = Pure (f a) fmap f (Ap x y) = Ap x ((f .) <$> y) instance Apply (Ap f) where Pure f <.> y = fmap f y Ap x y <.> z = Ap x (flip <$> y <.> z) instance Applicative (Ap f) where pure = Pure Pure f <*> y = fmap f y Ap x y <*> z = Ap x (flip <$> y <*> z) @jlgarhdez jlgarhdez HaskellMADpepegar
  • 29. Free applicatives @jlgarhdez Now let’s create a small ADT and create a Free Applicative out of it! import Control.Applicative.Free import Control.Applicative type Author = String type Post = String type Id = String data BlogF a where GetPost :: Id -> BlogF Post GetAuthor :: Id -> BlogF Author type Blog a = Ap BlogF a jlgarhdez HaskellMADpepegar
  • 30. And a bit of syntax… getPost :: Id -> Blog Post getPost id = liftAp $ GetPost id getAuthor :: Id -> Blog Author getAuthor id = liftAp $ GetAuthor id Free applicatives @jlgarhdez jlgarhdez HaskellMADpepegar
  • 31. Free applicatives @jlgarhdez Now, we might want to render a page of our blog: data Page = Page { post :: Post, author :: Author } deriving Show jlgarhdez HaskellMADpepegar
  • 32. Now, let’s do Applicative magic! jlgarhdez HaskellMADpepegar
  • 33. Free applicatives @jlgarhdez With all we have already learned, how would you implement the following? renderPage :: Id -> Id -> Blog Page jlgarhdez HaskellMADpepegar
  • 34. Free applicatives @jlgarhdez With all we have already learned, how would you implement the following? renderPage :: Id -> Id -> Blog Page renderPage postId authorId = Page <$> getPost postId <*> getAuthor authorId jlgarhdez HaskellMADpepegar
  • 36. But we are not doing anything in that renderPage function jlgarhdez HaskellMADpepegar
  • 37. Let’s do, let’s interpret! jlgarhdez HaskellMADpepegar
  • 38. Free applicatives @jlgarhdez First, we need an interpreter. This interpreter is called Natural Transformation in Category Theory, and transforms a value `f a` into a `g a`. interpIO :: BlogF a -> IO a interpIO (GetPost id) = putStrLn ("getting post " ++ show id ++ " from DB") *> pure "this is the post" interpIO (GetAuthor id) = putStrLn ("getting author " ++ show id ++ " from DB") *> pure "Pepe García" jlgarhdez HaskellMADpepegar
  • 39. Free applicatives @jlgarhdez And, last but not least, wire everything together: main :: IO () main = do page <- runAp interpIO $ renderPage 1 1 print page jlgarhdez HaskellMADpepegar
  • 40. Abstract Syntax Trees As you know, the most important part of functional programming is referential transparency. This means creating values, not executing effects. That’s what our free applicatives does, they create little programs, or Abstract Syntax Trees jlgarhdez HaskellMADpepegar
  • 41. Applicatives AST As you imagine from our implementation, this Blog does nothing, just create values. It does not go to the DB to fetch users/posts/whatever. It just creates what is called an Abstract Syntax Tree jlgarhdez HaskellMADpepegar
  • 42. Monadic AST And, if we compare this AST with the one created by its monadic counterpart, implemented in terms of monadic bind, we can see something interesting. We need to evaluate getPost 1 in order to evaluate getAuthor 1 jlgarhdez HaskellMADpepegar
  • 43. Applicative AST. Static Analysis @jlgarhdez Applicative functors’ ASTs allow you to explore them from the top down without evaluating a single line of code! This technique is called static analysis, and is awesome for: ● Optimizing performance (deduplicate requests, in a series of HTTP requests) ● Calculate dependencies automatically ● Lint your code jlgarhdez HaskellMADpepegar
  • 44. Static Analysis Analysis of a program that does not evaluate the program. It is possible with our Applicatives! jlgarhdez HaskellMADpepegar
  • 45. Static Analysis @jlgarhdez To demonstrate static analysis, let’s keep with our blog example: data BlogF a where GetPost :: Id -> BlogF Post GetAuthor :: Id -> BlogF Author GetComments :: Id -> BlogF [(Comment, Author)] type Blog a = Ap BlogF a jlgarhdez HaskellMADpepegar
  • 46. Static Analysis @jlgarhdez Also some convenience functions: getPost :: Id -> Blog Post getPost id = liftAp $ GetPost id getAuthor :: Id -> Blog Author getAuthor id = liftAp $ GetAuthor id getComments :: Id -> Blog [(Comment, Author)] getComments id = liftAp $ GetComments id jlgarhdez HaskellMADpepegar
  • 47. Static Analysis @jlgarhdez Then, our renderPage would look like this: renderPage :: Id -> Id -> Blog Page renderPage post author = Page <$> getPost post <*> getAuthor author <*> getComments post jlgarhdez HaskellMADpepegar
  • 48. Static Analysis @jlgarhdez And, as explained before, we will need to interpret the AST interpIO :: BlogF a -> IO a interpIO (GetPost id) = putStrLn ("getting post " ++ show id ++ " from DB") *> pure "this is the post" interpIO (GetAuthor id) = putStrLn ("getting author " ++ show id ++ " from DB") *> pure "@pepe" interpIO (GetComments id) = putStrLn ("getting comments for post " ++ show id ++ " from DB") *> pure [ ("this post rocks" , "@anler"), ("you're right, @anler" , "@lorenzo"), ("Oh boy, I love haskell so bad!" , "@dani"), ("Indeed, Haskell is better than Erlang!" , "@joseluis" ) ] jlgarhdez HaskellMADpepegar
  • 49. Static Analysis @jlgarhdez But, the most amazing thing is that we can calculate the number of operations that our renderProgram does, for example: instance Monoid Int where mempty = 0 mappend = (+) countInstructions :: BlogF a -> Int countInstructions _ = 1 jlgarhdez HaskellMADpepegar
  • 50. Static Analysis @jlgarhdez But, the most amazing thing is that we can calculate the number of operations that our renderProgram does, for example: main :: IO () main = do putStrLn "NUMBER OF REQUESTS TO THE DB:" print instructions putStrLn "" putStrLn "PAGE RENDERING:" page <- runAp interpIO page print page where instructions = runAp_ countInstructions page page = renderPage 1 1 jlgarhdez HaskellMADpepegar
  • 51. Composing Applicative Functors Unlike Monads, our Applicatives are closed on composition. This means that you can provide an instance of Applicative for the product of any other two applicatives! jlgarhdez HaskellMADpepegar
  • 52. Composing Applicative Functors @jlgarhdez data Product m n a = Product { first :: m a, second :: n a } instance (Functor m, Functor n) => Functor (Product m n) where fmap f fa = Product (f <$> first fa) (f <$> second fa) instance (Applicative m, Applicative n) => Applicative (Product m n) where pure x = Product (pure x) (pure x) mf <*> mx = Product (first mf <*> first mx) (second mf <*> second mx) jlgarhdez HaskellMADpepegar
  • 53. Applicative Do Notation Created at Facebook, ApplicativeDo extension allows you to write Applicative Code in a more familiar fashion, if you come from imperative programming. jlgarhdez HaskellMADpepegar
  • 54. {-# LANGUAGE ApplicativeDo #-} renderPage :: Id -> Id -> Blog Page renderPage postId authorId = do post <- getPost postId author <- getAuthor authorId return $ Page post author ApplicativeDo @jlgarhdez jlgarhdez HaskellMADpepegar
  • 56. Recap ● Use more applicatives, they DO ROCK! ● Always separate domain modelling from interpretation (with free applicatives) ● Eliminate boilerplate with meta-programming (with static analysis) ● Keep writing your code in do-block style (with ApplicativeDo) ● Have fun! ● Bonus point: Convince your boss to use Haskell! jlgarhdez HaskellMADpepegar
  • 57. Bibliography ● Idioms: applicative programming with effects. McBride, Paterson ● The Essence of the Iterator Pattern. Gibbons, Oliveira ● Origami programming. Gibbons ● Static Analysis with Applicatives. Gergő Érdi jlgarhdez HaskellMADpepegar