Skip to content

Commit a739b20

Browse files
committed
Merge pull request #45 from purescript/uncons-example
Add example to uncons documentation
2 parents d4f1fcf + 5f81986 commit a739b20

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

docs/Data/Array.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,20 @@ Running time: `O(n)` where `n` is the length of the array
170170
uncons :: forall a. Array a -> Maybe { head :: a, tail :: Array a }
171171
```
172172

173-
Break an array into its first element, and the remaining elements
173+
Break an array into its first element and remaining elements.
174+
175+
Using `uncons` provides a way of writing code that would use cons patterns
176+
in Haskell or pre-PureScript 0.7:
177+
``` purescript
178+
f (x : xs) = something
179+
f [] = somethingElse
180+
```
181+
Becomes:
182+
``` purescript
183+
f arr = case uncons arr of
184+
Just { head: x, tail: xs } -> something
185+
Nothing -> somethingElse
186+
```
174187

175188
#### `index`
176189

src/Data/Array.purs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,20 @@ init :: forall a. Array a -> Maybe (Array a)
210210
init xs | null xs = Nothing
211211
| otherwise = Just (slice zero (length xs - one) xs)
212212

213-
-- | Break an array into its first element, and the remaining elements
213+
-- | Break an array into its first element and remaining elements.
214+
-- |
215+
-- | Using `uncons` provides a way of writing code that would use cons patterns
216+
-- | in Haskell or pre-PureScript 0.7:
217+
-- | ``` purescript
218+
-- | f (x : xs) = something
219+
-- | f [] = somethingElse
220+
-- | ```
221+
-- | Becomes:
222+
-- | ``` purescript
223+
-- | f arr = case uncons arr of
224+
-- | Just { head: x, tail: xs } -> something
225+
-- | Nothing -> somethingElse
226+
-- | ```
214227
uncons :: forall a. Array a -> Maybe { head :: a, tail :: Array a }
215228
uncons = uncons' (const Nothing) \x xs -> Just { head: x, tail: xs }
216229

0 commit comments

Comments
 (0)