# Catamorphism

## Collapsible Foldable

Flatmappable

Chainable

AndThenable

Computation builder

( I'll come back to this one )

# Generalized

( You can often apply them to many different instances )

# Functions

## are types

``Int -> Int``
``(+3)``

# Writing functions in different ways

``````add :: Int -> Int -> Int
add x y = x + y``````
``````add :: Int -> Int -> Int
add = (\x y -> x + y)``````
``````add :: Int -> (Int -> Int)
add x = (\y -> x + y)``````
``three = 1 + 2``
``three = (+) 2 1``
``add1 = (+) 2``

Missing a parameter?

# Composition Everywhere!

``show :: Int -> String``
``length :: String -> Int``
``digitCount :: Int -> Int``
``````digitCount :: Int -> Int
digitCount value = length (show value)``````
``````digitCount :: Int -> Int
digitCount value = length . show value``````
``````digitCount :: Int -> Int
digitCount = length . show ``````

# . is a function!

## show is (a -> b) (Int -> String)length is (b -> c) (String -> Int) the number you pass in is a

``(.) :: (b -> c) -> (a -> b) -> a -> c``

# Use types to represent

## Constraints

``data Suit = Club | Diamond | Spades | Diamond``
``````data Rank = Two
| Three
| Four
| Five
| Six
| Seven
| Eight
| Nine
| Ten
| Jack
| Queen
| King
| Ace``````
``data Card = Card Suit Rank``
``type Deck = [Card]``

# Use types to indicate

## Errors

``parseInt :: String -> Int``
``parseInt :: String -> Maybe Int``

# Make illegal states

## Unrepresentable

``data Status = Verified | Unverified``

# Use types for

## State Machines

``data ShoppingCart = Empty | Active [Item] | Paid [Item]``

Beautiful clean

internal model

Dirty unclean outside world

The Great Gate

# Functions of type

## Transform values of type a to values of type b

``(+1) :: Int -> Int``
``show :: Int -> String``

( Increments a number by 1 )

( Returns the string representation of a number )

# Maybe

``data Maybe a = Nothing | Just a``
``````2      -- a value
Just 2 -- a value with context.
``````

# fmap

## fmap knows how to apply a function to a value in a context

``````fmap (+3) (Just 2)
``````
``Just 5``

# Functor is a typeclass

## that means it implements the behaviour the typeclass describes

``````class Functor f where
fmap :: (a -> b) -> f a -> f b``````

To make data type f a functor

You'll need to implement this function for your data type

# The map function

``map :: (a -> b) -> [a] -> [b]``
``map :: (Int -> String) -> [Int] -> [String]``
``map show [1, 2, 3, 4] ``
``["1", "2", "3", "4"]``
``map (+1) [1, 2, 3, 4]``
``[2, 3, 4, 5]``

# Examples

## fmap specialized to lists has the type:

``(a -> b) -> ([a] -> [b])``

## fmap specialized to Maybe has the type:

``(a -> b) -> (Maybe a -> Maybe b)``

# Context

## Just 1

``data Maybe a = Nothing | Just a``