Skip to content

Commit 147de3f

Browse files
committed
feat: map() now passes index as first param
1 parent f76758b commit 147de3f

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

crates/neko-lang/src/interpreter.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,13 @@ impl Interpreter {
341341

342342
Ok(Value::List(
343343
list.iter()
344-
.map(|item| interpreter.call_function(transform, vec![item.clone()]))
344+
.enumerate()
345+
.map(|(index, item)| {
346+
interpreter.call_function(
347+
transform,
348+
vec![Value::Number(index as f64), item.clone()],
349+
)
350+
})
345351
.collect::<Result<Vec<Value>, RuntimeError>>()?,
346352
))
347353
});

examples/hello.neko

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1+
cat = {
2+
"name" = "momo",
3+
}
4+
5+
println(cat["name"])
6+
17
cats = ["momo", "taro"]
28

3-
for message in map(cats, fn(cat) => "Hello, #{cat}!")
9+
for message in map(cats, fn(_, cat) => "Hello, #{cat}!")
410
println(message)
511
end

0 commit comments

Comments
 (0)