Skip to content

Instantly share code, notes, and snippets.

@eugeniyk
Created September 16, 2018 07:05
Show Gist options
  • Save eugeniyk/890d8a4ea523a82d48facdda0531fa8b to your computer and use it in GitHub Desktop.
Save eugeniyk/890d8a4ea523a82d48facdda0531fa8b to your computer and use it in GitHub Desktop.
trait KVStore[F[_], K, V] {
def get(key: K): F[Option[V]]
def put(key: K, value: V): F[Unit]
}
class Dict[F[_]: Monad, K, V](store: KVStore[F, K, V]) {
def computeIfAbsent(k: K, absent: => V)(implicit m: Monad[F]): F[V] = {
m.flatMap(store.get(k)) { value =>
m.pure(value.getOrElse(absent))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment