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
//test |
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
// Takes an array with possible nested arrays, and returns a **new** flat array. | |
const flatten = array => | |
array.reduce( | |
(flattened, elem) => | |
flattened.concat(Array.isArray(elem) ? flatten(elem) : elem), | |
[] | |
); | |
// From here on below we have only test utilities | |
const test = (testedFunc, input, expectedOutput, comparator) => { |
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
const GET_LINE = Symbol('GET_LINE'); | |
const PUT_LINE = Symbol('PUT_LINE'); | |
const RETURN = Symbol('RETURN'); | |
const NOTHING = 'NOTHING!!'; | |
const unit = undefined; | |
// Primitive IO actions, these are pure functions. | |
const returnIO = a => ({ | |
type: RETURN, | |
flatMap: f => f(a), |
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
newtype Cont r a = Cont { runCont :: (a -> r) -> r } |
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
myCont :: Cont r Int | |
myCont = Cont (\k -> k (10 + 30)) | |
double :: Int -> Int | |
double x = x * 2 | |
(runCont myCont) double -- 80 |
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
{-# LANGUAGE InstanceSigs #-} | |
instance Functor (Cont r) where | |
fmap :: (a -> b) -> Cont r a -> Cont r b | |
-- Equivalent to | |
-- fmap :: (a -> b) -> ((a -> r) -> r) -> ((b -> r) -> r) | |
fmap f (Cont g) = Cont $ \k -> g (\a -> k (f a)) |
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
myCont2 :: Cont Int Int | |
myCont2 = Cont (\k -> k 0 + k 15) | |
plus1 :: Int -> Int | |
plus1 x = x + 1 | |
runCont myCont2 plus1 -- plus1 0 + plus1 15 = 1 + 16 = 17 | |
plus100 :: Int -> Int | |
plus100 x = x + 100 |
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
instance Monad (Cont r) where | |
return x = Cont (\k -> k x) | |
-- (>>=) :: Cont r a -> (a -> Cont r b) -> Cont r b | |
(Cont c) >>= f = Cont $ \k -> c (\x -> runCont (f x) k) |
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
myCont = Cont (\k -> k 0 + k 15) | |
runCont myCont id -- id 0 + id 15 = 15 | |
myCont2 = myCont >>= (\x -> Cont ($ (x * 2)) | |
runCont myCont2 id -- 30. Wait, what? |
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
k0Plusk15 = \k -> k 0 + k 15 | |
myCont = Cont k0Plusk15 -- Extracted the function for ease of notation later below. | |
myCont2 = myCont >>= (\x -> return (x * 2)) | |
= (Cont k0Plusk15) >>= (\x -> Cont ($ (x * 2))) | |
= Cont $ \k' -> k0Plusk15 (\x -> runCont (Cont ($ (x * 2))) k') | |
= Cont $ \k' -> k0Plusk15 (\x -> ($ (x * 2)) k') | |
= Cont $ \k' -> k0Plusk15 (\x -> k' (x * 2)) | |
= Cont $ \k' -> k0Plusk15 (\x -> k' (x * 2)) |
OlderNewer