How to create all possible combinations from the elements of a list?

前端 未结 4 1439
醉话见心
醉话见心 2021-02-01 02:07

I have the following list:

List(a, b, c, d, e)

How to create all possible combinations from the above list?

I expect something like:

4条回答
  •  旧时难觅i
    2021-02-01 02:31

    val xs = List( 'a', 'b' , 'c' , 'd' , 'e' )
    (1 to xs.length flatMap (x => xs.combinations(x))) map ( x => x.mkString(""))
    

    This should give you all the combination concatenated by empty String.

提交回复
热议问题