Thread overview
Generating all combinations of length X in an array
Apr 08, 2015
wobbles
Apr 08, 2015
bearophile
Apr 08, 2015
wobbles
Apr 08, 2015
wobbles
Apr 09, 2015
bearophile
Apr 09, 2015
Messenger
Apr 09, 2015
weaselcat
Apr 10, 2015
Marc Schütz
April 08, 2015
Hi folks,

While trying to generate all combinations of length X in an array,
I came across the question on stackoverflow. [1]

Theres a couple good answers there, but one that caught my eye shows a C# code snippet that is quite nice and short:

public static IEnumerable<IEnumerable<T>> Combinations<T>(this IEnumerable<T> elements, int k)
{
  return k == 0 ? new[] { new T[0] } :
    elements.SelectMany((e, i) =>
      elements.Skip(i + 1).Combinations(k - 1).Select(c => (new[] {e}).Concat(c)));
}


I spent a couple hours trying to translate this to D, but couldnt get my head around the SelectMany statement.
I think it's analogous to std.algorithm.map, but it seems quite difficult to mimic the behaviour using map.

Anyone with more knowledge and/or skills like to have a crack?

Thanks!

[1] http://stackoverflow.com/questions/127704/algorithm-to-return-all-combinations-of-k-elements-from-n
April 08, 2015
wobbles:

> While trying to generate all combinations of length X in an array,
> I came across the question on stackoverflow. [1]
>
> Theres a couple good answers there, but one that caught my eye shows a C# code snippet that is quite nice and short:

Often short code is not the best code.

Take a look at the versions here, the usable one is the third:
http://rosettacode.org/wiki/Combinations#D

Bye,
bearophile
April 08, 2015
On Wednesday, 8 April 2015 at 10:54:45 UTC, bearophile wrote:
> wobbles:
>
>> While trying to generate all combinations of length X in an array,
>> I came across the question on stackoverflow. [1]
>>
>> Theres a couple good answers there, but one that caught my eye shows a C# code snippet that is quite nice and short:
>
> Often short code is not the best code.
>
> Take a look at the versions here, the usable one is the third:
> http://rosettacode.org/wiki/Combinations#D
>
> Bye,
> bearophile

Ah, excellent! Dunno why I didnt think of rosettacode before.

Is the 3rd version usable at compile time?

Thanks, all the D stuff on rosettacode is an excellent resource!
April 08, 2015
On Wednesday, 8 April 2015 at 11:08:00 UTC, wobbles wrote:
> On Wednesday, 8 April 2015 at 10:54:45 UTC, bearophile wrote:
>> wobbles:
>>
>>> While trying to generate all combinations of length X in an array,
>>> I came across the question on stackoverflow. [1]
>>>
>>> Theres a couple good answers there, but one that caught my eye shows a C# code snippet that is quite nice and short:
>>
>> Often short code is not the best code.
>>
>> Take a look at the versions here, the usable one is the third:
>> http://rosettacode.org/wiki/Combinations#D
>>
>> Bye,
>> bearophile
>
> Ah, excellent! Dunno why I didnt think of rosettacode before.
>
> Is the 3rd version usable at compile time?
>
> Thanks, all the D stuff on rosettacode is an excellent resource!

Have just tested, it is!
April 09, 2015
wobbles:

> Have just tested, it is!

But with the current D front-end it's not a good idea to generate too many combinations at compile-time. Efficient code doesn't save you from bad usages.

Bye,
bearophile
April 09, 2015
On Wednesday, 8 April 2015 at 11:08:00 UTC, wobbles wrote:
> On Wednesday, 8 April 2015 at 10:54:45 UTC, bearophile wrote:
>> wobbles:
> Thanks, all the D stuff on rosettacode is an excellent resource!

He's likely not the *only* contributor but I think bearophile is the big D champion there. <3
April 09, 2015
On Thursday, 9 April 2015 at 23:21:17 UTC, Messenger wrote:
> On Wednesday, 8 April 2015 at 11:08:00 UTC, wobbles wrote:
>> On Wednesday, 8 April 2015 at 10:54:45 UTC, bearophile wrote:
>>> wobbles:
>> Thanks, all the D stuff on rosettacode is an excellent resource!
>
> He's likely not the *only* contributor but I think bearophile is the big D champion there. <3

Rosetta Code should really be linked somewhere on the site, bearophile put a lot of effort into it.
April 10, 2015
On Thursday, 9 April 2015 at 23:49:22 UTC, weaselcat wrote:
> On Thursday, 9 April 2015 at 23:21:17 UTC, Messenger wrote:
>> On Wednesday, 8 April 2015 at 11:08:00 UTC, wobbles wrote:
>>> On Wednesday, 8 April 2015 at 10:54:45 UTC, bearophile wrote:
>>>> wobbles:
>>> Thanks, all the D stuff on rosettacode is an excellent resource!
>>
>> He's likely not the *only* contributor but I think bearophile is the big D champion there. <3
>
> Rosetta Code should really be linked somewhere on the site, bearophile put a lot of effort into it.

There are already lots of useful links on the start page of the wiki:
http://wiki.dlang.org/The_D_Programming_Language