scala - For multiple generators to handle Seq -
i'm new scala, , want unique seq[(int,int)] first component, code follow:
val seq = seq((1,1), (0,1), (2,1), (0, 1), (3,1), (2,1)) val prev = -1 val uniqueseq = for(tuple <- seq.sortby(_._1) if !tuple._1.equals(prev); prev = tuple._1) yield tuple
but why result is
uniqueseq: seq[(int, int)] = list((0,1), (0,1), (1,1), (2,1), (2,1), (3,1))
prev
in prev = tuple._1
different variable val prev = -1
! note compiles though first prev
val
, i.e. immutable (it can't changed).
if want use approach, can:
val seq = seq((1,1), (0,1), (2,1), (0, 1), (3,1), (2,1)) var prev = -1 val uniqueseq = for(tuple <- seq.sortby(_._1) if !tuple._1.equals(prev)) yield { prev = tuple._1; tuple }
but isn't idiomatic 1 in scala. i'll leave else, since don't have enough time right now.
Comments
Post a Comment