代码之家  ›  专栏  ›  技术社区  ›  Ajit Singh

如何使用ramda compose编写复合函数?

  •  0
  • Ajit Singh  · 技术社区  · 6 年前

    如何修改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)的形式接收输入数据。仪表板显示以下计算的公制1、公制2和公制3:

    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的结构。

    1 回复  |  直到 6 年前
        1
  •  1
  •   Scott Sauyet    6 年前

    我对你的工作没什么意义。我看不到兰达提供的任何帮助。虽然我是Ramda的作者,但我并不总是记得每一个函数,但似乎不太可能。

    你的要求看起来有点像 chain 带函数,其中 chain(f, g) ~~> x => 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)