@@ -11,6 +11,11 @@ class Foldable f where
1111 foldMap :: forall a m. (Monoid m) => (a -> m) -> f a -> m
1212```
1313
14+ ` Foldable ` represents data structures which can be _ folded_ .
15+
16+ - ` foldr ` folds a structure from the right
17+ - ` foldl ` folds a structure from the left
18+ - ` foldMap ` folds a structure by accumulating values in a ` Monoid `
1419
1520#### ` foldableArray `
1621
@@ -81,111 +86,154 @@ instance foldableMultiplicative :: Foldable Multiplicative
8186fold :: forall f m. (Foldable f, Monoid m) => f m -> m
8287```
8388
89+ Fold a data structure, accumulating values in some ` Monoid ` .
8490
8591#### ` traverse_ `
8692
8793``` purescript
8894traverse_ :: forall a b f m. (Applicative m, Foldable f) => (a -> m b) -> f a -> m Unit
8995```
9096
97+ Traverse a data structure, performing some effects encoded by an
98+ ` Applicative ` functor at each value, ignoring the final result.
99+
100+ For example:
101+
102+ ``` purescript
103+ traverse_ print [1, 2, 3]
104+ ```
91105
92106#### ` for_ `
93107
94108``` purescript
95109for_ :: forall a b f m. (Applicative m, Foldable f) => f a -> (a -> m b) -> m Unit
96110```
97111
112+ A version of ` traverse_ ` with its arguments flipped.
113+
114+ This can be useful when running an action written using do notation
115+ for every element in a data structure:
116+
117+ For example:
118+
119+ ``` purescript
120+ for_ [1, 2, 3] \n -> do
121+ print n
122+ trace "squared is"
123+ print (n * n)
124+ ```
98125
99126#### ` sequence_ `
100127
101128``` purescript
102129sequence_ :: forall a f m. (Applicative m, Foldable f) => f (m a) -> m Unit
103130```
104131
132+ Perform all of the effects in some data structure in the order
133+ given by the ` Foldable ` instance, ignoring the final result.
134+
135+ For example:
136+
137+ ``` purescript
138+ sequence_ [ trace "Hello, ", trace " world!" ]
139+ ```
105140
106141#### ` mconcat `
107142
108143``` purescript
109144mconcat :: forall f m. (Foldable f, Monoid m) => f m -> m
110145```
111146
147+ Fold a data structure, accumulating values in some ` Monoid ` .
112148
113149#### ` intercalate `
114150
115151``` purescript
116152intercalate :: forall f m. (Foldable f, Monoid m) => m -> f m -> m
117153```
118154
155+ Fold a data structure, accumulating values in some ` Monoid ` ,
156+ combining adjacent elements using the specified separator.
119157
120158#### ` and `
121159
122160``` purescript
123161and :: forall f. (Foldable f) => f Boolean -> Boolean
124162```
125163
164+ Test whether all ` Boolean ` values in a data structure are ` true ` .
126165
127166#### ` or `
128167
129168``` purescript
130169or :: forall f. (Foldable f) => f Boolean -> Boolean
131170```
132171
172+ Test whether any ` Boolean ` value in a data structure is ` true ` .
133173
134174#### ` any `
135175
136176``` purescript
137177any :: forall a f. (Foldable f) => (a -> Boolean) -> f a -> Boolean
138178```
139179
180+ Test whether a predicate holds for any element in a data structure.
140181
141182#### ` all `
142183
143184``` purescript
144185all :: forall a f. (Foldable f) => (a -> Boolean) -> f a -> Boolean
145186```
146187
188+ Test whether a predicate holds for all elements in a data structure.
147189
148190#### ` sum `
149191
150192``` purescript
151193sum :: forall f. (Foldable f) => f Number -> Number
152194```
153195
196+ Find the sum of the numeric values in a data structure.
154197
155198#### ` product `
156199
157200``` purescript
158201product :: forall f. (Foldable f) => f Number -> Number
159202```
160203
204+ Find the product of the numeric values in a data structure.
161205
162206#### ` elem `
163207
164208``` purescript
165209elem :: forall a f. (Eq a, Foldable f) => a -> f a -> Boolean
166210```
167211
212+ Test whether a value is an element of a data structure.
168213
169214#### ` notElem `
170215
171216``` purescript
172217notElem :: forall a f. (Eq a, Foldable f) => a -> f a -> Boolean
173218```
174219
220+ Test whether a value is not an element of a data structure.
175221
176222#### ` find `
177223
178224``` purescript
179225find :: forall a f. (Foldable f) => (a -> Boolean) -> f a -> Maybe a
180226```
181227
228+ Try to find an element in a data structure which satisfies a predicate.
182229
183230#### ` lookup `
184231
185232``` purescript
186233lookup :: forall a b f. (Eq a, Foldable f) => a -> f (Tuple a b) -> Maybe b
187234```
188235
236+ Lookup a value in a data structure of ` Tuple ` s, generalizing association lists.
189237
190238#### ` foldrArray `
191239
@@ -212,6 +260,24 @@ class (Functor t, Foldable t) <= Traversable t where
212260 sequence :: forall a m. (Applicative m) => t (m a) -> m (t a)
213261```
214262
263+ ` Traversable ` represents data structures which can be _ traversed_ ,
264+ accumulating results and effects in some ` Applicative ` functor.
265+
266+ - ` traverse ` runs an action for every element in a data structure,
267+ and accumulates the results.
268+ - ` sequence ` runs the actions _ contained_ in a data structure,
269+ and accumulates the results.
270+
271+ The ` traverse ` and ` sequence ` functions should be compatible in the
272+ following sense:
273+
274+ - ` traverse f xs = sequence (f <$> xs) `
275+ - ` sequence = traverse id `
276+
277+ ` Traversable ` instances should also be compatible with the corresponding
278+ ` Foldable ` instances, in the following sense:
279+
280+ - ` foldMap f = runConst <<< traverse (Const <<< f) `
215281
216282#### ` traversableArray `
217283
@@ -282,13 +348,28 @@ instance traversableMultiplicative :: Traversable Multiplicative
282348for :: forall a b m t. (Applicative m, Traversable t) => t a -> (a -> m b) -> m (t b)
283349```
284350
351+ A version of ` traverse ` with its arguments flipped.
352+
353+
354+ This can be useful when running an action written using do notation
355+ for every element in a data structure:
356+
357+ For example:
358+
359+ ``` purescript
360+ for [1, 2, 3] \n -> do
361+ print n
362+ return (n * n)
363+ ```
285364
286365#### ` zipWithA `
287366
288367``` purescript
289368zipWithA :: forall m a b c. (Applicative m) => (a -> b -> m c) -> [a] -> [b] -> m [c]
290369```
291370
371+ A generalization of ` zipWith ` which accumulates results in some ` Applicative `
372+ functor.
292373
293374#### ` functorStateL `
294375
@@ -317,13 +398,20 @@ instance applicativeStateL :: Applicative (StateL s)
317398scanl :: forall a b f. (Traversable f) => (b -> a -> b) -> b -> f a -> f b
318399```
319400
401+ Fold a data structure from the left, keeping all intermediate results
402+ instead of only the final result.
320403
321404#### ` mapAccumL `
322405
323406``` purescript
324407mapAccumL :: forall a b s f. (Traversable f) => (s -> a -> Tuple s b) -> s -> f a -> Tuple s (f b)
325408```
326409
410+ Fold a data structure from the left, keeping all intermediate results
411+ instead of only the final result.
412+
413+ Unlike ` scanl ` , ` mapAccumL ` allows the type of accumulator to differ
414+ from the element type of the final data structure.
327415
328416#### ` functorStateR `
329417
@@ -352,9 +440,17 @@ instance applicativeStateR :: Applicative (StateR s)
352440scanr :: forall a b f. (Traversable f) => (a -> b -> b) -> b -> f a -> f b
353441```
354442
443+ Fold a data structure from the right, keeping all intermediate results
444+ instead of only the final result.
355445
356446#### ` mapAccumR `
357447
358448``` purescript
359449mapAccumR :: forall a b s f. (Traversable f) => (s -> a -> Tuple s b) -> s -> f a -> Tuple s (f b)
360- ```
450+ ```
451+
452+ Fold a data structure from the right, keeping all intermediate results
453+ instead of only the final result.
454+
455+ Unlike ` scanr ` , ` mapAccumR ` allows the type of accumulator to differ
456+ from the element type of the final data structure.
0 commit comments