通过管道传递多个参数和最后一个函数的结果 [英] Passing multiple arguments and result of last function through pipe

查看:28
本文介绍了通过管道传递多个参数和最后一个函数的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 Ramda.js 构建一个管道,它接受三个参数.第一个函数需要这三个参数,它的结果在第二个函数中使用.但是,第二个函数也需要初始参数之一.我无法弄清楚构建类似内容的分支.

I'm building a pipe with Ramda.js which accepts three arguments. The first function needs those three arguments, and it's result is used in the second function. However, the second function also needs one of the initial arguments. I cannot figure out the branching to build something like it.

在伪代码风格中,我需要这样的东西:

In pseudocode style, I need something like this:

const composedFunction = R.pipe(
  firstFunction,
  secondFunction,
);

const firstFunction = (reusedArgument, secondArgument, thirdArgument) => someAnswer;
const secondFunction = (reusedArgument, someAnswer);

console.log(composedFunction({ foo: bar }, [5, 3, 4], [100, 12, 12]));

推荐答案

我能想到几个解决方案:

I can think of a few solutions:

将管道包裹在另一个函数中,以便组合中的函数仍然可以引用原始参数.

Wrap your pipe inside another function so that functions in your composition can still refer to the original parameters.

此处func2 接受func1 的输出,但也可以访问初始b 参数.显然,func2 必须被柯里化,并被设计为接受它的数据"作为最后一个参数(这是 Ramda 的原则和一般我会说的函数式编程).

Here func2 accepts the output of func1 but also has access to the initial b parameter. Obviously func2 must be curried and be designed to accept its "data" as the last parameter (which is a tenet of Ramda and functional programming in general I'd say).

const func3 = (a, b, c) =>
  pipe(func1, func2(b))
   (a, b, c);

func3(10, 20, 30);

其他选项,func1 返回一个可以在 func2 中解构的数组.

Other option, func1 returns an array which you can destructure in func2.

我认为这不是特别好,但它是一种选择:

I don't think this is particularly nice but it is an option:

const func1 = (a, b, c) => [a + c, b];
const func2 = ([sum, b]) => sum * b;

const func3 = pipe(func1, func2);
func3(10, 20, 30);

这篇关于通过管道传递多个参数和最后一个函数的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆