Skip to content

Instantly share code, notes, and snippets.

@leandrob13
Last active June 29, 2017 10:43
Show Gist options
  • Save leandrob13/8fdd70aa404b9375649a5b1fcfaf163e to your computer and use it in GitHub Desktop.
Save leandrob13/8fdd70aa404b9375649a5b1fcfaf163e to your computer and use it in GitHub Desktop.
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)
@leandrob13
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment