digitalmars.D.learn - Generating all combinations of length X in an array
- wobbles (21/21) Apr 08 2015 Hi folks,
- bearophile (6/11) Apr 08 2015 Often short code is not the best code.
- wobbles (4/16) Apr 08 2015 Ah, excellent! Dunno why I didnt think of rosettacode before.
- wobbles (2/22) Apr 08 2015 Have just tested, it is!
- bearophile (6/7) Apr 09 2015 But with the current D front-end it's not a good idea to generate
- Messenger (3/6) Apr 09 2015 He's likely not the *only* contributor but I think bearophile is
- weaselcat (3/10) Apr 09 2015 Rosetta Code should really be linked somewhere on the site,
- "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> (4/15) Apr 10 2015 There are already lots of useful links on the start page of the
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 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
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 eyeOften 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
On Wednesday, 8 April 2015 at 10:54:45 UTC, bearophile wrote:wobbles: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!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 eyeOften 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
On Wednesday, 8 April 2015 at 11:08:00 UTC, wobbles wrote:On Wednesday, 8 April 2015 at 10:54:45 UTC, bearophile wrote:Have just tested, it is!wobbles: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!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 myOften 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
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
On Wednesday, 8 April 2015 at 11:08:00 UTC, wobbles wrote:On Wednesday, 8 April 2015 at 10:54:45 UTC, bearophile wrote:He's likely not the *only* contributor but I think bearophile is the big D champion there. <3wobbles:Thanks, all the D stuff on rosettacode is an excellent resource!
Apr 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:Rosetta Code should really be linked somewhere on the site, bearophile put a lot of effort into it.On Wednesday, 8 April 2015 at 10:54:45 UTC, bearophile wrote:He's likely not the *only* contributor but I think bearophile is the big D champion there. <3wobbles:Thanks, all the D stuff on rosettacode is an excellent resource!
Apr 09 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:There are already lots of useful links on the start page of the wiki: http://wiki.dlang.org/The_D_Programming_LanguageOn Wednesday, 8 April 2015 at 11:08:00 UTC, wobbles wrote:Rosetta Code should really be linked somewhere on the site, bearophile put a lot of effort into it.On Wednesday, 8 April 2015 at 10:54:45 UTC, bearophile wrote:He's likely not the *only* contributor but I think bearophile is the big D champion there. <3wobbles:Thanks, all the D stuff on rosettacode is an excellent resource!
Apr 10 2015