假设我有一个商店的邮政编码列表,我想找出与给定用户的邮政编码最小距离。为此,我在scala中编写了一些代码 -
case class UserCityDistance(city:String, zip:String, distance:Double)
private def getUserLocation(user_loc:String, store_loc:String)={
println(s"# Calculating distance between user_loc $user_loc AND warehouse_loc $store_loc #")
// Code here to calculate distance between users and store
// This will give 3 values - store city, store zip and distance from user's zip
return UserCityDistance(city, zip, distance)
}
我从同一个班级调用此方法 -
productStoreMap包含具有产品(密钥)的商店列表,user_zip包含用户的邮政编码。
val userDistanceList = productStoreMap.mapValues(_.map(x=>getUserLocation(user_zip,x))) // Get distance of each store from user's location
println(s"# Distance lists obtained - $userDistanceList #")
val minStore = userDistanceList.mapValues(_.minBy(_.distance)) // Selecting the minimum distant store
println(s"# Minimum distant warehouse - ${minWarehouse} #")
它的工作好。但是在打印出#Distance lists to HER - ##之后,它再次开始像以前一样计算用户zip和每个仓库拉链之间的距离,就像懒惰评估一样。
我的代码或我遗失的任何内容是否有任何问题?
编辑 -
productStoreMap的类型为 - Map [List [String],List [String]]
这会造成这个麻烦吗?