h2non / hu

Small, generic functional helper library for node.js and browsers
MIT License
20 stars 3 forks source link

hu.curry #30

Closed rafinskipg closed 10 years ago

rafinskipg commented 10 years ago

Hi, maybe I missunderstood something, but I expected "curry" to work in other way

var hu = require('hu');
//HU CURRY SUM
var sum = hu.curry(function(a,b){
  return a+b;
})

var mas2 = sum(2);

console.log(mas2(40));
console.log(mas2(50));
console.log(mas2(10));

//REAL CURRIED SUM
var sum2 = function(a){
  return function(b){
      return a+b;
  }
}

var mas2_2 = sum2(2);

console.log(mas2_2(40));
console.log(mas2_2(50));
console.log(mas2_2(10));

And this is the result:

//HU
42
42
42
//REAL
42
52
12
h2non commented 10 years ago

Thanks for reporting the issue and adding the representative test cases for reproduce the trouble

You are right, there was an issue with the curry function.

The curried arguments are stored persistently (like using the memoization technique) in an array of arguments that shares the same scope in the subsequent call to the curried function. This causes that each curry() call creates a "new instance" of itself and cannot be used from other context in the case that the partial function be cached and reused with other arguments

In order to solve this issue, I done a complete refactor of the curry function that is a pure-function with continuous passing-style method to avoid object mutability and inner scope dependency

You can take a look to the new implementation here. Additionally, I added some more test cases to cover different use cases, you may take a look here

I just released a new version 0.1.0-rc.2 and its available from npm and bower, just re-provision your dependency

Thanks again for your time!

h2non commented 10 years ago

Closing issue!

rafinskipg commented 10 years ago

Awesome! Thanks!