ATuin

Scala Reader Monad Example

Jun 13th, 2013
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.49 KB | None | 0 0
  1. /*
  2.  * Scala Reader Monad example from: http://book.realworldhaskell.org/read/monad-transformers.html
  3.  */
  4.  
  5. def myName(step:String) = for {
  6.     name <- ask[Id, String]
  7.   } yield (step ++ ", I am " ++ name)
  8.  
  9. def localExample = for {
  10.     a <- myName("First")
  11.     b <- myName("Second").local(_ ++ "dy")
  12.     c <- myName("Third")
  13.   } yield (a,b,c)
  14.  
  15. localExample runReaderT "Fred" : (String,String,String)
  16. // Res: (String, String, String) = (First, I am Fred,Second, I am Freddy,Third, I am Fred)
Advertisement
Add Comment
Please, Sign In to add comment