Skip to content

Instantly share code, notes, and snippets.

@TheSeamau5
Created August 23, 2015 03:14
Show Gist options
  • Save TheSeamau5/c228c37f257802165f72 to your computer and use it in GitHub Desktop.
Save TheSeamau5/c228c37f257802165f72 to your computer and use it in GitHub Desktop.
import Task exposing (Task)
type Never = Never Never
type Behavior a = Behavior (a -> Task Never (Behavior a))
contramap : (b -> a) -> Behavior a -> Behavior b
contramap f (Behavior g) =
Behavior (\b -> Task.map (contramap f) (g (f b)))
divide : (c -> (a, b)) -> Behavior a -> Behavior b -> Behavior c
divide f (Behavior fa) (Behavior fb) =
Behavior <|
\c ->
case f c of
(a, b) ->
fa a
`Task.andThen` \behaviorA -> fb b
`Task.andThen` \behaviorB -> Task.succeed (divide f behaviorA behaviorB)
runBehavior : Behavior a -> List a -> Task Never ()
runBehavior (Behavior f) list =
case list of
[] ->
Task.succeed ()
x :: xs ->
f x
`Task.andThen` \behavior -> runBehavior behavior xs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment