Scala 应用程序中的 Val 不是 recognised/used
Val not recognised/used in Scala application
提供了以下文本文件:
Alabama (9),Democratic:849624,Republican:1441170,Libertarian:25176,Others:7312
Alaska (3),Democratic:153778,Republican:189951,Libertarian:8897,Others:6904
Arizona (11),Democratic:1672143,Republican:1661686,Libertarian:51465,Green:1557,Others:475
希望创建一个 Scala 应用程序 - 映射文本文件中的数据,然后通过该应用程序进行过滤。不确定为什么在以下代码块中未使用各方:
def readFile(filename: String): Map[(String, Int), Map[String, Int]] = {
val mapBuffer: Map[(String, Int), Map[String, Int]] = Map()
try {
for (line <- Source.fromFile(filename).getLines()) {
val strings = line.split("\n").toList
val parties = strings.map { x =>
val entries = x.split(",").toList
val stateAndCode = entries.head
val state = stateAndCode.takeWhile(_ != '(').trim
val code = stateAndCode.dropWhile(_ != '(').takeWhile(_ != ')').toInt
val votes = entries.tail.map { x =>
val Array(party, number) = x.split(":")
(party.trim, number.toInt)
}
((state, code), votes.toMap)}.groupBy(_._1).view.mapValues(_.head).toMap
}
}
catch {
case ex: Exception => println("Sorry, an exception happened.")
}
mapBuffer
}
非常感谢有关如何改进此块的任何反馈。地图的第一次使用是 return 基本上来自地图的所有信息 - 这是给出的指令:
获取所有 state/district 值并以适当的格式显示。
尝试实现如下:
def stateVotes = parties.keySet.map{case (party, code) => s"Party(district):", "\n", "\n" )
如有任何帮助,我们将不胜感激。
Would appreciate any feedback on how to improve this block.
我的建议:重新开始。
val dataRE = "([^(]+) \((\d+)\),(.+)".r
val pVotes = "([^:]+):(\d+)".r
case class State(name : String
,code : Int
,parties : Array[(String,Int)])
val states: List[State] =
util.Using(io.Source.fromFile("data.txt"))(_.getLines().toList)
.get //will throw if read file fails
.collect{case dataRE(name,code,votes) =>
State(name.trim
,code.toInt
,votes.split(",")
.collect{case pVotes(p,v) => (p,v.toInt)}
)
}
在将所有数据收集到 List
个 State
实例之后,您只需要一些工具来操作和格式化列表和字符串。
例如,您想以一致且井井有条的方式打印出所有数据。
states.sortBy(_.name) //alphabetical
.foreach{ st =>
println(st.name) //state name
st.parties
.sortBy(-_._2) //votes in decreasing order
.map{case (p,v) => f"\t$p%-12s:$v%9d"}
.foreach(println)
}
结果:
Alabama
Republican : 1441170
Democratic : 849624
Libertarian : 25176
Others : 7312
Alaska
Republican : 189951
Democratic : 153778
Libertarian : 8897
Others : 6904
Arizona
Democratic : 1672143
Republican : 1661686
Libertarian : 51465
Green : 1557
Others : 475
或者您可能希望查看每个州的总票数。
states.map(st => (st.name, st.parties.map(_._2).sum))
.sortBy(-_._2) //votes in decreasing order
.map{case (state,total) => f"$state%-9s:$total%8d"}
.foreach(println)
结果:
Arizona : 3387326
Alabama : 2323282
Alaska : 359530
你懂的。
提供了以下文本文件:
Alabama (9),Democratic:849624,Republican:1441170,Libertarian:25176,Others:7312
Alaska (3),Democratic:153778,Republican:189951,Libertarian:8897,Others:6904
Arizona (11),Democratic:1672143,Republican:1661686,Libertarian:51465,Green:1557,Others:475
希望创建一个 Scala 应用程序 - 映射文本文件中的数据,然后通过该应用程序进行过滤。不确定为什么在以下代码块中未使用各方:
def readFile(filename: String): Map[(String, Int), Map[String, Int]] = {
val mapBuffer: Map[(String, Int), Map[String, Int]] = Map()
try {
for (line <- Source.fromFile(filename).getLines()) {
val strings = line.split("\n").toList
val parties = strings.map { x =>
val entries = x.split(",").toList
val stateAndCode = entries.head
val state = stateAndCode.takeWhile(_ != '(').trim
val code = stateAndCode.dropWhile(_ != '(').takeWhile(_ != ')').toInt
val votes = entries.tail.map { x =>
val Array(party, number) = x.split(":")
(party.trim, number.toInt)
}
((state, code), votes.toMap)}.groupBy(_._1).view.mapValues(_.head).toMap
}
}
catch {
case ex: Exception => println("Sorry, an exception happened.")
}
mapBuffer
}
非常感谢有关如何改进此块的任何反馈。地图的第一次使用是 return 基本上来自地图的所有信息 - 这是给出的指令:
获取所有 state/district 值并以适当的格式显示。
尝试实现如下:
def stateVotes = parties.keySet.map{case (party, code) => s"Party(district):", "\n", "\n" )
如有任何帮助,我们将不胜感激。
Would appreciate any feedback on how to improve this block.
我的建议:重新开始。
val dataRE = "([^(]+) \((\d+)\),(.+)".r
val pVotes = "([^:]+):(\d+)".r
case class State(name : String
,code : Int
,parties : Array[(String,Int)])
val states: List[State] =
util.Using(io.Source.fromFile("data.txt"))(_.getLines().toList)
.get //will throw if read file fails
.collect{case dataRE(name,code,votes) =>
State(name.trim
,code.toInt
,votes.split(",")
.collect{case pVotes(p,v) => (p,v.toInt)}
)
}
在将所有数据收集到 List
个 State
实例之后,您只需要一些工具来操作和格式化列表和字符串。
例如,您想以一致且井井有条的方式打印出所有数据。
states.sortBy(_.name) //alphabetical
.foreach{ st =>
println(st.name) //state name
st.parties
.sortBy(-_._2) //votes in decreasing order
.map{case (p,v) => f"\t$p%-12s:$v%9d"}
.foreach(println)
}
结果:
Alabama
Republican : 1441170
Democratic : 849624
Libertarian : 25176
Others : 7312
Alaska
Republican : 189951
Democratic : 153778
Libertarian : 8897
Others : 6904
Arizona
Democratic : 1672143
Republican : 1661686
Libertarian : 51465
Green : 1557
Others : 475
或者您可能希望查看每个州的总票数。
states.map(st => (st.name, st.parties.map(_._2).sum))
.sortBy(-_._2) //votes in decreasing order
.map{case (state,total) => f"$state%-9s:$total%8d"}
.foreach(println)
结果:
Arizona : 3387326
Alabama : 2323282
Alaska : 359530
你懂的。