Files
modeling-app/docs/kcl/map.md
Nick Cameron c050739f41 Some improvements to the boxed signatures in the docs (#6593)
* Show a more reasonable name in function docs

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

* Fix buggy docs for union types

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

* Make types in the docs signatures into links

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

---------

Signed-off-by: Nick Cameron <nrc@ncameron.org>
2025-04-30 16:03:22 +00:00

85 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](/docs/kcl/types/KclValue)]](/docs/kcl/types/[KclValue](/docs/kcl/types/KclValue)),
  f: FunctionSource,
): [[[KclValue](/docs/kcl/types/KclValue)]](/docs/kcl/types/[KclValue](/docs/kcl/types/KclValue))

Arguments

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

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], 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