list 单个列表中的字符串连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7396083/
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-09-11 01:42:52  来源:igfitidea点击:

String concatenation from within a single list

listscalaconcatenation

提问by John Redyns

Scala is new to me so I'm not sure the best way to go about this.

Scala 对我来说是新的,所以我不确定最好的方法来解决这个问题。

I need to simply take the strings within a single list and join them. So, concat(List("a","b","c"))returns abc.

我需要简单地将字符串放在一个列表中并加入它们。所以,concat(List("a","b","c"))返回abc

Should I first see how many strings there are in the list, that way I can just loop through and join them all? I feel like that needs to be done first, that way you can use the lists just like an array and do list[1]append list[2]append list[3], etc..

我应该先看看列表中有多少个字符串,这样我就可以循环并加入它们吗?我觉得这需要先完成,这样您就可以像使用数组一样使用列表并执行list[1]append list[2]appendlist[3]等。

Edit:

编辑

Here's my idea, of course with compile errors..

这是我的想法,当然有编译错误..

def concat(l: List[String]): String = {
var len = l.length
var i = 0
    while (i < len) {
        val result = result :: l(i) + " "
    }
result
}

回答by Abhishek Tiwari

How about this, on REPL

这个怎么样,在 REPL 上

List("a","b","c") mkString("")

or in script file

或在脚本文件中

List("a","b","c").mkString("")

回答by maasg

Some options to explore for you:

一些可供您探索的选项:

  1. imperative: for-loop; use methods from the List object to determine loop length or use for-each List item
  2. classical functional: recursive function, one element at the time using
  3. higher-order functions: look at fold.
  1. 命令式:for循环;使用 List 对象中的方法来确定循环长度或使用 for-each List 项
  2. 经典泛函:递归函数,一次使用一个元素
  3. 高阶函数:看折叠。

Given the basic level of the problem, I think you're looking at learning some fundamentals in programming. If the language of choice is Scala, probably the focus is on functional programming, so I'd put effort on solving #2, then solve #1. #3 for extra credits.

鉴于问题的基本水平,我认为您正在学习一些编程基础知识。如果选择的语言是 Scala,可能重点是函数式编程,所以我会努力解决 #2,然后解决 #1。#3 获得额外学分。

回答by asthasr

This exercise is designed to encourage you to think about the problem from a functional perspective. You have a set of data over which you wish to move, performing a set of identical operations. You've already identified the imperative, looping construct (for). Simple enough. Now, how would you build that into a functional construct, not relying on "stateful" looping?

本练习旨在鼓励您从功能角度思考问题。您有一组要移动的数据,执行一组相同的操作。您已经确定了命令式循环结构 ( for)。足够简单。现在,您将如何将其构建到一个函数式构造中,而不依赖于“有状态”循环?

回答by Landei

In functional programming, fold ... is a family of higher-order functions that iterate an arbitrary function over a data structure in some order and build up a return value.

在函数式编程中, fold ... 是一系列高阶函数,它们以某种顺序在数据结构上迭代任意函数并构建返回值。

http://en.wikipedia.org/wiki/Fold_%28higher-order_function%29

http://en.wikipedia.org/wiki/Fold_%28higher-order_function%29

That sounds like something you could use.

这听起来像是你可以使用的东西。

As string concatenation is associative (to be exact, it forms a monoid having the empty String as neutral element), the "direction" of the fold doesn't matter (at least if you're not bothered by performance).

由于字符串连接是关联的(确切地说,它形成了一个以空字符串作为中性元素的幺半群),折叠的“方向”并不重要(至少如果你不为性能所困扰)。

Speaking of performance: In real life, it would be a good idea to use a StringBuilderfor the intermediate steps, but it's up to you if you want to use it.

说到性能:在现实生活中,将 aStringBuilder用于中间步骤是个好主意,但是否要使用它取决于您。

回答by agilesteel

I'm just assuming here that you are not only new to Scala, but also new to programming in general. I'm not saying SO is not made for newbies, but I'm sure there are many other places, which are better suited for your needs. For example books...

我只是在这里假设您不仅对 Scala 不熟悉,而且对一般的编程也很陌生。我并不是说 SO 不适合新手,但我确信还有很多其他地方更适合您的需求。比如书...

I'm also assuming that your problem doesn't have to be solved in a functional, imperative or some other way. It just has to be solved as a homework assignment.

我还假设您的问题不必以函数式、命令式或其他方式解决。它只需要作为家庭作业来解决。

So here are the list of things you should consider / ask yourself:

所以这里是你应该考虑/问问自己的事情清单:

  • If you want to concat allelements of the list do you really need to know how many there are?
  • If you think you do, fine, but after having solved this problem using this approach try to fiddle around with your solution a little bit to find out if there is another way.
  • Appending the elements to a resulting list is a thought in right direction, but think about this: in addition to being object-oriented Scala is also a full-blown functional language. You might not know what this means, but all you need to know for now is this: it is pretty darn good with things like lists (LISP is the most known functional language and it stands for LISt Processing, which has to be an indication of some kind, don't you think? ;)). So maybe there is some magical (maybe even Scala idiomatic) way to accomplish such a concatination without defining the resulting list yourself.
  • 如果你想连接列表的所有元素,你真的需要知道有多少吗?
  • 如果你认为你这样做了,那很好,但是在使用这种方法解决了这个问题之后,试着稍微摆弄一下你的解决方案,看看是否还有另一种方法。
  • 将元素附加到结果列表中是一个正确方向的想法,但请考虑一下:除了面向对象之外,Scala 还是一种成熟的函数式语言。你可能不知道这意味着什么,但你现在需要知道的是:它非常适合列表之类的东西(LISP 是最知名的函数式语言,它代表 LISt Processing,它必须表明某种,你不觉得吗?;))。所以也许有一些神奇的(甚至可能是 Scala 惯用的)方法来完成这样的连接,而无需自己定义结果列表。

回答by Grigory Kislin

A bit longer that mkString but more efficient:

比 mkString 长一点但效率更高:

s.foldLeft(new StringBuilder())(_ append _).toString()

s.foldLeft(new StringBuilder())(_ append _).toString()