-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest5.js
71 lines (63 loc) · 1.51 KB
/
test5.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
67
68
69
70
71
var fn = ( arg ) => {
let outer = "Visible";
let innerFn = () => {
console.log( outer );
console.log( arg );
}
return innerFn
}
var closureFn = fn( 5 );
closureFn();
const tap = (value) =>
(fn) => (
typeof( fn ) === "function" && fn( value ),
console.log( value )
) ;
tap( "fun" )( ( it ) => console.log( "Value is ", it ) );
const forEach = ( array, fn ) => {
for (let i = 0; i < array.length; i++ ) {
fn( array[ i ] )
}
}
forEach( [ 1, 2, 3 ], (a) =>
tap(a) ( () => {
console.log(a)
}
)
)
const unary = ( fn ) =>
fn.length === 1
? fn
: ( arg ) => fn( arg )
console.log( [ 1, 2, 3 ].map( (a) => { return a * a }));
console.log( [ "1", "2", "3" ].map( parseInt));
console.log( [ "1", "2", "3" ].map( unary(parseInt) ));
const once = ( fn ) => {
let done = false;
return function () {
return done ? undefined : ( ( done = true ), fn.apply( this, arguments ) )
}
}
var doPayment = once( () => {
console.log( "Payment is done !" )
} )
doPayment();
doPayment();
var factorial = ( n ) => {
if ( n === 0 ) {
return 1;
}
return n * factorial( n-1 );
}
const memoized = ( fn ) => {
const lookupTable = {};
return ( arg ) => lookupTable[ arg ] || ( lookupTable[ arg ] = fn( arg ))
}
let fastFactorial = memoized( ( n ) => {
if ( n === 0 ) {
return 1;
}
return n * fastFactorial( n - 1 );
})
console.log( factorial( 5 ) );
console.log( fastFactorial( 5 ) );