Search code examples
scalascalazscalaz7

point reader monad scala


is there a way, how to easily point a value in Reader context?
I can use Reader object and ignore the context:

Reader {
  _ ⇒ 3
}
Scalaz seems to have a method point for this specifically. I see, that is defined on Applicative. I suppose, that there is some trick, how to put a value into the Reader context.


Solution

  • There is in fact an Applicative instance for Reader, which you can use like this (you should of course replace String with whatever type you want your environment to be):

    3.point[({type L[X] = Reader[String, X]})#L]
    

    If you'd prefer to avoid the ugly type lambda and don't mind defining an extra type alias, you can write the following instead:

    type MyReader[X] = Reader[String, X]
    
    3.point[MyReader]
    

    Scalaz 7 also provides a nice bit of shorthand that makes this even cleaner:

    3.liftReader[String]
    

    These are all more or less equivalent to each other (and to your solution), though.