Idiomatic way to combine different monads with for in Scala -


suppose have 2 values wrapped in different monads (e.g. try , option):

val x: option[int] = some(10) val y: try[int] = success(4) 

and want have sum of values. 1 write

val z = {   xval <- x   yval <- y } yield xval + yval 

but won't compiled because of type error. there idiomatic scala way deal this?

the scala standard library missing useful functions / abstractions, fortunately there complementing library called scalaz provides of need.

in particular, suggested, looking monad transformers. see these 2 following posts:

http://eed3si9n.com/learning-scalaz/monad+transformers.html

http://underscoreconsulting.com/blog/posts/2013/12/20/scalaz-monad-transformers.html


Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -