Skip to content

Commit 724ac5f

Browse files
committed
Docs
1 parent 718d822 commit 724ac5f

File tree

3 files changed

+192
-1
lines changed

3 files changed

+192
-1
lines changed

README.md

Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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
8186
fold :: 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
8894
traverse_ :: 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
95109
for_ :: 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
102129
sequence_ :: 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
109144
mconcat :: 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
116152
intercalate :: 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
123161
and :: 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
130169
or :: 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
137177
any :: 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
144185
all :: 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
151193
sum :: 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
158201
product :: 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
165209
elem :: 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
172217
notElem :: 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
179225
find :: 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
186233
lookup :: 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
282348
for :: 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
289368
zipWithA :: 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)
317398
scanl :: 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
324407
mapAccumL :: 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)
352440
scanr :: 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
359449
mapAccumR :: 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.

src/Data/Foldable.purs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ import Data.Monoid.Last
1111
import Data.Monoid.Multiplicative
1212
import Data.Tuple
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
class Foldable f where
1520
foldr :: forall a b. (a -> b -> b) -> b -> f a -> b
1621
foldl :: forall a b. (b -> a -> b) -> b -> f a -> b
@@ -67,56 +72,99 @@ instance foldableMultiplicative :: Foldable Multiplicative where
6772
foldl f z (Multiplicative x) = z `f` x
6873
foldMap f (Multiplicative x) = f x
6974

75+
-- | Fold a data structure, accumulating values in some `Monoid`.
7076
fold :: forall f m. (Foldable f, Monoid m) => f m -> m
7177
fold = foldMap id
7278

79+
-- | Traverse a data structure, performing some effects encoded by an
80+
-- | `Applicative` functor at each value, ignoring the final result.
81+
-- |
82+
-- | For example:
83+
-- |
84+
-- | ```purescript
85+
-- | traverse_ print [1, 2, 3]
86+
-- | ```
7387
traverse_ :: forall a b f m. (Applicative m, Foldable f) => (a -> m b) -> f a -> m Unit
7488
traverse_ f = foldr ((*>) <<< f) (pure unit)
7589

90+
-- | A version of `traverse_` with its arguments flipped.
91+
-- |
92+
-- | This can be useful when running an action written using do notation
93+
-- | for every element in a data structure:
94+
-- |
95+
-- | For example:
96+
-- |
97+
-- | ```purescript
98+
-- | for_ [1, 2, 3] \n -> do
99+
-- | print n
100+
-- | trace "squared is"
101+
-- | print (n * n)
102+
-- | ```
76103
for_ :: forall a b f m. (Applicative m, Foldable f) => f a -> (a -> m b) -> m Unit
77104
for_ = flip traverse_
78105

106+
-- | Perform all of the effects in some data structure in the order
107+
-- | given by the `Foldable` instance, ignoring the final result.
108+
-- |
109+
-- | For example:
110+
-- |
111+
-- | ```purescript
112+
-- | sequence_ [ trace "Hello, ", trace " world!" ]
113+
-- | ```
79114
sequence_ :: forall a f m. (Applicative m, Foldable f) => f (m a) -> m Unit
80115
sequence_ = traverse_ id
81116

117+
-- | Fold a data structure, accumulating values in some `Monoid`.
82118
mconcat :: forall f m. (Foldable f, Monoid m) => f m -> m
83119
mconcat = foldl (<>) mempty
84120

121+
-- | Fold a data structure, accumulating values in some `Monoid`,
122+
-- | combining adjacent elements using the specified separator.
85123
intercalate :: forall f m. (Foldable f, Monoid m) => m -> f m -> m
86124
intercalate sep xs = (foldl go { init: true, acc: mempty } xs).acc
87125
where
88126
go { init = true } x = { init: false, acc: x }
89127
go { acc = acc } x = { init: false, acc: acc <> sep <> x }
90128

129+
-- | Test whether all `Boolean` values in a data structure are `true`.
91130
and :: forall f. (Foldable f) => f Boolean -> Boolean
92131
and = foldl (&&) true
93132

133+
-- | Test whether any `Boolean` value in a data structure is `true`.
94134
or :: forall f. (Foldable f) => f Boolean -> Boolean
95135
or = foldl (||) false
96136

137+
-- | Test whether a predicate holds for any element in a data structure.
97138
any :: forall a f. (Foldable f) => (a -> Boolean) -> f a -> Boolean
98139
any p = or <<< foldMap (\x -> [p x])
99140

141+
-- | Test whether a predicate holds for all elements in a data structure.
100142
all :: forall a f. (Foldable f) => (a -> Boolean) -> f a -> Boolean
101143
all p = and <<< foldMap (\x -> [p x])
102144

145+
-- | Find the sum of the numeric values in a data structure.
103146
sum :: forall f. (Foldable f) => f Number -> Number
104147
sum = foldl (+) 0
105148

149+
-- | Find the product of the numeric values in a data structure.
106150
product :: forall f. (Foldable f) => f Number -> Number
107151
product = foldl (*) 1
108152

153+
-- | Test whether a value is an element of a data structure.
109154
elem :: forall a f. (Eq a, Foldable f) => a -> f a -> Boolean
110155
elem = any <<< (==)
111156

157+
-- | Test whether a value is not an element of a data structure.
112158
notElem :: forall a f. (Eq a, Foldable f) => a -> f a -> Boolean
113159
notElem x = not <<< elem x
114160

161+
-- | Try to find an element in a data structure which satisfies a predicate.
115162
find :: forall a f. (Foldable f) => (a -> Boolean) -> f a -> Maybe a
116163
find p f = case foldMap (\x -> if p x then [x] else []) f of
117164
(x:_) -> Just x
118165
[] -> Nothing
119166

167+
-- | Lookup a value in a data structure of `Tuple`s, generalizing association lists.
120168
lookup :: forall a b f. (Eq a, Foldable f) => a -> f (Tuple a b) -> Maybe b
121169
lookup a f = runFirst $ foldMap (\(Tuple a' b) -> First (if a == a' then Just b else Nothing)) f
122170

0 commit comments

Comments
 (0)