Created
December 26, 2014 23:35
-
-
Save matthieubulte/64eb81c61d0ee36ccfd7 to your computer and use it in GitHub Desktop.
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
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