Skip to content

Instantly share code, notes, and snippets.

@Centaur
Created February 26, 2012 14:01
Show Gist options
  • Save Centaur/1916967 to your computer and use it in GitHub Desktop.
Save Centaur/1916967 to your computer and use it in GitHub Desktop.
For Freewind
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
@linqing
Copy link

linqing commented Feb 26, 2012

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment