[英]Ramda apply an argument to both functions and compose them point free
我有两个咖喱函数f
和g
:
f: a -> b -> c
g: a -> c -> d
我想创建h
:
h: a -> b -> d
目前我正在通过pipe
编写它们:
const h = a => pipe(f(a), g(a));
有没有办法免费做到这一点?
我之所以这么问,是因为我想让代码尽可能的可读。 在我的一些案例中,有两个以上的函数在组合中接收初始 arguments,我试图避免重复传递 arguments。
我试过类似的东西:
const h = converge(pipe, [f, g]);
由于某种原因,这是行不通的。 我想我没有正确理解converge
。
编辑:
我尝试了你建议的 Ori Drori,但它不起作用。
const modulo = divisor => dividend => dividend % divisor;
const maybeReduceByModulo = modulo => number =>
number >= 0 ? number : number + modulo;
export const maybeReducedModulo = flip(ap)(modulo, maybeReduceByModulo);
/**
* Before:
* export const maybeReducedModulo = divisor =>
* pipe(modulo(divisor), maybeReduceByModulo(divisor));
*/
Ramda(免责声明:我是 Ramda 作者)无法直接提供可能需要的每个组合器。 它确实包含其中的一些。 但对于那些它不包含的内容,编写自己的版本通常是微不足道的。 对我来说,最大的问题是命名。 很难为它们取名,甚至那些求助于鸟名的人也只能为无限的可能性中的一小部分取名。
我在 Avaq 的便捷组合器列表中找不到这个,也找不到:: (a -> b -> c) -> (a -> c -> d) -> a -> b -> d
或:: (a -> b -> c) -> (a -> c -> d) -> (a -> b -> d)
在Hoogle上,这让我怀疑这是一个相当不常见的要求。 但是,如果这是您的要求,请发明您自己的名字。
一旦你选择了一个名字,这样的组合器通常会自己写。
const foo = (f) => (g) => (x) => (y) => g (x) (f (x) (y)) const f = (a) => (b) => `f (${a}, ${b})` const g = (a) => (c) => `g (${a}, ${c})` const h = foo (f) (g) console.log (h ('a') ('b'))
当然,您可以通过各种方式使用签名。 Ramda 的curry
可能会在这里有所帮助。 也许我们想要这个:
const foo = (f, g) => (x) => (y) => g (x) (f (x) (y))
const h = foo (f, g)
甚至更进一步,这个:
const foo = curry ((f, g) => curry ((x, y) => g (x) (f (x) (y))))
const h = foo (f, g) // equivalent: `foo (f) (g)`
h ('a', 'b') // equivalent: `h ('a') ('b')`
customcommander 的建议确实有效:
const f = (a) => (b) => `f (${a}, ${b})` const g = (a) => (c) => `g (${a}, ${c})` const h = compose (apply (pipe), ap ([f, g]), of) console.log (h ('a') ('b'))
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.27.1/ramda.min.js"></script> <script> const {compose, apply, pipe, ap, of} = R </script>
但我认为它远不如更明确的 vanilla JS 版本那么容易理解。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.