【发布时间】:2012-04-27 10:25:25
【问题描述】:
谁能给我一个 underscore.js _.memoize() 的例子吗?
最好使用hashFunction,甚至最好用coffeescript?
这里是来自咖啡脚本中 SICP 的可爱变化计数功能的略微修改版本:
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)
例如,我如何使用下划线的 _.memoize() ?
非常感谢!
ps .. 另外,请不要犹豫,在函数编码方式上留下漏洞。我对 coffeescript 很陌生,也欢迎任何关于使该代码更惯用的帮助。
【问题讨论】:
标签: javascript coffeescript underscore.js sicp memoization