forked from ramda/ramda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemoizeWith.js
66 lines (58 loc) · 1.82 KB
/
memoizeWith.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
var R = require('../source/index.js');
var eq = require('./shared/eq.js');
describe('memoizeWith', function() {
it('calculates the value for a given input only once', function() {
var ctr = 0;
var fib = R.memoizeWith(R.identity, function(n) {
ctr += 1;
return n < 2 ? n : fib(n - 2) + fib(n - 1);
});
var result = fib(10);
eq(result, 55);
eq(ctr, 11); // fib(0), fib(1), ... fib(10), no memoization would take 177 iterations.
});
it('handles multiple parameters', function() {
var f = R.memoizeWith(function(a, b, c) {
return a + b + c;
}, function(a, b, c) {return a + ', ' + b + c;});
eq(f('Hello', 'World' , '!'), 'Hello, World!');
eq(f('Goodbye', 'Cruel World' , '!!!'), 'Goodbye, Cruel World!!!');
eq(f('Hello', 'how are you' , '?'), 'Hello, how are you?');
eq(f('Hello', 'World' , '!'), 'Hello, World!');
});
it('does not rely on reported arity', function() {
var identity = R.memoizeWith(R.identity, function() { return arguments[0]; });
eq(identity('x'), 'x');
eq(identity('y'), 'y');
});
it('can be applied to nullary function', function() {
var count = 0;
var f = R.memoizeWith(R.identity, function() {
count += 1;
return 42;
});
eq(f(), 42);
eq(f(), 42);
eq(f(), 42);
eq(count, 1);
});
it('can be applied to function with optional arguments', function() {
var count = 0;
var f = R.memoizeWith(R.concat, function concat(a, b) {
count += 1;
switch (arguments.length) {
case 0: a = 'foo';
case 1: b = 'bar';
}
return a + b;
});
eq(f(), 'foobar');
eq(f(), 'foobar');
eq(f(), 'foobar');
eq(count, 1);
});
it('retains arity', function() {
var f = R.memoizeWith(R.concat, function(a, b) { return a + b; });
eq(f.length, 2);
});
});