in reply to Permutations question
And here's the same algorithm without reduce (and without seg faulting):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));
sub multiplex { my $ret = [[]]; for (@{shift()}) { my $bref = ref($_) ? $_ : [$_]; $ret = [ map { my $b_item = $_; map [ @$_, $b_item], @$ret; } @$bref ]; } $ret; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Permutations question
by acid06 (Friar) on Feb 22, 2005 at 21:04 UTC |