Skip to content

Instantly share code, notes, and snippets.

@googya
Last active November 30, 2017 06:21
Show Gist options
  • Select an option

  • Save googya/1030b585f7c8877620942c50ce36ef83 to your computer and use it in GitHub Desktop.

Select an option

Save googya/1030b585f7c8877620942c50ce36ef83 to your computer and use it in GitHub Desktop.
function doubleAfter2Seconds(x) {
return new Promise((resolve, reject) => {
resolve(x * 2)
}, 2000)
}
async function addAsync(x) {
const a = doubleAfter2Seconds(10);
const b = doubleAfter2Seconds(20);
const c = doubleAfter2Seconds(30);
return x + (await a) + (await b) + (await c);
}
addAsync(10).then(e => console.log(e))
function foo() {
return () => {
return () => {
return () => {
console.log("id:", this.id);
};
};
};
}
var f = foo.call({id: 1});
var t1 = f.call({id: 2})()();
var t2 = f().call({id: 3})();
var t3 = f()().call({id: 4});
// from [关于箭头函数this的理解几乎完全是错误的](https://github.com/ruanyf/es6tutorial/issues/150)
// 箭头函数与普通函数的差异, 箭头函数的执行环境是, 创建箭头函数所在的对象(lexical scoping, 从源代码中即可知道环境是什么, global 环境, 还是其他对象)
function a(x200){
return (x300) => {
return (x400) => {
return this.x;
}
}
}
function a(x200){
return function(x300) {
return (x400) => {
return this.x;
}
}
}
function a(){
return function(){
return function(){
return this.x;
}
}
}
x = 100
console.log(a.call({x: 200}).call({x: 300}).call({x: 400}))
@googya

googya commented Nov 30, 2017

Copy link
Copy Markdown
Author
const R = require('ramda')

const sum = (args) => R.reduce((x,y) => x + y, 0, args)
const gte10 = R.gte(R.__, 10)
const lte20 = R.lte(R.__, 20)
const range10to20 = R.both(gte10, lte20)
constructMsg = (v) => `the magic number is: ${v}`

const sumOnlyFavorite = R.compose(
    sum,
    R.filter(range10to20)
)

const printMagicNumber = R.pipe(
    sumOnlyFavorite,
    constructMsg
)

const numbers = [4,10,0,27,42,17,15,-6,58];
printMagicNumber(numbers)

使用 ramda 重写 https://github.com/getify/Functional-Light-JS/blob/master/manuscript/ch1.md 中的例子

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment