新农上路
- 积分
- 91
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2009-11-22
- 最后登录
- 1970-1-1
|
第二题不知为啥格式有问题。
// This is the text editor interface.
// Anything you type or change here will be seen by the other person in real time.
// memoization: (x, y) => x + y
// fs.readFile('foo.txt', function(text) {})
// var memoizedReadFile = asyncMemoize(fs.readFile)
// memoizedReadFile('foo.txt', function(text) {}) // hits disk, caches result, and calls callback
// memoizedReadFile('foo.txt', function(text) {}) // only returns cache
// memoizedReadFile('bar.txt', function(text) {}) // hits disk
/**
* fn: asyncrounous function where the last argument is a callback
* (a function taking one argument that is a result)
*
* Given a function, return another function that when it is called
* with the same parameters (sans callback) of a previous invocation,
* does not call the original `fn` but "immediately" callbacks with
* with the cached result.
*/
function asyncMemoize(fn) {
}
var keyObject={
String funName;
ArrayList<Object> params;
}
// test cases
var greetingCalled = 0
var happyBirthdayGreet = function(person, age, cb) {
greetingCalled++
setTimeout(() => cb(`Happy ${age}${{1:'st', 2:'nd'}[age % 10] || 'th'} Birthday ${person}!`), 800)
}
var bd = asyncMemoize(happyBirthdayGreet)
bd('Bob', 24, function(msg) {
console.log(`got "${msg}", happyBirthdayGreet should have been called once and was called ${greetingCalled} time(s)`)
bd('Bob', 24, function(msg) {
console.log(`got "${msg}", happyBirthdayGreet should have been called once and was called ${greetingCalled} time(s)`)
bd('Anne', 31, function(msg) {
console.log(`got "${msg}", happyBirthdayGreet should have been called twice and was called ${greetingCalled} time(s)`)
})
})
})%
|
|