LINQ SelectMany equivalent in Scala -


i have quite simple .net logic i'm transplanting scala codebase, , don't know first thing scala. includes linq query groups collection of tagged objects making use of anonymous type projection flatten , join, followed grouping, eg:

var q = things.selectmany(t => t.tags, (t, tag) => new { thing = t, tag = tag })               .groupby(x => x.tag, x => x.thing); 

in scala looks flatmap might of use, can't figure out how combine groupby via anonymous.

is kind of thing lot more complicated in scala, or missing simple?

update:

i ended going with:

things.flatmap(t => t.tags.map(x => (x,t))).groupby(x => x._1) 

and of course later on when access value in map need do:

.map(x => x._2)  

to groups out of tuple.

simple when know how!

seems me want like.

case class tag(tag:string)  case class thing(tags : seq[tag])  val things :seq[thing] = seq(thing(seq(tag(""))))  val q = things.map {   thing => new {     val thing = thing     val tags = thing.tags   } }.flatmap {   thingandtags => thingandtags.tags.map {     tag => new {       val thing = thingandtags.thing       val tag = tag     }   } }. groupby {   thingandtag => thingandtag.tag }.map {   tagandseqofthingandtags =>     tagandseqofthingandtags._1 -> tagandseqofthingandtags._2.map(x => x.thing) } 

but in scala anonymous objects not common can use tuple2[t1,t2] instead of new { val ...}s,

val q = things.map {   thing => ( thing->thing.tags) }.flatmap {   thingandtags => thingandtags._2.map {     tag => (thingandtags._1, tag)   } }.groupby {   thingandtag => thingandtag._2 }.map {   tagandseqofthingandtags =>     tagandseqofthingandtags._1 -> tagandseqofthingandtags._2.map(x => x._1) } 

its little confusing ._1s , ._2s


Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -