@@ -5,6 +5,7 @@ module Data.Lens.Fold
5
5
, preview , foldOf , foldMapOf , foldrOf , foldlOf , toListOf , firstOf , lastOf
6
6
, maximumOf , minimumOf , allOf , anyOf , andOf , orOf , elemOf , notElemOf , sumOf
7
7
, productOf , lengthOf , findOf , sequenceOf_ , has , hasn't , replicated , filtered
8
+ , folded , unfolded
8
9
, ifoldMapOf , ifoldrOf , ifoldlOf , iallOf , ianyOf , itoListOf , itraverseOf_
9
10
, module ExportTypes
10
11
) where
@@ -14,12 +15,12 @@ import Prelude
14
15
import Control.Apply ((*>))
15
16
16
17
import Data.Either (Either (..), either )
17
- import Data.Foldable (Foldable , foldr )
18
- import Data.Functor.Contravariant (Contravariant )
18
+ import Data.Foldable (Foldable , foldMap )
19
19
import Data.List (List (..), (:))
20
20
import Data.Maybe (Maybe (..), maybe )
21
21
import Data.Maybe.First (First (..), runFirst )
22
22
import Data.Maybe.Last (Last (..), runLast )
23
+ import Data.Monoid (Monoid , mempty )
23
24
import Data.Monoid.Additive (Additive (..), runAdditive )
24
25
import Data.Monoid.Conj (Conj (..), runConj )
25
26
import Data.Monoid.Disj (Disj (..), runDisj )
@@ -28,7 +29,6 @@ import Data.Monoid.Endo (Endo(..), runEndo)
28
29
import Data.Monoid.Multiplicative (Multiplicative (..), runMultiplicative )
29
30
import Data.Profunctor (dimap )
30
31
import Data.Profunctor.Choice (Choice , right )
31
- import Data.Profunctor.Star (Star (..), runStar )
32
32
import Data.Tuple (Tuple (..), uncurry )
33
33
34
34
import Data.Lens.Internal.Void (coerce )
@@ -152,25 +152,19 @@ filtered :: forall p a. (Choice p) => (a -> Boolean) -> OpticP p a a
152
152
filtered f = dimap (\x -> if f x then Right x else Left x) (either id id) <<< right
153
153
154
154
-- | 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
161
159
162
160
-- | 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
167
163
168
164
-- | 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
174
168
175
169
-- | Fold map over an `IndexedFold`.
176
170
ifoldMapOf :: forall r i s t a b . IndexedFold r i s t a b -> (i -> a -> r ) -> s -> r
0 commit comments