diff --git a/applicative/applicative-slideshow.pdf b/applicative/applicative-slideshow.pdf index fd49b90..64989cc 100644 Binary files a/applicative/applicative-slideshow.pdf and b/applicative/applicative-slideshow.pdf differ diff --git a/applicative/applicative.md b/applicative/applicative.md index ca7c434..0d97ea9 100644 --- a/applicative/applicative.md +++ b/applicative/applicative.md @@ -84,6 +84,60 @@ pure (+1) <*> [1..3] [(1,3),(1,4),(2,3),(2,4)] ``` +# Use cases[^4] + +```haskell +Person + <$> parseString "name" o + <*> parseInt "age" o + <*> parseTelephone "telephone" o +``` + +Can also be written as + +```haskell +liftA3 Person + (parseString "name" o) + (parseInt "age" o) + (parseTelephone "telephone" o) +``` +[^4]: [FP Complete - Crash course to Applicative syntax](https://www.fpcomplete.com/haskell/tutorial/applicative-syntax/) + +# Use cases[^5] + +```haskell +parsePerson :: Parser Person +parsePerson = do + string "Name: " + name <- takeWhile (/= 'n') + endOfLine + string "Age: " + age <- decimal + endOfLine + pure $ Person name age +``` + +[^5]: [FP Complete - Crash course to Applicative syntax](https://www.fpcomplete.com/haskell/tutorial/applicative-syntax/) + +# Use cases[^6] + +```haskell +helper :: () -> Text -> () -> () -> Int -> () -> Person +helper () name () () age () = Person name age + +parsePerson :: Parser Person +parsePerson = helper + <$> string "Name: " + <*> takeWhile (/= 'n') + <*> endOfLine + <*> string "Age: " + <*> decimal + <*> endOfLine +``` + +[^6]: [FP Complete - Crash course to Applicative syntax](https://www.fpcomplete.com/haskell/tutorial/applicative-syntax/) + + # Lifting - Seeing Functor as unary lifting and Applicative as n-ary lifting @@ -155,6 +209,17 @@ pure f <*> pure x = pure (f x) u <*> pure y = pure ($ y) <*> u ``` +# Operators[^7] + +- `pure` wraps up a pure value into some kind of Applicative +- `liftA2` applies a pure function to the values inside two `Applicative` wrapped values +- `<$>` operator version of `fmap` +- `<*>` apply a wrapped function to a wrapped value +- `*>`, `<*` + +[^7]: [FP Complete - Crash course to Applicative syntax](https://www.fpcomplete.com/haskell/tutorial/applicative-syntax/) + + # Applicative vs monads - Applicative @@ -162,7 +227,7 @@ u <*> pure y = pure ($ y) <*> u * Batching and aggregation * Concurrency/Independent - Parsing context free grammar - - Exploring all branches of computation (see `Alternative`) + - Exploring all branches of computation (see [`Alternative`](https://hackage.haskell.org/package/base-4.20.0.1/docs/Control-Applicative.html#t:Alternative)) - Monads * Effects @@ -183,6 +248,7 @@ u <*> pure y = pure ($ y) <*> u - [Applicative Programming with Effects](https://www.staff.city.ac.uk/~ross/papers/Applicative.html) - [optparse-applicative](https://hackage.haskell.org/package/optparse-applicative) - [Control Applicative](https://hackage.haskell.org/package/base-4.19.1.0/docs/Control-Applicative.html) +- [Applicative functors for fun & parsing](https://arunraghavan.net/2018/02/applicative-functors-for-fun-and-parsing/) # Questions diff --git a/applicative/applicative.pdf b/applicative/applicative.pdf index b55e4bf..7ed00f5 100644 Binary files a/applicative/applicative.pdf and b/applicative/applicative.pdf differ