-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurry.js
36 lines (33 loc) · 826 Bytes
/
curry.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
// 《函数式编程指北》练习
// usage
var _ = require('ramda')
var words = _.split(' ')
var sentences = _.map(words)
//
var filterQs = _.filter(_.match(/q/i))
// a max curry function
var _keepHighest = function (x, y) {
return x >= y ? x : y
}
var max = _.reduce(_keepHighest, -Infinity)
// test
// var str = "it's a sentense."
// var strArr = [str, 'd c g', 'str tt d']
// console.log(words(str))
// console.log(sentences(strArr))
// console.log(max([35, 89, 57, 534, 354]))
// curry function
function curry(func, ...args) {
if (func.length <= args.length) {
return func(...args)
}
return function (...otherArgs) {
return curry(func, ...args, ...otherArgs)
}
}
// test curry
function min(a, b, c, d, e) {
return Math.min(a, b, c, d, e)
}
var curryMin = curry(min, 4, 56)
console.log(curryMin(32)(2)(34))