You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-- Given a simple 2 arg functionminus::Int->Int->Int
minus x y = x - y
-- We can easily paramterise on the second argumentsubtractFrom2::Int->Int
subtractFrom2 = minus 2-- The question is how do we parameterise the *first* argument?-- Pointful waysubtract2::Int->Int
subtract2 y = minus x 2-- Pointfree using `flip`. Boring...subtract2::Int->Int
subtract2 = flip minus 2-- Pointfree using `<@>`. Uses functor instance for functions. Cool!-- <@> :: Functor f => f (a -> b) -> a -> f b-- minus :: F (a -> b) === x -> (a -> b)-- 2 :: a-- minus <@> 2 :: F b === x -> bsubtract2::Int->Int
subtract2 = minus <@> 2
Effectful function as the first argument
-- It's a common pattern to take a monadic function as first arg, and an object to operate on as the second argwithName::forallma. (Name->ma) ->Person->ma
withName f p = f p.name
-- We take the function as the first arg because then it's easy to parameterise the objectprintName::Person->EffectUnit
printName = withName \n -> putStrLn ("Name : " <> n)
-- And of course, we can also parameterise on the function with `<@>`withJohn::forallma. (Name->ma) ->ma
withJohn = withName <@> john
-- But when using the function fully saturated it looks ugly, especially when the function is largevalidPerson::PersonId->EffectBool
validPerson pid = do
p <- getPerson pid
withName (\name ->dolet isTooShort = length name < 2
isValidIdentifier <- validateIdentifier name
pure $ not isTooShort && isValidIdentifier) p --- Ugly parens need closing, Plus this `p` is almost buried.-- What we can do instead is use `#`validPerson::PersonId->EffectBool
validPerson pid = do
p <- getPerson pid
p # withName \name ->dolet isTooShort = length name < 2
isValidIdentifier <- validateIdentifier name
pure $ not isTooShort && isValidIdentifier --- No ugly parens needed! No buried `p` argument.