Created
May 18, 2012 12:06
-
-
Save rirakkumya/2724922 to your computer and use it in GitHub Desktop.
List[Either[Throwable,_]]のリスト中にLeftが1個以上あったら最初のLeftを返し、Leftが1個もなければ、Rightを1個のリストにまとめて返すコード
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
def f[a] = (d:List[Either[Throwable,a]]) => | |
d.collect{case Left(x) => x}.headOption.toLeft(d.map{case Right(x) => x}) | |
scala> Some(List(Right("4"),Right("3"))) map f | |
res11: Option[Product with Either[Throwable,List[java.lang.String]] with Serializable] = Some(Right(List(4, 3))) | |
scala> Some(List(Right("4"),Left(new RuntimeException),Right("3"))) map f | |
res12: Option[Product with Either[Throwable,List[java.lang.String]] with Serializable] = Some(Left(java.lang.RuntimeException)) | |
scala> Some(List(Right("4"),Left(new RuntimeException("a")),Right("3"),Left(new RuntimeException("b")))) map f | |
res13: Option[Product with Either[Throwable,List[java.lang.String]] with Serializable] = Some(Left(java.lang.RuntimeException: a)) |
scalaz版ありがとうございます。勉強になります!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
遅レスですが、Scalazで。