diff --git a/core/src/main/scala-2.13+/cats/data/NonEmptyLazyList.scala b/core/src/main/scala-2.13+/cats/data/NonEmptyLazyList.scala index 846b960ef19..746d115f686 100644 --- a/core/src/main/scala-2.13+/cats/data/NonEmptyLazyList.scala +++ b/core/src/main/scala-2.13+/cats/data/NonEmptyLazyList.scala @@ -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) @@ -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) @@ -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]]. @@ -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) @@ -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` @@ -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 diff --git a/tests/shared/src/test/scala-2.13+/cats/tests/NonEmptyLazyListSuite.scala b/tests/shared/src/test/scala-2.13+/cats/tests/NonEmptyLazyListSuite.scala index a4cd1352e52..675f3c58e4f 100644 --- a/tests/shared/src/test/scala-2.13+/cats/tests/NonEmptyLazyListSuite.scala +++ b/tests/shared/src/test/scala-2.13+/cats/tests/NonEmptyLazyListSuite.scala @@ -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") {