From 74f619ad694b124ea35ddaab13b11f38350f0919 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Ferreira?= Date: Thu, 20 Apr 2017 14:27:35 +0100 Subject: [PATCH 1/4] add init and size methods to NonEmptyList --- .../main/scala/cats/data/NonEmptyList.scala | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/core/src/main/scala/cats/data/NonEmptyList.scala b/core/src/main/scala/cats/data/NonEmptyList.scala index 4c03eb5a42..a3c3b13817 100644 --- a/core/src/main/scala/cats/data/NonEmptyList.scala +++ b/core/src/main/scala/cats/data/NonEmptyList.scala @@ -17,14 +17,56 @@ final case class NonEmptyList[+A](head: A, tail: List[A]) { /** * Return the head and tail into a single list + * {{{ + * scala> import cats.data.NonEmptyList + * scala> val nel = NonEmptyList.of(1, 2, 3, 4, 5) + * scala> nel.toList + * res0: scala.collection.immutable.List[Int] = List(1, 2, 3, 4, 5) + * }}} */ def toList: List[A] = head :: tail + /** + * Selects the last element + * {{{ + * scala> import cats.data.NonEmptyList + * scala> val nel = NonEmptyList.of(1, 2, 3, 4, 5) + * scala> nel.last + * res0: Int = 5 + * }}} + */ def last: A = tail.lastOption match { case None => head case Some(a) => a } + /** + * Selects all elements except the last + * + * {{{ + * scala> import cats.data.NonEmptyList + * scala> val nel = NonEmptyList.of(1, 2, 3, 4, 5) + * scala> nel.init + * res0: scala.collection.immutable.List[Int] = List(1, 2, 3, 4) + * }}} + */ + def init: List[A] = tail match { + case Nil => List.empty + case t => head :: t.init + } + + /** + * The size of this NonEmptyList + * + * {{{ + * scala> import cats.data.NonEmptyList + * scala> val nel = NonEmptyList.of(1, 2, 3, 4, 5) + * scala> nel.size + * res0: Int = 5 + * }}} + */ + def size: Int = 1 + tail.size + /** * Applies f to all the elements of the structure */ From fac3c49c1cb134eef75df94095f4d95cd076a851 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Ferreira?= Date: Mon, 24 Apr 2017 10:31:30 +0100 Subject: [PATCH 2/4] add tests --- .../test/scala/cats/tests/NonEmptyListTests.scala | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/src/test/scala/cats/tests/NonEmptyListTests.scala b/tests/src/test/scala/cats/tests/NonEmptyListTests.scala index a1d501314e..7773f9d354 100644 --- a/tests/src/test/scala/cats/tests/NonEmptyListTests.scala +++ b/tests/src/test/scala/cats/tests/NonEmptyListTests.scala @@ -238,6 +238,18 @@ class NonEmptyListTests extends CatsSuite { } } + test("NonEmptyList#init is consistent with List#init") { + forAll { nel: NonEmptyList[Int] => + nel.init should === (nel.toList.init) + } + } + + test("NonEmptyList#size is consistent with List#size") { + forAll { nel: NonEmptyList[Int] => + nel.size should === (nel.toList.size) + } + } + test("NonEmptyList#sorted is consistent with List#sorted") { forAll { nel: NonEmptyList[Int] => nel.sorted.toList should === (nel.toList.sorted) From b14530c5408aa6fec99ed8818207f5f9db40e122 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Ferreira?= Date: Mon, 24 Apr 2017 17:41:18 +0100 Subject: [PATCH 3/4] change NonEmptyList.last implementation --- core/src/main/scala/cats/data/NonEmptyList.scala | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/core/src/main/scala/cats/data/NonEmptyList.scala b/core/src/main/scala/cats/data/NonEmptyList.scala index a3c3b13817..5c064bd961 100644 --- a/core/src/main/scala/cats/data/NonEmptyList.scala +++ b/core/src/main/scala/cats/data/NonEmptyList.scala @@ -35,10 +35,7 @@ final case class NonEmptyList[+A](head: A, tail: List[A]) { * res0: Int = 5 * }}} */ - def last: A = tail.lastOption match { - case None => head - case Some(a) => a - } + def last: A = tail.lastOption.getOrElse(head) /** * Selects all elements except the last From 63f74f1bf95a307ce98e7faf3af08e2986c9568a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Ferreira?= Date: Tue, 25 Apr 2017 17:49:28 +0100 Subject: [PATCH 4/4] Revert "change NonEmptyList.last implementation" This reverts commit 5745f5b3c9ad0ebd1ca8accae2d740fdf515e7de. --- core/src/main/scala/cats/data/NonEmptyList.scala | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/core/src/main/scala/cats/data/NonEmptyList.scala b/core/src/main/scala/cats/data/NonEmptyList.scala index 5c064bd961..a3c3b13817 100644 --- a/core/src/main/scala/cats/data/NonEmptyList.scala +++ b/core/src/main/scala/cats/data/NonEmptyList.scala @@ -35,7 +35,10 @@ final case class NonEmptyList[+A](head: A, tail: List[A]) { * res0: Int = 5 * }}} */ - def last: A = tail.lastOption.getOrElse(head) + def last: A = tail.lastOption match { + case None => head + case Some(a) => a + } /** * Selects all elements except the last