有没有Javascript depipe 方法?像 bind 或 call 但用于取消管道原型
Is there a Javascript depipe method? like bind or call but for unpiping a piped prototype
是否有官方方法将管道函数的头部作为普通函数参数传递?就像你如何用 .call
和 .bind
修改函数一样,我想修改 String.prototype.includes 使其看起来像 includes('a')('acd')
。基本上,我想要一个使它成为第一个参数的函数,比如 'abc',String.prototype.includes('a')
中的 String 实例
他是我最好的尝试......那是行不通的,因为,为什么会这样
['abc'].some(String.prototype.includes('b'))
基本上我想要一个很酷的令人困惑的版本
x => x.includes('b')
Javascript 中是否有满足我需要的 call、bind 或 depipe 方法版本?
相关:
没有执行此操作的内置方法。忽略像 ramda/lodash.fp 这样的库,它们可以让你通过辅助方法来做到这一点——你需要实现自己的辅助方法。由于参数是“颠倒的”,您不能简单地减少 eta-reduce(避免额外的 lambda):
// all these functions exist in Ramda
function makeThisAnArgument(fn) { // uncurry this
return (arg, ...args) => fn.call(arg, ...args)
}
function reverseArguments(fn) {
return (...args) => fn.call(null, ...args.reverse());
}
// this would let you easily do:
const reversed = reverseArguments(makeThisAnArgument(String.prototype.includes))
const includesA = reversed.bind(null, 'a');
includesA('abc'); // true
includesA('bc'); // false
总的来说,考虑看看有关 Ramda Hey Underscore, You're doing it Wrong! 的精彩演讲,它解释了启用可组合 API 所需的调用顺序、约定和方法签名,就像您要求的那样Ramda 公开的那个。
据我所知,您需要一个 curry
效用函数来将多参数函数转换为部分适用的函数。喜欢;
var curry = f => f.length ? (...a) => curry(f.bind(f,...a)) : f(),
现在 String.prototype.includes()
通常直接从调用对象获取 includes
函数中的 this
参数。然而,我们仍然可以强制 JS 将 includes
显示为双参数函数,如 (x,t) => String.prototype.includes.call(t,x)
,其中 t
是 this
,x
是现有参数。
现在有了这些工具,我们就可以继续实施您的功能了。
var curry = f => f.length ? (...a) => curry(f.bind(f,...a)) : f(),
incl = curry((x,t) => String.prototype.includes.call(t,x));
console.log(incl("llo")("Hello World..!"));
console.log(incl("g")("abcdefg"));
console.log(incl("i")("abcdefg"));
是否有官方方法将管道函数的头部作为普通函数参数传递?就像你如何用 .call
和 .bind
修改函数一样,我想修改 String.prototype.includes 使其看起来像 includes('a')('acd')
。基本上,我想要一个使它成为第一个参数的函数,比如 'abc',String.prototype.includes('a')
他是我最好的尝试......那是行不通的,因为,为什么会这样
['abc'].some(String.prototype.includes('b'))
基本上我想要一个很酷的令人困惑的版本
x => x.includes('b')
Javascript 中是否有满足我需要的 call、bind 或 depipe 方法版本?
相关:
没有执行此操作的内置方法。忽略像 ramda/lodash.fp 这样的库,它们可以让你通过辅助方法来做到这一点——你需要实现自己的辅助方法。由于参数是“颠倒的”,您不能简单地减少 eta-reduce(避免额外的 lambda):
// all these functions exist in Ramda
function makeThisAnArgument(fn) { // uncurry this
return (arg, ...args) => fn.call(arg, ...args)
}
function reverseArguments(fn) {
return (...args) => fn.call(null, ...args.reverse());
}
// this would let you easily do:
const reversed = reverseArguments(makeThisAnArgument(String.prototype.includes))
const includesA = reversed.bind(null, 'a');
includesA('abc'); // true
includesA('bc'); // false
总的来说,考虑看看有关 Ramda Hey Underscore, You're doing it Wrong! 的精彩演讲,它解释了启用可组合 API 所需的调用顺序、约定和方法签名,就像您要求的那样Ramda 公开的那个。
据我所知,您需要一个 curry
效用函数来将多参数函数转换为部分适用的函数。喜欢;
var curry = f => f.length ? (...a) => curry(f.bind(f,...a)) : f(),
现在 String.prototype.includes()
通常直接从调用对象获取 includes
函数中的 this
参数。然而,我们仍然可以强制 JS 将 includes
显示为双参数函数,如 (x,t) => String.prototype.includes.call(t,x)
,其中 t
是 this
,x
是现有参数。
现在有了这些工具,我们就可以继续实施您的功能了。
var curry = f => f.length ? (...a) => curry(f.bind(f,...a)) : f(),
incl = curry((x,t) => String.prototype.includes.call(t,x));
console.log(incl("llo")("Hello World..!"));
console.log(incl("g")("abcdefg"));
console.log(incl("i")("abcdefg"));