ricardoboss / STEP

The STEP programming language
http://step-lang.dev/
MIT License
5 stars 1 forks source link

`foreach` loop #96

Closed ricardoboss closed 11 months ago

ricardoboss commented 11 months ago

Working with certain data types can be cumbersome if the intention is to simply iterate over a list of values.

Currently, to iterate over a list of values or a map, you would need to use a while loop:

list a = [1, 2, 3]
number i = 0
while (i < length(a)) {
    println(a[i])
}

Since iterating over a list is a common use case, I propose to add a foreach statement, that automatically loops over a lists/maps values.

The design could look like that:

list a = [1, 2, 3]
foreach (number value in a) {
    println(value)
}

and for maps with key-value-pairs:

map b = {"a": 1, "b": 2}
foreach (string key: number value in b) {
    println(key, " -> ", value)
}