我正在阅读Eloquent Javascript,并且我对这个部分函数示例有点困惑。请帮忙解释一下 [英] I'm reading Eloquent Javascript and I am a little confused by this partial function example. Please help explain

查看:131
本文介绍了我正在阅读Eloquent Javascript,并且我对这个部分函数示例有点困惑。请帮忙解释一下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 函数asArray(quasiArray,start){
var result = []; (var i =(start || 0); i< quasiArray.length; i ++)
result.push(quasiArray [i]);
返回结果;
}

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


函数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));

为什么函数不能直接返回:

  func1(func2); 

并给出适当的输出。我认为存储在变量isUndefined中的部分函数已经返回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;}
/ *依此类推* /
};


解决方案

partial 和 compose 高级函数



isUndefined 将会返回一个函数,当被调用时,将调用最初传递的函数与原始参数加上调用时传递的任何新参数。



要回答您的问题,您将调用在从 partial 返回的函数上应用,然后在函数上调用 apply >最初传递给部分



您希望撰写到返回一个被调用的函数,将返回调用传递第二个函数作为参数的第一个函数的结果(第二个函数传递传递给 compose 调用的参数) 。如果 compose 返回了 func1(func2),那么您可以将调用结果赋给变量 isDefined



编辑:

现在我们有 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 / *当isUndefined被调用时它将变成'a'* /(argumentForisUndefined / *当isUndefined被调用时变成'b'* /) ; 

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函数变成'a'传递给第一个函数* /
函数(b,c){
return b === c;
},
undefined / * undefined变成'b '传递给部分* /


)(argumentForisDefined / * argumentForisDefined变成'c'传递给partial * /);

如果我们看看迄今为止的内容并代之以可读性,可以归结为一个函数,一个参数并将其与undefined进行比较,否定结果并返回一个布尔值b
$ b

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


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 can't the function compose simply return:

func1(func2);

and give the proper output. I thought the partial function which is stored in the variable isUndefined 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 */
};

解决方案

Both partial and compose are higher-order functions.

isUndefined will return a function that, when invoked, will invoke the originally passed function with the original arguments plus any new arguments passed at invocation.

To answer your question, you'd be calling apply on the function returned from partial which will in turn, call apply on the function originally passed to partial.

You want compose to return a function that when called, will return the result of calling the first function passed the second function as an argument (with the second function passed the arguments passed to the compose invocation). If compose returned func1(func2), then you'd assign the result of the invocation to the variable isDefined.

EDIT:

Now that we have op, let's try to decompose this:

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

this is equivalent to

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

isUndefined is assigned a function that, when called, will call the function passed as the first argument to partial, passing in undefined as the first argument to that function call, followed by the arguments passed to the function isUndefined i.e.

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 composes isUndefined with another function that negates the result of isUndefined.

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

is equivalent to

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

which is equivalent to (renamed variables for clarity)

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 */);

If we look at what we have so far and substituting for readability, boils down to a function that takes an argument and compares it to undefined, negates the result and returns a boolean

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

这篇关于我正在阅读Eloquent Javascript,并且我对这个部分函数示例有点困惑。请帮忙解释一下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆