1.0.3 • Published 4 years ago

babel-plugin-curried-functions v1.0.3

Weekly downloads
2
License
ISC
Repository
github
Last release
4 years ago

babel-plugin-curried-functions

Note: This is an experimental plugin made as part of my master thesis with the intent of getting to know the Babel-compiler and its extensibility.

This plugin adds the reserved keyword curry which can prefix a function definition in order to make it curried. For the sake of simplicity currying arrow-functions is not supported.

Syntax

In

curry function f(a, b, c) {
    ...
}

Out

function curry(fn) {
  const numParamsRequired = fn.length;

  function curryFactory(params) {
    return function (...args) {
      const newParams = params.concat(args);

      if (newParams.length >= numParamsRequired) {
        return fn(...newParams);
      }

      return curryFactory(newParams);
    }
  }

  return curryFactory([]);
}

const a = curry(function f(a, b, c) {
    ...
});