gpt4 book ai didi

javascript - 使用ramdajs进行迭代计算

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:56:52 24 4
gpt4 key购买 nike

我正在尝试全面了解 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 很好地解决这个问题。

我们将不胜感激。


更新 (2019-02-12)

根据@scott-sauyet 的回答,我确实尝试对 Ramda 与 Rambda 进行基准测试。由于我无法 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)

现在,我将这段代码依次粘贴到 Ramda REPLRambda REPL 。我在装有 Chrome 66 的核心 i7-6820HQ 和 Node.js v8.11.1 中的 Windows 7 上运行了测试。

令我惊讶的是,对于我的情况,Rambda 比 Ramda 慢。请注意,这是一个快速而肮脏的测试,我可能错过了为 Rambda 设置测试的正确方法(我只是想复制并粘贴每个 REPL 中的代码,并通过修改导入语句在节点中运行)。

这是我的结果:(请注意,该图是对数对数刻度)

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

最佳答案

使用 Ramda 可能有意义。请注意,Ramda 的最佳位置是将一系列简单的转换pipecompose 组合在一起以创建一个更复杂的转换。所以最直接的使用 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)
)

更新

评论询问性能。这是一个针对给定数量的记录测试许多 efg 样式函数的版本:

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>

对于 40 e、10 f 和 10 g 的 10000 条记录,我在新 MacBook Pro 上的 Chrome 中得到的时间约为 2.5 秒。我不知道这对你的申请是否合理。 (您也可以在 Ramda REPL 上玩这个。)

关于javascript - 使用ramdajs进行迭代计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54553735/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com