Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Currying & 柯里化 #63

Open
xgqfrms opened this issue Dec 4, 2018 · 6 comments
Open

Currying & 柯里化 #63

xgqfrms opened this issue Dec 4, 2018 · 6 comments
Labels
ad blocker checker ad blocker checker Currying & 柯里化 Currying & 柯里化 LeetCode LeetCode

Comments

@xgqfrms
Copy link
Owner

xgqfrms commented Dec 4, 2018

@xgqfrms xgqfrms added the Currying & 柯里化 Currying & 柯里化 label Dec 4, 2018
@xgqfrms
Copy link
Owner Author

xgqfrms commented Dec 4, 2018

Ad Blocker Checker

#33

http://www.ruanyifeng.com/blog/checker.js

image

function checker() {
  var img = document.querySelector('img[src^="http://www.ruanyifeng.com/blog/images"]');
  if (img && window.getComputedStyle(img).display === 'none'){
    var sponsor = document.querySelector('.entry-sponsor');
    var prompt = document.createElement('div');
    prompt.style = 'border: 1px solid #c6c6c6;border-radius: 4px;background-color: #f5f2f0;padding: 15px; font-size: 16px;';
    prompt.innerHTML = '<p>您使用了广告拦截器,导致本站内容无法显示。</p><p>请将 www.ruanyifeng.com 加入白名单,解除广告屏蔽后,刷新页面,谢谢。</p>';
    sponsor.parentNode.replaceChild(prompt, sponsor);
    document.querySelector('#main-content').innerHTML = '';
  }
}

setTimeout(checker, 1000);

image

@xgqfrms xgqfrms added the ad blocker checker ad blocker checker label Dec 4, 2018
Repository owner locked and limited conversation to collaborators Dec 4, 2018
@xgqfrms
Copy link
Owner Author

xgqfrms commented May 12, 2024

The function composition of [f(x), g(x), h(x)] is fn(x) = f(g(h(x))).

[f(x), g(x), h(x)] 的函数组合为 fn(x) = f(g(h(x)))

https://leetcode.com/problems/function-composition/?envType=study-plan-v2&envId=30-days-of-javascript

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

function compose(functions: F[]): F {
    // cury // 柯里化
    return function(x) {
        //
    }
};

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

solutions ✅

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

@xgqfrms
Copy link
Owner Author

xgqfrms commented May 12, 2024

https://www.cnblogs.com/xgqfrms/p/11342509.html#4744377

"use strict";

/**
 *
 * @author xgqfrms
 * @license MIT
 * @copyright xgqfrms
 * @created 2020-11-17
 * @modified
 *
 * @description
 * @description
 * @difficulty Easy Medium Hard
 * @complexity O(n)
 * @time O(n)
 * @augments
 * @example
 * @link https://javascript.info/currying-partials#advanced-curry-implementation
 * @solutions
 *
 * @best_solutions
 *
 */

const log = console.log;


function curry(func) {
  return function curried(...args) {
    // log(`func.length =`, func.length)
    log(`args.length =`, args.length, args)
    if (args.length >= func.length) {
      // 以获取全部的参数, 将参数传递给 callback function

      // apply 接受一个参数数组,改变方法绑定的对象后,方法会立即执行 callback function ✅
      // call 接受一个参数列表,改变方法绑定的对象后,方法会立即执行 callback function ✅
      // bind 仅仅改变方法绑定的对象,但方法不会立即执行,需要手动调用方法才会执行 callback function ✅
      // bind, apply, call改变 this 指向
      // ⚠️ 箭头函数,不适用需要使用改变 this 绑定的场景

      return func.apply(this, args);
    } else {
      return function(...args2) {
        log(`args2.length =`, args2.length, args2)
        // 分步获取参数,并将参数拼接到一起
        // 闭包,内部函数可以访问到外部函数的所有属性
        return curried.apply(this, args.concat(args2));
      }
    }
  };
}


function sum(a, b, c) {
  return a + b + c;
}

// ??? @decorator 装饰器-设计模式
const curriedSum = curry(sum);

log(`\n`, curriedSum(1, 2, 3));
// 6, still callable normally
log(`\n`, curriedSum(1)(2, 3));
// 6, currying of 1st arg
log(`\n`, curriedSum(1)(2)(3));
// 6, full currying


/*

// error
const test1 = isValid(`((}}`);
// ok
const test2 = isValid(`()[]{}`);

log(`❌test =`, test1 ? `✅` : `❌`);
log(`✅test ok =`, test2 ? `✅` : `❌`);

*/


???

https://www.cnblogs.com/xgqfrms/p/14025942.html

@xgqfrms
Copy link
Owner Author

xgqfrms commented May 12, 2024

// bug ❌
// obj = {a: 1, f: () => console.log(`a = ${this.a}`), };


obj = {a: 1, f: function (){ console.log(`a = ${this.a}`)}, };
// {a: 1, f: ƒ}
obj.f()
// a = 1

obj2 = {a: 2}
// {a: 2}
f = obj.f.bind(obj2);
// ƒ (){ console.log(`a = ${this.a}`)}
f();
// a = 2

obj.f.call(obj2);
// a = 2

obj.f.apply(obj2);
// a = 2


/* 


apply 接受一个参数数组,改变方法绑定的对象后,方法会立即执行 callback function ✅
call 接受一个参数列表,改变方法绑定的对象后,方法会立即执行 callback function ✅
bind 仅仅改变方法绑定的对象,但方法不会立即执行,需要手动调用方法才会执行 callback function ✅

bind, apply, call改变 this 指向

⚠️ 箭头函数,不适用需要使用改变 this 绑定的场景

*/

@xgqfrms
Copy link
Owner Author

xgqfrms commented May 12, 2024

function curry(func) {
  return function curried(...args) {
    if (args.length >= func.length) {
      return func.apply(this, args);
    } else {
      return function(...args2) {
        return curried.apply(this, args.concat(args2));
      }
    }
  };
}

https://javascript.info/currying-partials#advanced-curry-implementation

https://zh.javascript.info/currying-partials#gao-ji-ke-li-hua-shi-xian

const curry = fn =>
    curried = (...args) =>
        args.length === fn.length
            ? fn(...args)
            : (arg) => curried(...args, arg)

mqyqingfeng/Blog#42 (comment)

@xgqfrms xgqfrms added the LeetCode LeetCode label May 13, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
ad blocker checker ad blocker checker Currying & 柯里化 Currying & 柯里化 LeetCode LeetCode
Projects
None yet
Development

No branches or pull requests

1 participant