From e92c338a26abb60f797162b9fe73c718b9ed349f Mon Sep 17 00:00:00 2001 From: Christopher Davenport Date: Tue, 13 Nov 2018 17:21:36 -0500 Subject: [PATCH 1/6] Expose headOption for Chain --- core/src/main/scala/cats/data/Chain.scala | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/core/src/main/scala/cats/data/Chain.scala b/core/src/main/scala/cats/data/Chain.scala index 3131e12066..5a86a7231e 100644 --- a/core/src/main/scala/cats/data/Chain.scala +++ b/core/src/main/scala/cats/data/Chain.scala @@ -45,6 +45,11 @@ sealed abstract class Chain[+A] { // scalastyle:on null result } + + /** + * Returns the head of this Chain if non empty, none otherwise. Amortized O(1). + */ + def headOption: Option[A] = uncons.map(_._1) /** * Returns true if there are no elements in this collection. From 597de6a0c3783461e8602f700ffdbf5e81e99867 Mon Sep 17 00:00:00 2001 From: Christopher Davenport Date: Tue, 13 Nov 2018 19:32:20 -0500 Subject: [PATCH 2/6] Address Whitespace In Line --- core/src/main/scala/cats/data/Chain.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/main/scala/cats/data/Chain.scala b/core/src/main/scala/cats/data/Chain.scala index 5a86a7231e..ba94485b8a 100644 --- a/core/src/main/scala/cats/data/Chain.scala +++ b/core/src/main/scala/cats/data/Chain.scala @@ -45,7 +45,7 @@ sealed abstract class Chain[+A] { // scalastyle:on null result } - + /** * Returns the head of this Chain if non empty, none otherwise. Amortized O(1). */ From f84eb05dcb0af0a94e7cecd91101716efb6e3f95 Mon Sep 17 00:00:00 2001 From: Christopher Davenport Date: Tue, 13 Nov 2018 22:02:14 -0500 Subject: [PATCH 3/6] Test headOption behavior --- tests/src/test/scala/cats/tests/ChainSuite.scala | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/src/test/scala/cats/tests/ChainSuite.scala b/tests/src/test/scala/cats/tests/ChainSuite.scala index ae58c3618b..082aedd4ce 100644 --- a/tests/src/test/scala/cats/tests/ChainSuite.scala +++ b/tests/src/test/scala/cats/tests/ChainSuite.scala @@ -56,6 +56,12 @@ class ChainSuite extends CatsSuite { } } + test("headOption"){ + forAll { (ci: Chain[Int], i: Int) => + (i +: ci).headOption should ===(i.some) + } + } + test("size is consistent with toList.size") { forAll { (ci: Chain[Int]) => ci.size.toInt should ===(ci.toList.size) From a66a97bc53991360c7e79c6f364d077ea11b2450 Mon Sep 17 00:00:00 2001 From: Christopher Davenport Date: Wed, 14 Nov 2018 08:03:38 -0500 Subject: [PATCH 4/6] Scalafmt Chain Tests --- tests/src/test/scala/cats/tests/ChainSuite.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/src/test/scala/cats/tests/ChainSuite.scala b/tests/src/test/scala/cats/tests/ChainSuite.scala index 082aedd4ce..04c44b72cc 100644 --- a/tests/src/test/scala/cats/tests/ChainSuite.scala +++ b/tests/src/test/scala/cats/tests/ChainSuite.scala @@ -56,7 +56,7 @@ class ChainSuite extends CatsSuite { } } - test("headOption"){ + test("headOption") { forAll { (ci: Chain[Int], i: Int) => (i +: ci).headOption should ===(i.some) } From 0fa564731f4144a4a3d4ff83d854c2d401036f0a Mon Sep 17 00:00:00 2001 From: Chris Davenport Date: Wed, 14 Nov 2018 10:57:23 -0500 Subject: [PATCH 5/6] Incorporate Better Property Test --- tests/src/test/scala/cats/tests/ChainSuite.scala | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/src/test/scala/cats/tests/ChainSuite.scala b/tests/src/test/scala/cats/tests/ChainSuite.scala index 04c44b72cc..afae14c2ff 100644 --- a/tests/src/test/scala/cats/tests/ChainSuite.scala +++ b/tests/src/test/scala/cats/tests/ChainSuite.scala @@ -57,8 +57,8 @@ class ChainSuite extends CatsSuite { } test("headOption") { - forAll { (ci: Chain[Int], i: Int) => - (i +: ci).headOption should ===(i.some) + forAll { (s: Seq[Int]) => + Chain.fromSeq(s).headOption should ===(s.headOption) } } From af0a6afa558174761103d5b4554ee122e3b2a00b Mon Sep 17 00:00:00 2001 From: Chris Davenport Date: Wed, 14 Nov 2018 10:59:23 -0500 Subject: [PATCH 6/6] Scalafmt again --- tests/src/test/scala/cats/tests/ChainSuite.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/src/test/scala/cats/tests/ChainSuite.scala b/tests/src/test/scala/cats/tests/ChainSuite.scala index afae14c2ff..ca7f50970f 100644 --- a/tests/src/test/scala/cats/tests/ChainSuite.scala +++ b/tests/src/test/scala/cats/tests/ChainSuite.scala @@ -57,7 +57,7 @@ class ChainSuite extends CatsSuite { } test("headOption") { - forAll { (s: Seq[Int]) => + forAll { (s: Seq[Int]) => Chain.fromSeq(s).headOption should ===(s.headOption) } }