Last active
June 29, 2017 10:43
-
-
Save leandrob13/8fdd70aa404b9375649a5b1fcfaf163e to your computer and use it in GitHub Desktop.
This file contains hidden or 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 cats.data._ | |
import cats.syntax.traverse._ | |
import cats.std.list._ | |
import scala.util.Try | |
def tryInt(s: String): Try[Int] = Try(s.toInt) | |
def reader: Reader[Try[Int], String] = Reader[Try[Int], String] { t => | |
t.map(s => s"$s is a number").getOrElse("not") | |
} | |
val listReader1 = List(reader, reader, reader) | |
val listReader2 = List(reader, reader, reader) | |
val readers = listReader1 ++ listReader2 | |
val sequenced: Reader[Try[Int], List[String]] = readers.sequenceU | |
sequenced(tryInt("1")) | |
//res0: cats.Id[List[String]] = List(1 is a number, 1 is a number, 1 is a number, 1 is a number, 1 is a number, 1 is a number) | |
sequenced(tryInt("hola")) | |
//res1: cats.Id[List[String]] = List(not, not, not, not, not, not) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to: http://typelevel.org/cats/tut/traverse.html