Skip to content

Instantly share code, notes, and snippets.

@matthieubulte
Created December 26, 2014 23:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthieubulte/64eb81c61d0ee36ccfd7 to your computer and use it in GitHub Desktop.
Save matthieubulte/64eb81c61d0ee36ccfd7 to your computer and use it in GitHub Desktop.
type Reducer a r = r -> a -> r
type Transducer a b = forall r. Reducer a r -> Reducer b r
class Conjable f where
empty :: f a
conj :: Reducer a (f a)
class Foldable f where
fold :: Transducer a (f a)
instance Conjable [] where
empty = []
conj xs x = xs ++ [x]
instance Foldable [] where
fold = foldl
mapping :: (a -> b) -> Transducer b a
mapping f xf xs x = xf xs (f x)
filtering :: (a -> Bool) -> Transducer a a
filtering p xf xs x = if p x then xf xs x else xs
flatMapping :: Foldable f => (a -> f b) -> Transducer b a
flatMapping f xf xs x = fold xf xs (f x)
llist :: (Foldable f, Conjable f) => Transducer b a -> f a -> f b
llist xf xs = fold (xf conj) empty xs
times :: Int -> [Int]
times n = take n (repeat n)
op = flatMapping times . filtering even . mapping (2 ^)
l = llist op [1..10]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment