From 68bce83550247f1bcaf2c160b01e6740f1f5d471 Mon Sep 17 00:00:00 2001 From: Matthew Leon Date: Mon, 27 Sep 2021 11:08:08 -0400 Subject: [PATCH] Lazy.NonEmptyList cons (#143) * Lazy.NonEmptyList cons * Update changelog Co-authored-by: JordanMartinez Co-authored-by: Jordan Martinez --- CHANGELOG.md | 1 + src/Data/List/Lazy/NonEmpty.purs | 5 +++++ test/Test/Data/List/Lazy.purs | 3 +++ 3 files changed, 9 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 553e776..203f0dd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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: diff --git a/src/Data/List/Lazy/NonEmpty.purs b/src/Data/List/Lazy/NonEmpty.purs index 20ef04a..d455b1e 100644 --- a/src/Data/List/Lazy/NonEmpty.purs +++ b/src/Data/List/Lazy/NonEmpty.purs @@ -11,6 +11,7 @@ module Data.List.Lazy.NonEmpty , last , tail , init + , cons , uncons , length , concatMap @@ -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 } diff --git a/test/Test/Data/List/Lazy.purs b/test/Test/Data/List/Lazy.purs index 5eef700..c1c2978 100644 --- a/test/Test/Data/List/Lazy.purs +++ b/test/Test/Data/List/Lazy.purs @@ -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