使用了迭代计算ramdajs

问题描述 投票:3回答:2

我试图总结我的头在Ramda和函数式编程一般,看看它是否在我的情况下才有意义。

它下面的典型问题,我需要解决:

作为输入数据,执行以下操作:

const values = [
 { a: 1,   b: 2,   c: 3 },
 { a: 10,  b: 20,  c: 30 },
 ...
 { a: 100, b: 200, c: 300 }
]

下面的函数被施加上的数据:

const eFn = x => x.a + x.b
const fFn = ? // cumulative add: Sum of the e keys from first to current index, first iteration f = 3, then 3 + 30, then 3 + 30 +300
const gFN = x => x.f > x.e

在这个给定的顺序:

  1. EFN()
  2. FFN()
  3. GFN()

其结果是如下:

const results = [
 { a: 1,   b: 2,   c: 3,   e: 3,   f: 3,  g: true },
 { a: 10,  b: 20,  c: 30,  e: 30,  f: 33, g: true  },
 ...
 { a: 100, b: 200, c: 300, e: 300, f: 333, g: false }
]

题:

对于这种类型的问题,

  • 确实是有意义的使用ramda?
  • 它简化了问题,我能避免在数据明知FFN依赖于前行的数据和GFN必须FFN之后应用循环几次?

我发现很难使用Ramda一个不错的方式解决这个问题。

任何帮助将不胜感激。


Update (2019-02-12)

基于对@斯科特sauyet的答案,我曾尝试基准Ramda VS Rambda。因为我是unale复制100%他的测试,我修改它改变FFN的应用的每个功能的行为和时间手动设置的数字。

    const {floor, random} = Math

    const demo = counts => {

      const eFns = R.curry((n, x) => R.assoc(`e${n}`, x.a + x.b, x))
      const fFns = R.curry((n, x) => R.assoc(`f${n}`,  x.d * x.b, x))
      const gFns = R.curry((n, x) => R.assoc(`g${n}`,  x.f > x.e, x))

      const transform = R.pipe(
        R.map(eFns(1)),
        R.map(eFns(2)),
        R.map(eFns(3)),
        R.map(eFns(4)),
        R.map(eFns(5)),
        R.map(eFns(6)),
        R.map(eFns(7)),
        R.map(eFns(8)),
        R.map(eFns(9)),
        R.map(eFns(10)),
        R.map(eFns(12)),
        R.map(eFns(13)),
        R.map(eFns(14)),
        R.map(eFns(15)),
        R.map(eFns(16)),
        R.map(eFns(17)),
        R.map(eFns(18)),
        R.map(eFns(19)),
        R.map(eFns(20)),
        R.map(eFns(21)),
        R.map(eFns(22)),
        R.map(eFns(23)),
        R.map(eFns(24)),
        R.map(eFns(25)),
        R.map(eFns(26)),
        R.map(eFns(27)),
        R.map(eFns(28)),
        R.map(eFns(29)),
        R.map(eFns(30)),
        R.map(eFns(31)),
        R.map(eFns(32)),
        R.map(eFns(33)),
        R.map(eFns(34)),
        R.map(eFns(35)),
        R.map(eFns(36)),
        R.map(eFns(37)),
        R.map(eFns(38)),
        R.map(eFns(39)),
        R.map(eFns(40)),
        R.map(fFns(1)),
        R.map(fFns(2)),
        R.map(fFns(3)),
        R.map(fFns(4)),
        R.map(fFns(5)),
        R.map(fFns(6)),
        R.map(fFns(7)),
        R.map(fFns(8)),
        R.map(fFns(9)),
        R.map(fFns(10)),
        R.map(gFns(1)),
        R.map(gFns(2)),
        R.map(gFns(3)),
        R.map(gFns(4)),
        R.map(gFns(5)),
        R.map(gFns(6)),
        R.map(gFns(7)),
        R.map(gFns(8)),
        R.map(gFns(9)),
        R.map(gFns(10))
      )


      const vals = R.times(n => ({
        a: floor(random() * 1000),
        b: floor(random() * 1000),
        c: floor(random() * 1000),
        d: floor(random() * 1000)
      }), counts)

      const now = new Date()
      transform(vals)
      const time = new Date() - now

      console.log(`Ran ${counts} records through ${eFns.length} e's, ${fFns.length} f's, and ${gFns.length} g's in ${time} ms`)
    }

    console.clear()
    demo(10)
    demo(100)
    demo(1000)
    demo(10000)
    demo(100000)

现在,我将这些代码successivley到Ramda REPL然后Rambda REPL。我跑Windows 7的测试,在核心i7-6820HQ使用Chrome 66和Node.js的v8.11.1。

令我吃惊的Rambda比Ramda慢我的情况。请注意,这是一个快速和肮脏的测试,我可能已经错过了正确的方法建立了Rambda测试(我只是想复制和粘贴在每个REPL代码,并在节点通过修改import语句运行)。

下面是我的结果:(注意图中数坐标)

Record Number             [-] :  10 | 10 | 1000 | 10000  | 10000
Ramda Chrome 66  [time in ms] :  5  | 39 | 329  | 3673   | 38910  
Rambda Chrome 66 [time in ms] :  6  | 85 | 530  | 5306   | 53777  
Ramda Node.js    [time in ms] :  8  | 38 | 396  | 4219   | 45621
Rambda Node.js   [time in ms] :  7  | 62 | 537  | 5468   | 57540

Log-Log plot of Ramda vs Rambda

javascript ramda.js
2个回答
3
投票

它可能是有意义的使用Ramda。需要注意的是甜蜜点Ramda是pipe'd或compose'd共同创造一个更加复杂的一个简单的转换序列。因此,要使用Ramda不能满足您的循环只有一次的目标的最直接的方式。传感器可能会帮助这在某些情况下,但很多Ramda功能都没有换能器的准备,所以你必须要看看会有什么工作。

但我认为,像这样的开始与简单的代码,只解决性能问题,如果你真的让他们以正确的方式来编写一个问题。编写简单的代码,如果它原来是在应用程序中的瓶颈,那么解决这个问题。 (而且只有这样做,你解决了任何糟糕的瓶颈之后。)令人惊讶常常会发现它,你以为会是一个问题,是不是一个在所有的代码。

所以,我可以做这个问题是这样的:

const {assoc, curry, tail, scan, pipe, map} = R

const eFn = x => assoc('e', x.a + x.b, x)
const fFn = (a, x) => assoc('f', a.f + x.e, x)
const gFn = x => assoc('g', x.f > x.e, x)

// scan includes initial value -- should this be fixed?
const myScan = curry((fn, init, xs) => tail(scan(fn, init, xs)))

const transform = pipe(
  map(eFn),
  myScan(fFn, {f: 0}),
  map(gFn)  
)

const values = [
 { a: 1,   b: 2,   c: 3 },
 { a: 10,  b: 20,  c: 30 },
 { a: 100, b: 200, c: 300 }
]

console.log(transform(values))
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.js"></script>

这说明一个缺陷的scan东西(类似于mapAccum,但有一个简单的接口)。 scan(add, 0, [1, 2, 3, 4]) //=> [0, 1, 3, 6, 10]。我看不到一个很好的理由,结果应该包括初始0,虽然我没有去查历史,看看我失去了一些重要的东西。我解决这个问题通过通过tail跟随它的功能加以包装。但是,我们可以很容易地添加到tail管道:

const transform = pipe(
  map(eFn),
  scan(fFn, {f: 0}), 
  tail,
  map(gFn)  
)

更新

注释被问及性能。下面是测试许多ef,并记录给定数量g风格功能的版本:

const {curry, tail, scan, map, assoc, range, apply, pipe, addIndex, times} = R
const {floor, random} = Math
const myScan = curry((fn, init, xs) => tail(scan(fn, init, xs)))

const demo = (es, fs, gs, counts) => {

  const eFns = map(n => (x) => assoc(`e${n}`, x.a + x.b, x), range(1, es + 1))
  const fFns = map(n => (a, x) => assoc(`f${n}`, a[`f${n}`] + x[`e${n}`], x), range(1, fs + 1))
  const gFns = map(n => (x) => assoc(`g${n}`, x[`f${n}`] > x[`e${n}`], x), range(1, gs + 1))

  const transform = apply(pipe)([...map(map, eFns), ...addIndex(map)((f, i) => myScan(f, {[`f${i + 1}`]: 0}), fFns), ...map(map, gFns)])

  const vals = times(n => ({
    a: floor(random() * 1000),
    b: floor(random() * 1000),
    c: floor(random() * 1000),
  }), counts)

  const now = new Date()
  transform(vals)
  const time = new Date() - now

  console.log(`Ran ${counts} records through ${eFns.length} e's, ${fFns.length} f's, and ${gFns.length} g's in ${time} ms`)
}

console.clear()
demo(40, 10, 10, 100)
demo(40, 10, 10, 1000)
demo(40, 10, 10, 10000)
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.js"></script>

对于10000条记录与40e中的,10架F的,和10克的,我避开在Chrome2.5秒次在新望的MacBook Pro。我不知道这是否是合理的应用程序。 (你也可以玩这个的Ramda REPL。)


2
投票

我觉得很有道理使用Ramda:

  1. Ramda不发生变异数据
  2. Ramda确实有很多的功能,帮助您处理列表和对象

这将变异原来的对象:

const add_ab = obj => { obj.e = obj.a + obj.b; return obj };

虽然这不会:

const add_ab = obj => assoc('e', obj.a + obj.b, obj);

您可以将迭代和积累与mapAccum这可能是你的情况有用的值:

所述mapAccum函数的行为类似于地图和减少的组合;它适用的功能的列表中的每个元素,传递一个累加参数从左侧到右侧,并与新的列表一起返回这个累加器的最终值。

迭代函数接收两个参数,ACC和值,并且应返回的元组[ACC,值]。

const {assoc, compose, last, mapAccum} = R;

const data = [
 { a: 1,   b: 2,   c: 3 },
 { a: 10,  b: 20,  c: 30 },
 { a: 100, b: 200, c: 300 }
];


const set_e = obj => assoc('e', obj.a + obj.b, obj);
const set_f = (acc, obj) =>  assoc('f', acc.f + obj.e, obj);
const set_g = obj => assoc('g', obj.f > obj.e, obj);

const execute = compose(last, mapAccum((acc, cur) => [
  set_f(acc, set_e(cur)),
  set_g(set_f(acc, set_e(cur)))
], {f: 0}));

console.log(
 execute(data)
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.min.js"></script>
© www.soinside.com 2019 - 2024. All rights reserved.