in reply to P6: Combinations Solution

As I mentioned in an earlier post, this construct isn't implemented yet in Pugs. Also as I said before, I may be using it incorrectly, but I think this could be implemented using gather/take as follows:

sub combinations returns Array (@list is rw) { return () unless @list.elems; gather { for 1 .. 2**@list.elems-1-> $num { take [ @list[ (0 .. sqrt($num)).grep:{ $num +& (2**$_) } ] ] +; } } } my @list = (1..4); combinations(@list).perl.say;

Replies are listed 'Best First'.
Re^2: P6: Combinations Solution
by eric256 (Parson) on May 21, 2005 at 20:27 UTC

    Actualy it was implemented a few days ago. This post actualy oncovered a small bug where take is flattening the array-ref. Thanks for the input.


    ___________
    Eric Hodges

      I saw that gather/take was implemented, but didn't realize there was the flattening bug. Glad my post could help uncover it. I assume there was a test case written and committed already? :-)