mirror of
https://github.com/bearlanguageorg/bear.git
synced 2026-08-26 16:47:01 +00:00
Adds floats, bitwise ops, UTF-8 strings with methods, try/catch, closures, generics validation, a compile-time type checker, a mark-and-sweep GC, source-level debug info, constant folding, and the repl/fmt/package-manager commands. 🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
36 lines
760 B
Plaintext
36 lines
760 B
Plaintext
// for i, v in arr — iterate with both index and value
|
|
|
|
fn main() {
|
|
let fruits = ["apple", "banana", "cherry"]
|
|
|
|
// basic indexed iteration
|
|
for i, v in fruits {
|
|
println("${i}: ${v}")
|
|
}
|
|
|
|
// use the index for computation
|
|
let nums = [10, 20, 30, 40, 50]
|
|
let sum = 0
|
|
for i, v in nums {
|
|
if i % 2 == 0 {
|
|
sum += v
|
|
}
|
|
}
|
|
println("sum of even-indexed elements: ${sum}")
|
|
|
|
// build a new array using index
|
|
let letters = ["a", "b", "c", "d"]
|
|
let tagged = []
|
|
for i, v in letters {
|
|
push(tagged, "${v}${i}")
|
|
}
|
|
println(tagged)
|
|
|
|
// plain for-in still works (no index)
|
|
for v in fruits {
|
|
print(v)
|
|
print(" ")
|
|
}
|
|
println("")
|
|
}
|