Question

MindBlock ici, mais je ne peux pas comprendre comment rendre cela moins laid:

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  
  }

Pouvez-vous voir ce que je fais? J'ai une liste de sphères, où chaque sphère a une méthode intersective, renvoyant un double.

Je veux prendre la sphère avec le plus petit résultat de cette fonction. Je sais qu'il y a une belle construction fonctionnelle pour me laisser faire cela en une seule ligne, je ne peux pas le voir :(

Pas de solution correcte

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top