编辑代码

const reduce = f => b => l => {
    let r = b
    for (let i of l) {
        r = f(r)(i)
    }
    return r
}
//console.log(reduce(x => y => x + y)(0)([1, 2, 3, 4]), "reduce")//reduce
const map = f => l => {
    return reduce(x => y => x.concat(f(y)))([])(l)
}
//console.log(map(x => x + 1)([1, 2, 3, 4]), "map")//map
const apply = f => x => {
    return f(x)
}
const timesapply = n => f => x => {
    return reduce(x => y => f(x))(x)(new Array(n).fill(null))
}
//console.log(timesapply(4)(x => x + 3.14)(0), "timesapply")//timesapply
const filter = f => l => {
    return reduce(x => y => f(y) ? x.concat(y) : x)([])(l)
}
//console.log(filter(x => x % 2 == 0)([1, 2, 3, 4, 5, 6, 7, 8, 9]), "filter")//filter
const reject = f => l => {
    return reduce(x => y => f(y) ? x : x.concat(y))([])(l)
}
//console.log(filter(x => (Math.floor(Math.sqrt(x))) == (Math.sqrt(x)))([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]), "reject")//reject
const maths = {
    add: x => y => x + y,
    sub: x => y => x - y,
    mul: x => y => x * y,
    div: x => y => x / y,
    pow: x => y => x ** y,
    mod: x => y => x % y,
    abs: x => x >= 0 ? x : -x,
    neg: x => -x,
    sign: x => x > 0 ? 1 : x === 0 ? 0 :x<0? -1:NaN,
    sin: Math.sin,
    cos: Math.cos,
    tan: Math.tan,
    sinh: Math.sinh,
    cosh: Math.cosh,
    tanh: Math.tanh,
    PI:Math.PI
}
const max = l => {
    return reduce(x => y => y > x ? y : x)(-Infinity)(l)
}
const min = l => {
    return reduce(x => y => y < x ? y : x)(Infinity)(l)
}
const intersection = a => b => {
    return reduce(x => y => a.includes(y) ? x.concat(y) : x)([])(b)
}
//console.log(intersection([1, 2, 3, 4, 5, 6, 7])([2, 4, 6, 8, 10, 12, 14]), "intersection")
const union = a => b => {
    return reduce(x => y => a.includes(y) ? x : x.concat(y))(a)(b)
}
//console.log(union([1, 2, 3, 4, 5, 6, 7])([2, 4, 6, 8, 10, 12, 14]), "union")
const complement = a => b => {
    return reduce(x => y => b.includes(y) ? x : x.concat(y))([])(a)
}
//console.log(complement([1, 2, 3, 4, 5, 6, 7])([2, 4, 6, 8, 10, 12, 14]), "complement")
const distinct = a => {
    return reduce(x => y => x.includes(y) ? x : x.concat(y))([])(a)
}
//console.log(distinct([1, 2, 5, 4, 1, 2, 6, 5, 4, 1, 2, 3, 6, 8, 7, 7, 8, 9, 6, 5, 4, 7, 4, 1, 2, 5]), "distinct")
const findif = f => l => {
    return reduce(x => y => f(y) || x[0] != -1 ? [y, x[1]] : [x[0], x[1] + 1])([-1, 0])(l)[1]
}
const find = a => l => {
    return reduce(x => y => y === a || x[0] != -1 ? [y, x[1]] : [x[0], x[1] + 1])([-1, 0])(l)[1]
}
//console.log(find(3)([1,3,5,1,3,5,5,1,3,5,4,8,9]),"find")//find
const reverse = l => {
    return reduce(x => y => [y].concat(x))([])(l)
}
//console.log(reverse([1,2,3,4,5,6]),"reverse")//reverse
const sort = f => l => {
    if (l.length < 2) return l
    for (let i = 0; i < l.length; i++) {
        for (j = 0; j < i; j++) {
            if (!f(l[j])(l[i])) {
                let t = l[j]
                l[j] = l[i]
                l[i] = t
            }
        }
    }
    return l
}
//console.log(sort(x=>y=>x<y)([1,5,2,1,4,2,3,5,4,1]),"sort")//sort
const pipe = (...fn) => arg => {
    return reduce(x => y => y(x))(arg)(fn)
}
//console.log(pipe(map(maths.mul(2)),map(maths.add(1)))([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]),"pipe")//pipe