哪个Scala函数可以替换这个程序语句?
发布时间:2020-12-16 18:26:03 所属栏目:安全 来源:网络整理
导读: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(interse
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 } 你能看到我在做什么吗?我有一个球体列表,其中每个球体都有一个方法intersectRay,返回一个双精度. 我想采用具有该函数最小结果的Sphere.我知道有一个很好的功能构造让我在一行中做到这一点,我只是看不到它:( 解决方法
你可以这样做:
def getClosestSphere(ray: Ray,spheres: List[Sphere]): Sphere = { spheres.minBy(_ intersectRay ray) } 一个样式提示,作为旁边: 使用可变集合时,请使用合格的导入.对于java.util.SomeCollection,首先导入java.{util => ju},然后使用名称ju.SomeCollection.对于scala.collection.mutable.SomeCollection,首先导入collection.mutable,然后使用名称mutable.SomeCollection. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |