updates updates updates updates updates updates better updates array of updates updates updates updates Signed-off-by: Jess Frazelle <github@jessfraz.com>
48 lines
38 KiB
Markdown
48 lines
38 KiB
Markdown
---
|
|
title: "arrayReduce"
|
|
excerpt: "Take a starting value. Then, for each element of an array, calculate the next value,"
|
|
layout: manual
|
|
---
|
|
|
|
Take a starting value. Then, for each element of an array, calculate the next value,
|
|
|
|
using the previous value and the element.
|
|
|
|
```js
|
|
arrayReduce(array: [u64], start: Sketch, reduce_fn: FunctionParam) -> Sketch
|
|
```
|
|
|
|
|
|
### Arguments
|
|
|
|
| Name | Type | Description | Required |
|
|
|----------|------|-------------|----------|
|
|
| `array` | `[u64]` | | Yes |
|
|
| `start` | [`Sketch`](/docs/kcl/types/Sketch) | A sketch is a collection of paths. | Yes |
|
|
| `reduce_fn` | `FunctionParam` | | Yes |
|
|
|
|
### Returns
|
|
|
|
[`Sketch`](/docs/kcl/types/Sketch) - A sketch is a collection of paths.
|
|
|
|
|
|
### Examples
|
|
|
|
```js
|
|
fn decagon = (radius) => {
|
|
let step = 1 / 10 * tau()
|
|
let sketch001 = startSketchAt([cos(0) * radius, sin(0) * radius])
|
|
return arrayReduce([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], sketch001, (i, sg) => {
|
|
let x = cos(step * i) * radius
|
|
let y = sin(step * i) * radius
|
|
return lineTo([x, y], sg)
|
|
})
|
|
}
|
|
decagon(5.0)
|
|
|> close(%)
|
|
```
|
|
|
|

|
|
|
|
|