Files
modeling-app/docs/kcl-std/functions/std-array-map.md
Nick Cameron a049768f1c Move some more functions to be declared in KCL (#6856)
* Move the leg functions to KCL

Signed-off-by: Nick Cameron <nrc@ncameron.org>

* Move array functions to KCL

Signed-off-by: Nick Cameron <nrc@ncameron.org>

* Move clone to KCL

Signed-off-by: Nick Cameron <nrc@ncameron.org>

* Add a function type

Signed-off-by: Nick Cameron <nrc@ncameron.org>

---------

Signed-off-by: Nick Cameron <nrc@ncameron.org>
2025-05-13 08:29:38 +12:00

85 KiB

title, subtitle, excerpt, layout
title subtitle excerpt layout
map Function in std::array Apply a function to every element of a list. manual

Apply a function to every element of a list.

map(
  @array: [any],
  f: Fn,
): [any]

Given a list like [a, b, c], and a function like f, returns [f(a), f(b), f(c)]

Arguments

Name Type Description Required
array [any] Input array. The output array is this input array, but every element has had the function f run on it. Yes
f Fn A function. The output array is just the input array, but f has been run on every item. Yes

Returns

[any]

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],
  f = drawCircle
)

Rendered example of map 0

r = 10 // radius
// Call `map`, using an anonymous function instead of a named one.
circles = map(
  [1..3],
  f = fn(@id) {
    return startSketchOn(XY)
      |> circle( center= [id * 2 * r, 0], radius= r)
  }
)

Rendered example of map 1