-
-
Save fbrubacher/ef5560c2d6b1402abe643b16a73d9e02 to your computer and use it in GitHub Desktop.
Scalaz Writer and traverse / mapM example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scalaz._ | |
import Scalaz._ | |
object ScalazPlay extends App | |
{ | |
// having this one-param type is critical for the subsequent type inference! | |
type MyWriter[A] = Writer[List[String],A] | |
def f1(i:Int) : MyWriter[Int] = for { | |
_ <- List("f1 on " + i).tell | |
} yield (i+7) | |
//Writer(List("f1 on " + i), i+7) | |
val t = List(1,2,3).traverse(f1) | |
println(t.run) | |
val t2 = mapM(List(10,12,13))(f1) | |
println(t2.run) | |
// List(1,2,3).traverseU(f1) | |
// from test test in git: | |
// val s: Writer[String, Stream[Int]] = | |
// Stream(1, 2, 3).traverseU(x => Writer(x.toString, x)) | |
def mapM[A,B,M[_]:Monad](xs:List[A])(f:A=>M[B]) : M[List[B]] = | |
xs.map(f).sequence | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment