Skip to content
This repository has been archived by the owner on Aug 8, 2020. It is now read-only.

Latest commit

 

History

History
200 lines (153 loc) · 3.32 KB

loops-and-iteration.md

File metadata and controls

200 lines (153 loc) · 3.32 KB

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).

for in

tag app-root
		<self> 
			<ul> for item in ['Imba', 'Svelte', 'Javascript']
				<li> item

See on Scrimba

Keys of Object

# iterate through keys of object
for key,value of {a: 1, b: 2}
    console.log key + ": " + value * 2

# prints:
# a: 2
# b: 4

While Loop

while true
    break if Math.random < 0.1

🎮 Play with the code on Scrimba

Until Loop

let counter = 0
until Math.random() < 0.1
    counter++

🎮 Play with the code on Scrimba

Loop with Index Argument

# looping with index argument
for item, i in [1,2,3]
    console.log i + ": " + item * 1

# prints:
# 0: 1
# 1: 2
# 2: 3

Loop Expressions

# loops are expressions
var list = [1,2,3,4,5]
var doubles = for num in list
    num * 2
console.log doubles

# prints:
# [2,4,6,8,10]

Loop with Intervals

# go through every other element
for item in [1,2,3,4,5] by 2
    item * 2
    console.log item

# prints:
# 1
# 3
# 5

Filter by Condition

# filter by condition
let list = [1,2,3]
for num in list when num > 1
    console.log num
# prints:
# 2
# 3

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.toArrayif it exists, and then loop through this.

Loop Over Own Keys (for of)

The following example will render the names of the keys along with the values of the properties of each object.

let users = [
    id: 1
    name: "Eric"
    hobbies: [
        "tennis"
        " piano"
    ]
    ---
    id: 2
    name: "Joe"
    hobbies: [
        "none"
    ]
]
tag app-root
    def render
        <self>
            for user in users
                <div> 
                    <h3> user.name
                    <ul> for own key, item of user
                        <li> "{key}: {item}"

Play with the code on Scrimba

Example use of: for own key, value of object

Loop Over All Keys (for of)

# loop over all keys of object
var object = {a: 1, b: 2, c: 3, d: 4}
for k,value of object
    console.log "{k} 👉 " + value

# prints:
# a 👉 1
# b 👉 2
# c 👉 3
# d 👉 4

Continue

Continue without Arguments

var result = for num in [1,2,3,4,5]
    continue if num == 3
    num * 2
console.log result
# prints
# [2,4,8,10]

Continue with an Argument

var result = for num in [1,2,3,4,5]
    continue -1 if num == 3
    num * 2
console.log result
# acts like early return within map

# prints:
# [2,4,-1,8,10]

Break

Break without arguments

var result = for num in [1,2,3,4,5]
    break if num is 3
    num
console.log result

# prints:
# [1,2]

Break with Argument

var result = for num in [1,2,3,4,5]
    break 'done' if num is 3
    num
console.log result

# prints:
# [1,2, "done"]