Function composition on data pipelines and error handling.

We create a higher-order function that returns a new function while taking two series of arguments, during its first and second invocations.

//The arguments values are cached in the returned function
function translate2d(dx, dy) {

  return function (x, y){
    return [dx + x, dy + y]  
  }
}

const move2x = translate2d(2, 0);    //move2x() is the returned function 
const result = move2x(4, 8);         //result is the returned operation result
console.log( result )                //[6, 8]

This is called function composition, a process that combines functions to break down complex operations into smaller, more manageable pieces.

//Local variables can be set and used down the line
//Passed functions can be used with later invoked arguments
function memoize(f) {
  let preX, preY, preR
  
  return function (x, y) {
    if (preX === x && preY === y) {
      console.log("already casted")
      return preR
    }
    preX = x
    preY = y
    return preR = f(x, y)
  }
}

The returning functions can be invoked with new arguments, allowing for higher-order functions to be more modular across different invocations.

chevron-rightSpread operator arguments and function composition orderhashtag

We can use the spread operator on the array returned by a function, which allows us to use the destructured array elements as arguments of the composed function.

Both functions accept the same type of arguments and can perform different operations when their order is changed.

A function composition determines its evaluation order, based on its function (nesting), with the innermost function being applied first.

The argument passed to the composed function is used by its first function, and then each successive function receives the previous output as its argument, enabling pipeline data processing.

Functions execution order on reduce() function compositions.

The reduce() method accumulates values from an array, iterating between its elements.

We can combine multiple functions into a single variadic arrow-up-rightfunction composition, by destructuring their arguments as an array. The provided argument serves as the initial input, and each function's output becomes the input for the next one in the array.

In this function composition, each destructed function returns a new function rather than a value, returning a new function composition. The order of execution in the returned function composition will be inverted, from right-left (as in reduceRight) to top-bottom. Each function has access to the function input from the previous function and the arguments passed when the composition is invoked.

We compose reference functions, that will trigger once they start being reduced.

chevron-rightReturned function composition execution orderhashtag

The reduceRight() method composes functions from right to left, on an [primo, secondo] array, but the function composition returns the primo() function first.

The secondo function is triggered first, but it returns a function (...args), which is then called on the second function composition call.

The function composition being returned from the reduceRight() is.

Function composition on Error Handling

A function composition can include multiple error-handling blocks, each deconstructed function try/catch a specific error instance, and returns a safe output from the argument function.

The created error instances are used in the argument function, any thrown error will be handled by the next decostructed try/catch functions.

Each destructed function includes 2 arguments, the (type) error instance and the (fail) returned string. They then include the argument function (fn) and the composed function argument (...args).

If an error is thrown by the argument function, it will be passed to the rescue(), and, if no further error is thrown, no other try/catch function will be called.

The rescue function compares the returned error with the error instance from the destructed function, if the type doesn't match then it throws an error for the next try/catch function.

A try/catch function doesn't follow the standard top-bottom execution order. Instead, it skips to the first available function, and propagates any thrown errors to the outer try/catch blocks.

Last updated