Skip to content

Commit

Permalink
Expose headOption for Chain (#2610)
Browse files Browse the repository at this point in the history
* Expose headOption for Chain

* Address Whitespace In Line

* Test headOption behavior

* Scalafmt Chain Tests

* Incorporate Better Property Test

* Scalafmt again
  • Loading branch information
ChristopherDavenport authored and Luka Jacobowitz committed Nov 15, 2018
1 parent bdb460c commit 9e6ecc3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
5 changes: 5 additions & 0 deletions core/src/main/scala/cats/data/Chain.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ sealed abstract class Chain[+A] {
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.
*/
Expand Down
6 changes: 6 additions & 0 deletions tests/src/test/scala/cats/tests/ChainSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ class ChainSuite extends CatsSuite {
}
}

test("headOption") {
forAll { (s: Seq[Int]) =>
Chain.fromSeq(s).headOption should ===(s.headOption)
}
}

test("size is consistent with toList.size") {
forAll { (ci: Chain[Int]) =>
ci.size.toInt should ===(ci.toList.size)
Expand Down

0 comments on commit 9e6ecc3

Please sign in to comment.