www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Generating all combinations of length X in an array

reply "wobbles" <grogan.colin gmail.com> writes:
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
Apr 08 2015
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
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
Apr 08 2015
parent reply "wobbles" <grogan.colin gmail.com> writes:
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!
Apr 08 2015
next sibling parent reply "wobbles" <grogan.colin gmail.com> writes:
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!
Apr 08 2015
parent "bearophile" <bearophileHUGS lycos.com> writes:
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
Apr 09 2015
prev sibling parent reply "Messenger" <dont shoot.me> writes:
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
Apr 09 2015
parent reply "weaselcat" <weaselcat gmail.com> writes:
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.
Apr 09 2015
parent "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> writes:
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
Apr 10 2015