Dom*_*mra 4 procedural functional-programming scala
Mindblock在这里,但我无法弄清楚如何使这不那么难看:
def getClosestSphere(ray: Ray, spheres: List[Sphere]): Sphere = {
val map = new HashMap[Double, Sphere]
for (sphere <- spheres) {
val intersectPoint = sphere.intersectRay(ray)
map.put(intersectPoint, sphere)
}
map.minBy(_._1)._2
}
Run Code Online (Sandbox Code Playgroud)
你能看到我在做什么吗?我有一个球体列表,其中每个球体都有一个方法intersectRay,返回一个双精度.
我想采用具有该函数最小结果的Sphere.我知道有一个很好的功能构造让我在一行中做到这一点,我只是看不到它:(
mis*_*tor 14
你可以这样做:
def getClosestSphere(ray: Ray, spheres: List[Sphere]): Sphere = {
spheres.minBy(_ intersectRay ray)
}
Run Code Online (Sandbox Code Playgroud)
一个样式提示,作为旁边:
使用可变集合时,请使用合格的导入.对于java.util.SomeCollection,第一import java.{util => ju},然后使用该名称ju.SomeCollection.对于scala.collection.mutable.SomeCollection,第一import collection.mutable,然后使用该名称mutable.SomeCollection.
| 归档时间: |
|
| 查看次数: |
179 次 |
| 最近记录: |