in reply to Permutations question

Here's a List::Util reduce solution that seg faults under 5.8.0 on Solaris, but works in ActiveState. :-)
use strict; use warnings; use Data::Dumper; my $params = [ 1, [ 2, 3, 4 ], 5 ]; use List::Util 'reduce'; sub multiplex { reduce { my $aref = ref($a) ? $a : [[$a]]; my $bref = ref($b) ? $b : [$b]; [ map { my $b_item = $_; map [ @$_, $b_item], @$aref } @$bref ] ; } @{shift()}; } print Dumper(multiplex($params));
And here's the same algorithm without reduce (and without seg faulting):
sub multiplex { my $ret = [[]]; for (@{shift()}) { my $bref = ref($_) ? $_ : [$_]; $ret = [ map { my $b_item = $_; map [ @$_, $b_item], @$ret; } @$bref ]; } $ret; }

Caution: Contents may have been coded under pressure.

Replies are listed 'Best First'.
Re^2: Permutations question
by acid06 (Friar) on Feb 22, 2005 at 21:04 UTC
    Note that using reduce seems to be a little bit faster.
    Nothing concerning, though.
    Rate no_reduce reduce no_reduce 42123/s -- -2% reduce 42955/s 2% --


    acid06
    perl -e "print pack('h*', 16369646), scalar reverse $="