【问题标题】:How to write a composed function using Ramda compose?如何使用 Ramda compose 编写组合函数?
【发布时间】:2018-06-18 10:38:53
【问题描述】:

如何修改zComposedFn函数,使z和zComposedOutput的输出相同?

const R = require('ramda');

let f1 = R.curry((p1, p2, p3) => {
    let result = {
        n1: p1,
        n2: p2,
        n3: p3
    };
    return result;
}
);

let x = f1(1, 2, 3);
let y = f1(1, 2, x);
let z = f1(1, x , y);

console.log(z);

let zComposedFn = R.compose(f1);

let input = [1,2,3];
let zComposedOutput = zComposedFn(...input);

console.log(zComposedOutput);

目标是创建一些具有相同签名和输出类型但实现不同的度量计算函数。

const MetricFn = (m,f,a) => {<to be implemented using switch case on m> return b}

m : name of metric as string
f : Array of functions utilizing  input data objects
a : input data object

示例:

有一个财务仪表板,它接收输入数据为 (1,2,3)。 Dashboard 显示 metric1、metric2 和 metric3 计算如下:

metric1 = MetricFn('metric1',[f1])(1,2,3);
metric2 = MetricFn('metric2',[f1, f2])(1,2,3);
metric3 = MetricFn('metric3',[f1, f2, f3])(1,2,3);

我想知道如何创建 MetricFn 的结构。

【问题讨论】:

  • ramdajs.com/docs/#compose 注意:最右边的函数可以有任意数量;其余函数必须是一元的。
  • 您能否举例说明metric3 的函数可能是什么样的?我仍然对每个指标的行为方式以及它们如何与f1f2 等相关联感到困惑。

标签: javascript functional-programming ramda.js


【解决方案1】:

我无法理解您的功能。而且我看不到 Ramda 提供的任何帮助。虽然我是 Ramda 的作者,但我并不总是记得每个函数,但这似乎不太可能。

您的要求看起来有点像 chain 与函数的使用,其中 chain(f, g) ~~&gt; x =&gt; f(g(x))(x)。但这只是一个模糊的联系,我看不出如何使用链来做你想做的事情。

您是否正在尝试解决我们可能能够帮助解决的潜在问题?

这是一个简单的实现,但它主要是在没有中间变量的情况下重述您的代码:

const foo = curry((f, a, b, c) => f(a, f(a, b, c), f(a, b, f(a, b, c))))
foo(f1)(1, 2, 3)

【讨论】:

  • 感谢您的回复。添加了用例。在创建“MetricFn”函数时请求一些帮助。
  • 我还不清楚。请参阅我对这个问题的评论。
猜你喜欢
  • 2019-08-19
  • 1970-01-01
  • 2018-03-13
  • 2018-11-12
  • 1970-01-01
  • 1970-01-01
  • 2017-11-23
  • 1970-01-01
  • 2018-06-29
相关资源
最近更新 更多