Skip to content

Commit

Permalink
unapplySeq in Chain (issue #2960) (#2990)
Browse files Browse the repository at this point in the history
  • Loading branch information
vasiliybondarenko authored and kailuowang committed Aug 15, 2019
1 parent 5f24192 commit 3cf3366
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
8 changes: 8 additions & 0 deletions core/src/main/scala/cats/data/Chain.scala
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,14 @@ object Chain extends ChainInstances {
false // b/c `fromSeq` constructor doesn't allow either branch to be empty
}

def unapplySeq[A](chain: Chain[A]): Option[Seq[A]] =
Some(chain.toList)

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

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

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

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

test("list-like pattern match") {
Chain(1, 2, 3) match {
case Chain(a, b, c) => (a, b, c) should ===((1, 2, 3))
}

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

}

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

0 comments on commit 3cf3366

Please sign in to comment.