Say I have some code:
def foo(s:String):Either[Bar, Baz] = // some code here ...
I want to use it as:
val a = Array("a", "b", "c")
a.map(foo) match {
case something => // should match when all elements in array are of type Right
case _ =>
}
Can anyone suggest code for “something”
EDIT: Prefer to match and use array of Right[Bar,Baz] directly rather than having to extract after match.
Use the
forallmethod to check that all elements in the array areisRight:Regarding your comment, if you want the match and convert-to-right all in one pass, then try a custom extractor: