Created
February 26, 2012 14:01
-
-
Save Centaur/1916967 to your computer and use it in GitHub Desktop.
For Freewind
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
case class Message(username:String, content:String) | |
val list = List(Message("aaa", "111"), Message("aaa","222"), | |
Message("bbb","333"),Message("aaa", "444")) | |
import collection.mutable.ListBuffer | |
list.foldLeft(new ListBuffer[(String,ListBuffer[Message])]()){ | |
case (buff, item) => | |
if(buff.nonEmpty && buff.last._1 == item.username) buff.last._2 += item | |
else buff += (item.username->ListBuffer(item)) | |
buff | |
}.map{pair => (pair._1, pair._2.toList)}.toList |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
case class Message(username:String, content:String)
val list = List(Message("aaa", "111"), Message("aaa","222"),
Message("bbb","333"),Message("aaa", "444"))
if (list.isEmpty) Nil
else
for (List(m1, m2) <- list.sliding(2) )
yield if (m1.username == m2.username) m2.copy(username = "")
else m2