【发布时间】:2015-09-25 15:28:38
【问题描述】:
假设我有一张包含多属性值的地图,我想从中选择一个特定的属性。
例如,一张地图,表示一张包含姓名、性别、年龄、描述的人的表格。
在 SQL 中,我会写“从 name='whomever' 的人中选择年龄”
如何在 Scala 中获得这种效果?
val people = Map(
"Walter White" -> ("male",52,"bad boy"),
"Skyler White" -> ("female",42,"morally challenged mom")
)
// equivalent of select * from people. This works.
for ((name,(gender,age,desc)) <- people) println(s"$name is a $age year old $gender and is a $desc")
// what should be the syntax to get "the age of Walter White is 52"?
// in SQL, it would be "'The age of Walter White is ' || (select age from people where name='Walter White')"
// what would it be in Scala?
println("The age of Walter White is " + people("Walter White")(1)) // not this!
【问题讨论】: