Skip to content

Instantly share code, notes, and snippets.

@abdheshkumar
Last active May 5, 2017 11:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abdheshkumar/f187d02b497c0d74fe1c1b757d8a9639 to your computer and use it in GitHub Desktop.
Save abdheshkumar/f187d02b497c0d74fe1c1b757d8a9639 to your computer and use it in GitHub Desktop.
import cats.Id
import cats.implicits._
import freestyle._
import freestyle.implicits._
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
/**
* Created by abdhesh on 05/05/17.
*/
object TraverseFreestyle extends App {
@free trait Calc {
def sum(a: Int, b: Int): FS[Int]
def product(a: Int, b: Int): FS[Int]
}
implicit val idHandler = new Calc.Handler[Id] {
def sum(a: Int, b: Int): Id[Int] = a + b
def product(a: Int, b: Int): Id[Int] = a * b
}
implicit val futureHandler = new Calc.Handler[Future] {
def sum(a: Int, b: Int): Future[Int] =
Future {
Thread.sleep(a * 100L);
a + b
}
def product(a: Int, b: Int): Future[Int] =
Future {
Thread.sleep(a * 100L);
a * b
}
}
val incrPar: Int => FreeS.Par[Calc.Op, Int] = Calc[Calc.Op].sum(_, 1)
val incrSeq: Int => FreeS[Calc.Op, Int] = incrPar.andThen(_.freeS)
val numbers = List.range(1, 10)
val traversingPar = numbers.traverseU(f => incrPar(f))
val traversingSeq = numbers.traverseU(incrSeq)
traversingPar.interpret[Id]
traversingSeq.interpret[Id]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment