为什么Scala和像Spark和Scalding这样的框架都有reduce和foldLeft?那么reduce和之间的区别是fold什么?
嗨伙计们,我正在寻找方法来扭转Kotlin中字符串中单词的顺序.
例如,输入字符串将是:
怎么了,Pal!
输出字符串将是:
朋友!起来,是什么
我知道我需要使用反转模块,但我不确定如何...
我尝试连接一些条形码值:
barcodeScanner.process(image)
.addOnSuccessListener {
barcodes ->
if (barcodes.isNotEmpty()) {
val barcode = barcodes.reduce {acc, barcode -> acc + barcode.rawValue() }
debug ("analyze: barcodes: $barcode")
} else {
debug ("analyze: No barcode scanned")
}
}
Run Code Online (Sandbox Code Playgroud)
该代码产生以下错误:
barcodeScanner.process(image)
.addOnSuccessListener {
barcodes ->
if (barcodes.isNotEmpty()) {
val barcode = barcodes.reduce {acc, barcode -> acc + barcode.rawValue() }
debug ("analyze: barcodes: $barcode")
} else {
debug ("analyze: No barcode scanned")
}
}
Run Code Online (Sandbox Code Playgroud)
我都不明白他们的意思。有人能解释一下吗?
特别是最后一条错误消息对我来说听起来很奇怪。为什么我要尝试调用rawValuea String?该变量barcodes的类型应该是List<Barcode>,而不是List<String>。
我有一个String数组,我需要找到最短字符串的长度.例如,对于这个字符串数组,["abc", "ab", "a"]我应该得到值1.
我编写了获取字符串数组并返回int值的方法
val minLength = fun(strs: Array<String>): Int {
var minLength = strs[0].length
for (index in 1..strs.size - 1) {
val elemLen = strs[index].length
if (minLength > elemLen) {
minLength = elemLen
}
}
return minLength
}
Run Code Online (Sandbox Code Playgroud)
另一种方法是使用reduce方法
val minLengthReduce = strs.reduce ({
str1, str2 ->
if (str1.length < str2.length) str1 else str2
}).length
Run Code Online (Sandbox Code Playgroud)
是否可以直接从reduce()方法而不是字符串值获取int 值?
我发现这个问题,大约reduce和fold方法.
我应该使用折叠方法代替减少?