Skip to content

Instantly share code, notes, and snippets.

@fanatoly
Created February 25, 2014 21:46
Show Gist options
  • Save fanatoly/9218552 to your computer and use it in GitHub Desktop.
Save fanatoly/9218552 to your computer and use it in GitHub Desktop.
LazyMap ftw
package com.yieldmo.rtb.util
class LazyMap[A,B](delegate: Map[A,() => B]) extends scala.collection.immutable.Map[A,B]{
def get(key: A): Option[B] = delegate.get(key).map{ _() }
def iterator: Iterator[(A,B)] = delegate.iterator.map{ pair => (pair._1, pair._2()) }
def +[B1 >:B](kv: (A, B1)): Map[A, B1] = new LazyMap[A,B1](delegate + (kv._1 -> (() => kv._2)))
def -(key: A): Map[A, B] = new LazyMap[A,B](delegate - key)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment