Skip to content

Instantly share code, notes, and snippets.

@saml
Created June 28, 2013 15:48
Show Gist options
  • Select an option

  • Save saml/5885695 to your computer and use it in GitHub Desktop.

Select an option

Save saml/5885695 to your computer and use it in GitHub Desktop.
def findFirstSuccess[A, B](f: A => B)(iter: Iterator[A]): Option[B] = {
var result: Option[B] = None
iter.find { a: A =>
Try {
result = Option(f(a))
}.isSuccess
}
result
}
@Refefer

Refefer commented Jun 28, 2013

Copy link
Copy Markdown

def collectFirstSuccess[A,B](it: Iterator[A], f:A =>B) = it.map({i => Try(f(i))}).collectFirst({case Success(i) => i})

@saml

saml commented Jun 28, 2013

Copy link
Copy Markdown
Author

yup thanks.

iter.map(x => Try(f(x))).collectFirst { case Success(y) => y }

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