相关疑难解决方法(0)

函数式编程,Scala映射和向左折叠

折叠上有什么好的教程?

原始问题,从删除中恢复以提供其他答案的上下文:

我正在尝试实现一种方法来查找矩形,圆形,位置和所有扩展形状的组的boudning框.组基本上是一组形状

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  
Run Code Online (Sandbox Code Playgroud)

我得到了除第一组之外的所有三个计算的边界框.所以现在对于边界框方法我知道我应该使用map并向左折叠为Group,但我无法找到创建它的确切语法.

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 …
Run Code Online (Sandbox Code Playgroud)

functional-programming scala map fold

53
推荐指数
3
解决办法
5万
查看次数

标签 统计

fold ×1

functional-programming ×1

map ×1

scala ×1