JsLIGO "for-of" loops can iterate through the contents of a
collection, that is, a list, a set or a map. This is done with a loop
of the form for (const <element var> of <collection var>) <block>.
Here is an example where the integers in a list are summed up.
functionsum_list(l : list<int>){
let total =0;
for(const i of l) total = total + i;
return total;
};
You can call the function sum_list defined above using the LIGO compiler
like so:
Loops over maps are actually loops over the bindings of the map.
Given a map from strings to integers, here is how to sum
all the integers and concatenate all the strings.
functionsum_map(m: map<string, int>){
let string_total =""
let int_total =0
for(const item of m){
let[key, value]= item;
string_total = string_total + key;
int_total = int_total + value
}
return[string_total, int_total]
}
You can call the function sum_map defined above using the LIGO compiler
like so: