Saturday, 15 September 2012

collections - how to make combination of values in a string using scala? -



collections - how to make combination of values in a string using scala? -

i want combine values in string. example,

let = a,b,c,d

i want combination as,

acomb = a,b,c,d,ab,ac,ad,bc,bd,cd,abc,abd,bcd,acd

i assuming a set

scala> val =set("a","b","c","d") a: scala.collection.immutable.set[string] = set(a, b, c, d) scala> val acomb=a.toset[string].subsets.map(_.mkstring).tovector acomb: vector[string] = vector("", a, b, c, d, ab, ac, ad, bc, bd, cd, abc, abd, acd, bcd, abcd)

i thinking didn't need first element can try

scala> val acomb=a.toset[string].subsets.map(_.mkstring).tovector.tail acomb: scala.collection.immutable.vector[string] = vector(a, b, c, d, ab, ac, ad, bc, bd, cd, abc, abd, acd, bcd, abcd)

removing first , lastly element

scala> val acomb=a.toset[string].subsets.map(_.mkstring).tovector.init.tail acomb: scala.collection.immutable.vector[string] = vector(a, b, c, d, ab, ac, ad, bc, bd, cd, abc, abd, acd, bcd)

updated according comment

scala> val xc1=set("sunny","hot","high","false","no") xc1: scala.collection.immutable.set[string] = set(sunny, false, hot, no, high) scala> val acomb=xc1.toset[string].subsets.map(_.mkstring(" ")).tovector.tail; acomb: scala.collection.immutable.vector[string] = vector(sunny, false, hot, no, high, sunny false, sunny hot, sunny no, sunny high, false hot, false no, false high, hot no, hot high, no high, sunny false hot, sunny false no, sunny false high, sunny hot no, sunny hot high, sunny no high, false hot no, false hot high, false no high, hot no high, sunny false hot no, sunny false hot high, sunny false no high, sunny hot no high, false hot no high)

scala collections combinations scala-collections

No comments:

Post a Comment