Skip to content

Commit

Permalink
Merge pull request #816 from notxcain/master
Browse files Browse the repository at this point in the history
Traverse instance for OneAnd
  • Loading branch information
ceedubs committed Jan 26, 2016
2 parents d8e7e78 + 96e92b9 commit acbe54e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
21 changes: 20 additions & 1 deletion core/src/main/scala/cats/data/OneAnd.scala
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ final case class OneAnd[F[_], A](head: A, tail: F[A]) {
s"OneAnd(${A.show(head)}, ${FA.show(tail)})"
}

private[data] sealed trait OneAndInstances extends OneAndLowPriority1 {
private[data] sealed trait OneAndInstances extends OneAndLowPriority2 {

implicit def oneAndEq[A, F[_]](implicit A: Eq[A], FA: Eq[F[A]]): Eq[OneAnd[F, A]] =
new Eq[OneAnd[F, A]]{
Expand Down Expand Up @@ -163,4 +163,23 @@ trait OneAndLowPriority1 extends OneAndLowPriority0 {
}
}

trait OneAndLowPriority2 extends OneAndLowPriority1 {
implicit def oneAndTraverse[F[_]](implicit F: Traverse[F]): Traverse[OneAnd[F, ?]] =
new Traverse[OneAnd[F, ?]] {
def traverse[G[_], A, B](fa: OneAnd[F, A])(f: (A) => G[B])(implicit G: Applicative[G]): G[OneAnd[F, B]] = {
val tail = F.traverse(fa.tail)(f)
val head = f(fa.head)
G.ap2(head, tail)(G.pure(OneAnd(_, _)))
}

def foldLeft[A, B](fa: OneAnd[F, A], b: B)(f: (B, A) => B): B = {
fa.foldLeft(b)(f)
}

def foldRight[A, B](fa: OneAnd[F, A], lb: Eval[B])(f: (A, Eval[B]) => Eval[B]): Eval[B] = {
fa.foldRight(lb)(f)
}
}
}

object OneAnd extends OneAndInstances
5 changes: 4 additions & 1 deletion tests/src/test/scala/cats/tests/OneAndTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package tests
import algebra.laws.{GroupLaws, OrderLaws}

import cats.data.{NonEmptyList, OneAnd}
import cats.laws.discipline.{ComonadTests, FunctorTests, SemigroupKTests, FoldableTests, MonadTests, SerializableTests, MonoidalTests}
import cats.laws.discipline.{ComonadTests, FunctorTests, SemigroupKTests, FoldableTests, MonadTests, SerializableTests, MonoidalTests, TraverseTests}
import cats.laws.discipline.arbitrary.{evalArbitrary, oneAndArbitrary}
import cats.laws.discipline.eq._

Expand All @@ -13,6 +13,9 @@ import scala.util.Random
class OneAndTests extends CatsSuite {
checkAll("OneAnd[List, Int]", OrderLaws[OneAnd[List, Int]].eqv)

checkAll("OneAnd[List, Int] with Option", TraverseTests[OneAnd[List, ?]].traverse[Int, Int, Int, Int, Option, Option])
checkAll("Traverse[OneAnd[List, A]]", SerializableTests.serializable(Traverse[OneAnd[List, ?]]))

implicit val iso = MonoidalTests.Isomorphisms.invariant[OneAnd[ListWrapper, ?]](OneAnd.oneAndFunctor(ListWrapper.functor))

// Test instances that have more general constraints
Expand Down

0 comments on commit acbe54e

Please sign in to comment.