Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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