Skip to content

Instantly share code, notes, and snippets.

@x7c1
Created March 9, 2017 04:45
Show Gist options
  • Select an option

  • Save x7c1/f2bedd66b594bd19b326ea43e3ef46f5 to your computer and use it in GitHub Desktop.

Select an option

Save x7c1/f2bedd66b594bd19b326ea43e3ef46f5 to your computer and use it in GitHub Desktop.
object Sample {
def main(args: Array[String]): Unit = {
val x = Seq(
Seq("F", "F", "M", "M", "M") map ("SEX" -> _),
Seq(158, 162, 177, 173, 166) map ("HEIGHT" -> _),
Seq(51D, 55D, 72D, 57D, 64D) map ("WEIGHT" -> _)
).transpose
val heights = x flatMap (_ collect { case ("HEIGHT", n) => n })
println(heights) // List(158, 162, 177, 173, 166)
val mean = {
val weights = x flatMap (_ collect { case ("WEIGHT", n: Double) => n })
weights.sum / weights.length
}
println(mean) // 59.8
val seconds = x collect { case _ :+ ((_, n: Int)) :+ _ => n }
println(seconds) // List(158, 162, 177, 173, 166)
val line0 = x(0)
println(line0) // List((SEX,F), (HEIGHT,158), (WEIGHT,51.0))
val height0 = line0 collectFirst { case ("HEIGHT", n) => n }
println(height0) // Some(158)
}
}
@x7c1
Copy link
Author

x7c1 commented Mar 9, 2017

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