@@ -8,34 +8,36 @@ module Data.Lens.Fold
88 , folded , unfolded
99 , ifoldMapOf , ifoldrOf , ifoldlOf , iallOf , ianyOf , itoListOf , itraverseOf_
1010 , module ExportTypes
11- ) where
11+ )
12+ where
1213
1314import Prelude
1415
1516import Data.Either (Either (..), either )
1617import Data.Foldable (class Foldable , foldMap )
1718import Data.HeytingAlgebra (tt , ff )
18- import Data.Lens.Internal.Forget (Forget (..), runForget )
19- import Data.Lens.Types (Fold , FoldP ) as ExportTypes
20- import Data.Lens.Types (IndexedFold , Fold , OpticP , Indexed (..))
19+ import Data.Lens.Internal.Forget (Forget (..))
20+ import Data.Lens.Types (Fold , Fold' ) as ExportTypes
21+ import Data.Lens.Types (IndexedFold , Fold , Optic' , Indexed (..))
2122import Data.List (List (..), (:))
2223import Data.Maybe (Maybe (..), maybe )
23- import Data.Maybe.First (First (..), runFirst )
24- import Data.Maybe.Last (Last (..), runLast )
24+ import Data.Maybe.First (First (..))
25+ import Data.Maybe.Last (Last (..))
2526import Data.Monoid (class Monoid , mempty )
26- import Data.Monoid.Additive (Additive (..), runAdditive )
27- import Data.Monoid.Conj (Conj (..), runConj )
28- import Data.Monoid.Disj (Disj (..), runDisj )
29- import Data.Monoid.Dual (Dual (..), runDual )
30- import Data.Monoid.Endo (Endo (..), runEndo )
31- import Data.Monoid.Multiplicative (Multiplicative (..), runMultiplicative )
27+ import Data.Monoid.Additive (Additive (..))
28+ import Data.Monoid.Conj (Conj (..))
29+ import Data.Monoid.Disj (Disj (..))
30+ import Data.Monoid.Dual (Dual (..))
31+ import Data.Monoid.Endo (Endo (..))
32+ import Data.Monoid.Multiplicative (Multiplicative (..))
33+ import Data.Newtype (unwrap , under )
3234import Data.Profunctor (dimap )
3335import Data.Profunctor.Choice (class Choice , right )
3436import Data.Tuple (Tuple (..), uncurry )
3537
3638-- | Previews the first value of a fold, if there is any.
3739preview :: forall s t a b . Fold (First a ) s t a b -> s -> Maybe a
38- preview p = runFirst <<< foldMapOf p (First <<< Just )
40+ preview p = unwrap <<< foldMapOf p (First <<< Just )
3941
4042-- | Synonym for `preview`, flipped.
4143previewOn :: forall s t a b . s -> Fold (First a ) s t a b -> Maybe a
@@ -45,71 +47,71 @@ infixl 8 previewOn as ^?
4547
4648-- | Folds all foci of a `Fold` to one. Note that this is the same as `view`.
4749foldOf :: forall s t a b . Fold a s t a b -> s -> a
48- foldOf p = runForget (p ( Forget id))
50+ foldOf p = foldMapOf p id
4951
5052-- | Maps and then folds all foci of a `Fold`.
5153foldMapOf :: forall s t a b r . Fold r s t a b -> (a -> r ) -> s -> r
52- foldMapOf p f = runForget (p ( Forget f))
54+ foldMapOf = under Forget
5355
5456-- | Right fold over a `Fold`.
5557foldrOf :: forall s t a b r . Fold (Endo r ) s t a b -> (a -> r -> r ) -> r -> s -> r
56- foldrOf p f r = flip runEndo r <<< foldMapOf p (Endo <<< f)
58+ foldrOf p f r = flip unwrap r <<< foldMapOf p (Endo <<< f)
5759
5860-- | Left fold over a `Fold`.
5961foldlOf :: forall s t a b r . Fold (Dual (Endo r )) s t a b -> (r -> a -> r ) -> r -> s -> r
60- foldlOf p f r = flip runEndo r <<< runDual <<< foldMapOf p (Dual <<< Endo <<< flip f)
62+ foldlOf p f r = flip unwrap r <<< unwrap <<< foldMapOf p (Dual <<< Endo <<< flip f)
6163
6264-- | Whether all foci of a `Fold` satisfy a predicate.
63- allOf :: forall s t a b r . ( BooleanAlgebra r ) => Fold (Conj r ) s t a b -> (a -> r ) -> s -> r
64- allOf p f = runConj <<< foldMapOf p (Conj <<< f)
65+ allOf :: forall s t a b r . HeytingAlgebra r => Fold (Conj r ) s t a b -> (a -> r ) -> s -> r
66+ allOf p f = unwrap <<< foldMapOf p (Conj <<< f)
6567
6668-- | Whether any focus of a `Fold` satisfies a predicate.
67- anyOf :: forall s t a b r . ( BooleanAlgebra r ) => Fold (Disj r ) s t a b -> (a -> r ) -> s -> r
68- anyOf p f = runDisj <<< foldMapOf p (Disj <<< f)
69+ anyOf :: forall s t a b r . HeytingAlgebra r => Fold (Disj r ) s t a b -> (a -> r ) -> s -> r
70+ anyOf p f = unwrap <<< foldMapOf p (Disj <<< f)
6971
7072-- | The conjunction of all foci of a `Fold`.
71- andOf :: forall s t a b . ( BooleanAlgebra a ) => Fold (Conj a ) s t a b -> s -> a
73+ andOf :: forall s t a b . HeytingAlgebra a => Fold (Conj a ) s t a b -> s -> a
7274andOf p = allOf p id
7375
7476-- | The disjunction of all foci of a `Fold`.
75- orOf :: forall s t a b . ( BooleanAlgebra a ) => Fold (Disj a ) s t a b -> s -> a
77+ orOf :: forall s t a b . HeytingAlgebra a => Fold (Disj a ) s t a b -> s -> a
7678orOf p = anyOf p id
7779
7880-- | Whether a `Fold` contains a given element.
79- elemOf :: forall s t a b . ( Eq a ) => Fold (Disj Boolean ) s t a b -> a -> s -> Boolean
81+ elemOf :: forall s t a b . Eq a => Fold (Disj Boolean ) s t a b -> a -> s -> Boolean
8082elemOf p a = anyOf p (_ == a)
8183
8284-- | Whether a `Fold` not contains a given element.
83- notElemOf :: forall s t a b . ( Eq a ) => Fold (Conj Boolean ) s t a b -> a -> s -> Boolean
85+ notElemOf :: forall s t a b . Eq a => Fold (Conj Boolean ) s t a b -> a -> s -> Boolean
8486notElemOf p a = allOf p (_ /= a)
8587
8688-- | The sum of all foci of a `Fold`.
87- sumOf :: forall s t a b . ( Semiring a ) => Fold (Additive a ) s t a b -> s -> a
88- sumOf p = runAdditive <<< foldMapOf p Additive
89+ sumOf :: forall s t a b . Semiring a => Fold (Additive a ) s t a b -> s -> a
90+ sumOf p = unwrap <<< foldMapOf p Additive
8991
9092-- | The product of all foci of a `Fold`.
91- productOf :: forall s t a b . ( Semiring a ) => Fold (Multiplicative a ) s t a b -> s -> a
92- productOf p = runMultiplicative <<< foldMapOf p Multiplicative
93+ productOf :: forall s t a b . Semiring a => Fold (Multiplicative a ) s t a b -> s -> a
94+ productOf p = unwrap <<< foldMapOf p Multiplicative
9395
9496-- | The number of foci of a `Fold`.
9597lengthOf :: forall s t a b . Fold (Additive Int ) s t a b -> s -> Int
96- lengthOf p = runAdditive <<< foldMapOf p (const $ Additive 1 )
98+ lengthOf p = unwrap <<< foldMapOf p (const $ Additive 1 )
9799
98100-- | The first focus of a `Fold`, if there is any. Synonym for `preview`.
99101firstOf :: forall s t a b . Fold (First a ) s t a b -> s -> Maybe a
100- firstOf p = runFirst <<< foldMapOf p (First <<< Just )
102+ firstOf p = unwrap <<< foldMapOf p (First <<< Just )
101103
102104-- | The last focus of a `Fold`, if there is any.
103105lastOf :: forall s t a b . Fold (Last a ) s t a b -> s -> Maybe a
104- lastOf p = runLast <<< foldMapOf p (Last <<< Just )
106+ lastOf p = unwrap <<< foldMapOf p (Last <<< Just )
105107
106108-- | The maximum of all foci of a `Fold`, if there is any.
107- maximumOf :: forall s t a b . ( Ord a ) => Fold (Endo (Maybe a )) s t a b -> s -> Maybe a
109+ maximumOf :: forall s t a b . Ord a => Fold (Endo (Maybe a )) s t a b -> s -> Maybe a
108110maximumOf p = foldrOf p (\a -> Just <<< maybe a (max a)) Nothing where
109111 max a b = if a > b then a else b
110112
111113-- | The minimum of all foci of a `Fold`, if there is any.
112- minimumOf :: forall s t a b . ( Ord a ) => Fold (Endo (Maybe a )) s t a b -> s -> Maybe a
114+ minimumOf :: forall s t a b . Ord a => Fold (Endo (Maybe a )) s t a b -> s -> Maybe a
113115minimumOf p = foldrOf p (\a -> Just <<< maybe a (min a)) Nothing where
114116 min a b = if a < b then a else b
115117
@@ -125,7 +127,7 @@ sequenceOf_
125127 => Fold (Endo (f Unit )) s t (f a ) b
126128 -> s
127129 -> f Unit
128- sequenceOf_ p = flip runEndo (pure unit) <<< foldMapOf p \f -> Endo (f *> _)
130+ sequenceOf_ p = flip unwrap (pure unit) <<< foldMapOf p \f -> Endo (f *> _)
129131
130132-- | Traverse the foci of a `Fold`, discarding the results.
131133traverseOf_
@@ -149,29 +151,30 @@ infixl 8 toListOfOn as ^..
149151
150152-- | Determines whether a `Fold` has at least one focus.
151153has :: forall s t a b r . HeytingAlgebra r => Fold (Disj r ) s t a b -> s -> r
152- has p = runDisj <<< foldMapOf p (const (Disj tt))
154+ has p = unwrap <<< foldMapOf p (const (Disj tt))
153155
154156-- | Determines whether a `Fold` does not have a focus.
155157hasn't :: forall s t a b r . HeytingAlgebra r => Fold (Conj r ) s t a b -> s -> r
156- hasn't p = runConj <<< foldMapOf p (const (Conj ff))
158+ hasn't p = unwrap <<< foldMapOf p (const (Conj ff))
157159
158160-- | Filters on a predicate.
159- filtered :: forall p a . ( Choice p ) => (a -> Boolean ) -> OpticP p a a
161+ filtered :: forall p a . Choice p => (a -> Boolean ) -> Optic' p a a
160162filtered f =
161163 right >>>
162164 dimap
163165 (\x -> if f x then Right x else Left x)
164166 (either id id)
165167
166168-- | Replicates the elements of a fold.
167- replicated :: forall a b t r . (Monoid r ) => Int -> Fold r a b a t
168- replicated n = Forget <<< go n <<< runForget where
169+ replicated :: forall a b t r . Monoid r => Int -> Fold r a b a t
170+ replicated i (Forget a) = Forget (go i a)
171+ where
169172 go 0 x = mempty
170173 go n x = x <> go (n - 1 ) x
171174
172175-- | Folds over a `Foldable` container.
173176folded :: forall g a b t r . (Monoid r , Foldable g ) => Fold r (g a ) b a t
174- folded = Forget <<< foldMap <<< runForget
177+ folded ( Forget a) = Forget ( foldMap a)
175178
176179-- | Builds a `Fold` using an unfold.
177180unfolded
@@ -181,7 +184,7 @@ unfolded
181184 -> Fold r s t a b
182185unfolded f p = Forget go
183186 where
184- go = maybe mempty (\(Tuple a sn) -> runForget p a <> go sn) <<< f
187+ go = maybe mempty (\(Tuple a sn) -> unwrap p a <> go sn) <<< f
185188
186189-- | Fold map over an `IndexedFold`.
187190ifoldMapOf
@@ -190,7 +193,7 @@ ifoldMapOf
190193 -> (i -> a -> r )
191194 -> s
192195 -> r
193- ifoldMapOf p f = runForget $ p $ Indexed $ Forget (uncurry f)
196+ ifoldMapOf p f = unwrap $ p $ Indexed $ Forget (uncurry f)
194197
195198-- | Right fold over an `IndexedFold`.
196199ifoldrOf
@@ -200,7 +203,7 @@ ifoldrOf
200203 -> r
201204 -> s
202205 -> r
203- ifoldrOf p f r = flip runEndo r <<< ifoldMapOf p (\i -> Endo <<< f i)
206+ ifoldrOf p f r = flip unwrap r <<< ifoldMapOf p (\i -> Endo <<< f i)
204207
205208-- | Left fold over an `IndexedFold`.
206209ifoldlOf
@@ -211,8 +214,8 @@ ifoldlOf
211214 -> s
212215 -> r
213216ifoldlOf p f r =
214- flip runEndo r
215- <<< runDual
217+ flip unwrap r
218+ <<< unwrap
216219 <<< ifoldMapOf p (\i -> Dual <<< Endo <<< flip (f i))
217220
218221-- | Whether all foci of an `IndexedFold` satisfy a predicate.
@@ -223,7 +226,7 @@ iallOf
223226 -> (i -> a -> r )
224227 -> s
225228 -> r
226- iallOf p f = runConj <<< ifoldMapOf p (\i -> Conj <<< f i)
229+ iallOf p f = unwrap <<< ifoldMapOf p (\i -> Conj <<< f i)
227230
228231-- | Whether any focus of an `IndexedFold` satisfies a predicate.
229232ianyOf
@@ -233,7 +236,7 @@ ianyOf
233236 -> (i -> a -> r )
234237 -> s
235238 -> r
236- ianyOf p f = runDisj <<< ifoldMapOf p (\i -> Disj <<< f i)
239+ ianyOf p f = unwrap <<< ifoldMapOf p (\i -> Disj <<< f i)
237240
238241-- | Find the first focus of an `IndexedFold` that satisfies a predicate, if
239242-- | there is any.
0 commit comments