0
が、これは動作します:同等のOptionのシーケンスおよびトラバースメソッドは何ですか? scalaz 7.2.6上
// import scalaz.syntax.traverse._
List(1.some, 2.some, 3.some).sequence
res12: Option[List[Int]] = Some(List(1, 2, 3))
List(1.some, none, 3.some).sequence
res13: Option[List[Int]] = None
def doubleSmall(x: Int) = if (x < 30) (x * 2).some else none
List(1,2,3).traverse(doubleSmall)
res15: Option[List[Int]] = Some(List(2, 4, 6))
次のコードはコンパイルされません。 \/
にはOption
のsequence
とtraverse
の方法がありますか?
List(1.right, 2.right, 3.right).sequence
expecting something like: List(1, 2, 3).right
List(1.right, "error2".left, "error3".left).sequence
expecting something like: Seq("error2", "error3").left
val doubleSmall(x: Int) = if (x < 30) (x * 2).right else "error".left
List(1,2,3).traverse(doubleSmall)
1つ以上のレフトがある場合、これは最初のものだけをとります。私はとにかくその単純さのために答えを受け入れるでしょう。私は左のリストを取得する方法についてもう少し詳細な質問を投稿しました:http://stackoverflow.com/questions/39471935/scalaz-disjunction-sequence-returning-a-list-of-lefts –