Last active
October 27, 2015 10:41
-
-
Save fanf/ac8135b48dd8bc669f26 to your computer and use it in GitHub Desktop.
This file contains 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
# | |
# 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