Skip to content

Instantly share code, notes, and snippets.

@TheSeamau5
Created August 22, 2015 00:32
Show Gist options
  • Save TheSeamau5/f9e5bdba1e56aa14d433 to your computer and use it in GitHub Desktop.
Save TheSeamau5/f9e5bdba1e56aa14d433 to your computer and use it in GitHub Desktop.
type alias Reducer input state = input -> state -> state
dimapR : (a -> b) -> (b -> a) -> Reducer x a -> Reducer x b
dimapR f g reducer x b =
f (reducer x (g b))
contramap : (b -> a) -> Transducer a b r
contramap f reducer b =
reducer (f b)
keeping : (a -> Bool) -> Transducer a a r
keeping predicate reducer a r =
if predicate a
then
reducer a r
else
r
dropping : (a -> Bool) -> Transducer a a r
dropping predicate =
keeping (predicate >> not)
divide : (c -> (a, b)) -> Reducer a r -> Reducer b r -> Reducer c r
divide f reducerA reducerB c =
let
(a, b) = f c
in
reducerA a >> reducerB b
conquer : Reducer a x
conquer _ x = x
type alias Transducer a b r = Reducer a r -> Reducer b r
map : (a -> b) -> Transducer a x y -> Transducer b x y
map f transducer reducer =
transducer (contramap f reducer)
compose : Transducer a b r -> Transducer b c r -> Transducer a c r
compose = (>>)
constant : Transducer a b r
constant _ = conquer
id : Transducer a a r
id x = x
dimap : (a -> b) -> (b -> a) -> Transducer x y a -> Transducer x y b
dimap f g transducer reducer y b =
f (transducer (dimapR g f reducer) y (g b))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment