Abstraction for working with continuations.
Continuation type
return : a -> Continue r a
construct continuation from value
return 42
|> exec
--> 42
run : (a -> r) -> Continue r a -> r
Run function within continuation
return "foo"
|> run (\h -> h :: [ "bar" ])
--> [ "foo", "bar" ]
This will inject timesTwo
as continuation to
squareAndAdd
. Final calculation should then be:
n ^ 2 + n * 2
where n
is argument passed to squereAndAdd
.
squareAndAdd : Int -> Continue Int Int
squareAndAdd a =
Cont <| \k -> a ^ 2 + k a
times2 : Int -> Int
times2 a =
2 * a
run times2 (squareAndAdd 3)
--> 15
run times2 (squareAndAdd 4)
--> 24
exec : Continue r r -> r
Run with identity function.
This is useful only in case of very simple scenarios.
return ()
|> exec
--> ()
map : (a -> b) -> Continue r a -> Continue r b
Map value within continuation
return 5
|> map (\a -> a ^ 2)
|> exec
--> 25
andMap : Continue r a -> Continue r (a -> b) -> Continue r b
Apply values to function within continuation
return (\a b -> a / b)
|> andMap (return 5)
|> andMap (return 2)
|> exec
--> 2.5
andThen : (a -> Continue r b) -> Continue r a -> Continue r b
Chain continuations
return 3
|> andThen (\a -> return <| a * 2)
|> exec
--> 6