Profile picture
, 38 tweets, 5 min read Read on Twitter
1 like = 1 haskell base function.
`foldMap :: (Foldable t, Monoid m) => (a -> m) -> t a -> m`

Fold down a structure using a Monoid.

λ> foldMap Sum [2,4,4]
Sum {getSum = 10}
`($>) :: Functor f => f a -> b -> f b`

Replace the value in a functor

λ> Just "Foo" $> 4
Just 4
`guard :: Alternative f => Bool -> f ()`

Build an Alternative of Unit from a boolean value

λ> guard @[] True
[()]
λ> guard @ Maybe False
Nothing
λ> guard @ Maybe (even 2) $> "Foo"
Just "Foo"
`recip :: Fractional a => a -> a`

reciprocal fraction

λ> recip (1/4)
4.0
`sequenceA :: (Traversable t, Applicative f) => t (f a) -> f (t a)`

Move an Applicative out of a Traversable

λ> sequenceA [(+1), (*2)] 4
[5,8]
`elem :: (Foldable t, Eq a) => a -> t a -> Bool`

Check if the value in this traversable (works with any Traversable):

λ> elem 5 (Just 5)
True

`elem @ Maybe x` = `Just x ==`
`until :: (a -> Bool) -> (a -> a) -> a -> a`

Repeatedly call a endomorphism until a condition holds:

λ> until (> 2048) (*2) 1
4096
`const :: a -> b -> a`

The constant function (yeah, whatever):

λ> const "Foo" ()
"Foo"
`asTypeOf :: a -> a -> a`

Just as `const` but reliques that the two params has the same type:

λ> asTypeOf "Foo" ()
error
λ> asTypeOf "Foo" "Bar"
"Foo"
`iterate :: (a -> a) -> a -> [a]`

Build a list from repetitive calls to a function:

λ> take 8 $ iterate (*2) 1
[1,2,4,8,16,32,64,128]
`mfilter :: MonadPlus m => (a -> Bool) -> m a -> m a`

"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
`traverse :: (Traversable t, Applicative f) => (a -> f b) -> t a -> f (t b)`

"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
`void :: Functor f => f a -> f ()`

Just discard the value(s) in a functor:

λ> void [1,2,3,4]
[(),(),(),()]

`void fx` = `fx $> ()`
`filterM :: Applicative m => (a -> m Bool) -> [a] -> m [a]`

`filter` with a monadic predicate:

λ> filterM (>) [1..10] 5
[6,7,8,9,10]
λ> filterM (>) [1..10] 9
[10]
foldl' :: Foldable t => (b -> a -> b) -> b -> t a -> b

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
`bimap :: Bifunctor p => (a -> b) -> (c -> d) -> p a c -> p b d`

Map both part of a Bifunctor:

λ> bimap length head ("Hello", "World")
(5,'W')
`repeat :: a -> [a]`

Infinite list that repeat the same value again and again:

λ> take 4 $ repeat "Hello"
["Hello","Hello","Hello","Hello"]
`zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]`

zip two list with a custom binary function:

λ> zipWith (++) (repeat "Hello ") ["Twitter","World"]
["Hello Twitter","Hello World"]
`join :: Monad m => m (m a) -> m a`

Remove one level of a monadic structure:

λ> join [[1,2],[3,4]]
[1,2,3,4]
λ> join (+) 4
8
`inRange :: Ix a => (a, a) -> a -> Bool`

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
`curry :: ((a, b) -> c) -> a -> b -> c`

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
`First :: Maybe a -> First a`

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}
And of Course:

`Last :: Maybe a -> Last a`

λ> foldMap Last [Nothing, Just 2, Nothing, Just 3]
Last {getLast = Just 3}
`(&&&) :: Arrow a => a b c -> a b c' -> a b (c, c')`

Nice to use with `a` = `->`:
two functions, one input, two outputs

λ> (+2) &&& (*3) $ 4
(6,12)
(|||) :: ArrowChoice a => a b d -> a c d -> a (Either b c) d

You prefer branching?

λ> (+2) ||| (*3) $ Left 4
6
λ> (+2) ||| (*3) $ Right 4
12
unfoldr :: (b -> Maybe (a, b)) -> b -> [a]

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]
`ZipList :: [a] -> ZipList a`

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]}
`on :: (b -> b -> c) -> (a -> b) -> a -> a -> c`

Apply a morphism on both terms before applying a binary function. Usually used in its infix form:

λ> ((+) `on` length) "Hello" "World"
10
`bool :: a -> a -> Bool -> a`

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
`maybe : b -> (a -> b) -> Maybe a -> b`

The Maybe catamoprhism:

λ> maybe (-1) length (Just "Hello")
5
λ> maybe (-1) length Nothing
-1
`either :: (a -> c) -> (b -> c) -> Either a b -> c`

The Either catamorphism:

λ> either (\n -> replicate n '_') id $ Right "Hello"
"Hello"
λ> either (\n -> replicate n '_') id $ Left 5
"_____"
`newtype Fixed a = MkFixed Integer`

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
`Dual :: a -> Dual a`

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"}
`readMaybe :: Read a => String -> Maybe a`

Yes, it's in `base` (`Text.Read`)

λ> readMaybe @ Int "123"
Just 123
λ> readMaybe @ Int "Foo"
Nothing
`mzip :: MonadZip m => m a -> m b -> m (a, b)`

`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)]
```
`generalCategory :: Char -> GeneralCategory`

Classify characters by categories:

```
λ> generalCategory ':'
OtherPunctuation
λ> generalCategory 'A'
UppercaseLetter
λ> generalCategory '3'
DecimalNumber
λ> generalCategory '😱'
OtherSymbol
```
`partitionEithers :: [Either a b] -> ([a], [b])`

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])
```
Missing some Tweet in this thread?
You can try to force a refresh.

Like this thread? Get email updates or save it to PDF!

Subscribe to Nicoλas
Profile picture

Get real-time email alerts when new unrolls are available from this author!

This content may be removed anytime!

Twitter may remove this content at anytime, convert it as a PDF, save and print for later use!

Try unrolling a thread yourself!

how to unroll video

1) Follow Thread Reader App on Twitter so you can easily mention us!

2) Go to a Twitter thread (series of Tweets by the same owner) and mention us with a keyword "unroll" @threadreaderapp unroll

You can practice here first or read more on our help page!

Follow Us on Twitter!

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just three indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3.00/month or $30.00/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!