For Enumerate
for_enumerate.nr34 lines
for_enumerate.nr· neuro
// `.enumerate()` in a `for` head: iterate a sequence and its position at once.
//
// The head binds a pair `(index, value)`. The index is a `u64` counting from
// zero (a POSITION, not a value the sequence holds), which is what lets it
// index the very sequence it walks. A range makes the distinction visible: the
// positions of `10..14` are still 0, 1, 2, 3.
func label_scores(scores: [i32; 4]) -> i32 {
mut weighted: i32 = 0
for (place, score) in scores.enumerate() {
println("place {place}: {score} (also readable as scores[{place}] = {scores[place]})")
weighted = weighted + (place as i32) * score
}
return weighted
}
func main() -> i32 {
val scores: [i32; 4] = [5, 4, 3, 2]
val weighted = label_scores(scores)
println("weighted total = {weighted}")
// A range needs parentheses: `..` binds looser than a method call, so
// `10..14.enumerate()` would enumerate the bound rather than the range.
mut offsets: i32 = 0
for (step, value) in (10..14).enumerate() {
println("step {step} reaches {value}")
offsets = offsets + (value - (step as i32))
}
println("value minus position, summed = {offsets}")
return weighted + offsets
}
neurc run for_enumerate.nr
place 0: 5 (also readable as scores[0] = 5)
place 1: 4 (also readable as scores[1] = 4)
place 2: 3 (also readable as scores[2] = 3)
place 3: 2 (also readable as scores[3] = 2)
weighted total = 16
step 0 reaches 10
step 1 reaches 11
step 2 reaches 12
step 3 reaches 13
value minus position, summed = 40