Last active
August 29, 2015 14:00
-
-
Save davidpeklak/11282735 to your computer and use it in GitHub Desktop.
KleisliTest
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package smt | |
import scalaz.concurrent.Future | |
import scalaz.Scalaz._ | |
import org.scalatest.FunSuite | |
import scalaz._ | |
class KleisliTest extends FunSuite { | |
lazy val s: List[Int] = List.fill(1000000)(1) | |
test("Future traverse") { | |
def fun(i: Int): Future[Int] = Future.delay(i) | |
val f: Future[List[Int]] = s.traverse(fun) | |
f.run | |
} | |
test("Future traverse_") { | |
def fun(i: Int): Future[Unit] = Future.delay(()) | |
val f: Future[Unit] = s.traverse_(fun) | |
f.run | |
} | |
trait Resource { | |
def query(i: Int): Int | |
def effect(i: Int): Unit | |
} | |
lazy val R = new Resource { | |
override def query(i: Int): Int = i * 2 | |
override def effect(i: Int): Unit = () | |
} | |
type λ[α] = Kleisli[Future, Resource, α] | |
test("Kleisli traverse") { | |
def fun(i: Int): Kleisli[Future, Resource, Int] = Kleisli(r => Future.delay(r.query(i))) | |
val k: Kleisli[Future, Resource, List[Int]] = s.traverse[λ, Int](fun) | |
val f: Future[List[Int]] = k.run(R) | |
f.run | |
} | |
// throws SOE | |
ignore("Kleisli traverse_") { | |
def fun(i: Int): Kleisli[Future, Resource, Unit] = Kleisli(r => Future.delay(r.effect(i))) | |
type λ[α] = Kleisli[Future, Resource, α] | |
val k: Kleisli[Future, Resource, Unit] = s.traverse_[λ](fun) | |
val f: Future[Unit] = k.run(R) | |
f.run | |
} | |
// does the same as the test above, but does not throw SOE | |
test("Kleisli traverse map _ => ()") { | |
def fun(i: Int): Kleisli[Future, Resource, Int] = Kleisli(r => Future.delay(r.query(i))) | |
val k: Kleisli[Future, Resource, List[Int]] = s.traverse[λ, Int](fun) | |
val k2: Kleisli[Future, Resource, Unit] = k.map(_ => ()) | |
val f: Future[Unit] = k2.run(R) | |
f.run | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment