Skip to content

Instantly share code, notes, and snippets.

@emaxerrno
Forked from fanatoly/LazyMap.scala
Created February 26, 2014 02:01
Show Gist options
  • Save emaxerrno/9221957 to your computer and use it in GitHub Desktop.
Save emaxerrno/9221957 to your computer and use it in GitHub Desktop.
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