更新不可变对象 [英] Updating immutable objects

查看:102
本文介绍了更新不可变对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  class Player(val name:String,val onField:Boolean,val draft: Int,val perc:Int,val height:Int,val timePlayed:Int){
override def toString:String = name

}



我想要做的是

  def play(team:List [Player]):List [Player] = 
team map(p => new Player(p.name,p.onField,p.draft,p.perc, p.height,p.timePlayed + 1))

这实际上增加了字段timePlayed一个,并返回新的列表的球员。



有没有更方便的方法来做到这一点?也许:

  def play(team:List [Player]):List [Player] = 
team map(p => p.timeIncremented())

我的问题是如何在更多方便的方法?所以我不必这样做:

 新玩家(p.name,p.onField,p.draft,p .perc,p.height,p.timePlayed + 1)

谢谢!

解决方案

您可以使用define Player作为 case class 并使用编译器生成的方法 copy

  case class Player(name:String,onField:Boolean,draft:Int, perc:Int,height:Int,timePlayed:Int){
override def toString:String = name
}

def play(team:List [Player]):List [ Player] =
team map(p => p.copy(timePlayed = p.timePlayed + 1))

另外,正如你所看到的,默认情况下,构造函数的参数是 val



播放器中定义 timeIncremented ,并按照您的要求使用它:

  case class Player(name:String,onField:Boolean,draft:Int,perc:Int,height:Int,timePlayed:Int){ 
覆盖def toString:String = name
def timeIncremented:Player = copy(timePlayed = this.timePlayed + 1)
}

def play(team:List [ Player]):List [Player] =
team map(_.timeIncremented)

更复杂的情况下,你可以看看镜头:

http://akisaarinen.fi/blog/2012/12/07/boilerplate-free-functional-lenses-for-scala/

更新嵌套结构的方法


I have the following class built:

class Player(val name: String, val onField: Boolean, val draft: Int, val perc: Int, val height: Int, val timePlayed: Int) {
override def toString: String = name

}

I'm trying to do

def play(team: List[Player]): List[Player] =
team map (p => new Player(p.name, p.onField, p.draft, p.perc, p.height, p.timePlayed + 1))

which is actually incrementing the field "timePlayed" by one, and return the new "List" of players.

Is there a more convenient way to do this? Perhaps:

def play(team: List[Player]): List[Player] =
team map (p => p.timeIncremented())

My question is how to implement timeIncremented() in a more convenient way? so that I don't have to do:

new Player(p.name, p.onField, p.draft, p.perc, p.height, p.timePlayed + 1)

Thanks!

解决方案

You can use define Player as case class and use compiler generated method copy:

case class Player(name: String, onField: Boolean, draft: Int, perc: Int, height: Int, timePlayed: Int) {
    override def toString: String = name
}

def play(team: List[Player]): List[Player] =
    team map (p => p.copy(timePlayed = p.timePlayed + 1))

Also, as you see, constructor parameters are val by default.

And you can define timeIncremented in Player and use it exactly as you want:

case class Player(name: String, onField: Boolean, draft: Int, perc: Int, height: Int, timePlayed: Int) {
    override def toString: String = name
    def timeIncremented: Player = copy(timePlayed = this.timePlayed + 1)
}

def play(team: List[Player]): List[Player] =
    team map (_.timeIncremented)

For more complex cases you can take a look at lenses:
http://akisaarinen.fi/blog/2012/12/07/boilerplate-free-functional-lenses-for-scala/
Cleaner way to update nested structures

这篇关于更新不可变对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆