scala 使用 mkString 与 foldRight 合并字符串列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16447681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 05:16:47  来源:igfitidea点击:

Merging a list of Strings using mkString vs foldRight

scalafunctional-programmingstring-concatenation

提问by posdef

I am currently trying out things in Scala, trying to get accustomed to functional programming as well as leaning a new language again (it's been a while since last time).

我目前正在 Scala 中尝试一些东西,试图习惯函数式编程以及再次学习一门新语言(距离上次已经有一段时间了)。

Now given a list of strings if I want to merge them into one long string (e.g. "scala", "is", "fun" => "scalaisfun") I figured one way to do it would be to do a foldRightand apply concatenation on the respective elements. Another way, admittedly much simpler, is to call mkString.

现在给出一个字符串列表,如果我想将它们合并成一个长字符串(例如"scala", "is", "fun" => "scalaisfun"),我想一种方法是foldRight在相应的元素上执行并应用连接。无可否认,另一种更简单的方法是调用mkString.

I checked on github but couldn't really find the source code for the respective functions (any help on that would be appreciated), so I am not sure how the functions are implemented. From the top of my head, I think the mkStringis more flexible but it feels that there might be a foldRightin the implementation somewhere. Is there any truth to it?

我在 github 上检查过,但无法真正找到相应功能的源代码(任何帮助将不胜感激),所以我不确定这些功能是如何实现的。在我的脑海中,我认为mkString更灵活,但感觉可能foldRight在某处实施。有什么道理吗?

Otherwise the scaladocs mention that mkStringcalls on toStringfor each respective element. Seeing that they are already strings to start with, that could be one negative point for mkStringin this particular case. Any comments on the pros and cons of both methods, with respect to performance, simplicity/elegance etc?

否则,scaladocs 会提到mkString调用toString每个相应的元素。看到它们已经是字符串开始,mkString在这种特殊情况下这可能是一个负面因素。对这两种方法在性能、简单性/优雅等方面的优缺点有何评论?

回答by senia

Simple answer: use mkString.

简单的答案:使用mkString.

someString.toStringreturnsthe same object.

someString.toString返回相同的对象。

mkStringis implemented with a single StringBuilderand it creates only 1 new string. With foldLeftyou'll create N-1new strings.

mkString用单个实现,StringBuilder它只创建 1 个新字符串。随着foldLeft您将创建N-1新的字符串。

You could use StringBuilderin foldLeft, it will be as fast as mkString, but mkStringis shorter:

您可以使用StringBuilderin foldLeft,它将与 一样快mkString,但mkString更短:

strings.foldLeft(new StringBuilder){ (sb, s) => sb append s }.toString
strings.mkString // same result, at least the same speed

回答by Rex Kerr

Don't use foldRightunless you really need it, as it will overflow your stack for large collections (for some types of collections). foldLeftor foldwill work (does not store intermediate data on the stack), but will be slower and more awkward than mkString. If the list is nonempty, reduceand reduceLeftwill also work.

foldRight除非你真的需要它,否则不要使用它,因为它会溢出你的大集合(对于某些类型的集合)的堆栈。 foldLeftorfold会工作(不会在堆栈上存储中间数据),但会比mkString. 如果列表不为空,reduce并且reduceLeft还将工作。

回答by cmbaxter

Im memory serves, mkStringuses a StringBuilder to build the String which is efficient. You could accomplish the same thing using a Scala StringBuilderas the accumulator to foldRight, but why bother if mkStringcan already do all that good stuff for you. Plus mkStringgives you the added benefit of also including an optional delimiter. You could do that in foldRightbut it's already done for you with mkString

我没有记错,mkString使用 StringBuilder 来构建高效的 String 。你可以使用 ScalaStringBuilder作为累加器来完成同样的事情foldRight,但是如果mkString已经可以为你做所有这些好事,为什么还要麻烦呢。PlusmkString为您提供了额外的好处,包括可选的分隔符。你可以这样做,foldRight但它已经为你完成了mkString