English 中文(简体)
B. 更好利用减排方法 案件类别清单中的左侧/左侧
原标题:better way of using reduceLeft/foldLeft on list of case class
  • 时间:2012-05-08 11:01:44
  •  标签:
  • scala

My intention is simple: to get comma seperated values of emaild from list of User objects. I have done this in Java with a for loop and if else conditions.

现在我想在Schala这样做,这样一来就是尝试。

case class User(name: String, email: String)

val userList = List(User("aaa", "[email protected]"),
                    User("bbb", "[email protected]"),
                    User("ccc", "[email protected]"))

现在

val mailIds = userList.foldLeft(""){(a: String, b: User) => ( b.email+","+a) } 

发放

[email protected],[email protected],[email protected],

(note the comma at the end.)

以及

val mailids = userList.map(x => x.email).reduceLeft(_+","+_)

发放

[email protected],[email protected],[email protected]

i 仅使用reduceLeft进行审判 ......

val emailids = userList.reduceLeft((emails: String, user: User) => emails+", "+user.email)

但它投下了汇编错误。

type mismatch;  found   : (String, User) => java.lang.String  required: (java.io.Serializable, User) => java.io.Serializable

因此,在上述情况下,是否有使用<代码>reduceLeft的<><><><>>>>>>><>>> >>>>> ><>>> > > 。

最佳回答

No, reduceLeft is a foldLeft where the accumulator is the first element of the collection. In your case this first element is a User and you have to pass an argument of the same type for the next step of the iteration. That is why you get a type missmatch.
You can do the map within a lazy collection and reduce this like the following:

val mailIds = userList.view map (_.email) reduceLeft (_ + "," + _)

http://www.un.org Jessie Eichar写道,在这方面,“指导

http://www.ohchr.org。

如果不收集 la,将通过以下方式删除最后 com:

val rawMailIds = userList.foldLeft("")((acc, elem) => acc + elem.email + ",")
val mailIds = rawMailIds.substring(0, rawMailIds.length - 1)
问题回答

为什么不要把地图结合起来,减少? 无论如何,你的问题是,你的<代码>redLeft<>/code”只有版本才能显示其初步价值。





相关问题
How to flatten a List of different types in Scala?

I have 4 elements:List[List[Object]] (Objects are different in each element) that I want to zip so that I can have a List[List[obj1],List[obj2],List[obj3],List[obj4]] I tried to zip them and I ...

To use or not to use Scala for new Java projects? [closed]

I m impressed with Twitter and investigating to use Scala for a new large scale web project with Hibernate and Wicket. What do you think about Scala, and should I use it instead of Java? EDIT: And, ...

Why does Scala create a ~/tmp directory when I run a script?

When I execute a Scala script from the command line, a directory named "tmp" is created in my home directory. It is always empty, so I simply deleted it without any apparent problem. Of course, when I ...

Include jar file in Scala interpreter

Is it possible to include a jar file run running the Scala interpreter? My code is working when I compile from scalac: scalac script.scala -classpath *.jar But I would like to be able to include a ...

Scala and tail recursion

There are various answers on Stack Overflow which explain the conditions under which tail recursion is possible in Scala. I understand the limitations and how and where I can take advantage of tail ...

热门标签