如何使用 Ramda compose 编写组合函数?
How to write a composed function using Ramda compose?
如何修改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)。仪表板显示如下计算的 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 的结构。
我无法理解你的功能。而且我看不到 Ramda 提供的任何帮助。虽然我是 Ramda 的作者,但我并不总是记得每个函数,但这似乎不太可能。
您的要求看起来有点像使用 chain
函数,其中 chain(f, g) ~~> x => f(g(x))(x)
。但这只是一个模糊的联系,我看不出如何使用 chain 来做你想做的事情。
是否有您正在尝试解决的潜在问题我们可以提供帮助?
这是一个简单的实现,但它主要只是在没有中间变量的情况下重述您的代码:
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)
如何修改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)。仪表板显示如下计算的 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 的结构。
我无法理解你的功能。而且我看不到 Ramda 提供的任何帮助。虽然我是 Ramda 的作者,但我并不总是记得每个函数,但这似乎不太可能。
您的要求看起来有点像使用 chain
函数,其中 chain(f, g) ~~> x => f(g(x))(x)
。但这只是一个模糊的联系,我看不出如何使用 chain 来做你想做的事情。
是否有您正在尝试解决的潜在问题我们可以提供帮助?
这是一个简单的实现,但它主要只是在没有中间变量的情况下重述您的代码:
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)