Loops

Loops in Imba behaves similar to array comprehensions in CoffeeScript and Python. They are expressions, and can be returned and assigned. When used as expressions they will always return an array (like Array#map, but with a few additional powerful features, like break / continue).

# basic for in for num in [1,2,3] num * 2// basic for in for (let i = 0, items = [1,2,3], len = items.length; i < len; i++) { items[i] * 2; };
# iterate through keys of object for key,value of {a: 1, b: 2} value * 2var dict; // iterate through keys of object for (let key in dict = {a: 1,b: 2}){ let value; value = dict[key];value * 2; };
while true break if Math.random < 0.1while (true){ if (Math.random() < 0.1) { break; }; };
until Math.random < 0.1 gamblewhile (Math.random() >= 0.1){ this.gamble(); };

For in

# looping with index argument for num,i in [1,2,3] num * i// looping with index argument for (let i = 0, items = [1,2,3], len = items.length; i < len; i++) { items[i] * i; };
# loops are expressions var list = [1,2,3,4,5] var doubles = for num in [1,2,3] num * 2// loops are expressions var list = [1,2,3,4,5]; let res = []; for (let i = 0, items = [1,2,3], len = items.length; i < len; i++) { res.push(items[i] * 2); }; var doubles = res;
# go through every other element for num in [1,2,3] by 2 num * 2// go through every other element for (let i = 0, items = [1,2,3], len = items.length; i < len; i = i + 2) { items[i] * 2; };
# filter by condition for num in list when num > 1 numfunction iter$(a){ return a ? (a.toArray ? a.toArray() : a) : []; }; // filter by condition for (let i = 0, items = iter$(this.list()), len = items.length, num; i < len; i++) { num = items[i]; if (num <= 1) { continue; }; num; };

Tip! Any type of object can support being iterated with forin in Imba. If the compiler does not know that the target is an array (at compile-time) it will look for (and call) target.toArray if it exists, and then loop through this.

For of

# loop over all keys of object var object = {a: 1, b: 2, c: 3, d: 4} for k,value of object value == 2// loop over all keys of object var object = {a: 1,b: 2,c: 3,d: 4}; for (let k in object){ let value; value = object[k];value == 2; };
# loop over own keys of object var obj = Object.create({a: 1, b: 1, c: 1}) obj:b = obj:d = 2 for own key,value of obj "{key} is {value}"// loop over own keys of object var obj = Object.create({a: 1,b: 1,c: 1}); obj.b = obj.d = 2; for (let value, i = 0, keys = Object.keys(obj), l = keys.length, key; i < l; i++){ key = keys[i];value = obj[key];("" + key + " is " + value); };

Continue

var res = for num in [1,2,3,4,5] continue if num == 3 num * 2 # continue without arguments # res => [2,4,8,10]let res1 = []; for (let i = 0, items = [1,2,3,4,5], len = items.length, num; i < len; i++) { num = items[i]; if (num == 3) { continue; }; res1.push(num * 2); }; var res = res1; // continue without arguments // res => [2,4,8,10]
var res = for num in [1,2,3,4,5] continue -1 if num == 3 num * 2 # continue with an argument # acts like early return within map # res => [2,4,-1,8,10]let res1 = []; for (let i = 0, items = [1,2,3,4,5], len = items.length, num; i < len; i++) { num = items[i]; if (num == 3) { res1.push(-1);continue; }; res1.push(num * 2); // continue with an argument // acts like early return within map // res => [2,4,-1,8,10] }; var res = res1;

Break

# break without argument var res = for num in [1,2,3,4,5] break if num == 3 num * 2 res == [2,4]// break without argument let res1 = []; for (let i = 0, items = [1,2,3,4,5], len = items.length, num; i < len; i++) { num = items[i]; if (num == 3) { break; }; res1.push(num * 2); }; var res = res1; res == [2,4];
# break with argument var res = for num in [1,2,3,4,5] break -1 if num == 3 num * 2 res == [2,4,-1]// break with argument let res1 = []; for (let i = 0, items = [1,2,3,4,5], len = items.length, num; i < len; i++) { num = items[i]; if (num == 3) { res1.push(-1);break; }; res1.push(num * 2); }; var res = res1; res == [2,4,-1];