首页 > 解决方案 > 带有前缀的scala组合

问题描述

在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")

标签: scalacombinationsprefix

解决方案


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

推荐阅读