Search code examples
splitraku

How can I split a string in every possible way?


Given the word abcd, how would I construct the following nested list?

[ (abcd) (a bcd) (ab cd) (abc d) (a b cd) (a bc d) (ab c d) (a b c d) ]

That is splitting the word in every way it can be, while keeping the letters in order.

Nemokosch on #raku-beginner pointed me to .combinations and the module snip, but I'm having trouble putting it all together.


Solution

  • You could use match with :exhaustive:

    "abcd"
    andthen .match: /^ (\w+)+ $/,:ex
    andthen .map: *.[0].put