Skip to content

Instantly share code, notes, and snippets.

@yuroyoro
Created September 8, 2010 05:04
Show Gist options
  • Save yuroyoro/569660 to your computer and use it in GitHub Desktop.
Save yuroyoro/569660 to your computer and use it in GitHub Desktop.
trait Memoized[T,R] {
import scala.collection.mutable._
private[this] val cache = new HashMap[T,R]
def cacheOrApply( t:T )( f: => R ):R = cache get(t) getOrElse{
val rv = f
cache += t -> rv
rv
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment