This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const event1 = 1; | |
const event2 = 2; | |
function trace(event, keypress = () => []) { | |
return function trace() { | |
return [...keypress(), event]; | |
} | |
} | |
let keypress = trace(event1); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function throttle(fn, delay, cxt) { | |
var start; | |
var timer = null; | |
cxt = cxt || this; | |
delay = delay || 100; | |
return function() { | |
var current = Date.now(); | |
clearTimeout(timer); | |
if (!start) { | |
start = current; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
functino mergeSort(array) { | |
if (array.length === 1) { | |
return array; | |
} | |
var middle = Math.floor(array.length / 2); | |
var leftArray = array.slice(0, middle); | |
var rightArray = array.slice(middle); | |
return merge(mergeSort(leftArray), mergeSort(rightArray)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function compose(fn) { | |
var fn = [].slice.call(arguments); | |
return function(value) { | |
while(fn.length > 0) { | |
value = fn.pop()(value); | |
} | |
return value; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function compose(fn) { | |
var fnArray = [].slice.call(arguments); | |
return function(value) { | |
return fnArray.reverse().reduce(function(result, fn) { | |
return fn(result) | |
}, value); | |
}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function compose(fn) { | |
var fnArray = [].slice.call(arguments); | |
return fnArray.reverse().reduce(function(fgr, fn) { | |
// 因为传入的是一个函数,最终输出一个函数 fgr =====> function | |
return function() { | |
var args = [].slice.call(arguments); | |
return fn(fgr.apply(null, args)); | |
}; | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function compose(fn) { | |
// 传入的函数大于等于2 | |
var fnArray = [].slice.call(arguments).reverse(); | |
var fn1 = fnArray[0]; | |
var fn2 = fnArray[1]; | |
if (fnArray.length > 2) { | |
var restArray = fnArray.slice(2); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function reverseArgs(fn) { | |
return function reverse(...args) { | |
return fn(...args.reverse()); | |
} | |
} | |
function partial(fn, ...preArgs) { | |
return function partialApplied(...laterArgs) { | |
return fn(...preArgs, ...laterArgs); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function curry(fn, arity = fn.length) { | |
return (function nextCurried(prevArgs) { | |
return function curried(...nextArgs) { | |
var args = prevArgs.concat(nextArgs); | |
if (args.length >= arity) { | |
return fn(...args); | |
} | |
return nextCurried(args); | |
} | |
})([]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 原始方法 | |
function compose(...fns) { | |
return function composed(result) { | |
var list = fns.slice(); | |
while(list.length > 0) { | |
result = list.pop()(result); | |
} | |
return result; | |
} | |
} |
OlderNewer