Skip to content

Commit 958042b

Browse files
authored
Merge pull request #125 from natefaubion/nonempty-cons
Add cons, snoc, unsnoc
2 parents 550a4c4 + f70da41 commit 958042b

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/Data/List/NonEmpty.purs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@ module Data.List.NonEmpty
55
, fromList
66
, toList
77
, singleton
8+
, cons
9+
, snoc
810
, head
911
, last
1012
, tail
1113
, init
1214
, uncons
15+
, unsnoc
1316
, length
1417
, concatMap
1518
, appendFoldable
@@ -47,6 +50,12 @@ toList (NonEmptyList (x :| xs)) = x : xs
4750
singleton :: forall a. a -> NonEmptyList a
4851
singleton = NonEmptyList <<< NE.singleton
4952

53+
cons :: forall a. a -> NonEmptyList a -> NonEmptyList a
54+
cons y (NonEmptyList (x :| xs)) = NonEmptyList (y :| x : xs)
55+
56+
snoc :: forall a. NonEmptyList a -> a -> NonEmptyList a
57+
snoc (NonEmptyList (x :| xs)) y = NonEmptyList (x :| L.snoc xs y)
58+
5059
head :: forall a. NonEmptyList a -> a
5160
head (NonEmptyList (x :| _)) = x
5261

@@ -62,6 +71,11 @@ init (NonEmptyList (x :| xs)) = maybe L.Nil (x : _) (L.init xs)
6271
uncons :: forall a. NonEmptyList a -> { head :: a, tail :: L.List a }
6372
uncons (NonEmptyList (x :| xs)) = { head: x, tail: xs }
6473

74+
unsnoc :: forall a. NonEmptyList a -> { init :: L.List a, last :: a }
75+
unsnoc (NonEmptyList (x :| xs)) = case L.unsnoc xs of
76+
Nothing -> { init: L.Nil, last: x }
77+
Just un -> { init: x : un.init, last: un.last }
78+
6579
length :: forall a. NonEmptyList a -> Int
6680
length (NonEmptyList (x :| xs)) = 1 + L.length xs
6781

0 commit comments

Comments
 (0)