Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
foldl, type RFn a r = r -> a -> r
type Reducer a = forall r . RFn a r -> r -> r
type Transducer b a = forall r . RFn a r -> RFn b r
-- трансдьюсер `#(map %)`
map_t :: (a -> b) -> Transducer a b
map_t f xf r a = xf r (f a)
-- аналог `clojure.core.reducers/reducer`
reducer :: Transducer a b -> [a] -> Reducer b
reducer xf ls rf a = foldl (xf rf) a ls
-- трансдьюсер `(map inc)`
my_transducer :: Transducer Int Int
my_transducer = map_t (+1)
my_list :: [Int]
my_list = [1, 2, 3]
my_reducer :: Reducer Int
my_reducer = reducer my_transducer my_list
main = print $ my_reducer (*) 1
foldl (*) 1 $ map (+1) [1,2,3]
foldl (+) 0 $ (map succ). (filter (> 2)). (map (*3)) $ [1,2,3,4]
foldl ((succ. (> 2). (* 3)) (+)) 0 [1,2,3,4]
filter (getAll. foldMap (All.) [odd, (>7), (<100), prime])
[x | x < — xs, odd x, x > 7, x < 100, prime x]
class Foldable t where
-- | Combine the elements of a structure using a monoid.
fold :: Monoid m => t m -> m
fold = foldMap id
-- | Map each element of the structure to a monoid,
-- and combine the results.
foldMap :: Monoid m => (a -> m) -> t a -> m
foldMap f = foldr (mappend . f) mempty
foldr :: (a -> b -> b) -> b -> t a -> b
Clojure — трансдьюсеры, редьюсеры и прочая муть