Functor ᐧ Applicative ᐧ Monad
Michael Trotter
Jane.com
slack.utahjavascript.com
#fun-ctional
haskellbook.com
mostly adequate guide (to fp)
learn you a haskell
Abstraction
ints :: [Int]
sumInts :: [Int] -> Intfloats :: [Float]
sumFloats :: [Float] -> Floatbools :: [Bool]
sumBools :: [Bool] -> BoolsuperInts :: [SuperInt]
sumSuperInts :: [SuperInt] -> SuperIntxs :: [a]
sumXs :: [a] -> adoStuff :: [a] -> a
doStuff = doAllTheThingsTypeclasses
doStuff :: [a] -> aTypeclasses
doStuff = doAllTheThingsclass Functor f where
fmap :: (a -> b) -> f a -> f bTypeclasses
instance Functor [] where
fmap = mapclass Monoid a where
mempty :: a
mappend :: a -> a -> a
mconcat :: [a] -> aMonoid
class Monoid a where
mempty :: a
mappend :: a -> a -> a
mconcat :: [a] -> aPerfect!
ints :: [Int]
ints = [1, 2, 3]
sumInts :: [Int] -> Int
sumInts xs = mconcat xs
sumInts intsNope, sorry.. :(
ints :: [Int]
ints = [1, 2, 3]
sumInts :: [Int] -> Int
sumInts xs = mconcat xs
multiplyInts :: [Int] -> Int
multiplyInts xs = mconcat xs
sumInts ints
multiplyInts intsnewtype
newtype Sum = Sum Int
instance Monoid Sum where
mempty = Sum 0
mappend (Sum x) (Sum y) = Sum (x + y)
newtype Product = Product Int
instance Monoid Product where
mempty = Product 1
mappend (Product x) (Product y) = Product (x * y)there we go!
ints :: [Int]
ints = [1, 2, 3, 4]
sumInts :: [Int] -> Int
sumInts xs = getInt (mconcat (map Sum xs))
multiplyInts :: [Int] -> Int
multiplyInts xs = getProduct (mconcat (map Product xs))
sumInts ints -- | 10
multiplyInts ints -- | 24Functor
class Functor f where
fmap :: (a -> b) -> f a -> f binstance Functor [] where
-- | :: (a -> b) -> [a] -> [b]
fmap fn xs = map fn xsinstance Functor [] where
fmap = mapFunctor
ints :: [Int]
ints = [1, 2, 3, 4]
sumInts :: [Int] -> Int
sumInts xs = getInt (mconcat (fmap Sum xs))
multiplyInts :: [Int] -> Int
multiplyInts xs = getProduct (mconcat (fmap Product xs))
sumInts ints -- | 10
multiplyInts ints -- | 24Functor
fmap (+1) [1, 2, 3, 4] -- | [2, 3, 4, 5]Functor
fmap (+1) (Just 1) -- | Just 2
fmap (+1) Nothing -- | Nothingdata Maybe a = Nothing | Just aFunctor
fmap (+1) [1] -- | [2]
fmap (+1) [] -- | []Functor
fmap (+1) (Just 1) -- | Just 2
fmap (+1) Nothing -- | Nothinginstance Functor (Maybe a) where
fmap fn (Just a) = Just (fn a)
fmap _ Nothing = NothingMonoid: reducible types
(Sum, Product, String, etc)
Functor: any data structure you map over -- fmap!
(List, Maybe, String, lots more!)
Another way to think about Functors:
A data structure you can run a computation inside
Functor-Applicative-Monad
By spicydonuts
Functor-Applicative-Monad
(the infamous)
- 318