Skip to content

Commit 68bce83

Browse files
Lazy.NonEmptyList cons (#143)
* Lazy.NonEmptyList cons * Update changelog Co-authored-by: JordanMartinez <[email protected]> Co-authored-by: Jordan Martinez <[email protected]>
1 parent 6383c4f commit 68bce83

File tree

3 files changed

+9
-0
lines changed

3 files changed

+9
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Notable changes to this project are documented in this file. The format is based
77
Breaking changes:
88

99
New features:
10+
- Added `cons` for `Lazy.NonEmptyList` (#143 by @matthewleon)
1011

1112
Bugfixes:
1213

src/Data/List/Lazy/NonEmpty.purs

+5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module Data.List.Lazy.NonEmpty
1111
, last
1212
, tail
1313
, init
14+
, cons
1415
, uncons
1516
, length
1617
, concatMap
@@ -69,6 +70,10 @@ init (NonEmptyList nel) =
6970
of x :| xs ->
7071
maybe L.nil (x : _) (L.init xs)
7172

73+
cons :: forall a. a -> NonEmptyList a -> NonEmptyList a
74+
cons y (NonEmptyList nel) =
75+
NonEmptyList (defer \_ -> case force nel of x :| xs -> y :| x : xs)
76+
7277
uncons :: forall a. NonEmptyList a -> { head :: a, tail :: L.List a }
7378
uncons (NonEmptyList nel) = case force nel of x :| xs -> { head: x, tail: xs }
7479

test/Test/Data/List/Lazy.purs

+3
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,9 @@ testListLazy = do
441441
log "transpose (singleton nil) == nil"
442442
assert $ transpose (singleton nil) == (nil :: List (List Int))
443443

444+
log "NonEmptyList cons should add an element"
445+
assert $ NEL.toList (NEL.cons 0 (NEL.singleton 1)) == fromFoldable [0, 1]
446+
444447
log "unfoldr should maintain order"
445448
assert $ (1..5) == unfoldr step 1
446449

0 commit comments

Comments
 (0)