Skip to content

Commit 4a681d9

Browse files
committed
Merge pull request #40 from hdgarrood/document-scanl
Update documentation for scanl, scanr. fixes #23
2 parents 30706e0 + 51b2ecf commit 4a681d9

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

docs/Data/Traversable.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,13 @@ scanl :: forall a b f. (Traversable f) => (b -> a -> b) -> b -> f a -> f b
9494
```
9595

9696
Fold a data structure from the left, keeping all intermediate results
97-
instead of only the final result.
97+
instead of only the final result. Note that the initial value does not
98+
appear in the result (unlike Haskell's `Prelude.scanl`).
99+
100+
```purescript
101+
scanl (+) 0 [1,2,3] = [1,3,6]
102+
scanl (-) 10 [1,2,3] = [9,7,4]
103+
```
98104

99105
#### `mapAccumL`
100106

@@ -115,7 +121,13 @@ scanr :: forall a b f. (Traversable f) => (a -> b -> b) -> b -> f a -> f b
115121
```
116122

117123
Fold a data structure from the right, keeping all intermediate results
118-
instead of only the final result.
124+
instead of only the final result. Note that the initial value does not
125+
appear in the result (unlike Haskell's `Prelude.scanr`).
126+
127+
```purescript
128+
scanr (+) 0 [1,2,3] = [1,3,6]
129+
scanr (flip (-)) 10 [1,2,3] = [4,5,7]
130+
```
119131

120132
#### `mapAccumR`
121133

src/Data/Traversable.purs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,13 @@ instance applicativeStateL :: Applicative (StateL s) where
143143
pure a = StateL \s -> { accum: s, value: a }
144144

145145
-- | Fold a data structure from the left, keeping all intermediate results
146-
-- | instead of only the final result.
146+
-- | instead of only the final result. Note that the initial value does not
147+
-- | appear in the result (unlike Haskell's `Prelude.scanl`).
148+
-- |
149+
-- | ```purescript
150+
-- | scanl (+) 0 [1,2,3] = [1,3,6]
151+
-- | scanl (-) 10 [1,2,3] = [9,7,4]
152+
-- | ```
147153
scanl :: forall a b f. (Traversable f) => (b -> a -> b) -> b -> f a -> f b
148154
scanl f b0 xs = (mapAccumL (\b a -> let b' = f b a in { accum: b', value: b' }) b0 xs).value
149155

@@ -173,7 +179,13 @@ instance applicativeStateR :: Applicative (StateR s) where
173179
pure a = StateR \s -> { accum: s, value: a }
174180

175181
-- | Fold a data structure from the right, keeping all intermediate results
176-
-- | instead of only the final result.
182+
-- | instead of only the final result. Note that the initial value does not
183+
-- | appear in the result (unlike Haskell's `Prelude.scanr`).
184+
-- |
185+
-- | ```purescript
186+
-- | scanr (+) 0 [1,2,3] = [1,3,6]
187+
-- | scanr (flip (-)) 10 [1,2,3] = [4,5,7]
188+
-- | ```
177189
scanr :: forall a b f. (Traversable f) => (a -> b -> b) -> b -> f a -> f b
178190
scanr f b0 xs = (mapAccumR (\b a -> let b' = f a b in { accum: b', value: b' }) b0 xs).value
179191

0 commit comments

Comments
 (0)