Created
February 25, 2014 21:46
-
-
Save fanatoly/9218552 to your computer and use it in GitHub Desktop.
LazyMap ftw
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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