@@ -5,6 +5,7 @@ module Data.Lens.Fold
55 , preview , foldOf , foldMapOf , foldrOf , foldlOf , toListOf , firstOf , lastOf
66 , maximumOf , minimumOf , allOf , anyOf , andOf , orOf , elemOf , notElemOf , sumOf
77 , productOf , lengthOf , findOf , sequenceOf_ , has , hasn't , replicated , filtered
8+ , folded , unfolded
89 , ifoldMapOf , ifoldrOf , ifoldlOf , iallOf , ianyOf , itoListOf , itraverseOf_
910 , module ExportTypes
1011 ) where
@@ -14,12 +15,12 @@ import Prelude
1415import Control.Apply ((*>))
1516
1617import Data.Either (Either (..), either )
17- import Data.Foldable (Foldable , foldr )
18- import Data.Functor.Contravariant (Contravariant )
18+ import Data.Foldable (Foldable , foldMap )
1919import Data.List (List (..), (:))
2020import Data.Maybe (Maybe (..), maybe )
2121import Data.Maybe.First (First (..), runFirst )
2222import Data.Maybe.Last (Last (..), runLast )
23+ import Data.Monoid (Monoid , mempty )
2324import Data.Monoid.Additive (Additive (..), runAdditive )
2425import Data.Monoid.Conj (Conj (..), runConj )
2526import Data.Monoid.Disj (Disj (..), runDisj )
@@ -28,7 +29,6 @@ import Data.Monoid.Endo (Endo(..), runEndo)
2829import Data.Monoid.Multiplicative (Multiplicative (..), runMultiplicative )
2930import Data.Profunctor (dimap )
3031import Data.Profunctor.Choice (Choice , right )
31- import Data.Profunctor.Star (Star (..), runStar )
3232import Data.Tuple (Tuple (..), uncurry )
3333
3434import Data.Lens.Internal.Void (coerce )
@@ -152,25 +152,19 @@ filtered :: forall p a. (Choice p) => (a -> Boolean) -> OpticP p a a
152152filtered f = dimap (\x -> if f x then Right x else Left x) (either id id) <<< right
153153
154154-- | Replicates the elements of a fold.
155- replicated
156- :: forall a b t f . (Applicative f , Contravariant f )
157- => Int -> Optic (Star f ) a b a t
158- replicated n p = Star (flip go n <<< runStar p) where
159- go x 0 = coerce (pure unit)
160- go x n = x *> go x (n - 1 )
155+ replicated :: forall a b t r . (Monoid r ) => Int -> Fold r a b a t
156+ replicated n = Forget <<< go n <<< runForget where
157+ go 0 x = mempty
158+ go n x = x <> go (n - 1 ) x
161159
162160-- | Folds over a `Foldable` container.
163- folded
164- :: forall f g a b t . (Applicative f , Contravariant f , Foldable g )
165- => Optic (Star f ) (g a ) b a t
166- folded p = Star $ foldr (\a r -> runStar p a *> r) (coerce $ pure unit)
161+ folded :: forall g a b t r . (Monoid r , Foldable g ) => Fold r (g a ) b a t
162+ folded = Forget <<< foldMap <<< runForget
167163
168164-- | Builds a `Fold` using an unfold.
169- unfolded
170- :: forall f s t a b . (Applicative f , Contravariant f )
171- => (s -> Maybe (Tuple a s )) -> Optic (Star f ) s t a b
172- unfolded f p = Star go where
173- go = maybe (coerce $ pure unit) (\(Tuple a s) -> runStar p a *> go s) <<< f
165+ unfolded :: forall r s t a b . (Monoid r ) => (s -> Maybe (Tuple a s )) -> Fold r s t a b
166+ unfolded f p = Forget go where
167+ go = maybe mempty (\(Tuple a sn) -> runForget p a <> go sn) <<< f
174168
175169-- | Fold map over an `IndexedFold`.
176170ifoldMapOf :: forall r i s t a b . IndexedFold r i s t a b -> (i -> a -> r ) -> s -> r
0 commit comments