Loops/Wrong ranges: Difference between revisions

Content added Content deleted
Line 796: Line 796:
You don't always have to convert a range to a series explicitly. Since 0.6.15, the functions map(), fold(), foldfrom(), and zip() accept ranges in place of arrays.
You don't always have to convert a range to a series explicitly. Since 0.6.15, the functions map(), fold(), foldfrom(), and zip() accept ranges in place of arrays.


In the following examples, we could have just started with an array of arrays, but following the Python example, we process the .data string into a table.
In the following example, we could have just started with an array of arrays, but following the Python example, we process the .data string into a table.


{{trans|Python}}
{{trans|Python}}
{{works with|langur|0.6.15}}

<lang langur>val .data = q:block END
start stop increment comment
-2 2 1 Normal
-2 2 0 Zero increment
-2 2 -1 Increments away from stop value
-2 2 10 First increment is beyond stop value
2 -2 1 Start more than stop: positive increment
2 2 1 Start equal stop: positive increment
2 2 -1 Start equal stop: negative increment
2 2 0 Start equal stop: zero increment
0 0 0 Start equal stop equal zero: zero increment
END

var .table = submatches(RE/([^ ]+) +([^ ]+) +([^ ]+) +(.+)\n?/, .data)
for .i in 2..len(.table) {
.table[.i] = map(f { if(.i2==4: .s2; toNumber(.s2)) }, 1..4, .table[.i])
}

for .test in rest(.table) {
var (.start, .stop, .inc, .comment) = .test
{
val .series = series(.start to .stop, .inc)
catch {
writeln $"\.comment;\nERROR: \.err["msg"]:L200(...);\n"
} else {
writeln $"\.comment;\nresult: \.series;\n"
}
}
}</lang>

{{works with|langur|0.7.1}}
{{works with|langur|0.7.1}}
Langur 0.7.0 changed the implicit exception variable from .err to _err, and 0.7.1 allows you to map to multiple functions.
Langur 0.7.0 changed the implicit exception variable from .err to _err, and 0.7.1 allows you to map to multiple functions.