在codewars上看到的一道函数式编程题要求写出0~9十个函数和加减乘除四个函数,最终实现以下功能seven(times(five()));//mustreturn35four(plus(nine()));//mustreturn13eight(minus(three()));//mustreturn5six(dividedBy(two()));//mustreturn3有点绕晕了,麻烦大能解惑下,不胜感激。
2 回答
回首忆惘然
TA贡献1847条经验 获得超11个赞
大概这个意思吧.functionone(fun){if(fun)returnfun(1);return1;}functiontwo(fun){if(fun)returnfun(2);return2;}functionplus(num){returnfunction(a){returna+num;}}one(plus(two()));
慕娘9325324
TA贡献1783条经验 获得超4个赞
对js不熟,应该是这样:functionpartial_op_with_oper2(op,oper2){returnfunction(oper1){returnop(oper1,oper2);};}functionmake_operand(value){returnfunction(oper2){if(oper2){returnoper2(value);}else{returnvalue;}};}//"Operators"vartimes=function(b){varop=function(a,b){returna*b;};returnpartial_op_with_oper2(op,b);};varplus=function(b){varop=function(a,b){returna+b;};returnpartial_op_with_oper2(op,b);};//"Operands"varzero=make_operand(0);varone=make_operand(1);vartwo=make_operand(2);varthree=make_operand(3);varfour=make_operand(4);varfive=make_operand(5);varsix=make_operand(6);varseven=make_operand(7);vareight=make_operand(8);varnine=make_operand(9);跟@brayden的一个意思
添加回答
举报
0/150
提交
取消