All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
org.specs2.fp.Memo.scala Maven / Gradle / Ivy
package org.specs2.fp
sealed abstract class Memo[@specialized(Int) K, @specialized(Int, Long, Double) V]:
def apply(z: K => V): K => V
object Memo:
def memo[@specialized(Int) K, @specialized(Int, Long, Double) V](f: (K => V) => K => V): Memo[K, V] = new Memo[K, V] {
def apply(z: K => V) = f(z)
}
def nilMemo[@specialized(Int) K, @specialized(Int, Long, Double) V]: Memo[K, V] = memo[K, V](z => z)
def mutableMapMemo[K, V](a: collection.mutable.Map[K, V]): Memo[K, V] =
memo[K, V](f => k => a.getOrElseUpdate(k, f(k)))
def mutableHashMapMemo[K, V]: Memo[K, V] =
mutableMapMemo(new collection.mutable.HashMap[K, V])
def weakHashMapMemo[K, V]: Memo[K, V] =
mutableMapMemo(new collection.mutable.WeakHashMap[K, V])
def immutableMapMemo[K, V](m: Map[K, V]): Memo[K, V] =
var a = m
memo[K, V](f =>
k => {
a.getOrElse(
k, {
val v = f(k)
a = a updated (k, v)
v
}
)
}
)
import collection.immutable.{HashMap, ListMap, TreeMap}
def immutableHashMapMemo[K, V]: Memo[K, V] = immutableMapMemo(HashMap.empty[K, V])
def immutableListMapMemo[K, V]: Memo[K, V] = immutableMapMemo(ListMap.empty[K, V])
def immutableTreeMapMemo[K: scala.Ordering, V]: Memo[K, V] = immutableMapMemo(TreeMap.empty[K, V])