Skip to content

Instantly share code, notes, and snippets.

@songfei1983
Last active December 23, 2015 01:01
Show Gist options
  • Save songfei1983/08030cbd03cb481b814d to your computer and use it in GitHub Desktop.
Save songfei1983/08030cbd03cb481b814d to your computer and use it in GitHub Desktop.
不等長リストの入れ替え ref: http://qiita.com/songfei1983/items/2a19b1772889e5231516
/*
List(
List(1,2,3,4),
List(5,6,7,8),
List(9,0)
)
List(
List(1,5,9),
List(2,6,0),
List(3,7),
List(4,8)
)*/
case class Player(id: Int, name: String, score: Int)
val t: List[(Int, String, Int)] =
(1 to 17) zip ('a' to 'z') map (x => (x._1, x._2.toString, scala.util.Random.nextInt(100))) toList
val players = t.map(x => Player(x._1, x._2, x._3))
val sortedPlayers = players.sortBy(x => (- x.score, x.id))
def transpose[A](xs: List[List[A]]): List[List[A]] = xs.filter(_.nonEmpty) match {
case Nil => Nil
case ys: List[List[A]] => ys.map{ _.head }::transpose(ys.map{ _.tail })
}
transpose(sortedPlayers.grouped(4).toList)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment