1
1
-- | This module defines functions for working with folds.
2
-
3
2
module Data.Lens.Fold
4
3
( (^?), previewOn
5
4
, (^..), toListOfOn
@@ -13,10 +12,12 @@ module Data.Lens.Fold
13
12
14
13
import Prelude
15
14
16
- import Control.Apply ((*>))
17
-
18
15
import Data.Either (Either (..), either )
19
16
import 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 (..))
20
21
import Data.List (List (..), (:))
21
22
import Data.Maybe (Maybe (..), maybe )
22
23
import Data.Maybe.First (First (..), runFirst )
@@ -32,15 +33,6 @@ import Data.Profunctor (dimap)
32
33
import Data.Profunctor.Choice (class Choice , right )
33
34
import Data.Tuple (Tuple (..), uncurry )
34
35
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
-
44
36
-- | Previews the first value of a fold, if there is any.
45
37
preview :: forall s t a b . Fold (First a ) s t a b -> s -> Maybe a
46
38
preview p = runFirst <<< foldMapOf p (First <<< Just )
@@ -49,6 +41,8 @@ preview p = runFirst <<< foldMapOf p (First <<< Just)
49
41
previewOn :: forall s t a b . s -> Fold (First a ) s t a b -> Maybe a
50
42
previewOn s p = preview p s
51
43
44
+ infixl 8 previewOn as ^?
45
+
52
46
-- | Folds all foci of a `Fold` to one. Note that this is the same as `view`.
53
47
foldOf :: forall s t a b . Fold a s t a b -> s -> a
54
48
foldOf 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
125
119
126
120
-- | Sequence the foci of a `Fold`, pulling out an `Applicative`, and ignore
127
121
-- | 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
129
128
sequenceOf_ p = flip runEndo (pure unit) <<< foldMapOf p \f -> Endo (f *> _)
130
129
131
130
-- | 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
133
138
traverseOf_ p f = foldrOf p (\a fu -> void (f a) *> fu) (pure unit)
134
139
135
140
-- | Collects the foci of a `Fold` into a list.
@@ -140,17 +145,23 @@ toListOf p = foldrOf p (:) Nil
140
145
toListOfOn :: forall s t a b . s -> Fold (Endo (List a )) s t a b -> List a
141
146
toListOfOn s p = toListOf p s
142
147
148
+ infixl 8 toListOfOn as ^..
149
+
143
150
-- | 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 ))
146
153
147
154
-- | 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 ))
150
157
151
158
-- | Filters on a predicate.
152
159
filtered :: 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)
154
165
155
166
-- | Replicates the elements of a fold.
156
167
replicated :: 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
163
174
folded = Forget <<< foldMap <<< runForget
164
175
165
176
-- | 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
168
184
go = maybe mempty (\(Tuple a sn) -> runForget p a <> go sn) <<< f
169
185
170
186
-- | 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
172
193
ifoldMapOf p f = runForget $ p $ Indexed $ Forget (uncurry f)
173
194
174
195
-- | 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
176
203
ifoldrOf p f r = flip runEndo r <<< ifoldMapOf p (\i -> Endo <<< f i)
177
204
178
205
-- | 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))
181
217
182
218
-- | 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
184
226
iallOf p f = runConj <<< ifoldMapOf p (\i -> Conj <<< f i)
185
227
186
228
-- | 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
188
236
ianyOf p f = runDisj <<< ifoldMapOf p (\i -> Disj <<< f i)
189
237
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
193
251
194
252
-- | 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 )
196
258
itoListOf p = ifoldrOf p (\i x xs -> Tuple i x : xs) Nil
197
259
198
260
-- | Traverse the foci of an `IndexedFold`, discarding the results.
199
261
itraverseOf_
200
262
:: 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
202
267
itraverseOf_ p f = ifoldrOf p (\i a fu -> void (f i a) *> fu) (pure unit)
0 commit comments