Funktionale Programmierung, Scala Map und Fold Left [closed]
Welche guten Tutorials gibt es noch?
Ursprüngliche Frage, die nach dem Löschen wiederhergestellt wurde, um den Kontext für andere Antworten bereitzustellen:
Ich versuche, eine Methode zu implementieren, um die Boudning-Box aus Rechteck, Kreis, Position und der Gruppe zu finden, die Shape erweitert. Gruppe ist im Grunde eine Reihe von Formen
abstract class Shape
case class Rectangle(width: Int, height: Int) extends Shape
case class Location(x: Int, y: Int, shape: Shape) extends Shape
case class Circle(radius: Int) extends Shape
case class Group(shape: Shape*) extends Shape
Ich habe den Begrenzungsrahmen für alle drei außer für die erste Gruppe berechnen lassen. Jetzt weiß ich, dass ich für die Bounding-Box-Methode Map and Fold Left für Group verwenden sollte, aber ich kann die genaue Syntax für die Erstellung einfach nicht herausfinden.
object BoundingBox {
def boundingBox(s: Shape): Location = s match {
case Circle(c)=>
new Location(-c,-c,s)
case Rectangle(_, _) =>
new Location(0, 0, s)
case Location(x, y, shape) => {
val b = boundingBox(shape)
Location(x + b.x, y + b.y, b.shape)
}
case Group(shapes @ _*) => ( /: shapes) { } // i dont know how to proceed here.
}
}
Gruppenbegrenzungsrahmen ist im Grunde der kleinste Begrenzungsrahmen mit allen eingeschlossenen Formen.