Last active
May 5, 2017 11:47
-
-
Save abdheshkumar/f187d02b497c0d74fe1c1b757d8a9639 to your computer and use it in GitHub Desktop.
This file contains 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
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