我正在阅读Kotlin的基础知识,我对Kotlin中的函数fold()和reduce()非常困惑,有人能给我一个区分两者的具体例子吗?
为什么Scala和像Spark和Scalding这样的框架都有reduce和foldLeft?那么reduce和之间的区别是fold什么?
我在MSDN的Linq示例中找到了一个名为Fold()的简洁方法,我想使用它.他们的例子:
double[] doubles = { 1.7, 2.3, 1.9, 4.1, 2.9 };
double product =
doubles.Fold((runningProduct, nextFactor) => runningProduct * nextFactor);
Run Code Online (Sandbox Code Playgroud)
不幸的是,无论是在他们的示例中还是在我自己的代码中,我都无法进行编译,而且我在MSDN中找不到任何其他地方(如Enumerable或Array扩展方法).我得到的错误是一个普通的"不知道任何关于那个"的错误:
error CS1061: 'System.Array' does not contain a definition for 'Fold' and no
extension method 'Fold' accepting a first argument of type 'System.Array' could
be found (are you missing a using directive or an assembly reference?)
Run Code Online (Sandbox Code Playgroud)
我正在使用其他我认为来自Linq的方法(比如Select()和Where()),我正在"使用System.Linq",所以我认为一切都好.
这种方法确实存在于C#3.5中,如果是这样,我做错了什么?
在Hadoop中什么时候开始减少任务?它们是在完成一定百分比(阈值)的映射器后开始的吗?如果是这样,这个门槛是否固定?通常使用什么样的阈值?
如何在reduce方法上打破迭代?
对于
for (var i = Things.length - 1; i >= 0; i--) {
if(Things[i] <= 0){
break;
}
};
Run Code Online (Sandbox Code Playgroud)
降低
Things.reduce(function(memo, current){
if(current <= 0){
//break ???
//return; <-- this will return undefined to memo, which is not what I want
}
}, 0)
Run Code Online (Sandbox Code Playgroud) 我试图将一个对象数组减少到Swift中的一个集合,这是我的代码:
objects.reduce(Set<String>()) { $0.insert($1.URL) }
Run Code Online (Sandbox Code Playgroud)
但是,我收到一个错误:
Type of expression is ambiguous without more context.
Run Code Online (Sandbox Code Playgroud)
我不明白问题是什么,因为URL的类型绝对是String.有任何想法吗?
我使用了这两种方法,但我对这两种方法的使用感到很困惑.
有什么map可以做但不能做reduce,反之亦然?
注意:我知道如何使用这两种方法我正在质疑这些方法之间的主要区别以及何时需要使用.
有很多方法可以编写计算直方图的Python程序.
通过直方图,我的意思是一个函数,它计算a中对象的出现次数iterable并输出字典中的计数.例如:
>>> L = 'abracadabra'
>>> histogram(L)
{'a': 5, 'b': 2, 'c': 1, 'd': 1, 'r': 2}
Run Code Online (Sandbox Code Playgroud)
编写此函数的一种方法是:
def histogram(L):
d = {}
for x in L:
if x in d:
d[x] += 1
else:
d[x] = 1
return d
Run Code Online (Sandbox Code Playgroud)
是否有更简洁的方法来编写此功能?
如果我们在Python中有字典理解,我们可以写:
>>> { x: L.count(x) for x in set(L) }
Run Code Online (Sandbox Code Playgroud)
但由于Python 2.6没有它们,我们必须写:
>>> dict([(x, L.count(x)) for x in set(L)])
Run Code Online (Sandbox Code Playgroud)
虽然这种方法可以读取,但效率不高:L经过多次.此外,这对单寿命发电机不起作用; 该函数应该对迭代器生成器同样有效,例如:
def gen(L):
for x in L:
yield x
Run Code Online (Sandbox Code Playgroud)
我们可能会尝试使用该reduce函数(RIP):
>>> reduce(lambda d,x: dict(d, …Run Code Online (Sandbox Code Playgroud) 似乎有一些问题将async/await与.reduce()结合起来,如下所示:
const data = await bodies.reduce(async(accum, current, index) => {
const methodName = methods[index]
const method = this[methodName]
if (methodName == 'foo') {
current.cover = await this.store(current.cover, id)
console.log(current)
return {
...accum,
...current
}
}
return {
...accum,
...method(current.data)
}
}, {})
console.log(data)
Run Code Online (Sandbox Code Playgroud)
该data对象被记录之前的this.store完成...
我知道你可以使用Promise.all异步循环,但这适用于.reduce()?
我试图了解fold和foldLeft以及各自的reduce和reduceLeft是如何工作的.我用fold和foldLeft作为例子
scala> val r = List((ArrayBuffer(1, 2, 3, 4),10))
scala> r.foldLeft(ArrayBuffer(1,2,4,5))((x,y) => x -- y._1)
scala> res28: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(5)
scala> r.fold(ArrayBuffer(1,2,4,5))((x,y) => x -- y._1)
<console>:11: error: value _1 is not a member of Serializable with Equals
r.fold(ArrayBuffer(1,2,4,5))((x,y) => x -- y._1)
Run Code Online (Sandbox Code Playgroud)
为什么fold不工作foldLeft?什么是Serializable with Equals?我理解fold和foldLeft在参数泛型类型方面有轻微不同的API签名.请指教.谢谢.