unit module AllYourBase:ver<3>; sub convert-base ( :%bases! where all(.keys ~~ .Set, .values.all > 1), :@digits! where %bases > .all ~~ UInt:D, --> Array[UInt:D] ) is export { from-decimal %bases, to-decimal(%bases, @digits); } sub to-decimal ($input-base, @input-digits) { my $elems = @input-digits.elems; $_ == 0 ?? $elems-- !! last for @input-digits; (loop (my $i = 0; $i < $elems; $i++) { @input-digits.reverse[$i] * $input-base ** $i; }).sum; } sub from-decimal ($output-base, $num is copy) { my UInt:D @output-digits; while $num >= $output-base { unshift @output-digits, $num % $output-base; $num div= $output-base; } @output-digits.unshift: $num; }