Skip to content

Instantly share code, notes, and snippets.

@julsam
Last active August 29, 2015 13:57
Show Gist options
  • Save julsam/9502207 to your computer and use it in GitHub Desktop.
Save julsam/9502207 to your computer and use it in GitHub Desktop.
breaking nested loop in haxe - which solution is the best ?
canPlace = 1;
for (y in 0...height)
{
for (x in 0...width)
{
if (mapArr[x][y] != ' ')
{
canPlace = 2;
break;
}
}
if (canPlace == 2) break;
}
canPlace = 1;
for (y in 0...height)
{
for (x in 0...width)
{
if (mapArr[x][y] != ' ')
{
canPlace = 2;
// we want to break here ! but we can't because of the nested loop ! and there is no goto in haxe :(
}
}
}
canPlace = 1;
(function() { // self invoking local function
for (y in 0...height)
{
for (x in 0...width)
{
if (mapArr[x][y] != ' ')
{
canPlace = 2;
return;
}
}
}
})();
canPlace = 1;
for (y in 0...height)
{
for (x in 0...width)
{
if (mapArr[x][y] != ' ')
{
canPlace = 2;
y = height; // cause the outer loop to break
break; // break the inner loop
}
}
}
canPlace = 1;
var exitedInner:Bool = false;
for (y in 0...height)
{
for (x in 0...width)
{
if (mapArr[x][y] != ' ')
{
canPlace = 2;
exitedInner = true;
break;
}
}
if (exitedInner) break;
}
var canPlace = 1;
var y = [for (i in 0...height) i];
Lambda.foreach(a, function(y) {
for (x in 0...width)
{
if (mapArr[x][y] != ' ')
{
canPlace = 2;
return false;
}
}
return true;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment