Functional Programing Cheat Sheet
Monad Examples
- List
- Maybe/Optional
- Either
- Try (Scala specialized Either that catches exceptions)
- Future
- Observable/Signal
Async
| One | Many | |
|---|---|---|
| Synchronous | T/Try[T] | Iterable[T] |
| Asynchronous | Future[T] | Observable[T] |
Haskell
data type
| Haskell name | Haskell def | Equivalent in Java or similar |
|---|---|---|
| type class | class Smthg | Interface/Protocol |
| type constructor | data Smthg a | Generic |
| concrete type | Smthg Int data Smthg |
Parametrized Generic |
| type synonym | type | typealias |
| data type | data Smthg param | Class |
| instance of type class | instance (TypeClass) type data deriving (TypeClass) |
implements |
Concepts
| Name | Haskell | type | desciption |
|---|---|---|---|
| Functor | fmap | a->b -> f a -> f b | map (infix synonym: <$>) |
| Applicative(Functor) | pure | a -> f a | id/constructor, lift a value |
| <*> | f (a -> b) -> f a -> f b | sequential application, apply a functored map | |
| Monoid | mempty | a | id |
| mappend | a -> a -> a | combine 2 | |
| mconcat | [a] -> a | fold | |
| Monad | return | a -> m a | pure |
| >>= | m a -> (a -> m b) -> m b | bind, chain, flatmap | |
| >> | m a -> m b -> m b | discard first |
Instances of 'Monad' should satisfy the following laws:
> return a >>= k == k a
> m >>= return == m
> m >>= (\x -> k x >>= h) == (m >>= k) >>= h
Instances of both 'Monad' and 'Functor' should additionally satisfy the law:
> fmap f xs == xs >>= return . f