Further additions to Applicative

This commit is contained in:
Sanchayan Maity 2024-06-25 23:20:51 +05:30
parent fffa677663
commit 7dac0905b9
3 changed files with 83 additions and 1 deletions

View file

@ -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
@ -122,6 +176,22 @@ instance Monoid a => Applicative ((,) a) where
(u, f) <*> (v, x) = (u `mappend` v, f x)
```
# Where are monoids again
```haskell
fmap (+1) ("blah", 0)
("blah",1)
("Woo", (+1)) <*> (" Hoo!", 0)
("Woo Hoo!", 1)
(,) <$> [1, 2] <*> [3, 4]
[(1,3),(1,4),(2,3),(2,4)]
liftA2 (,) [1, 2] [3, 4]
[(1,3),(1,4),(2,3),(2,4)]
```
# Function apply
- Applying a function to an `effectful` argument
@ -155,6 +225,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 +243,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 +264,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

Binary file not shown.