[英]Return array of applied functions with Ramda
I would like to call an Array of functions with a value and get an Array of the partially applied functions back. 我想用一个值调用一个函数数组,并获取部分应用函数的数组。
My current code: 我当前的代码:
const my_array = pipe(flip(call), flip(map)([add, subtract]))(1)
// = [add(1), subtract(1)]
Is there a better way of doing this? 有更好的方法吗?
My goal (possibly unreasonable) would be to have a point-free alternative to the following function: 我的目标(可能是不合理的)是为以下功能提供一种无积分的替代品:
const bind_all = (funcs) => pipe(flip(call), flip(map)(funcs))
bind_all([add, subtract])(1)
// = [add(1), subtract(1)]
This seems to be similar to juxt , but returning an Array instead of a function. 这似乎类似于juxt ,但是返回一个Array而不是一个函数。
Update: In response to the answers, I agree that my current code is far from readable, due to trying to force a point-free approach (with limited knowledge of existing patterns). 更新:作为对答案的回应,我同意由于试图强制采用无点方法(对现有模式的了解有限),我的当前代码远非易读。 This question is simply trying to explore the limits of point-free design that maintains readability. 这个问题只是试图探索保持可读性的无点设计的局限性。
The current answers seem to indicate that this is actually unreasonable. 当前的答案似乎表明这实际上是不合理的。
I don't know why people insist on doing everything point-free when your code ends up looking like junk - flip this, flip that. 我不知道为什么当代码最终看起来像垃圾时,人们坚持要无意义地做所有事情-翻转,翻转。 what a brainf*ck. 真是令人费解。
pipe(flip(call), flip(map)([add, subtract]))(1)
If you have [add, subtract]
and 1
and you wish to have [add(1), subtract(1)]
just write the damned mapping function 如果您有[add, subtract]
和1
并且希望有[add(1), subtract(1)]
只需编写该死的映射函数
map(f => f(1), [add, subtract])
// => [add(1), subtract(1)]
We can see how repeating this would give us fully applied functions 我们可以看到重复执行此操作将如何为我们提供完全应用的功能
map(f => f(1), map(f => f(1), [add, subtract]))
// => [add(1)(1), subtract(1)(1)]
// => [2, 0]
If you're that fixated on writing everything point-free, write your own combinator 如果您全心全意地编写所有内容,请编写自己的组合器
const $ = x => f => f (x)
R.map($(1), [R.add, R.subtract])
// => [add(1), subtract(1)]
R.map($(1), R.map($(1), [R.add, R.subtract]))
// => [add(1)(1), subtract(1)(1)]
// => [2,0]
Maybe I'm missing something, but juxt
does seem to do what you need 也许我缺少了一些东西,但是juxt
确实juxt
您的需求
juxt([add, flip(subtract)])(1)(7); //=> [8, 6]
Update: As pointed out in the comments, this did not capture the original requirements. 更新:正如评论中指出的那样,这没有满足原始要求。 I think the following approach does. 我认为以下方法可以。 It is similar to the one from @naomik , but creating a reusable function rather than simply running inline: 它与@naomik中的类似,但是创建了一个可重用的函数,而不是简单地内联运行:
const callAll = curry((fns, arg) => map(fn => fn(arg), fns));
const partials = callAll([add, subtract], 10); //=> [add(10), subtract(10)]
callAll(partials, 3); //=> [10 + 3, 10 - 3] => [13, 7]
Obviously we could write this in a way that handles multiple parameters, if we desired. 显然,如果需要的话,我们可以使用处理多个参数的方式编写此代码。 Using it once to create the partial functions, then again to apply the argument to them feels pretty elegant, too. 一次使用它来创建部分函数,然后再次将参数应用于它们也感觉很优雅。
You can see this in action on the Ramda REPL . 您可以在Ramda REPL上看到这一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.