有人能给我一个underscore.js _.memoize()的实例吗?
最好使用hashFunction,更好的是用coffeescript编写。
这里是一个稍微修改过的SICP中可爱的计数函数的coffeescript版本:
countChange = (amount)->
cc = (amount, kindsOfCoins)->
firstDenomination = (kindsOfCoins)->
switch kindsOfCoins
when 1 then 1
when 2 then 5
when 3 then 10
when 4 then 25
if amount is 0 then 1
else if amount < 0 or kindsOfCoins is 0 then 0
else
(cc amount, (kindsOfCoins - 1)) +
(cc (amount - firstDenomination(kindsOfCoins)), kindsOfCoins)
cc amount*100, 4
console.log "Ways to make change for $0.85: " + countChange(.85)
如何在示例中使用underscore的_.memoize()函数呢?非常感谢您提前的帮助!另外,请不要犹豫地指出函数编写方式中的问题,因为我对coffeescript非常陌生,希望能得到更多关于代码风格的建议。
memo
对象中键的内容,最好明确转换而不是依赖浏览器对[a,k].toString()
做出合理处理。 - mu is too short