11-- | This module defines functions for working with folds.
2-
32module Data.Lens.Fold
43 ( (^?), previewOn
54 , (^..), toListOfOn
@@ -13,10 +12,12 @@ module Data.Lens.Fold
1312
1413import Prelude
1514
16- import Control.Apply ((*>))
17-
1815import Data.Either (Either (..), either )
1916import Data.Foldable (class Foldable , foldMap )
17+ import 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 (..))
2021import Data.List (List (..), (:))
2122import Data.Maybe (Maybe (..), maybe )
2223import Data.Maybe.First (First (..), runFirst )
@@ -32,15 +33,6 @@ import Data.Profunctor (dimap)
3233import Data.Profunctor.Choice (class Choice , right )
3334import Data.Tuple (Tuple (..), uncurry )
3435
35- import Data.Lens.Internal.Void (coerce )
36- import Data.Lens.Internal.Forget (Forget (..), runForget )
37- import Data.Lens.Types (Fold (), FoldP ()) as ExportTypes
38- import Data.Lens.Types (Optic (), OpticP (), Fold ())
39- import Data.Lens.Types (IndexedOptic (), IndexedFold (), Indexed (..))
40-
41- infixl 8 previewOn as ^?
42- infixl 8 toListOfOn as ^..
43-
4436-- | Previews the first value of a fold, if there is any.
4537preview :: forall s t a b . Fold (First a ) s t a b -> s -> Maybe a
4638preview p = runFirst <<< foldMapOf p (First <<< Just )
@@ -49,6 +41,8 @@ preview p = runFirst <<< foldMapOf p (First <<< Just)
4941previewOn :: forall s t a b . s -> Fold (First a ) s t a b -> Maybe a
5042previewOn s p = preview p s
5143
44+ infixl 8 previewOn as ^?
45+
5246-- | Folds all foci of a `Fold` to one. Note that this is the same as `view`.
5347foldOf :: forall s t a b . Fold a s t a b -> s -> a
5448foldOf p = runForget (p (Forget id))
@@ -125,11 +119,22 @@ findOf p f = foldrOf p (\a -> maybe (if f a then Just a else Nothing) Just) Noth
125119
126120-- | Sequence the foci of a `Fold`, pulling out an `Applicative`, and ignore
127121-- | the result. If you need the result, see `sequenceOf` for `Traversal`s.
128- sequenceOf_ :: forall f s t a b . (Applicative f ) => Fold (Endo (f Unit )) s t (f a ) b -> s -> f Unit
122+ sequenceOf_
123+ :: forall f s t a b
124+ . Applicative f
125+ => Fold (Endo (f Unit )) s t (f a ) b
126+ -> s
127+ -> f Unit
129128sequenceOf_ p = flip runEndo (pure unit) <<< foldMapOf p \f -> Endo (f *> _)
130129
131130-- | Traverse the foci of a `Fold`, discarding the results.
132- traverseOf_ :: forall f s t a b r . (Applicative f ) => Fold (Endo (f Unit )) s t a b -> (a -> f r ) -> s -> f Unit
131+ traverseOf_
132+ :: forall f s t a b r
133+ . Applicative f
134+ => Fold (Endo (f Unit )) s t a b
135+ -> (a -> f r )
136+ -> s
137+ -> f Unit
133138traverseOf_ p f = foldrOf p (\a fu -> void (f a) *> fu) (pure unit)
134139
135140-- | Collects the foci of a `Fold` into a list.
@@ -140,17 +145,23 @@ toListOf p = foldrOf p (:) Nil
140145toListOfOn :: forall s t a b . s -> Fold (Endo (List a )) s t a b -> List a
141146toListOfOn s p = toListOf p s
142147
148+ infixl 8 toListOfOn as ^..
149+
143150-- | Determines whether a `Fold` has at least one focus.
144- has :: forall s t a b r . ( BooleanAlgebra r ) => Fold (Disj r ) s t a b -> s -> r
145- has p = runDisj <<< foldMapOf p (const (Disj top ))
151+ has :: 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 ))
146153
147154-- | Determines whether a `Fold` does not have a focus.
148- hasn't :: forall s t a b r . ( BooleanAlgebra r ) => Fold (Conj r ) s t a b -> s -> r
149- hasn't p = runConj <<< foldMapOf p (const (Conj bottom ))
155+ hasn'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 ))
150157
151158-- | Filters on a predicate.
152159filtered :: forall p a . (Choice p ) => (a -> Boolean ) -> OpticP p a a
153- filtered f = dimap (\x -> if f x then Right x else Left x) (either id id) <<< right
160+ filtered f =
161+ right >>>
162+ dimap
163+ (\x -> if f x then Right x else Left x)
164+ (either id id)
154165
155166-- | Replicates the elements of a fold.
156167replicated :: forall a b t r . (Monoid r ) => Int -> Fold r a b a t
@@ -163,40 +174,94 @@ folded :: forall g a b t r. (Monoid r, Foldable g) => Fold r (g a) b a t
163174folded = Forget <<< foldMap <<< runForget
164175
165176-- | Builds a `Fold` using an unfold.
166- unfolded :: forall r s t a b . (Monoid r ) => (s -> Maybe (Tuple a s )) -> Fold r s t a b
167- unfolded f p = Forget go where
177+ unfolded
178+ :: forall r s t a b
179+ . Monoid r
180+ => (s -> Maybe (Tuple a s ))
181+ -> Fold r s t a b
182+ unfolded f p = Forget go
183+ where
168184 go = maybe mempty (\(Tuple a sn) -> runForget p a <> go sn) <<< f
169185
170186-- | Fold map over an `IndexedFold`.
171- ifoldMapOf :: forall r i s t a b . IndexedFold r i s t a b -> (i -> a -> r ) -> s -> r
187+ ifoldMapOf
188+ :: forall r i s t a b
189+ . IndexedFold r i s t a b
190+ -> (i -> a -> r )
191+ -> s
192+ -> r
172193ifoldMapOf p f = runForget $ p $ Indexed $ Forget (uncurry f)
173194
174195-- | Right fold over an `IndexedFold`.
175- ifoldrOf :: forall i s t a b r . IndexedFold (Endo r ) i s t a b -> (i -> a -> r -> r ) -> r -> s -> r
196+ ifoldrOf
197+ :: forall i s t a b r
198+ . IndexedFold (Endo r ) i s t a b
199+ -> (i -> a -> r -> r )
200+ -> r
201+ -> s
202+ -> r
176203ifoldrOf p f r = flip runEndo r <<< ifoldMapOf p (\i -> Endo <<< f i)
177204
178205-- | Left fold over an `IndexedFold`.
179- ifoldlOf :: forall i s t a b r . IndexedFold (Dual (Endo r )) i s t a b -> (i -> r -> a -> r ) -> r -> s -> r
180- ifoldlOf p f r = flip runEndo r <<< runDual <<< ifoldMapOf p (\i -> Dual <<< Endo <<< flip (f i))
206+ ifoldlOf
207+ :: forall i s t a b r
208+ . IndexedFold (Dual (Endo r )) i s t a b
209+ -> (i -> r -> a -> r )
210+ -> r
211+ -> s
212+ -> r
213+ ifoldlOf p f r =
214+ flip runEndo r
215+ <<< runDual
216+ <<< ifoldMapOf p (\i -> Dual <<< Endo <<< flip (f i))
181217
182218-- | Whether all foci of an `IndexedFold` satisfy a predicate.
183- iallOf :: forall i s t a b r . (BooleanAlgebra r ) => IndexedFold (Conj r ) i s t a b -> (i -> a -> r ) -> s -> r
219+ iallOf
220+ :: forall i s t a b r
221+ . HeytingAlgebra r
222+ => IndexedFold (Conj r ) i s t a b
223+ -> (i -> a -> r )
224+ -> s
225+ -> r
184226iallOf p f = runConj <<< ifoldMapOf p (\i -> Conj <<< f i)
185227
186228-- | Whether any focus of an `IndexedFold` satisfies a predicate.
187- ianyOf :: forall i s t a b r . (BooleanAlgebra r ) => IndexedFold (Disj r ) i s t a b -> (i -> a -> r ) -> s -> r
229+ ianyOf
230+ :: forall i s t a b r
231+ . HeytingAlgebra r
232+ => IndexedFold (Disj r ) i s t a b
233+ -> (i -> a -> r )
234+ -> s
235+ -> r
188236ianyOf p f = runDisj <<< ifoldMapOf p (\i -> Disj <<< f i)
189237
190- -- | Find the first focus of an `IndexedFold` that satisfies a predicate, if there is any.
191- ifindOf :: forall i s t a b . IndexedFold (Endo (Maybe a )) i s t a b -> (i -> a -> Boolean ) -> s -> Maybe a
192- ifindOf p f = ifoldrOf p (\i a -> maybe (if f i a then Just a else Nothing ) Just ) Nothing
238+ -- | Find the first focus of an `IndexedFold` that satisfies a predicate, if
239+ -- | there is any.
240+ ifindOf
241+ :: forall i s t a b
242+ . IndexedFold (Endo (Maybe a )) i s t a b
243+ -> (i -> a -> Boolean )
244+ -> s
245+ -> Maybe a
246+ ifindOf p f =
247+ ifoldrOf
248+ p
249+ (\i a -> maybe (if f i a then Just a else Nothing ) Just )
250+ Nothing
193251
194252-- | Collects the foci of an `IndexedFold` into a list.
195- itoListOf :: forall i s t a b . IndexedFold (Endo (List (Tuple i a ))) i s t a b -> s -> List (Tuple i a )
253+ itoListOf
254+ :: forall i s t a b
255+ . IndexedFold (Endo (List (Tuple i a ))) i s t a b
256+ -> s
257+ -> List (Tuple i a )
196258itoListOf p = ifoldrOf p (\i x xs -> Tuple i x : xs) Nil
197259
198260-- | Traverse the foci of an `IndexedFold`, discarding the results.
199261itraverseOf_
200262 :: forall i f s t a b r . (Applicative f )
201- => IndexedFold (Endo (f Unit )) i s t a b -> (i -> a -> f r ) -> s -> f Unit
263+ => IndexedFold (Endo (f Unit )) i s t a b
264+ -> (i -> a -> f r )
265+ -> s
266+ -> f Unit
202267itraverseOf_ p f = ifoldrOf p (\i a fu -> void (f i a) *> fu) (pure unit)
0 commit comments