Skip to content

Commit

Permalink
Add :== unapply to Chain. (#2996)
Browse files Browse the repository at this point in the history
* Add :== unapply to Chain.

* use 'init' and 'last'.
  • Loading branch information
tanaka takaya authored and kailuowang committed Aug 20, 2019
1 parent a97bb9a commit 48c8b34
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
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 @@ -541,6 +541,11 @@ object Chain extends ChainInstances {
c.uncons
}

object :== {
def unapply[T](c: Chain[T]): Option[(Chain[T], T)] =
c.initLast
}

/** Empty Chain. */
val nil: Chain[Nothing] = Empty

Expand Down
7 changes: 6 additions & 1 deletion tests/src/test/scala/cats/tests/ChainSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tests

import cats.data.Chain
import cats.data.Chain.==:
import cats.data.Chain.`:==`
import cats.laws.discipline.{
AlternativeTests,
CoflatMapTests,
Expand Down Expand Up @@ -75,7 +76,7 @@ class ChainSuite extends CatsSuite {
}
}

test("list-like pattern match") {
test("seq-like pattern match") {
Chain(1, 2, 3) match {
case Chain(a, b, c) => (a, b, c) should ===((1, 2, 3))
}
Expand All @@ -84,6 +85,10 @@ class ChainSuite extends CatsSuite {
case h ==: t => (h, t) should ===(1 -> Chain(2, 3))
}

Chain(1, 2, 3) match {
case init :== last => (init, last) should ===(Chain(1, 2) -> 3)
}

}

test("size is consistent with toList.size") {
Expand Down

0 comments on commit 48c8b34

Please sign in to comment.