Skip to content

Commit

Permalink
Functor strengthL and strengthR
Browse files Browse the repository at this point in the history
Add tests
  • Loading branch information
edmundnoble committed Nov 16, 2016
1 parent b71a25d commit 277923a
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 strengthL[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 strengthR[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("strengthL/R tuple values with a constant value preserving structure") {
forAll { (l: List[Int], o: Option[Int], m: Map[String, Int], i: Int) =>
l.strengthL(i) should === (List.tabulate(l.length)(in => (i, l(in))))
o.strengthL(i) should === (if (o.nonEmpty) Some((i, o.get)) else None)
m.strengthL(i) should === (m.map { case (k, v) => (k, (i, v)) }.toMap)
l.strengthR(i) should === (List.tabulate(l.length)(in => (l(in), i)))
o.strengthR(i) should === (if (o.nonEmpty) Some((o.get, i)) else None)
m.strengthR(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 277923a

Please sign in to comment.