相关疑难解决方法(0)

函数式编程中的reduce和foldLeft/fold之间的区别(特别是Scala和Scala API)?

为什么Scala和像Spark和Scalding这样的框架都有reducefoldLeft?那么reduce和之间的区别是fold什么?

reduce functional-programming scala fold scalding

93
推荐指数
2
解决办法
3万
查看次数

在Kotlin中反转String中单词的顺序

嗨伙计们,我正在寻找方法来扭转Kotlin中字符串中单词的顺序.

例如,输入字符串将是:

怎么了,Pal!

输出字符串将是:

朋友!起来,是什么

我知道我需要使用反转模块,但我不确定如何...

string kotlin

3
推荐指数
1
解决办法
1720
查看次数

构建错误:类型不匹配:推断类型为 Unit

我尝试连接一些条形码值:

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>

type-mismatch kotlin

1
推荐指数
1
解决办法
250
查看次数

如何将字符串数组减少为int值(最短元素的长度)

我有一个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 值?

我发现这个问题,大约reducefold方法.

我应该使用折叠方法代替减少?

arrays string kotlin

0
推荐指数
2
解决办法
102
查看次数