kcl

map

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

NameTypeDescriptionRequired
array[KclValue]Yes
map_fnFunctionParamYes

Returns

[KclValue]

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)

Rendered example of map 0

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 }, %)
})

Rendered example of map 1