Skip to content

Instantly share code, notes, and snippets.

@shuhrat
Created November 8, 2012 05:55
Show Gist options
  • Save shuhrat/4037080 to your computer and use it in GitHub Desktop.
Save shuhrat/4037080 to your computer and use it in GitHub Desktop.
var s = make(1)(2)(3)(4)(5);
var add = function (a, b) { return a + b; };
var mul = function (a, b) { return a * b; };
s(add); //15
s(mul); // 120
//Write make =)
@shuhrat
Copy link
Author

shuhrat commented Dec 8, 2012

    var make = (function() {
      var cache = [],
          cacheHandler,
          result, max;

      cacheHandler = function(value) {
        if (typeof value === 'function') {
          result = null;
          max = cache.length;
          if (max >= 2) {
            result = cache[--max];
            while(max--) {
              result = value(result, cache[max]);
            }
          }
          return result;
        } else {
          cache.push(value);
          return cacheHandler;
        }
      }

      return cacheHandler;
    }());

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment