Permutations of array (slice) ?

Kevin Bailey keraba at yahoo.com
Tue Dec 12 14:57:48 UTC 2023


Could I get some help with a trivial question, I'm sure? How do 
pass a slice as a range? This code:

import std.algorithm; // permutations
import std.range; // replicate
import std.stdio; // writeln

int main()
{
     char[] as = replicate(['a'], 5);
     as[0] = 'b';
     foreach (perm; as.permutations)
         writeln(perm);
}

appears to complain (actual error below) that 'as' is not a range.

Using the slice trick "as[]" from:

https://forum.dlang.org/thread/oxohqaicmpteesugsdnk@forum.dlang.org

doesn't help either. Something obvious I'm missing, I hope.

perm.d:8:26: error: none of the overloads of template 
‘std.algorithm.iteration.permutations’ are callable using 
argument types ‘!()(char[])’
     8 |         foreach (perm; as.permutations)
       |                          ^
/usr/lib/gcc/x86_64-linux-gnu/13/include/d/std/algorithm/iteration.d:7919:20: note: Candidate is: ‘permutations(Range)(Range r)’
      with `Range = char[]`
   must satisfy the following constraint:
`       isRandomAccessRange!Range`
  7919 | Permutations!Range permutations(Range)(Range r)
       |                    ^



More information about the Digitalmars-d-learn mailing list