Skip to content

Instantly share code, notes, and snippets.

@amita-shukla
Last active May 14, 2020 11:31
Show Gist options
  • Save amita-shukla/d11f40bdb0664a285fb226a959fd7797 to your computer and use it in GitHub Desktop.
Save amita-shukla/d11f40bdb0664a285fb226a959fd7797 to your computer and use it in GitHub Desktop.
-- basic functions
id :: a -> a
const :: a -> b -> a
flip :: (a -> b -> c) -> b -> a -> c
-- function composition
-- compose
(.) :: (b -> c) -> (a -> b) -> a -> c
g :: Int -> Bool
f :: String -> Int
h :: String -> Bool
h = (g . f)
h x = g $ f x
-- fmap
<$> :: Functor f => (a->b) -> f a -> f b
<$> :: (a -> b) -> [a] -> [b]
-- apply
<*> :: Applicative f => f (a -> b) -> f a -> f b
<*> :: [(a -> b)] -> [a] -> [b]
<**> :: Applicative f => f a -> f (a -> b) -> f b -- <*> with arguments reversed, this is not equivalent to `flip <*>`
pure :: Applicative f => a -> f a
pure :: a -> [a]
-- lifts
lift0 :: Applicative f => a -> f a
lift1 :: Applicative f => (a -> b) -> f a -> f b -- liftA
lift2 :: Applicative f => (a -> b -> c) -> f a -> f b -> f c --liftA2
lift3 :: Applicative f => (a -> b -> c -> d) -> f a -> f b -> f c -> f d --liftA3
-- When liftA2 is fully applied,
-- as in `liftA2 f arg1 arg2`, it is typically better style to instead use `f <$> arg1 <*> arg2`.
lift0 = pure
lift1 = <$>
lift2 f a b = f <$> a <*> b
lift3 f a b c = lift2 f a b <*> c
lift4 f a b c d = lift3 f a b c <*> d
-- bind
>>= :: Monad m => m a -> (a -> m b) -> m b
-- reverse bind, observe the resemblence with `<$>` and `<*>` type signatures
=<< :: Monad m => (a -> m b) -> m a -> m b
concatMap :: (a -> [b]) -> [a] -> [b] -- concatMap :: Foldable t => (a -> [b]) -> t a -> t b
--join
join :: Monad m => m (m a) -> m a
concat :: [[a]] -> a -- concat :: Foldable t => t [a] -> [a]
return :: Monad m => a -> m a -- same as `pure`, for monads
-- kleisli composition : composition within Monad environment
<=< :: Monad m => (b -> m c) -> (a -> m b) -> a -> m c -- note resemblance with function composition
g :: Int -> [Bool]
f :: String -> [Int]
h :: String -> [Bool]
h = (g <=< f)
-- compare it with function composition:
(.) :: (b -> c) -> (a -> b) -> a -> c
g :: Int -> Bool
f :: String -> Int
h :: String -> Bool
h = (g . f)
-- mappend -- semigroup + monoid
<> :: a -> a -> a
-- Foldable
foldl :: Foldable t => (b -> a -> b) -> b -> t a -> b
foldr :: Foldable t => (a -> b -> b) -> b -> t a -> b
-- Traversable
traverse :: (Traversable t, Applicative f) => (a -> f b) -> t a -> f (t b)
sequence :: (Traversable t, Monad m) => t (m a) -> m (t a)
-- State
newtype State s a = State {runState :: s -> (s, a)}
exec :: State s a => s -> s
eval :: State s a => s -> a
-- Transformer
newtype StateT s f a => StateT { runStateT :: s -> f (s,a) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment