View mode: basic / threaded / horizontal-split · Log in · Help
June 11, 2012
Re: Build all combinations of strings
On 6/11/12, Andrej Mitrovic <andrej.mitrovich@gmail.com> wrote:
> Is there a Phobos function..

Almost forgot, it can't repeat one string multiple times, so no "foo foo foo".
June 11, 2012
Re: Build all combinations of strings
On 6/11/12, Andrej Mitrovic <andrej.mitrovich@gmail.com> wrote:
> On 6/11/12, Andrej Mitrovic <andrej.mitrovich@gmail.com> wrote:
>> Is there a Phobos function..
>
> Almost forgot, it can't repeat one string multiple times, so no "foo foo
> foo".
>

Also I think the formal word of what I'm looking for is a "powerset".
June 11, 2012
Re: Build all combinations of strings
On 6/11/12, Andrej Mitrovic <andrej.mitrovich@gmail.com> wrote:
> Also I think the formal word of what I'm looking for is a "powerset".
>

Hmm wrong, not looking a powerset. Here's one anyway which bearophile
posted in another thread:
import std.stdio: writeln;
auto powerSet(T)(T[] items) {
  auto r = new T[][](1, 0);
  foreach (e; items) {
      T[][] rs;
      foreach (x; r)
          rs ~= x ~ [e];
      r ~= rs;
  }
  return r;
}
void main() {
  writeln(powerSet([1, 2, 3]));
}

So that's not what I'm looking for.
June 11, 2012
Re: Build all combinations of strings
On 6/11/12, Andrej Mitrovic <andrej.mitrovich@gmail.com> wrote:
> So that's not what I'm looking for.

Here's what I'm looking for, but in Python:
http://docs.python.org/library/itertools.html#itertools.combinations

They also have a version that has repeated elements:
http://docs.python.org/library/itertools.html#itertools.combinations_with_replacement

I wouldn't know how to translate that to D though.
June 11, 2012
Re: Build all combinations of strings
On 6/11/12, Andrej Mitrovic <andrej.mitrovich@gmail.com> wrote:
> I wouldn't know how to translate that to D though.

Found Phillipe's implementation. Cut from dranges.algorithm:
http://pastebin.com/26s7wNYJ

There's a github clone here: https://github.com/dawgfoto/dranges

Problem solved for now. :)
June 11, 2012
Re: Build all combinations of strings
On Mon, Jun 11, 2012 at 8:32 PM, Andrej Mitrovic
<andrej.mitrovich@gmail.com> wrote:
> On 6/11/12, Andrej Mitrovic <andrej.mitrovich@gmail.com> wrote:
>> I wouldn't know how to translate that to D though.
>
> Found Phillipe's implementation. Cut from dranges.algorithm:
> http://pastebin.com/26s7wNYJ
>
> There's a github clone here: https://github.com/dawgfoto/dranges

Ah yes, permutation().

> Problem solved for now. :)

Does it still work? It must be almost 2 years old now.
June 11, 2012
Re: Build all combinations of strings
On 6/11/12, Philippe Sigaud <philippe.sigaud@gmail.com> wrote:
> Does it still work? It must be almost 2 years old now.

Yup!
Top | Discussion index | About this forum | D home