Skip to content

Commit

Permalink
Lazy.NonEmptyList cons (#143)
Browse files Browse the repository at this point in the history
* Lazy.NonEmptyList cons

* Update changelog

Co-authored-by: JordanMartinez <[email protected]>
Co-authored-by: Jordan Martinez <[email protected]>
  • Loading branch information
3 people authored Sep 27, 2021
1 parent 6383c4f commit 68bce83
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Notable changes to this project are documented in this file. The format is based
Breaking changes:

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

Bugfixes:

Expand Down
5 changes: 5 additions & 0 deletions src/Data/List/Lazy/NonEmpty.purs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module Data.List.Lazy.NonEmpty
, last
, tail
, init
, cons
, uncons
, length
, concatMap
Expand Down Expand Up @@ -69,6 +70,10 @@ init (NonEmptyList nel) =
of x :| xs ->
maybe L.nil (x : _) (L.init xs)

cons :: forall a. a -> NonEmptyList a -> NonEmptyList a
cons y (NonEmptyList nel) =
NonEmptyList (defer \_ -> case force nel of x :| xs -> y :| x : xs)

uncons :: forall a. NonEmptyList a -> { head :: a, tail :: L.List a }
uncons (NonEmptyList nel) = case force nel of x :| xs -> { head: x, tail: xs }

Expand Down
3 changes: 3 additions & 0 deletions test/Test/Data/List/Lazy.purs
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,9 @@ testListLazy = do
log "transpose (singleton nil) == nil"
assert $ transpose (singleton nil) == (nil :: List (List Int))

log "NonEmptyList cons should add an element"
assert $ NEL.toList (NEL.cons 0 (NEL.singleton 1)) == fromFoldable [0, 1]

log "unfoldr should maintain order"
assert $ (1..5) == unfoldr step 1

Expand Down

0 comments on commit 68bce83

Please sign in to comment.