简体   繁体   中英

How to write a composed function using Ramda compose?

How to modify the zComposedFn function so that the output of both z and zComposedOutput is same?

 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); 

The goal is to create some metric calculation functions having the same signature and output type but different implementation.

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

example:

There is a financial dashboard, which receives the input data as (1,2,3). The dashboard displays the metric1, metric2 and metric3 calculated as below:

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

I am wondering on how to create the structure of MetricFn.

I can make very little sense out of your function. And I don't see anything that Ramda offers to help. While I'm a Ramda author, I don't always recall every function, but it seems unlikely.

Your requirements looks vaguely like the use of chain with functions, where chain(f, g) ~~> x => f(g(x))(x) . But it's only a vague connection, and I can't see how to use chain to do what you are trying to do.

Is there an underlying problem you're trying to solve that we might be able to help with?

Here is a trivial implementation, but it's mostly just restating your code without the intermediate variables:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM