Last active
October 30, 2023 17:48
-
-
Save crosstyan/ba7004896ab5516a93e0da74121c83bc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Flow | |
-- could replace following Just to pure (keep the first one?) | |
-- infix bind, boring! | |
-- https://www.youtube.com/watch?v=ZhuHCtR3xq8 | |
-- \a -> f a >>= \a -> g a | |
-- where f :: a -> M b | |
-- g :: b -> M c | |
-- when the type a = b = c | |
-- it could be seem as Monoid? | |
test' = \a -> Just a | |
>>= \a -> Just (a * 2) | |
>>= \a -> Just (a * 3) | |
>>= \a -> Just (a + 15) | |
>>= \a -> Just (a > 20) | |
-- (>>=) :: forall a b . Monad m => m a -> (a -> m b) -> m b | |
-- (& is |> in Flow) | |
-- (&) :: a -> (a -> b) -> b | |
-- We need to flip the >>= to make the lifting function (a -> m b) as | |
-- the first parameter | |
test = \a -> | |
((>>=) (Just a) $ \b -> Just (b * 2)) | |
|> (flip (>>=) (\c -> Just (c * 3))) | |
|> (flip (>>=) (\n -> Just (n + 15))) | |
|> (flip (>>=) (\n -> Just (n > 20))) | |
-- pure is Just in the context | |
-- https://en.wikibooks.org/wiki/Haskell/do_notation | |
test'' a = do | |
a <- Just a | |
b <- pure $ a * 2 -- hlint would complaint | |
c <- pure $ b * 3 -- why not use let? | |
d <- pure $ c + 15 | |
e <- pure $ d > 20 | |
pure e | |
-- use let | |
test''' a = do | |
a <- Just a | |
let b = a * 2 | |
let c = b * 3 | |
let d = c + 15 | |
let e = d > 20 | |
pure e | |
-- https://wiki.haskell.org/Let_vs._Where | |
-- use let, but in lambda | |
test'''' = \a -> Just a | |
>>= ( \a -> | |
let b = a * 2 | |
in let c = b * 3 | |
in let d = c + 15 | |
in let e = d > 20 | |
in pure e) | |
-- same function as test```` but with nice indentation | |
test''''' = \a -> Just a | |
>>= ( \a -> | |
let b = a * 2 in | |
let c = b * 3 in | |
let d = c + 15 in | |
let e = d > 20 in | |
pure e) | |
-- or make an intermedia function to make it cleaner | |
f a = e | |
where b = a * 2 | |
c = b * 3 | |
d = c + 15 | |
e = d > 20 | |
test'''''' = \a -> Just a >>= (\a -> pure $ f a) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment