函数式编程初探

admin2021-01-15  2.4K+

import console; 

// 简单的高阶函数
forEach = function(t,f) for(k,v in t) f(v,k)
times = function(n, f) for(i=1;n) f(i)
unless = function(p,f) if(!p) f()


var t = {1,2,3,4,5};
  
forEach(t, lambda(v) unless( 
	v%2, λ() console.log(v, " is even") 
))

times(10, lambda(n) unless(
	n%2, λ() console.log(n, " is even")
))

console.log("--------------------------------")

// 函数调试
tap = function(v){
	return lambda(f) unless ( 
		!(type(f)==="function")
		,λ() console.dumpJson (
			["参数值"] = v,
			["返回值"] = f(v) || "No return values!"
	))
}

forEach(t, lambda(v) tap(v)( math.sqrt ))

console.pause(true);
转载请注明原文地址: https://aardio.net/read-135.html
最新回复(0)
aardio问答
aardio编程语言