[英]Scala - Vector contains (types comparison)
I'm trying to check in a Vector of "paths" which ones contain all the stops wanted. 我正在尝试检查“路径”的向量,其中包含所需的所有停靠点。 I already created a function that gives all the paths that have a given stop.
我已经创建了一个函数,该函数给出具有给定停止点的所有路径。
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)))
)
}
I'm trying to check if the Vector contains the path wanted : 我正在尝试检查Vector是否包含所需的路径:
pathList.paths.filter(path => stopsWanted.forall(stopWanted => pathIncludesPoint(pathList, stopWanted).contains(path)))
but the last path return an error because I'm comparing a Vector[Path] (what returns the function "pathIncludesPoint") and a Path. 但最后一条路径返回错误,因为我正在比较Vector [Path](返回函数“ pathIncludesPoint”的内容)和Path。 I don't understand using the scala Library my error.
我不理解使用scala库出现了我的错误。
Thanks! 谢谢!
Here is the structure of Path and PathList if needed : 如果需要,这是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]) {
}
The error occurs because pathIncludesPoint(pathList, stopWanted)
has type Option[Vector[Path]]
, so your .contains(path)
is actually working on the Option
, not on the Vector
. 发生错误是因为
pathIncludesPoint(pathList, stopWanted)
类型为Option[Vector[Path]]
,因此您的.contains(path)
实际上是在Option
上工作,而不是在Vector
。
To fix this, maybe you can drop some uses of Option
and just return an empty Vector
where you currently return None
? 要解决此问题,也许您可以放弃使用
Option
某些用途,而只需在当前返回None
地方返回一个空的Vector
。
Or if you want to keep all uses of Option
and just want to fix the line with the .contains
, you can use .exists
as follows: 或者,如果您想保留
Option
所有使用,而只想用.contains
修改行,则可以如下使用.exists
:
pathIncludesPoint(pathList, stopWanted).exists(_.contains(path))
Here, the .exists
handles the Option
and the .contains
handles the Vector
. 在这里,
.exists
处理Option
,而.contains
处理Vector
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.