Closes #4021 Allows array ranges (e.g., `[0..10]`) to take expression instead of just numeric literals as their start and end values. Both expressions are required (we don't support `[0..]`, etc.). I've created a new kind of expression in the AST. The alternative was to represent the internals of an array as some kind of pattern which could initially be fully explicit or ranges. I figured the chosen version was simpler and easier to extend to open ranges, whereas the latter would be easier to extend to mixed ranges or other patterns. I chose simpler, it'll be easy enough to refactor if necessary. Parsing is tested implicitly by the tests of execution and unparsing. --------- Signed-off-by: Nick Cameron <nrc@ncameron.org> Co-authored-by: Adam Chalmers <adam.chalmers@zoo.dev>
91 KiB
91 KiB
title, excerpt, layout
title | excerpt | layout |
---|---|---|
map | Apply a function to every element of a list. | manual |
Apply a function to every element of a list.
Given a list like [a, b, c]
, and a function like f
, returns [f(a), f(b), f(c)]
map(array: [KclValue], map_fn: FunctionParam) -> [KclValue]
Arguments
Name | Type | Description | Required |
---|---|---|---|
array |
[KclValue] |
Yes | |
map_fn |
FunctionParam |
Yes |
Returns
Examples
r = 10 // radius
fn drawCircle = (id) => {
return startSketchOn("XY")
|> circle({ center: [id * 2 * r, 0], radius: r }, %)
}
// Call `drawCircle`, passing in each element of the array.
// The outputs from each `drawCircle` form a new array,
// which is the return value from `map`.
circles = map([1..3], drawCircle)
r = 10 // radius
// Call `map`, using an anonymous function instead of a named one.
circles = map([1..3], (id) => {
return startSketchOn("XY")
|> circle({ center: [id * 2 * r, 0], radius: r }, %)
})