FeatureScript makeArray with index value

Rootentity
Rootentity Member Posts: 21 ✭✭
I would like a clean way to fill an array on creation with reference to the current cell being filled without using a for loop to do it.

Example:
var myValue = 10;
const myArray = makeArray(10, myValue * i);

with the result being [0,10,20,30,40,50,60,70,80,90]
Tagged:

Comments

  • kevin_o_toole_1
    kevin_o_toole_1 Onshape Employees, Developers, HDM Posts: 565 image
    edited April 2017
    Your case is what the math module's range functions are designed for (the function is optimized to be faster than doing it with a loop yourself).

    If you need numbers that aren't linear, you can call mapArray on a linear range. e.g.
    const myArray = mapArray(range(0, 10), function(i) { return cos(i * 30 * degree) });

    The non-linear case is probably slower than a simple for loop – but profiling would say for sure.