javascript - js 函数式编程问题
阿神
阿神 2017-04-10 14:21:40
0
7
777

在 codewars 上看到的一道函数式编程题
要求写出0~9十个函数和加减乘除四个函数,最终实现以下功能

seven(times(five())); // must return 35
four(plus(nine())); // must return 13
eight(minus(three())); // must return 5
six(pidedBy(two())); // must return 3

有点绕晕了,麻烦大能解惑下,不胜感激。

阿神
阿神

闭关修行中......

reply all(7)
小葫芦

大概这个意思吧.

function one(fun)
{
    if(fun) return fun(1); 
    return 1;
}

function two(fun)
{
    if(fun) return fun(2); 
    return 2;
}

function plus(num)
{
    return function(a){return a + num;}
}

one(plus(two()));
大家讲道理

对js不熟,应该是这样:

function partial_op_with_oper2(op, oper2) {
    return function(oper1) {
        return op(oper1, oper2);
    };
}

function make_operand(value) {
    return function(oper2) {
        if (oper2) {
            return oper2(value);
        } else {
            return value;
        }
    };
}

// "Operators"
var times = function(b) {
    var op = function(a, b) { return a * b; };
    return partial_op_with_oper2(op, b);
};

var plus = function(b) {
    var op = function(a, b) { return a + b; };
    return partial_op_with_oper2(op, b);
};

// "Operands"
var zero = make_operand(0);
var one = make_operand(1);
var two = make_operand(2);
var three = make_operand(3);
var four = make_operand(4);
var five = make_operand(5);
var six = make_operand(6);
var seven = make_operand(7);
var eight = make_operand(8);
var nine = make_operand(9);

跟 @brayden 的一个意思

左手右手慢动作

正确答案

var names = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'];

names.forEach(function (name, i) {
  window[name] = function (fn) {
    if (!fn) return i + 1;
    return fn(i + 1);
  }
})

function times(i) {
  return function (j) {
    return i*j;
  }
}

function plus(i) {
  return function (j) {
    return i+j;
  }
}

function minus(i) {
  return function (j) {
    return j-i;
  }
}

function pidedBy(i) {
  return function (j) {
    return j/i;
  }
}
小葫芦

感觉 @Theo 做的还不是特别彻底.

var createValue = function( v ) {
    return function( f ) {
        if ( typeof f === 'function' ) {
            return f( v );
        }
        else return v;
    };
};

var createAction = function( action ) {
    return function(m) {
        return function(n) {
            return action( n, m );
        };
    };
};

var one = createValue( 1 )
var two = createValue( 2 );
var plus = createAction(function( m, n ) {
    return m + n;
});
var minu = createAction(function( m, n ) {
    return m - n;
});
Peter_Zhu

十个数字函数,四个操作函数,思路最简单的话这样就行。。

function one(x){
    if(x == null){
        return 1;
    }else{
        return eval('1'+x);
    }
}
function two(x){
    if(x == null){
        return 2;
    }else{
        return eval('2'+x);
    }
}
function three(x){
    if(x == null){
        return 3;
    }else{
        return eval('3'+x);
    }
}
function four(x){
    if(x == null){
        return 4;
    }else{
        return eval('4'+x);
    }
}
function five(x){
    if(x == null){
        return 5;
    }else{
        return eval('5'+x);
    }
}
function six(x){
    if(x == null){
        return 6;
    }else{
        return eval('6'+x);
    }
}
function seven(x){
    if(x == null){
        return 7;
    }else{
        return eval('7'+x);
    }
}
function eight(x){
    if(x == null){
        return 8;
    }else{
        return eval('8'+x);
    }
}
function nine(x){
    if(x == null){
        return 9;
    }else{
        return eval('9'+x);
    }
}
function zero(x){
    if(x == null){
        return 0;
    }else{
        return eval('0'+x);
    }
}
function minus(x){
    return '-'+x;
}
function times(x){
    return '*'+x;
}
function plus(x){
    return '+'+x;
}
function pidedBy(x){
    return '/'+x;
}
伊谢尔伦

没有使用价值,不必纠结

左手右手慢动作
function _num(num,fn){
  if(typeof fn === 'function')return fn(num);
  return num;
}
function zero(fn) {return _num(0,fn);}
function one(fn) {return _num(1,fn);}
function two(fn) {return _num(2,fn);}
function three(fn) {return _num(3,fn);}
function four(fn) {return _num(4,fn); }
function five(fn) {return _num(5,fn);}
function six(fn) {return _num(6,fn);}
function seven(fn) {return _num(7,fn);}
function eight(fn) {return _num(8,fn);}
function nine(fn) {return _num(9,fn);}

function plus(num) {
  return function(a){
    return a + num;
  };
}
function minus(num) {
  return function(a){
    return a - num;
  };
}
function times(num) {
  return function(a){
    return a * num;
  };
}
function pidedBy(num) {
  return function(a){
      return a / num;
   };
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template