1 {-# LANGUAGE ExistentialQuantification #-}
2 {-# LANGUAGE RankNTypes #-}
4 import Control.Lens (Getting, foldMapOf)
6 data Fold i o = forall m . Monoid m => Fold (i -> m) (m -> o)
9 instance Functor (Fold i) where
10 fmap k (Fold tally summarize) = Fold tally (k . summarize)
12 instance Applicative (Fold i) where
13 pure o = Fold (\_ -> ()) (\_ -> o)
15 Fold tallyF summarizeF <*> Fold tallyX summarizeX = Fold tally summarize
17 tally i = (tallyF i, tallyX i)
18 summarize (nF, nX) = summarizeF nF (summarizeX nX)
20 focus :: (forall m . Monoid m => Getting m b a) -> Fold a o -> Fold b o
21 focus lens (Fold tally summarize) = Fold (foldMapOf lens tally) summarize