Skip to content

Commit

Permalink
Add ByColor.traverseReduce
Browse files Browse the repository at this point in the history
  • Loading branch information
lenguyenthanh committed Jul 10, 2023
1 parent 920c286 commit edbd99d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/main/scala/ByColor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ case class ByColor[A](white: A, black: A):
def traverse[F[_], B](f: A => F[B]): Applicative[F] ?=> F[ByColor[B]] =
(f(white), f(black)).mapN(ByColor(_, _))

def traverseReduce[F[_], B, C](f: A => F[B])(r: (B, B) => C): Applicative[F] ?=> F[C] =
(f(white), f(black)).mapN(r)

object ByColor:
inline def fill[A](a: A): ByColor[A] = ByColor(a, a)
inline def fromPair[A](p: (A, A)): ByColor[A] = ByColor(p._1, p._2)
Expand Down
4 changes: 4 additions & 0 deletions src/test/scala/ByColorTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,7 @@ class ByColorTest extends ScalaCheckSuite:
test("traverse"):
forAll: (bc: ByColor[Int], f: Int => Option[String]) =>
bc.traverse(f).map(_.all) == bc.all.traverse(f)

test("traverseReduce == traverse.map.reduce"):
forAll: (bc: ByColor[Int], f: Int => Option[String], r: (String, String) => Long) =>
bc.traverseReduce(f)(r) == bc.traverse(f).map(_.reduce(r))

0 comments on commit edbd99d

Please sign in to comment.