zoukankan      html  css  js  c++  java
  • Thunk

    Thunk

    https://en.wikipedia.org/wiki/Thunk

    In computer programming, a thunk is a subroutine used to inject an additional calculation into another subroutine. Thunks are primarily used to delay a calculation until its result is needed, or toinsert operations at the beginning or end of the other subroutine. They have a variety of other applications in compiler code generation and modular programming.

    JS DEMO

    // 'hypot' is a binary function
    const hypot = (x, y) => Math.sqrt(x * x + y * y);
    
    // 'thunk' is a function that takes no arguments and, when invoked, performs a potentially expensive
    // operation (computing a square root, in this example) and/or causes some side-effect to occur
    const thunk = () => hypot(3, 4);
    
    // the thunk can then be passed around...
    doSomethingWithThunk(thunk);
    
    // ...or evaluated
    thunk(); // === 5

    词典

    https://cn.bing.com/dict/search?q=thunk&FORM=BDVSP2&qpvt=Thunk

    • 〔计〕形实替换程序;计算机的形式实在转换程序
    • 网络形实转换程序;替换程式;出现在互操作中

    结合上面的例子我们理解为, 将函数的参数提前进行绑定, 在需要的时候调用。

    绑定的过程, 就是将 实际参数 绑定到  形式参数上, 可以对应到 形实替换的意思。

    阮大解释

    http://www.ruanyifeng.com/blog/2015/05/thunk.html

    编译器的"传名调用"实现,往往是将参数放到一个临时函数之中,再将这个临时函数传入函数体。这个临时函数就叫做 Thunk 函数。

    
    function f(m){
      return m * 2;     
    }
    
    f(x + 5);
    
    // 等同于
    
    var thunk = function () {
      return x + 5;
    };
    
    function f(thunk){
      return thunk() * 2;
    }
    

    上面代码中,函数 f 的参数 x + 5 被一个函数替换了。凡是用到原参数的地方,对 Thunk 函数求值即可。

    这就是 Thunk 函数的定义,它是"传名调用"的一种实现策略,用来替换某个表达式。

    JavaScript 语言是传值调用,它的 Thunk 函数含义有所不同。在 JavaScript 语言中,Thunk 函数替换的不是表达式,而是多参数函数,将其替换成单参数的版本,且只接受回调函数作为参数。

    
    // 正常版本的readFile(多参数版本)
    fs.readFile(fileName, callback);
    
    // Thunk版本的readFile(单参数版本)
    var readFileThunk = Thunk(fileName);
    readFileThunk(callback);
    
    var Thunk = function (fileName){
      return function (callback){
        return fs.readFile(fileName, callback); 
      };
    };
    

    上面代码中,fs 模块的 readFile 方法是一个多参数函数,两个参数分别为文件名和回调函数。经过转换器处理,它变成了一个单参数函数,只接受回调函数作为参数。这个单参数版本,就叫做 Thunk 函数。

    Thunk实现

    原理:

    
    var Thunk = function(fn){
      return function (){
        var args = Array.prototype.slice.call(arguments);
        return function (callback){
          args.push(callback);
          return fn.apply(this, args);
        }
      };
    };


    
    var readFileThunk = Thunk(fs.readFile);
    readFileThunk(fileA)(callback);
     

    Node thunkify

    https://github.com/tj/node-thunkify

    var thunkify = require('thunkify');
    var fs = require('fs');
    
    var read = thunkify(fs.readFile);
    
    read('package.json', 'utf8')(function(err, str){
      
    });

    类似概念

    curry

    https://www.sitepoint.com/currying-in-functional-javascript/

    var greetCurried = function(greeting) {
      return function(name) {
        console.log(greeting + ", " + name);
      };
    };
    

    var greetHello = greetCurried("Hello");
    greetHello("Heidi"); //"Hello, Heidi"
    greetHello("Eddie"); //"Hello, Eddie"
    

         使得函数更加可读和弹性

    More Readable And More Flexible

    One of the advantages touted for functional JavaScript is shorter, tighter code that gets right to the point in the fewest lines possible, and with less repetition. Sometimes this can come at the expense of readability; until you’re familiar with the way the functional programming works, code written in this way can be harder to read and understand.

    If you’ve come across the term currying before, but never knew what it meant, you can be forgiven for thinking of it as some exotic, spicy technique that you didn’t need to bother about. But currying is actually a very simple concept, and it addresses some familiar problems when dealing with function arguments, while opening up a range of flexible options for the developer.

    https://www.npmjs.com/package/curry

    var curry require('curry');
     
    //-- creating a curried function is pretty
    //-- straight forward:
    var add curry(function(ab)return a + b });
     
    //-- it can be called like normal:
    add(12//= 3
     
    //-- or, if you miss off any arguments,
    //-- a new funtion that expects all (or some) of
    //-- the remaining arguments will be created:
    var add1 add(1);
    add1(2//= 3;
     

    bind

    https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Function/bind

    bind()方法创建一个新的函数,在调用时设置this关键字为提供的值。并在调用新函数时,将给定参数列表作为原函数的参数序列的前若干项。

    var module = {
      x: 42,
      getX: function() {
        return this.x;
      }
    }

    var unboundGetX = module.getX;
    console.log(unboundGetX()); // The function gets invoked at the global scope
    // expected output: undefined

    var boundGetX = unboundGetX.bind(module);
    console.log(boundGetX());
    // expected output: 42

  • 相关阅读:
    Pyinstaller(python打包为exe文件)
    matplotlib 填充颜色
    Visual Studio 2015 Enterprise
    latex中pdflatex与xelatex的区别
    latex插图续
    dva+umi+antd项目从搭建到使用(没有剖验证,不知道在说i什么)
    umi+dva+antd新建项目(亲测可用)
    HTTP缓存机制
    企业网站常见需求整理
    立足于运维与监控的前端框架 NoahV
  • 原文地址:https://www.cnblogs.com/lightsong/p/10699748.html
Copyright © 2011-2022 走看看