Skip to content

Commit

Permalink
Functor strengthL and strengthR (#1468)
Browse files Browse the repository at this point in the history
Add tests

tupleRight/tupleLeft
  • Loading branch information
edmundnoble authored and johnynek committed Dec 22, 2016
1 parent b35610b commit 974b840
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
10 changes: 10 additions & 0 deletions core/src/main/scala/cats/Functor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ import simulacrum.typeclass
*/
def as[A, B](fa: F[A], b: B): F[B] = map(fa)(_ => b)

/**
* Tuples the `A` value in `F[A]` with the supplied `B` value, with the `B` value on the left.
*/
def tupleLeft[A, B](fa: F[A], b: B): F[(B, A)] = map(fa)(a => (b, a))

/**
* Tuples the `A` value in `F[A]` with the supplied `B` value, with the `B` value on the right.
*/
def tupleRight[A, B](fa: F[A], b: B): F[(A, B)] = map(fa)(a => (a, b))

def compose[G[_]: Functor]: Functor[λ[α => F[G[α]]]] =
new ComposedFunctor[F, G] {
val F = self
Expand Down
11 changes: 11 additions & 0 deletions tests/src/test/scala/cats/tests/FunctorTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ class FunctorTest extends CatsSuite {
}
}

test("tupleLeft and tupleRight tuple values with a constant value preserving structure") {
forAll { (l: List[Int], o: Option[Int], m: Map[String, Int], i: Int) =>
l.tupleLeft(i) should === (List.tabulate(l.length)(in => (i, l(in))))
o.tupleLeft(i) should === (if (o.nonEmpty) Some((i, o.get)) else None)
m.tupleLeft(i) should === (m.map { case (k, v) => (k, (i, v)) }.toMap)
l.tupleRight(i) should === (List.tabulate(l.length)(in => (l(in), i)))
o.tupleRight(i) should === (if (o.nonEmpty) Some((o.get, i)) else None)
m.tupleRight(i) should === (m.map { case (k, v) => (k, (v, i)) }.toMap)
}
}

test("widen equals map(identity)") {
forAll { (i: Int) =>
val list: List[Some[Int]] = List(Some(i))
Expand Down

0 comments on commit 974b840

Please sign in to comment.