[英]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.