DEV Community

loading...

Discussion on: Daily Challenge #123 - Curry me Softly

Collapse
craigmc08 profile image
Craig McIlwrath

This really doesn't seem like currying. It seems counter-productive to even call it that because it seems to go exactly against functional programming's principles by relying on mutability.

Anyways, here's my JavaScript solution for this.

function CurryIt(f) {
  let currentArgs = [];
  const curriedF = (...as) => {
    if (as.length === 0) {
      const v = f(...currentArgs);
      currentArgs = [];
      return v;
    } else {
      currentArgs = currentArgs.concat(as);
      return curriedF;
    }
  };

  return curriedF;
}