部分应用-雄辩的Javascript [英] Partial Application - Eloquent Javascript

查看:62
本文介绍了部分应用-雄辩的Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读Eloquent Javascript,并且很难理解下面的示例.任何人都可以逐行进行解释吗?具体来说,我对为什么第一个循环从一个循环开始以及为什么在已知的Args和参数上同时使用push方法感到困惑.我知道这与部分应用程序"有关,但是想对逐行发生的情况进行更详细的说明.

I am reading Eloquent Javascript and am having a difficult time understand the example below. Would anyone be able to do a line by line type explanation? Specifically, I'm confused as to why the first loop is starting at one, and why the push method is being used on both knownArgs and arguments. I know that this is related to "partial application", but would like a more detailed explanation of what exactly is happening line by line.

var op = {
 "+": function(a,b){return a + b;}
};

function partial(func) {
 var knownArgs = arguments;

 return function() {
  var realArgs = [];

  for (var i=1; i<knownArgs.length; i++)
   realArgs.push(knownArgs[i]);

  for (var i=0; i<arguments.length; i++)
   realArgs.push(arguments[i]);

  return func.apply(null, realArgs);
 };
}

map(partial(op["+"], 1), [0, 2, 4, 6, 8, 10]);

推荐答案

knownArgs变量保留arguments值的副本,就像调用partial()时一样.该调用返回了另一个函数,并且在代码arguments中是一个完全不同的列表—它们是传递给该返回函数的参数.换句话说:

The knownArgs variable keeps a copy of the value of arguments as it was when partial() was called. That call returns another function, and inside that code arguments are a whole different list — they're the arguments passed to that returned function. In other words:

var p = partial(someFunction,"hello","world");

var p = partial(someFunction, "hello", "world");

当调用p()时,knownArgs将分别为"hello"和"world"(也和someFunction相同,但请注意,第一个循环从1开始).如果对p()的调用看起来像这样:

When p() is called, knownArgs will be "hello" and "world" (well and someFunction too but note that the first loop starts at 1). If the call to p() looks like this:

p(如何",是",你");

p("how", "are", "you");

然后,它将首先将"hello"和"world"推入realArgs列表(来自knownArgs),然后将三个参数从arguments传递给p().

then it will first push "hello" and "world" onto the realArgs list (from knownArgs), and then the three parameters passed to p(), from arguments.

edit —逐步评估map(partial(op["+"], 1), [0, 2, 4, 6, 8, 10]);的评估方式:

edit — step-by-step breakdown of how map(partial(op["+"], 1), [0, 2, 4, 6, 8, 10]); is evaluated:

  1. 首先,必须评估op["+"].我猜它会返回一个函数,可能是这样的:

  1. First, op["+"] has to be evaluated. I'm guessing it returns a function, probably something like this:

function add(a, b) {
  return a + b;
}

  • 该"add"函数和值1传递给partial().因此,在partial()中,arguments伪数组看起来像

  • That "add" function and the value 1 are passed to partial(). Thus inside partial() the arguments pseudo-array looks like

    [ add, 1 ]
    

    也就是说,第一个参数是op["+"]中的"add"函数,第二个参数是该值1.在返回匿名函数之前,partial()真正唯一要做的就是将arguments保存到knownArgs中.之所以必须这样做,是因为在每次调用每个函数时,总是 分配一个奇怪的arguments伪变量.它被保留在这里,以便匿名函数中的代码以后可以访问它.

    That is, the first parameter is the "add" function from op["+"] and the second is that value 1. The only thing that partial() really does before returning the anonymous function is to save arguments into knownArgs. That has to be done because the weird arguments pseudo-variable is always assigned a new value upon each and every function call. It's being preserved here so that the code in the anonymous function can access it later.

    现在,有了从partial()返回的匿名函数和偶数数组,我们将其称为map().该功能可能看起来像这样(我没有书):

    Now, with the anonymous function returned from partial() and that array of even numbers, we call map(). That function probably looks something like this (I don't have the book):

    function map(fn, list) {
      var i, result = [];
      for (i = 0; i < list.length; ++i) {
        result.push( fn( list[i] ) );
      }
      return result;
    }
    

    map()内部,第一个参数是从先前对partial()的调用返回的匿名函数.该功能有什么作用?好吧,它结合了来自原始partial()调用—的参数. 第一个除外—以及传递给它的参数. map()函数仅传递一个参数,因此,对匿名函数的每次调用所得到的参数列表将是传递给partial()的值1,然后在每次迭代中,将使用与列表不同的偶数./p>

  • Inside map(), then, the first parameter is the anonymous function returned from the earlier call to partial(). What does that function do? Well, it combines the parameters from the original partial() call — except the first one — with the parameters passed into it. The map() function only passes one parameter, so the resulting parameter list on each call to the anonymous function will be the value 1 passed to partial() and then, on each iteration, a different even number from the list.

    一个更简单的示例是考虑呼叫时发生的情况:

    A simpler example would be to consider what happens when you call:

    partial(op["+"], 1)(2);
    

    也就是说,如果您调用partial(),然后立即使用其返回值(匿名函数).效果将与调用相同:

    That is, if you call partial() and then immediately use its return value (the anonymous function). The effect will be the same as calling:

    add(1, 2);
    

    这篇关于部分应用-雄辩的Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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