Scala 如何映射具有嵌套for循环的两个结构?

Scala 如何映射具有嵌套for循环的两个结构?,scala,for-loop,Scala,For Loop,我有两种结构;'类型为(Double,Double)的pointsArray和类型为类列表[BoundingBox]的ListObbs。我想将每个点映射到一个边界框。这是我正在尝试的代码,它运行时给出了一个空列表“()” var PointsMappedtoGrids1=Map[(双精度,双精度),BoundingBox]() 对于(点如果您不想进行大的更改,则可以尝试PointsMappedtoGrids1=PointsMappedtoGrids1+(点->eachBB)而不是使用Points

我有两种结构;'类型为(Double,Double)的pointsArray和类型为类列表[BoundingBox]的ListObbs。我想将每个点映射到一个边界框。这是我正在尝试的代码,它运行时给出了一个空列表“()”

var PointsMappedtoGrids1=Map[(双精度,双精度),BoundingBox]()

对于(点如果您不想进行大的更改,则可以尝试
PointsMappedtoGrids1=PointsMappedtoGrids1+(点->eachBB)
而不是使用
PointsMappedtoGrids1+(点->eachBB)
,但如果您只使用
map
就更好了,因为
嵌套for loops
将返回
单元
数据类型。

如果您不想做大的更改,那么您可以尝试
PointsMappedtoGrids1=PointsMappedtoGrids1+(point->eachBB)
而不是使用
PointsMappedtoGrids1+(point->->eachBB)
,但如果只使用
map
就更好了,因为
嵌套for循环
将返回
单元
数据类型。

我认为这就是您要寻找的:

假定BoundingBox是一个案例类,结构如下:

case class BoundingBox(lowerLeft: (Double, Double), lowerRight: (Double, Double), upperLeft: (Double, Double),
                         upperRight: (Double, Double))

  val pointArray: Array[(Double, Double)] = Array((1, 2), (2, 1), (3, 3), (4, 8), (5, 6))

val listOfBBs = List(BoundingBox((1, 2), (2, 3), (4, 5), (6, 7)))

  val x: Array[Map[(Double, Double), BoundingBox]] =for {
    point <- pointArray
    eachBB <- listOfBBs

    if point._1 >= eachBB.lowerLeft._1 &&
      point._1 <= eachBB.upperRight._1 &&
      point._2 >= eachBB.lowerLeft._2 &&
      point._2 <= eachBB.upperRight._2
  } yield PointsMappedtoGrids1 + (point -> eachBB)
大小写类边界框(lowerLeft:(Double,Double),lowerRight:(Double,Double),upperLeft:(Double,Double),
右上:(双人,双人)
val pointArray:Array[(Double,Double)]=数组((1,2)、(2,1)、(3,3)、(4,8)、(5,6))
val LISTOFBS=列表(边界框((1,2)、(2,3)、(4,5)、(6,7)))
val x:数组[映射[(双精度,双精度),边界框]]=for{

点我想这就是你想要的:

假定BoundingBox是一个案例类,结构如下:

case class BoundingBox(lowerLeft: (Double, Double), lowerRight: (Double, Double), upperLeft: (Double, Double),
                         upperRight: (Double, Double))

  val pointArray: Array[(Double, Double)] = Array((1, 2), (2, 1), (3, 3), (4, 8), (5, 6))

val listOfBBs = List(BoundingBox((1, 2), (2, 3), (4, 5), (6, 7)))

  val x: Array[Map[(Double, Double), BoundingBox]] =for {
    point <- pointArray
    eachBB <- listOfBBs

    if point._1 >= eachBB.lowerLeft._1 &&
      point._1 <= eachBB.upperRight._1 &&
      point._2 >= eachBB.lowerLeft._2 &&
      point._2 <= eachBB.upperRight._2
  } yield PointsMappedtoGrids1 + (point -> eachBB)
大小写类边界框(lowerLeft:(Double,Double),lowerRight:(Double,Double),upperLeft:(Double,Double),
右上:(双人,双人)
val pointArray:Array[(Double,Double)]=数组((1,2)、(2,1)、(3,3)、(4,8)、(5,6))
val LISTOFBS=列表(边界框((1,2)、(2,3)、(4,5)、(6,7)))
val x:数组[映射[(双精度,双精度),边界框]]=for{

点这里是一个更实用的逻辑版本:

val PointsMappedtoGrids1: Map[(Double,Double),BoundingBox] =
  (for {
    point <- pointsArray
    bb <- listOfBBs
    if point._1 >= bb.lowerleft._1 &&
       point._1 <= bb.upperright._1 &&
       point._2 >= bb.lowerleft._2 &&
       point._2 <= bb.upperright._2
  } yield point -> bb
  )(collection.breakOut)
val PointsMappedtoGrids1:Map[(双精度,双精度),边界框]=
(用于{

点这里是一个更实用的逻辑版本:

val PointsMappedtoGrids1: Map[(Double,Double),BoundingBox] =
  (for {
    point <- pointsArray
    bb <- listOfBBs
    if point._1 >= bb.lowerleft._1 &&
       point._1 <= bb.upperright._1 &&
       point._2 >= bb.lowerleft._2 &&
       point._2 <= bb.upperright._2
  } yield point -> bb
  )(collection.breakOut)
val PointsMappedtoGrids1:Map[(双精度,双精度),边界框]=
(用于{
指向