Skip to content

Commit b2c8b5d

Browse files
committed
Merge pull request #5 from sharkdp/add-head-tail
Add head, tail
2 parents 6520f39 + b7474cd commit b2c8b5d

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

docs/Data/NonEmpty.md

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ nonEmptyList = 0 :| empty
2121

2222
##### Instances
2323
``` purescript
24-
instance showNonEmpty :: (Show a, Show (f a)) => Show (NonEmpty f a)
25-
instance eqNonEmpty :: (Eq a, Eq (f a)) => Eq (NonEmpty f a)
26-
instance ordNonEmpty :: (Ord a, Ord (f a)) => Ord (NonEmpty f a)
27-
instance functorNonEmpty :: (Functor f) => Functor (NonEmpty f)
28-
instance foldableNonEmpty :: (Foldable f) => Foldable (NonEmpty f)
29-
instance traversableNonEmpty :: (Traversable f) => Traversable (NonEmpty f)
24+
(Show a, Show (f a)) => Show (NonEmpty f a)
25+
(Eq a, Eq (f a)) => Eq (NonEmpty f a)
26+
(Ord a, Ord (f a)) => Ord (NonEmpty f a)
27+
(Functor f) => Functor (NonEmpty f)
28+
(Foldable f) => Foldable (NonEmpty f)
29+
(Traversable f) => Traversable (NonEmpty f)
3030
```
3131

3232
#### `singleton`
@@ -50,7 +50,7 @@ An infix synonym for `NonEmpty`.
5050
#### `foldl1`
5151

5252
``` purescript
53-
foldl1 :: forall f a s. (Foldable f) => (a -> a -> a) -> NonEmpty f a -> a
53+
foldl1 :: forall f a. (Foldable f) => (a -> a -> a) -> NonEmpty f a -> a
5454
```
5555

5656
Fold a non-empty structure, collecting results using a binary operation.
@@ -83,4 +83,20 @@ fromNonEmpty :: forall f a r. (a -> f a -> r) -> NonEmpty f a -> r
8383
oneOf :: forall f a. (Alternative f) => NonEmpty f a -> f a
8484
```
8585

86+
#### `head`
87+
88+
``` purescript
89+
head :: forall f a. NonEmpty f a -> a
90+
```
91+
92+
Get the 'first' element of a non-empty container.
93+
94+
#### `tail`
95+
96+
``` purescript
97+
tail :: forall f a. NonEmpty f a -> f a
98+
```
99+
100+
Get everything but the 'first' element of a non-empty container.
101+
86102

src/Data/NonEmpty.purs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ module Data.NonEmpty
1010
, fold1
1111
, fromNonEmpty
1212
, oneOf
13+
, head
14+
, tail
1315
) where
1416

1517
import Prelude
@@ -59,6 +61,14 @@ fromNonEmpty f (NonEmpty a fa) = a `f` fa
5961
oneOf :: forall f a. (Alternative f) => NonEmpty f a -> f a
6062
oneOf (NonEmpty a fa) = pure a <|> fa
6163

64+
-- | Get the 'first' element of a non-empty container.
65+
head :: forall f a. NonEmpty f a -> a
66+
head (NonEmpty x _) = x
67+
68+
-- | Get everything but the 'first' element of a non-empty container.
69+
tail :: forall f a. NonEmpty f a -> f a
70+
tail (NonEmpty _ xs) = xs
71+
6272
instance showNonEmpty :: (Show a, Show (f a)) => Show (NonEmpty f a) where
6373
show (NonEmpty a fa) = "(NonEmpty " ++ show a ++ " " ++ show fa ++ ")"
6474

0 commit comments

Comments
 (0)