Here is the structure : - Points - Segments - Paths
case class Point(name: String, x: Long, y: Long)
case class Segment(from: Point, to: Point)
case class Path(segments: Vector[Segment])
I'm trying to find all the possibles paths from a list of segments available to join two points (from and to). Here is my function :
def allPossiblePaths(segments: Vector[Segment], from: Point, to: Point) : Option[Vector[Path]] = {
if (from == to) Option(Vector())
for {
segment <- segments.filter(segment => segment.from == from)
nextSegment <- segments.filter(segmentSuivant => segmentSuivant.from == segment.to)
if nextSegment.to != segment.from
} yield allPossiblePaths(segments.filter(segment => segment.from == from) ,segment.to, nextSegment.to)
}
If I try :
allPossiblePaths(topSegments, tl, tr)
with :
val tl = Point("tl", 0, -10)
val t = Point("t", 0, 0)
val tr = Point("tr", 0, 10)
// tl - t - tr
// | | |
// bl - b --- br
// Segments
val tlt = Segment(tl, t)
val tlbl = Segment(tl, bl)
val tb = Segment(t, b)
val ttr = Segment(t, tr)
val topSegments = Vector(tlt, ttr, bbr)
I have this error :
Error:(63, 15) type mismatch;
found : scala.collection.immutable.Vector[Option[Vector[chemins.Path]]]
required: Option[Vector[chemins.Path]]
segment <- segments.filter(segment => segment.from == from)
But when I do
for {
segment <- segments.filter(segment => segment.from == from)
}
I am using the for on a Vector[Segment] so I don't understand why the "scala.collection.immutable.Vector" appears
Thanks in advance !
Edit 1
Introduced a class "PathList" :
case class PathList(paths: Vector[Path])
Changed the code adding the "else" and "some"
def allPossiblePaths(segments: Vector[Segment], from: Point, to: Point) : Option[PathList] = {
if (from == to) Some(PathList(Vector()))
else {
for {
segment <- segments.filter(segment => segment.from == from)
nextSegment <- segments.filter(segmentSuivant => segmentSuivant.from == segment.to)
if nextSegment.to != segment.from
} yield allPossiblePaths(segments.filter(segment => segment.from == from), segment.to, nextSegment.to)
}
}
The error hasn't really changed :
Error:(65, 17) type mismatch;
found : scala.collection.immutable.Vector[chemins.PathList]
required: chemins.PathList
segment <- segments.filter(segment => segment.from == from)
Edit 2
Tried to not specify the type of return and it does compile
def allPossiblePaths( segments: Vector[Segment], from: Point, to: Point) {
if (from == to) Path(Vector())
else {
for {
segment <- segments.filter(segment => segment.from == from)
nextSegment <- segments.filter(segmentSuivant => segmentSuivant.from == segment.to)
if nextSegment.to != segment.from
} yield allPossiblePaths(segments.filter(segment => segment.from == from), segment.to, nextSegment.to)
}
}
it returns :
Expected :Some(Path(Vector(Segment(Point(tl,0,-10),Point(t,0,0)), Segment(Point(t,0,0),Point(b,10,0)), Segment(Point(b,10,0),Point(br,10,20)))))
Actual :<(), the Unit value>
Well the result it's not what what I'm expecting but it's something I guess
segmentsis aVector, so after thefilteryou will still get aVector. Also it seems you missed theelseinif(and it might be better to express the intent to returnSome(Vector())in the then part ofifwithout usingOption#apply.) - Gábor BakosVectorand not anOption. Not sure why you wantOption, what it should mean. - Gábor Bakos