Created
September 16, 2018 07:05
-
-
Save eugeniyk/890d8a4ea523a82d48facdda0531fa8b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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