簡體   English   中英

Scala-向量包含(類型比較)

[英]Scala - Vector contains (types comparison)

我正在嘗試檢查“路徑”的向量,其中包含所需的所有停靠點。 我已經創建了一個函數,該函數給出具有給定停止點的所有路徑。

    def pathIncludesPoint(pathList: PathList, stopWanted: Point): Option[Vector[Path]] = {

     if (pathList.paths.isEmpty) None

     else Some(

       for {
         path <- pathList.paths
         stop <- path.stops
         if stop.contains(stopWanted)
       } yield path)

    }

   def pathIncludesListOfPoint(pathList: PathList, stopsWanted: Vector[Point]): Option[Vector[Path]] = {

      if (pathList.paths.isEmpty) None

      else Some(

        pathList.paths.filter(path => stopsWanted.forall(stopWanted => pathIncludesPoint(pathList, stopWanted).contains(path)))

      )

   }

我正在嘗試檢查Vector是否包含所需的路徑:

pathList.paths.filter(path => stopsWanted.forall(stopWanted => pathIncludesPoint(pathList, stopWanted).contains(path)))

但最后一條路徑返回錯誤,因為我正在比較Vector [Path](返回函數“ pathIncludesPoint”的內容)和Path。 我不理解使用scala庫出現了我的錯誤。

謝謝!

如果需要,這是Path和PathList的結構:

case class Path(segments: Vector[Segment]) {

  def stops: Option[Vector[Point]] = {

    if (segments.isEmpty) None

    else Some({

      for {
        segment <- segments
      } yield segment.from

     }.tail)}

}



case class PathList(paths: Vector[Path]) {

}

在此處輸入圖片說明

發生錯誤是因為pathIncludesPoint(pathList, stopWanted)類型為Option[Vector[Path]] ,因此您的.contains(path)實際上是在Option上工作,而不是在Vector

要解決此問題,也許您可​​以放棄使用Option某些用途,而只需在當前返回None地方返回一個空的Vector

或者,如果您想保留Option所有使用,而只想用.contains修改行,則可以如下使用.exists

pathIncludesPoint(pathList, stopWanted).exists(_.contains(path))

在這里, .exists處理Option ,而.contains處理Vector

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM