Fold down a structure using a Monoid.
λ> foldMap Sum [2,4,4]
Sum {getSum = 10}
Replace the value in a functor
λ> Just "Foo" $> 4
Just 4
Build an Alternative of Unit from a boolean value
λ> guard @[] True
[()]
λ> guard @ Maybe False
Nothing
λ> guard @ Maybe (even 2) $> "Foo"
Just "Foo"
reciprocal fraction
λ> recip (1/4)
4.0
Move an Applicative out of a Traversable
λ> sequenceA [(+1), (*2)] 4
[5,8]
Check if the value in this traversable (works with any Traversable):
λ> elem 5 (Just 5)
True
`elem @ Maybe x` = `Just x ==`
Repeatedly call a endomorphism until a condition holds:
λ> until (> 2048) (*2) 1
4096
The constant function (yeah, whatever):
λ> const "Foo" ()
"Foo"
Just as `const` but reliques that the two params has the same type:
λ> asTypeOf "Foo" ()
error
λ> asTypeOf "Foo" "Bar"
"Foo"
Build a list from repetitive calls to a function:
λ> take 8 $ iterate (*2) 1
[1,2,4,8,16,32,64,128]
"Keep" the value "in" a MonadPlus only if it satisfies the predicate, otherwise, discard it:
λ> mfilter odd (Just 3)
Just 3
λ> mfilter odd (Just 4)
Nothing
"It's always traverse."
Apply an applicative morphism and collect the results.
λ> traverse (mfilter odd . Just) [3,5,9]
Just [3,5,9]
λ> traverse (mfilter odd . Just) [3,5,8]
Nothing
Just discard the value(s) in a functor:
λ> void [1,2,3,4]
[(),(),(),()]
`void fx` = `fx $> ()`
`filter` with a monadic predicate:
λ> filterM (>) [1..10] 5
[6,7,8,9,10]
λ> filterM (>) [1..10] 9
[10]
The left fold, without space leak. Sum of the odd values in a list:
λ> foldl' (\acc x -> if odd x then x + acc else acc) 0 [1..10]
25
Map both part of a Bifunctor:
λ> bimap length head ("Hello", "World")
(5,'W')
Infinite list that repeat the same value again and again:
λ> take 4 $ repeat "Hello"
["Hello","Hello","Hello","Hello"]
zip two list with a custom binary function:
λ> zipWith (++) (repeat "Hello ") ["Twitter","World"]
["Hello Twitter","Hello World"]
Remove one level of a monadic structure:
λ> join [[1,2],[3,4]]
[1,2,3,4]
λ> join (+) 4
8
Name doesn't matter, but sometime it's transparent:
λ> inRange (0,10) 10
True
λ> inRange (0,10) 11
False
There is Ix instance for tuples too:
λ> inRange ((0,0), (4,3)) (4,0)
True
Allow you to use 2 parameters instead of a tuple, mix nicely with the previous function:
λ> curry inRange (0,0) (4,3) (4,4)
False
Newtype used to provide a Monoid instance for Maybe that keeps the first `Just` value:
λ> foldMap First [Nothing, Just 2, Nothing, Just 3]
First {getFirst = Just 2}
`Last :: Maybe a -> Last a`
λ> foldMap Last [Nothing, Just 2, Nothing, Just 3]
Last {getLast = Just 3}
Nice to use with `a` = `->`:
two functions, one input, two outputs
λ> (+2) &&& (*3) $ 4
(6,12)
You prefer branching?
λ> (+2) ||| (*3) $ Left 4
6
λ> (+2) ||| (*3) $ Right 4
12
An anamorphism for list (generate a list from a seed):
λ> unfoldr (\x -> if x < 2048 then Just (x, 2*x) else Nothing) 1
[1,2,4,8,16,32,64,128,256,512,1024]
A new type that provide a zip-like applicative for Lists:
λ> [(+1), (*10)] <*> [1,2]
[2,3,10,20]
λ> ZipList [(+1), (*10)] <*> ZipList [1,2]
ZipList {getZipList = [2,20]}
Apply a morphism on both terms before applying a binary function. Usually used in its infix form:
λ> ((+) `on` length) "Hello" "World"
10
If-then-else with the values in the correct order (aka the Bool cata morphism):
λ> bool 2 4 False
2
λ> bool 2 4 True
4
The Maybe catamoprhism:
λ> maybe (-1) length (Just "Hello")
5
λ> maybe (-1) length Nothing
-1
The Either catamorphism:
λ> either (\n -> replicate n '_') id $ Right "Hello"
"Hello"
λ> either (\n -> replicate n '_') id $ Left 5
"_____"
Arbitrary precision arithmetic:
λ> 0.26 :: Fixed E0
0.0
λ> 0.26 :: Fixed E1
0.2
λ> 0.26 :: Fixed E2
0.26
λ> 0.1 + 0.2
0.30000000000000004
λ> (0.1 :: Fixed E1) + 0.2
0.3
Mainly use for it's Monoid instance:
λ> "Foo" <> "Bar"
"FooBar"
λ> Dual "Foo" <> Dual "Bar"
Dual {getDual = "BarFoo"}
λ> foldMap (Dual . pure) ['a'..'z'] :: Dual String
Dual {getDual = "zyxwvutsrqponmlkjihgfedcba"}
Yes, it's in `base` (`Text.Read`)
λ> readMaybe @ Int "123"
Just 123
λ> readMaybe @ Int "Foo"
Nothing
`MonadZip` is a typeclass that support `zip and `unzip` operations:
```
λ> mzip (Just 2) (Just 3)
Just (2,3)
λ> mzip [1,3] [2,4]
[(1,2),(3,4)]
```
Classify characters by categories:
```
λ> generalCategory ':'
OtherPunctuation
λ> generalCategory 'A'
UppercaseLetter
λ> generalCategory '3'
DecimalNumber
λ> generalCategory '😱'
OtherSymbol
```
Clowns to the Left, jokers to the Right.
```
λ> join (bool Right Left . even) <$> [1..5]
[Right 1,Left 2,Right 3,Left 4,Right 5]
λ> partitionEithers it
([2,4],[1,3,5])
```