Skip to content

Instantly share code, notes, and snippets.

@fanf
Last active October 27, 2015 10:41
Show Gist options
  • Save fanf/ac8135b48dd8bc669f26 to your computer and use it in GitHub Desktop.
Save fanf/ac8135b48dd8bc669f26 to your computer and use it in GitHub Desktop.
#
# Is this code safe? It seems that it can broke at least at two point:
# - adding in a mutable structure from a parallele processing does not seems to be a good idea
# - throwing an exception for control flow (early out) is kind of acceptable for sequential loop,
# but what happen if two thread of the ".par" processing throw it at the same time ?
# If this is not the good way of doing that, how one process a sequence of things (could be vector
# in place of Seq, the genericity is not really needed here) so that:
# - the processing is parallelized,
# - there is an early out ?
#
def sequencePar[U,T](seq:Seq[U])(f:U => Option[T]) : Option[Seq[T]] = {
val buf = scala.collection.mutable.Buffer[T]()
seq.par.foreach { u => f(u) match {
case None => return e
case Some(x) => buf += x
} }
Some(buf)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment