函数式编程

admin2021-01-15  1.1K+

import console;
  
_meta = {
    _get = {
        add = lambda(b){ _v = owner._v + b; @_meta }
        sub = lambda(b){ _v = owner._v - b; @_meta }
        mul = lambda(b){ _v = owner._v * b; @_meta }
        div = lambda(b){ _v = owner._v / b; @_meta }
        equ = lambda() owner._v
    }
}
 
// 链式调用
var num = lambda(a){ _v = a; @_meta }
var value = num(7).add(9).div(4).mul(9).sub(6).equ()
 
console.log( value )
 
/*
 Currying:
 把一个函数的多个参数分解成多个函数,
 然后把函数多层封装起来,
 每层函数都返回一个函数去接收下一个参数。
*/
 
var sum = lambda(x) lambda(y) lambda(z) x + y + z;
var value = sum(8)(5)(7)
 
console.log( value )
 
// 高阶函数
// 仿string.reduce 写个 table.reduce 
table.reduce = function(t, f,...){
    if( type(f) !="table" ) f = {f;...};
    return reduce(f, lambda(t, f) f(t), t);
};
 
// 计算数组中正数的平均值
var nums = {2, -5, 9, 7, -2, 5, 3, 1, 0, -3, 8}
 
var average = table.reduce(nums, {
    lambda(t) table.filter(t, lambda(x) x>0);
    lambda(t) reduce(t, lambda(x, y) x+y) / #t
});
  
console.log( average )
 
// 找出偶数乘以3
var nums = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
 
var t = table.reduce(nums, {
    lambda(t) table.filter(t, lambda(x) x%2 == 0);
    lambda(t) table.map(t, lambda(x) x*3)
})
 
console.varDump( t )
console.pause(true);
转载请注明原文地址: https://seopai.com/read-130.html
最新回复(0)
源码分享
aardio源码分享