xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

js currying All In One

js currying All In One

js 柯里化 All In One

"use strict";

/**
 *
 * @author xgqfrms
 * @license MIT
 * @copyright xgqfrms
 * @created 2019-08-12
 * @modified 2022-09-29
 *
 * @description js 柯里化 All In One
 * @description js currying All In One
 * @difficulty Medium
 * @time_complexity O(n)
 * @space_complexity O(n)
 * @augments
 * @example
 * @link https://www.cnblogs.com/xgqfrms/p/11342509.html
 * @solutions
 *
 * @best_solutions
 *
 */

const log = console.log;

/*

// 闭包,一个函数内部的函数可以访问到该函数外层的局部变量
function multi(a) {
  log(`a  =`, a)
  return function(b) {
    log(`b  =`, b)
    return function(c) {
      log(`c  =`, c)
      return a * b * c;
    }
  }
}

multi(2)(3)(4);
// 24



*/

currying 一个函数执行后的返回值是另一个函数,导致函数可以连续调用,暨可以连续传递多个参数

函数柯里化: 将能够接收多个参数的函数转化为仅接收单个参数的函数,并且返回接收余下参数返回结果的新函数的技术。

函数柯里化的主要作用和特点就是: 参数复用提前返回延迟执行

// 如何实现 multi(2)(3)(4) = 24?


function curry(fn) {
  log('\narguments 1 =', arguments);
  // curry 函数的`第一个参数`是要动态创建`柯里化的函数`,`剩余参数`存储在 args 变量中
  const args = Array.prototype.slice.call(arguments, 1);
  log(`args =`, args);
  // 返回匿名函数
  return function () {
    log('\narguments 2 =', arguments);
    const otherArgs = Array.prototype.slice.call(arguments);
    log(`otherArgs =`, otherArgs);
    const newArgs = args.concat(otherArgs);
    return fn.apply(this, newArgs);
  }
}


function fn(a, b, c) {
  return a * b * c;
}

const multi = curry(fn);
log(`\nmulti =`, multi);

multi(2, 3, 4);


// $ node ./js-currying.js

arguments 1 = [Arguments] { '0': [Function: fn] }
args = []

multi = [Function (anonymous)]

arguments 2 = [Arguments] { '0': 2, '1': 3, '2': 4 }
otherArgs = [ 2, 3, 4 ]

// export {};


demos


var test = (
    function (a){
        console.log(`a2 =`, a);// 1
        // console.log(`b2 =`, b);
        // undefined
        this.a = a;
        console.log(`this.a1 =`, this.a);// 1
        return function (b) {
            console.log(`this.a2 =`, this.a);// 1
            console.log(`b3 =`, b);// 4
            // ??? b always equal to test passed param's value
            // passed & unused args/params
            return this.a + b;
        };
    }(
        function (a, b) {
            console.log(`a1 =`, a);// 1
            console.log(`b1 =`, b);// 2
            return a;
        }(1, 2)
        // fixed args
    )
);

console.log(`test(4) = `, test(4));

// a1 = 1
// b1 = 2
// a2 = 1
// this.a1 = 1
// this.a2 = 1
// b3 = 4
// test(4) =  5

// js curry & js 柯里化

console.log(`\ntest(5) = `, test(5));
// b3 = 5
// test(5) =  6

https://github.com/xgqfrms/FEIQA/issues/63#issuecomment-2106272073

type F = (x: number) => number;

function compose(functions: F[]): F {
    // cury // 柯里化
    return function(x) {
      for(let i = 0; i < functions.length; i++) {
        // let func = functions[functions.length - 1 - i];
        // x = func(x);
        x = functions[functions.length - 1 - i](x);
      }
      return x;
    }
};

/**
 * const fn = compose([x => x + 1, x => 2 * x])
 * fn(4) // 9
 */


// Typescript
type F = (x: number) => number;

function compose(functions: F[]): F {
    return function(x) {
        return functions.reverse().reduce((acc, current) => acc = current(acc), x)
    }
};

/**
 * const fn = compose([x => x + 1, x => 2 * x])
 * fn(4) // 9
 */


image

柯里化

所谓"柯里化",就是把一个多参数的函数,转化为单参数函数;

https://www.ruanyifeng.com/blog/2017/02/fp-tutorial.html

柯里化(Currying), 又称部分求值(Partial Evaluation),是把接受多个参数的函数变换成接受一个单一参数(最初函数的第一个参数)的函数,并且返回接受余下的参数而且返回结果的新函数的技术;

https://www.zhangxinxu.com/wordpress/2013/02/js-currying/

柯里化有3个常见作用:

  1. 参数复用
  2. 提前返回
  3. 延迟计算/运行;

https://www.haorooms.com/post/js_currying

http://www.alloyteam.com/2013/08/javascript-zhong-you-qu-di-fan-ke-li-hua-ji-shu/

https://llh911001.gitbooks.io/mostly-adequate-guide-chinese/content/ch4.html

https://juejin.im/post/5af13664f265da0ba266efcf

js in deep

https://github.com/mqyqingfeng/Blog

https://blog.fundebug.com/2018/10/30/33-js-concepts/

refs

https://lodash.com/docs#curry

https://zh.javascript.info/currying-partials



©xgqfrms 2012-2020

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2019-08-12 20:54  xgqfrms  阅读(127)  评论(10编辑  收藏  举报