All Downloads are FREE. Search and download functionalities are using the official Maven repository.

scalaz.example.concurrent.Fibs.scala Maven / Gradle / Ivy

package scalaz.example
package concurrent

object Fibs {
  def main(args: Array[String]) = run

  import java.util.concurrent._
  import scalaz.concurrent._
  import scalaz.Scalaz._

  val cutoff = 30

  def run {
    implicit val pool = Executors.newFixedThreadPool(5)
    implicit val s = Strategy.Executor

    val out = actor {(xs: Stream[Int]) =>
      xs.zipWithIndex.foreach(p => println("n=" + (p._2 + 1) + " => " + p._1))
      pool.shutdown
    }

    def seqFib(n: Int): Int = if (n < 2) n else seqFib(n - 1) + seqFib(n - 2)

    def fib(n: Int): Promise[Int] =
      if (n < cutoff)
        promise(seqFib(n))
      else
        fib(n - 1).<**>(fib(n - 2))(_ + _)

    Stream.range(1, 41).traverse(fib) to out
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy