added iterators: any function that takes no arguments can be used in a for loop. Each iteration, it will be called once. If its value matches, the for loop will execute its block with that value. If the value doesn't match, the for loop will stop. This also required adding function types: fn((input11 input12 output1) (input21 input22 output2)). The type maps inputs to outputs, because the add function isn't add(int/float int/float) -> int/float, but rather add((int int int) (int float float) (float int float) (float float float)) (notice that add(int int) can't return float and any other add can't return ints).

This commit is contained in:
Dummi26
2023-04-19 15:40:05 +02:00
parent 2293e9365c
commit 2ac19dcd00
5 changed files with 223 additions and 13 deletions

21
iterators.mers Normal file
View File

@@ -0,0 +1,21 @@
fn map_string_to_int(list [string ...] func fn((string int))) {
// return a function that can be used as an iterator
() {
// this function will be called by the for loop
// whenever it wants to retrieve the next item in the collection.
// get the element
elem = &list.remove(0)
switch! elem {
// if the element was present, run the map function to convert it from a string to an int and return the result
[string] {
[func.run(elem.0)]
}
// if there are no more elements, return something that doesn't match.
[] []
}
}
}
for v ["1" "2" "5" "-10" ...].map_string_to_int((s string) s.parse_int().assume1("list contained strings that can't be parsed to an int!")) {
debug(v)
}