split - Scala Pattern matching leading to two outputs -
i matching text file has columns [string double double double double]. obtain next each row of file [string double double] , [string double double] wherein string label same both splitting first 2 doubles , lastly 2 doubles 2 independent rows.
i using next not working:
val out = source.fromfile(filename).getlines.collect(_.split("\\s+").tolist match { case s1 :: points1 :: points2 => (s1,"4",point(points1.map(_.todouble).toindexedseq)) => (s1,"6",point(points2.map(_.todouble).toindexedseq)) my doubles co-ordinates of points.
first of points1 matches sec column , points2 rest columns. it's because in :: notation left side first element of list (head), sec rest sublist (tail).
it may easy decompose row list of columns this:
... match { case s1 :: p1x :: p1y :: p2x :: p2y :: nil => then can compose 1 time again 2 rows, placing them in two-element list:
=> list( (s1,"4",point(vector(p1x,p1y).map(_.todouble))), (s1,"6",point(vector(p2x,p2y).map(_.todouble))) ) but then, in result you'll have list[list[..]], need flatten it. simplest way utilize flatmap instead of collect.
so total code this:
val out = source.fromfile(filename).getlines.flatmap(_.split("\\s+").tolist match { case s1 :: p1x :: p1y :: p2x :: p2y :: nil => list( (s1,"4",point(vector(p1x,p1y).map(_.todouble))), (s1,"6",point(vector(p2x,p2y).map(_.todouble))) ) }) scala split match case
No comments:
Post a Comment