Function createWith

  • Creates a new array with length elements. Element values are determined by a function f of the element index. Returns null for invalid lengths (non-integer or negative). createWith also treats a length greater than maxLength as invalid, to prevent errors that could halt the interface, use excessive memory, etc. If you truly intend to create a very long array, you may set maxLength to a value higher than the default of 10000 to accommodate this need.

    Remarks

    Note that as an array creation function, createWith is one of the few functions in this module that neither takes an array as an argument nor returns such a function.

    Example

    import * as A from "flurp/array";
    import * as L from "flurp/logic";

    A.createWith(4, L.always(5)); // [5, 5, 5, 5]
    A.createWith(3, L.identity) // [0, 1, 2]
    A.createWith(5, i => (i + 1) * 5); // [5, 10, 15, 20, 25]
    A.createWith(100000, L.identity); // null
    A.createWith(100001, L.identity, 200000); // [0, 1, 2, 3, ..., 99999, 100000]

    Type Parameters

    • T

    Parameters

    • length: number
    • f: ((x: number) => T)
        • (x: number): T
        • Parameters

          • x: number

          Returns T

    • maxLength: number = 10000

    Returns null | any[]