Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move NaturalTransformation.or from companion to trait #697

Merged
merged 2 commits into from
Nov 25, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
}
}
}