Function replace

  • A convenience function to streamline some map operations. Applies transform to those elements which satisfy condition while leaving other elements unchanged.

    Example

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

    const negativeToPositive = A.replace(N.isNegative, Math.abs);
    negativeToPositive([-1, 2, -3, 4, -5]); // [1, 2, 3, 4, 5]

    const zeroToOne = A.replace(L.equals(0), always(1));
    zeroToOne([0, 3, 4, 0]); // [1, 3, 4, 1]

    Type Parameters

    • T

    • U

    Parameters

    • condition: ((x: T) => boolean)
        • (x: T): boolean
        • Parameters

          • x: T

          Returns boolean

    • transform: ((x: T) => U)
        • (x: T): U
        • Parameters

          • x: T

          Returns U

    Returns ((arr: readonly T[]) => (T | U)[])

      • (arr: readonly T[]): (T | U)[]
      • Parameters

        • arr: readonly T[]

        Returns (T | U)[]