I am reading Eloquent Javascript and I am a little confused by this partial function example. Please help explain

function asArray(quasiArray, start) {
  var result = [];
  for (var i = (start || 0); i < quasiArray.length; i++)
    result.push(quasiArray[i]);
  return result;
}

function partial(func) {
  var fixedArgs = asArray(arguments, 1);
  return function(){
    return func.apply(null, fixedArgs.concat(asArray(arguments)));
  };
}

function compose(func1, func2) {
  return function() {
    return func1(func2.apply(null, arguments));
  };
}

var isUndefined = partial(op["==="], undefined);
var isDefined = compose(op["!"], isUndefined);
show(isDefined(Math.PI));
show(isDefined(Math.PIE));

Why a function cannot just return:

func1(func2);

and give the right conclusion. I thought that the partial function that is stored in the isUndefined variable already returns func.apply (null, [fixed, arguments])

var op = {
"+": function(a, b){return a + b;},
"==": function(a, b){return a == b;},
"===": function(a, b){return a === b;},
"!": function(a){return !a;}
/* and so on */
};
+5
source share
2 answers

Both partialand functions of a higher order .compose

isUndefined will return a function that, when called, will call the original function passed with the original arguments, plus any new arguments passed during the call.

, apply , partial, , , apply , partial.

, compose , , ( , compose). compose func1(func2), isDefined.

EDIT:

, op, :

var isUndefined = partial(op["==="], undefined);

var isUndefined = partial(function(a, b){return a === b;}, undefined);

isUndefined , , , partial, undefined , , isUndefined ..

partial(function(a, b){return a === b;}, undefined /* this will become 'a' when isUndefined is invoked */)(argumentForisUndefined /* this will become 'b' when isUndefined is invoked */);

isDefined isUndefined , isUndefined.

var isDefined = compose(op["!"], isUndefined);

var isDefined = compose(function(a){return !a;}, isUndefined);

( )

var isDefined = compose(

    function(a){return !a;}, 

    partial(  /* partial function becomes 'a' passed to first function */
        function(b, c) {
            return b === c;
        }, 
        undefined /* undefined becomes 'b' passed to partial */
    ) 

)(argumentForisDefined /* argumentForisDefined becomes 'c' passed to partial */);

, , , , undefined,

var isDefined = function (b) { return !undefined === b; } 
+2

. , :

function compose(func1, func2) {
  return func1(func2.apply(null, arguments));
}

, ?

a = compose(function(){console.log(1)}, function(){console.log(2)});

- , 2, 1. a undefined, .

, , , , .

, , , a(), 2, 1.

0

All Articles