Skip to content

Commit

Permalink
Merge pull request #697 from ceedubs/natx-or
Browse files Browse the repository at this point in the history
Move NaturalTransformation.or from companion to trait
  • Loading branch information
adelbertc committed Nov 25, 2015
2 parents 5d879c1 + f496356 commit efe4af7
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 15 deletions.
16 changes: 8 additions & 8 deletions core/src/main/scala/cats/arrow/NaturalTransformation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@ trait NaturalTransformation[F[_], G[_]] extends Serializable { self =>

def andThen[H[_]](f: NaturalTransformation[G, H]): NaturalTransformation[F, H] =
f.compose(self)

def or[H[_]](h: H ~> G): Coproduct[F, H, ?] ~> G =
new (Coproduct[F, H, ?] ~> G) {
def apply[A](fa: Coproduct[F, H, A]): G[A] = fa.run match {
case Xor.Left(ff) => self(ff)
case Xor.Right(gg) => h(gg)
}
}
}

object NaturalTransformation {
def id[F[_]]: NaturalTransformation[F, F] =
new NaturalTransformation[F, F] {
def apply[A](fa: F[A]): F[A] = fa
}

def or[F[_], G[_], H[_]](f: F ~> H, g: G ~> H): Coproduct[F, G, ?] ~> H =
new (Coproduct[F, G, ?] ~> H) {
def apply[A](fa: Coproduct[F, G, A]): H[A] = fa.run match {
case Xor.Left(ff) => f(ff)
case Xor.Right(gg) => g(gg)
}
}
}
3 changes: 1 addition & 2 deletions docs/src/main/tut/freemonad.md
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,6 @@ lets us compose different algebras in the context of `Free`.
Let's see a trivial example of unrelated ADT's getting composed as a `Coproduct` that can form a more complex program.

```tut:silent
import cats.arrow.NaturalTransformation
import cats.data.{Xor, Coproduct}
import cats.free.{Inject, Free}
import cats.{Id, ~>}
Expand Down Expand Up @@ -448,7 +447,7 @@ object InMemoryDatasourceInterpreter extends (DataOp ~> Id) {
}
}
val interpreter: CatsApp ~> Id = NaturalTransformation.or(InMemoryDatasourceInterpreter, ConsoleCatsInterpreter)
val interpreter: CatsApp ~> Id = InMemoryDatasourceInterpreter or ConsoleCatsInterpreter
```

Now if we run our program and type in "snuggles" when prompted, we see something like this:
Expand Down
3 changes: 1 addition & 2 deletions free/src/test/scala/cats/free/InjectTests.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cats
package free

import cats.arrow.NaturalTransformation
import cats.data.{Xor, Coproduct}
import cats.laws.discipline.arbitrary
import cats.tests.CatsSuite
Expand Down Expand Up @@ -53,7 +52,7 @@ class InjectTests extends CatsSuite {
}
}

val coProductInterpreter: T ~> Id = NaturalTransformation.or(Test1Interpreter, Test2Interpreter)
val coProductInterpreter: T ~> Id = Test1Interpreter or Test2Interpreter

val x: Free[T, Int] = Free.inject[Test1Algebra, T](Test1(1, identity))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ class NaturalTransformationTests extends CatsSuite {
}

test("or") {
val combinedInterpreter = NaturalTransformation.or(Test1NT, Test2NT)
val combinedInterpreter = Test1NT or Test2NT
forAll { (a : Int, b : Int) =>
(combinedInterpreter(Coproduct.left(Test1(a))) == Id.pure(a)) should ===(true)
(combinedInterpreter(Coproduct.right(Test2(b))) == Id.pure(b)) should ===(true)
combinedInterpreter(Coproduct.left(Test1(a))) should === (Id.pure(a))
combinedInterpreter(Coproduct.right(Test2(b))) should === (Id.pure(b))
}
}
}

0 comments on commit efe4af7

Please sign in to comment.