TaskMap: memoizes task-producing functions

This commit is contained in:
Mark Harrah 2010-09-17 19:41:20 -04:00
parent 16d6ec7f94
commit 47e9428144
1 changed files with 20 additions and 0 deletions

View File

@ -0,0 +1,20 @@
/* sbt -- Simple Build Tool
* Copyright 2010 Mark Harrah
*/
package sbt
import std._
import TaskExtra._
import collection.mutable
object TaskMap
{
/** Memoizes a function that produces a Task. */
def apply[A,B](f: A => Task[B]): A => Task[B] =
{
val map = new mutable.HashMap[A,Task[B]]
a => map.synchronized { map.getOrElseUpdate(a, f(a)) }
}
/** Lifts `f` to produce a Task and memoizes the lifted function. */
def make[A,B](f: A => B): A => Task[B] = apply( a => task(f(a)) )
}