Skip to content

Instantly share code, notes, and snippets.

@harrah
Created August 30, 2010 01:24
Show Gist options
  • Save harrah/556888 to your computer and use it in GitHub Desktop.
Save harrah/556888 to your computer and use it in GitHub Desktop.
import sbt._
import std._
class TaskTest extends SingleProject
{
lazy val hello: Task[Unit] =
task { println("Hi!") }
lazy val three: Task[Int] =
task { 3 }
lazy val goodbye: Task[Unit] = task { println("Goodbye") } dependsOn(hello)
lazy val four: Task[Int] = three map { (t: Int) => t + 1 }
lazy val rand: Task[Boolean] = task { math.random > 0.5 }
lazy val num: Task[Int] = rand flatMap { (b: Boolean) => if(b) four else three }
lazy val par: Task[Int] = (three, four) map { (t: Int, f: Int) => t * f }
lazy val forkReduce: Task[Int] = (0 to 1000).fork( _ * 3).reduce { _ + _ }
lazy val forkJoin: Task[Seq[Int]] = (0 to 1000).fork(_ * 3).join
lazy val fail: Task[Int] = task { error("A failure.") }
lazy val catchLike: Task[Unit] = fail mapFailure { (t: Incomplete) => t.printStackTrace }
lazy val alwaysRunA: Task[Int] = fail andFinally { println("Finally 1") }
lazy val alwaysRunB: Task[Int] = four andFinally { println("Finally 2") }
// succeeds because 'hello' succeeds. The result is that of the final task, which is `three` here.
lazy val allA: Task[Int] = hello && three
// fails because 'fail' fails
lazy val allB: Task[Unit] = fail && hello
lazy val orA: Task[Int] = fail || four
lazy val orB: Task[Int] = four || fail
lazy val orC: Task[Int] = four || three
lazy val fullA: Task[Int] = four mapR {
case Inc(i) => 0
case Value(v) => v
}
val ScalaVersion = AttributeKey[String]("scala-version")
val Number = AttributeKey[Int]("num")
lazy val scalaVersion: Task[String] = cross(ScalaVersion)("2.7.7", "2.8.0")
lazy val number: Task[Int] = cross(Number)(1,2,3)
lazy val printVersion: Task[Unit] = scalaVersion map println
lazy val printNumber: Task[Unit] = number map println
lazy val printBoth: Task[Unit] = (scalaVersion, number) map { (v: String, n: Int) => println(n + ": " + v) }
lazy val numA: Task[Int] = cross(Number)(1,2,3)
lazy val numB: Task[Int] = cross(Number)(4,5,6)
//lazy val numC = (numA, numB) map { (a: Int, b: Int) => a * b }
lazy val stringBoth: Task[String] = (scalaVersion, number) map { (v: String, n: Int) => n + ": " + v }
lazy val merged: Task[Seq[(AttributeMap, String)]] = stringBoth.merge
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment