On Tuesday, 12 December 2023 at 15:20:23 UTC, Paul Backus wrote:

Because of autodecoding [1], slices of char and wchar are treated by Phobos as bidirectional ranges of dchar. (They are not random-access ranges because UTF-8 and UTF-16 are variable-length encodings.)

To work around this, use std.utf.byChar:

    import std.utf: byChar;
    foreach (perm; as.byChar.permutations)
        writeln(perm);

[1] http://ddili.org/ders/d.en/ranges.html#ix_ranges.decoding,%20automatic

ah, many thanks, I never would have figured that out.

But unfortunately, the code shown now prints 120 lines of:

baaaa

120 being suspiciously equal to 5!. The documentation[2] seems to imply that this should be:

baaaa
abaaa
...

When I replace the char[] with int[] [2, 1, 1, 1, 1], it produces 120 lines of random arrangements of the input, but mostly just repeating the input.

Clearly it's doing permutations of a separate array of [0, 1, 2, 3, 4], and then replacing each index with the element at that index (or the equivalent) but it's not even doing that right.

I'll play with nextPermutation. I suspect it would have to be implemented the way that I'm expecting.

[2] https://dlang.org/library/std/algorithm/iteration/permutations.html


Reply via email to