scalaz - Scala flattening an Option around a higher kinded type, looking for a more idiomatic approach -


given higher kinded type m, , monad type class, can operate on values within m through for-comprehension. working function return options, im looking more appropriate way flatten these options solution have. follows

class test[m[+_]:monad](calc:calculator[m]) {   import monad._    def dosomething(x:float):m[option[float]] = {     {       y:option[float] <- calc.divideby(x)   // divideby returns m[option[float]]       z:option[float] <- y.fold[m[option[float]]](implicitly[monad[m]].point(none))(i => calc.divideby(i))     }  yield z   } }  

so following i'm looking correct:

y.fold[m[option[float]]](implicitly[monad[m]].point(none))(i => calc.divideby(i)) 

also case instead of calling second divideby, call multiplyby returns m[float]

y.fold[m[option[float]]](implicitly[monad[m]].point(none))(i => calc.multipleby(i).map(some(_))  

maybe case monad transformers im not sure how go it.

it seems monad transformers can here. example, following compiles , think want:

import scalaz._, scalaz._  abstract class calculator[m[_]: monad] {   def divideby(x: float): m[option[float]]   def multiplyby(x: float): m[float] }  class test[m[_]: monad](calc: calculator[m]) {   def dosomething(x: float): optiont[m, float] = {     y <- optiont(calc.divideby(x))     z <- calc.multiplyby(y).liftm[optiont]   } yield z } 

now dosomething returns optiont[m, float], kind of wrapper m[option[float]] allows work contents way inside option monadically. m[option[float]] optiont[m, float] can use run method.


Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

Kivy: Swiping (Carousel & ScreenManager) -