Advertisement
Guest User

Untitled

a guest
Sep 16th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.34 KB | None | 0 0
  1. import cats.implicits._
  2. import cats.{Id, Monad}
  3. trait KVStore[F[_], K, V] {
  4.     def get(k: K): F[Option[V]]
  5.     def put(k:K, v:V): F[Unit]
  6.   }
  7.  
  8.  
  9. class Dict[F[_]: Monad, K, V](store: KVStore[F, K, V]) {
  10.     def computeIfAbsent(k: K, absent: => V): F[V] = {
  11.       store.get(k).flatMap(opt => Monad[F].pure(opt.getOrElse(absent)))
  12.     }
  13.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement