Further additions to Applicative
This commit is contained in:
parent
fffa677663
commit
c756592074
3 changed files with 67 additions and 1 deletions
Binary file not shown.
|
@ -84,6 +84,60 @@ pure (+1) <*> [1..3]
|
||||||
[(1,3),(1,4),(2,3),(2,4)]
|
[(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
|
# Lifting
|
||||||
|
|
||||||
- Seeing Functor as unary lifting and Applicative as n-ary 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
|
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 vs monads
|
||||||
|
|
||||||
- Applicative
|
- Applicative
|
||||||
|
@ -162,7 +227,7 @@ u <*> pure y = pure ($ y) <*> u
|
||||||
* Batching and aggregation
|
* Batching and aggregation
|
||||||
* Concurrency/Independent
|
* Concurrency/Independent
|
||||||
- Parsing context free grammar
|
- 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
|
- Monads
|
||||||
* Effects
|
* 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)
|
- [Applicative Programming with Effects](https://www.staff.city.ac.uk/~ross/papers/Applicative.html)
|
||||||
- [optparse-applicative](https://hackage.haskell.org/package/optparse-applicative)
|
- [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)
|
- [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
|
# Questions
|
||||||
|
|
||||||
|
|
Binary file not shown.
Loading…
Reference in a new issue