christopher clark
christopher clark

Reputation: 2103

Currying makes the base function not work?

Am I doing this incorrectly, or is this a problem with currying in general?

Take this example in Javascript.

let addNormal = (x,y) => x+y;
let addCurry = x => y => x+y;

let increment = addCurry(1);

/// Below returns 4.
alert(increment(3));

/// Below returns 4.
alert(addNormal(1,3));

/// Below returns y => x+y
alert(addCurry(1,3));

I mean functional programming seems great, but this seems like an anti-pattern if the base function doesn't work as intended. The example from above was taken from a hackernoon blog. Partial Application of Functions

Upvotes: 0

Views: 67

Answers (1)

wisn
wisn

Reputation: 1024

You called the addCurry wrong.

Call addCurry(1)(3) instead of addCurry(1,3). Why? Because

addCurry = x => y => x + y;

is the same as

function addCurry(x) {
  return function(y) {
    return x + y;
  }
}

let addNormal = (x,y) => x+y;
let addCurry = x => y => x+y;

let increment = addCurry(1);

/// Below returns 4.
alert(increment(3));

/// Below returns 4.
alert(addNormal(1,3));

/// Below returns y => x+y
alert(addCurry(1)(3));

Upvotes: 3

Related Questions