Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deal with Reader[A, Future[B]]

Tags:

scala

scalaz

Given the 3 following functions,

def f[A, B] : Reader[A, B] = ???
def g[A, B, C](b: B) : Reader[A, Future[C]] = ???
def h[A, C, D](c: C) : Reader[A, D] = ???

How to write a monadic combinator ?

Here is my current solution, but I'm not really happy with all these boilerplates

def f2[A, B] : ReaderT[Future, A, B] = kleisli {
  a => Future.successful(f.run(a))
}

def h2[A, C, D](c: C) : ReaderT[Future, A, D] = kleisli {
  a => Future.successful(h(c).run(a))
}

def g2[A, B, C](b: B) : ReaderT[Future, A, C] = kleisli {
  a => g(b).run(a)
}

def i[A,B,C,D] : ReaderT[Future, A, (B,C,D)] =
  for {
    b <- f2[A, B]
    c <- g2[A, B, C](b)
    d <- h2[A, C, D](c)
  } yield (b,c,d)
like image 575
Yann Moisan Avatar asked Oct 31 '22 11:10

Yann Moisan


1 Answers

def i[A,B,C,D] : ReaderT[Future, A, (B,C,D)] =
  for {
    b <- f[A, B].lift[Future]
    c <- g[A, B, C](b).mapK[Future, C](identity)
    d <- h[A, C, D](c).lift[Future]
  } yield (b,c,d)

I'm not sure if there is a better way to replace the mapK[Future, C](identity).

like image 186
drstevens Avatar answered Nov 15 '22 06:11

drstevens