- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试全面了解 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
在这个给定的序列中:
结果如下:
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 很好地解决这个问题。
我们将不胜感激。
根据@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 REPL 和 Rambda 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
最佳答案
使用 Ramda 可能有意义。请注意,Ramda 的最佳位置是将一系列简单的转换pipe
或compose
组合在一起以创建一个更复杂的转换。所以最直接的使用 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)
)
评论询问性能。这是一个针对给定数量的记录测试许多 e
、f
和 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>
对于 40 e、10 f 和 10 g 的 10000 条记录,我在新 MacBook Pro 上的 Chrome 中得到的时间约为 2.5 秒。我不知道这对你的申请是否合理。 (您也可以在 Ramda REPL 上玩这个。)
关于javascript - 使用ramdajs进行迭代计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54553735/
如果您有超过 1 个具有相同类名的(动态)文本框,并使用 jquery 循环遍历每个所述文本框,您是否可以假设每次选择文本框的顺序都是相同的? 示例: 文本框 1 值 = 1文本框 2 值 = 2文本
有人知道为什么这段代码无法顺利运行吗?它似乎不喜欢使用yield关键字进行迭代:我正在尝试从任何级别的列表或字典中挖掘所有数字(对列表特别感兴趣)。在第二次迭代中,它找到 [2,3] 但无法依次打印
我关于从 mysql 数据库导出数据并将其保存到 Excel 文件(多表)的创建脚本。我需要让细胞动态基因化。该脚本正确地显示了标题,但数据集为空。当我“回显”$value 变量时,我检查了数据是否存
我正在尝试在 Python 中运行模拟,由此我绘制了一个数组的随机游走图,给定了两个变量参数的设定水平。 但是,我遇到了一个问题,我不确定如何迭代以便生成 250 个不同的随机数以插入公式。例如我已经
我是学习 jquery 的新手,所以如果这是一个相对简单的问题,我深表歉意。我有一个 ID 为 ChartstoDisplay 的 asp.net 复选框列表。我正在尝试创建 jquery 来根据是否
我正在尝试根据在任意数量的部分中所做的选择找出生成有效案例列表的最佳方法。也许它不是真正的算法,而只是关于如何有效迭代的建议,但对我来说这似乎是一个算法问题。如果我错了,请纠正我。实现实际上是在 Ja
如果我使用 sr1 为 www.google.com 发送 DNSQR,我会收到几个 DNSRR(s) 作为回复,例如(使用 ans[DNSRR].show() 完成): ###[ DNS Resou
假设有这样一个实体类 @Entity public class User { ... public Collection followers; ... } 假设用户有成千上万的用户关注者。我想分页..
这个问题已经有答案了: 已关闭11 年前。 Possible Duplicate: Nested jQuery.each() - continue/break 这是我的代码: var steps =
我刚从 F# 开始,我想遍历字典,获取键和值。 所以在 C# 中,我会说: IDictionary resultSet = test.GetResults; foreach (DictionaryEn
我知道已经有很多关于如何迭代 ifstream 的答案,但没有一个真正帮助我找到解决方案。 我的问题是:我有一个包含多行数据的txt文件。 txt 文件的第一行告诉我其余数据是如何组成的。例如这是我的
我有 12 个情态动词。我想将每个模态的 .modal__content 高度与 viewport 高度 进行比较,并且如果特定模态 .modal__content 高度 vh addClass("c
在此JSFiddle (问题代码被注释掉)第一次单击空单元格会在隐藏输入中设置一个值,并将单元格的背景颜色设置为绿色。单击第二个空表格单元格会设置另一个隐藏输入的值,并将第二个单元格的背景颜色更改为红
这是一个非常具体的问题,我似乎找不到任何特别有帮助的内容。我有一个单链表(不是一个实现的链表,这是我能找到的全部),其中节点存储一个 Student 对象。每个 Student 对象都有变量,尽管我在
有没有办法迭代 IHTMLElementCollection? 比如 var e : IHTMLLinkElement; elementCollection:IHTMLElementCollect
我正在尝试用 Java 取得高分。基本上我想要一个 HashMap 来保存 double 值(因此索引从最高的 double 值开始,这样我更容易对高分进行排序),然后第二个值将是客户端对象,如下所示
我想在宏函数中运行 while/until 循环,并限制其最大迭代次数。我找到了如何在“通常”sas 中执行此操作: data dataset; do i=1 to 10 until(con
Iterator iterator = plugin.inreview.keySet().iterator(); while (iterator.hasNext()) { Player key
晚上好我有一个简单的问题,我警告你我是序言的新手。假设有三个相同大小的列表,每个列表仅包含 1、0 或 -1。我想验证对于所有 i,在三个列表的第 i 个元素中,只有一个非零。 此代码针对固定的 i
我在 scheme 中构建了一个递归函数,它将在某些输入上重复给定函数 f, n 次。 (define (recursive-repeated f n) (cond ((zero? n) iden
我是一名优秀的程序员,十分优秀!