Comparing version 0.0.4 to 0.0.5
@@ -37,2 +37,12 @@ // pasta.js | ||
function memo (fn, cache) { | ||
cache = memoed._cache = cache || {} | ||
function memoed () { | ||
var args = JSON.stringify(p.slice(arguments)) | ||
if (!(args in cache)) cache[args] = fn.apply(this, arguments) | ||
return cache[args] | ||
} | ||
return memoed | ||
} | ||
var numOfArgs = | ||
@@ -47,3 +57,3 @@ { '1': function (fn) { return function (a) { return fn(a) } } | ||
return function () { | ||
return fn.apply(null, p.slice(arguments).slice(0, num)) | ||
return fn.apply(null, p.slice(arguments, 0, num)) | ||
} | ||
@@ -127,2 +137,4 @@ } | ||
, all: all | ||
, memo: memo | ||
, memoize: memo | ||
} | ||
@@ -129,0 +141,0 @@ |
{ "name": "fn-pasta" | ||
, "version": "0.0.4" | ||
, "version": "0.0.5" | ||
, "author": "Nick Niemeir <nick.niemeir@gmail.com> (http://nrn.io)" | ||
@@ -4,0 +4,0 @@ , "main": "fn-pasta.js" |
@@ -38,2 +38,9 @@ Functional Pasta | ||
memo(fn[, cache]) | ||
----------------- | ||
Return a memoized version of fn, with \_cache attached. Can provide | ||
an optional starting cache (be careful, can be used to provide incorrect | ||
answers to function calls). Cache keys are the stringified array of arguments. | ||
op(operator) | ||
@@ -40,0 +47,0 @@ ------------ |
@@ -49,7 +49,21 @@ var p = require('../fn-pasta')() | ||
t.equal(p.curry(add)(1)(1), 2, 'Curry default') | ||
t.equal(p.curry(sum, 5)(1)(2)(3)(4)(5), 15, 'Curry 5 times') | ||
var called = 0 | ||
var memoAdd = p.memo(function (a, b) { | ||
called++ | ||
t.same(called, 1, 'Only called once') | ||
return add(a, b) | ||
}) | ||
var memoSum = p.memo(sum, { '["broken",2]': 'Surprise!' }) | ||
t.same(memoAdd(1, 2), 3, 'First memo call') | ||
t.same(memoAdd(1, 2), 3, 'Second memo call') | ||
t.same(memoAdd(1, 2), 3, 'Third memo call') | ||
t.same(memoSum('broken', 2), 'Surprise!', 'Provided cache object') | ||
t.same(memoSum(1, 1), 2, 'Avoid cache object') | ||
t.same(memoSum._cache, { '["broken",2]': 'Surprise!', '[1,1]': 2 }, 'Check cache') | ||
t.end() | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8903
180
66