Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cats.implicits._
- import cats.{Id, Monad}
- trait KVStore[F[_], K, V] {
- def get(k: K): F[Option[V]]
- def put(k:K, v:V): F[Unit]
- }
- class Dict[F[_]: Monad, K, V](store: KVStore[F, K, V]) {
- def computeIfAbsent(k: K, absent: => V): F[V] = {
- store.get(k).flatMap(opt => Monad[F].pure(opt.getOrElse(absent)))
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement