mirror of
https://github.com/Dummi26/mers.git
synced 2025-03-10 14:13:52 +01:00
39 lines
1.1 KiB
Plaintext
Executable File
39 lines
1.1 KiB
Plaintext
Executable File
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] [elem] {
|
|
[func.run(elem)]
|
|
}
|
|
// 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)
|
|
}
|
|
|
|
|
|
// returns an iterator to iteratively compute square numbers
|
|
// using the fact that (n+1)^2 = n^2 + 2n + 1
|
|
fn square_numbers() {
|
|
i := 0
|
|
val := 0
|
|
() {
|
|
&val = val + { 2 * i } + 1
|
|
&i = i + 1
|
|
[val]
|
|
}
|
|
}
|
|
|
|
for n square_numbers() {
|
|
println(n.to_string())
|
|
n >= 100
|
|
} |