diff --git a/core/src/main/scala/cats/data/NonEmptyChain.scala b/core/src/main/scala/cats/data/NonEmptyChain.scala index cff35c1029d..63011a0f504 100644 --- a/core/src/main/scala/cats/data/NonEmptyChain.scala +++ b/core/src/main/scala/cats/data/NonEmptyChain.scala @@ -207,6 +207,13 @@ class NonEmptyChainOps[A](val value: NonEmptyChain[A]) extends AnyVal { /** * Tests if some element is contained in this chain. + * {{{ + * scala> import cats.data.NonEmptyChain + * scala> import cats.implicits._ + * scala> val nec = NonEmptyChain(4, 5, 6) + * scala> nec.contains(5) + * res0: Boolean = true + * }}} */ final def contains(a: A)(implicit A: Eq[A]): Boolean = toChain.contains(a) @@ -227,6 +234,13 @@ class NonEmptyChainOps[A](val value: NonEmptyChain[A]) extends AnyVal { /** * Returns a new `Chain` containing all elements where the result of `pf` is final defined. + * {{{ + * scala> import cats.data.NonEmptyChain + * scala> import cats.implicits._ + * scala> val nec = NonEmptyChain(4, 5, 6).map(n => if (n % 2 == 0) Some(n) else None) + * scala> nec.collect { case Some(n) => n } + * res0: cats.data.Chain[Int] = Chain(4, 6) + * }}} */ final def collect[B](pf: PartialFunction[A, B]): Chain[B] = toChain.collect(pf) @@ -255,6 +269,12 @@ class NonEmptyChainOps[A](val value: NonEmptyChain[A]) extends AnyVal { /** * Left-associative reduce using f. + * {{{ + * scala> import cats.data.NonEmptyChain + * scala> val nec = NonEmptyChain(4, 5, 6) + * scala> nec.reduceLeft(_ + _) + * res0: Int = 15 + * }}} */ final def reduceLeft(f: (A, A) => A): A = { val iter = toChain.iterator @@ -266,6 +286,12 @@ class NonEmptyChainOps[A](val value: NonEmptyChain[A]) extends AnyVal { /** * Apply `f` to the "initial element" of this chain and lazily combine it * with every other value using the given function `g`. + * {{{ + * scala> import cats.data.NonEmptyChain + * scala> val nec = NonEmptyChain(4, 5, 6) + * scala> nec.reduceLeftTo(_.toString)((acc, cur) => acc + cur.toString) + * res0: String = 456 + * }}} */ final def reduceLeftTo[B](f: A => B)(g: (B, A) => B): B = { val iter = toChain.iterator @@ -276,6 +302,12 @@ class NonEmptyChainOps[A](val value: NonEmptyChain[A]) extends AnyVal { /** * Right-associative reduce using f. + * {{{ + * scala> import cats.data.NonEmptyChain + * scala> val nec = NonEmptyChain(4, 5, 6) + * scala> nec.reduceRight(_ + _) + * res0: Int = 15 + * }}} */ final def reduceRight(f: (A, A) => A): A = { val iter = toChain.reverseIterator @@ -287,6 +319,12 @@ class NonEmptyChainOps[A](val value: NonEmptyChain[A]) extends AnyVal { /** * Apply `f` to the "initial element" of this chain and lazily combine it * with every other value using the given function `g`. + * {{{ + * scala> import cats.data.NonEmptyChain + * scala> val nec = NonEmptyChain(4, 5, 6) + * scala> nec.reduceLeftTo(_.toString)((cur, acc) => acc + cur.toString) + * res0: String = 654 + * }}} */ final def reduceRightTo[B](f: A => B)(g: (A, B) => B): B = { val iter = toChain.reverseIterator diff --git a/tests/src/test/scala/cats/tests/NonEmptyChainSuite.scala b/tests/src/test/scala/cats/tests/NonEmptyChainSuite.scala index 164b210f604..77e26a9d358 100644 --- a/tests/src/test/scala/cats/tests/NonEmptyChainSuite.scala +++ b/tests/src/test/scala/cats/tests/NonEmptyChainSuite.scala @@ -2,7 +2,7 @@ package cats package tests import cats.data.{Chain, NonEmptyChain} -import cats.kernel.laws.discipline.{OrderTests, SemigroupTests} +import cats.kernel.laws.discipline.{EqTests, OrderTests, PartialOrderTests, SemigroupTests} import cats.laws.discipline.{BimonadTests, NonEmptyTraverseTests, SemigroupKTests, SerializableTests} import cats.laws.discipline.arbitrary._ @@ -22,6 +22,22 @@ class NonEmptyChainSuite extends CatsSuite { checkAll("NonEmptyChain[Int]", OrderTests[NonEmptyChain[Int]].order) checkAll("Order[NonEmptyChain[Int]", SerializableTests.serializable(Order[NonEmptyChain[Int]])) + { + implicit val partialOrder = ListWrapper.partialOrder[Int] + checkAll("NonEmptyChain[ListWrapper[Int]]", + PartialOrderTests[NonEmptyChain[ListWrapper[Int]]].partialOrder) + checkAll("PartialOrder[NonEmptyChain[ListWrapper[Int]]", + SerializableTests.serializable(PartialOrder[NonEmptyChain[ListWrapper[Int]]])) + } + + { + implicit val eqv = ListWrapper.eqv[Int] + checkAll("NonEmptyChain[ListWrapper[Int]]", + EqTests[NonEmptyChain[ListWrapper[Int]]].eqv) + checkAll("Eq[NonEmptyChain[ListWrapper[Int]]", + SerializableTests.serializable(Eq[NonEmptyChain[ListWrapper[Int]]])) + } + test("show"){ Show[NonEmptyChain[Int]].show(NonEmptyChain(1, 2, 3)) should === ("NonEmptyChain(1, 2, 3)") }