Skip to content

Commit

Permalink
Make NonEmptyLazyList properly lazy
Browse files Browse the repository at this point in the history
Make `NonEmptyLazyList` properly lazy and remove unnecessary
laziness.
  • Loading branch information
NthPortal committed Sep 2, 2023
1 parent ba032a4 commit 60bb1fe
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
14 changes: 7 additions & 7 deletions core/src/main/scala-2.13+/cats/data/NonEmptyLazyList.scala
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ object NonEmptyLazyList extends NonEmptyLazyListInstances {
s.asInstanceOf[LazyList[A]]

def fromLazyList[A](as: LazyList[A]): Option[NonEmptyLazyList[A]] =
if (as.nonEmpty) Option(create(as)) else None
if (as.nonEmpty) Some(create(as)) else None

def fromLazyListUnsafe[A](ll: LazyList[A]): NonEmptyLazyList[A] =
if (ll.nonEmpty) create(ll)
Expand All @@ -69,7 +69,7 @@ object NonEmptyLazyList extends NonEmptyLazyListInstances {
create(ca :+ a)

def apply[A](a: => A, as: A*): NonEmptyLazyList[A] =
create(LazyList.concat(LazyList(a), LazyList.from(as)))
create(a #:: LazyList.from(as))

implicit def catsNonEmptyLazyListOps[A](value: NonEmptyLazyList[A]): NonEmptyLazyListOps[A] =
new NonEmptyLazyListOps(value)
Expand Down Expand Up @@ -110,7 +110,7 @@ class NonEmptyLazyListOps[A](private val value: NonEmptyLazyList[A])
* Returns a new NonEmptyLazyList consisting of `a` followed by this
*/
final def prepend[AA >: A](a: AA): NonEmptyLazyList[AA] =
create(a #:: toLazyList)
create(toLazyList.prepended(a))

/**
* Alias for [[prepend]].
Expand All @@ -121,6 +121,8 @@ class NonEmptyLazyListOps[A](private val value: NonEmptyLazyList[A])
/**
* Alias for [[prepend]].
*/
// TODO: `a` should be by-name and this method should not be listed as an
// alias for `prepend`, but it's too late to change that in this version
final def #::[AA >: A](a: AA): NonEmptyLazyList[AA] =
prepend(a)

Expand Down Expand Up @@ -158,8 +160,7 @@ class NonEmptyLazyListOps[A](private val value: NonEmptyLazyList[A])
* Appends the given LazyList
*/
final def appendLazyList[AA >: A](nell: LazyList[AA]): NonEmptyLazyList[AA] =
if (nell.isEmpty) value
else create(toLazyList ++ nell)
create(toLazyList ++ nell)

/**
* Alias for `appendLazyList`
Expand All @@ -171,8 +172,7 @@ class NonEmptyLazyListOps[A](private val value: NonEmptyLazyList[A])
* Prepends the given LazyList
*/
final def prependLazyList[AA >: A](c: LazyList[AA]): NonEmptyLazyList[AA] =
if (c.isEmpty) value
else create(c ++ toLazyList)
create(c ++ toLazyList)

/**
* Prepends the given NonEmptyLazyList
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,26 @@ class NonEmptyLazyListSuite extends NonEmptyCollectionSuite[LazyList, NonEmptyLa

(1 to 100).sum === sum
}

test("NonEmptyLazyList#appendLazyList is properly lazy") {
var evaluated = false
val ll = { evaluated = true; 1 } #:: LazyList.empty
val _ = NonEmptyLazyList(0).appendLazyList(ll)
assert(!evaluated)
}

test("NonEmptyLazyList#prependLazyList is properly lazy") {
var evaluated = false
val ll = { evaluated = true; 0 } #:: LazyList.empty
val _ = NonEmptyLazyList(1).prependLazyList(ll)
assert(!evaluated)
}

test("NonEmptyLazyList.apply is properly lazy") {
var evaluated = false
val _ = NonEmptyLazyList({ evaluated = true; 0 }, Nil: _*)
assert(!evaluated)
}
}

class ReducibleNonEmptyLazyListSuite extends ReducibleSuite[NonEmptyLazyList]("NonEmptyLazyList") {
Expand Down

0 comments on commit 60bb1fe

Please sign in to comment.