Skip to content

Instantly share code, notes, and snippets.

@DRMacIver
Created March 14, 2009 17:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DRMacIver/79133 to your computer and use it in GitHub Desktop.
Save DRMacIver/79133 to your computer and use it in GitHub Desktop.
object Loops{
def loop(cond : =>Boolean)(action : =>Unit){
try {
while(cond){
try { action } catch { case Continue => }
}
} catch { case Break => }
}
def foreach[T](its : Iterable[T])(action : T=>Unit){
try {
its.foreach{t =>
try { action(t) } catch { case Continue => }
}
} catch { case Break => }
}
def foldLeft[S, T](its : Iterable[T], start : S)(f : (S, T) => S) = {
var s = start;
foreach(its)(t => s = f(s, t));
s
}
def break = throw Break;
def continue = throw Continue;
private object Break extends Error;
private object Continue extends Error;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment