Creates a function that applies multiple functions to the same iterable, returning an array of results. Uses memoization to ensure the iterable is only traversed once.
Type of elements in the iterable
Array of functions that take an iterable and return some value
One or more functions to apply to the iterable
A function that takes an iterable and returns an array of results from each function
// Calculate sum and product in a single passconst sumAndProduct = fork( values => [...values].reduce((a, b) => a + b, 0), values => [...values].reduce((a, b) => a * b, 1))const [sum, product] = sumAndProduct([1, 2, 3, 4])// Result: [10, 24] Copy
// Calculate sum and product in a single passconst sumAndProduct = fork( values => [...values].reduce((a, b) => a + b, 0), values => [...values].reduce((a, b) => a * b, 1))const [sum, product] = sumAndProduct([1, 2, 3, 4])// Result: [10, 24]
// Calculate multiple statisticsconst stats = fork( values => Math.min(...values), values => Math.max(...values), values => [...values].reduce((a, b) => a + b, 0) / [...values].length)const [min, max, avg] = stats([5, 10, 15, 20])// Result: [5, 20, 12.5] Copy
// Calculate multiple statisticsconst stats = fork( values => Math.min(...values), values => Math.max(...values), values => [...values].reduce((a, b) => a + b, 0) / [...values].length)const [min, max, avg] = stats([5, 10, 15, 20])// Result: [5, 20, 12.5]
Creates a function that applies multiple functions to the same iterable, returning an array of results. Uses memoization to ensure the iterable is only traversed once.