Search code examples
scalacombinationsprefix

scala combinations with prefix


What is an elegant way to generate combinations with prefixes in scala?

"""
   ((pre_first, pre_second), 
   (pre_first, pre_second, thing1),
   (pre_first, pre_second, thing2),
   (pre_first, pre_second, thing3),
   (pre_first, pre_second, thing1, thing2),
   (pre_first, pre_second, thing1, thing3),
   (pre_first, pre_second, thing2, thing3))
""".stripMargin
val prefixes = Seq("pre_first", "pre_second")
val things = Seq("thing1", "thing2", "thing3")

Solution

  • (0 to things.size).flatMap(i=>things.combinations(i)).map(prefixes ++ _)