loading...

Discussion on: Daily Challenge #82 - English Beggars

Collapse
choroba profile image
E. Choroba

Going functional in Perl:

#!/usr/bin/perl
use warnings;
use strict;

use List::Util      qw{ sum };
use List::MoreUtils qw{ part };

sub beggars {
    my ($money, $number_of_beggars) = @_;
    my $i = 0;
    [ map sum(@$_), part { $i++ % $number_of_beggars } @$money ];
}

use Test::More tests => 2;
is_deeply beggars([1, 2, 3, 4, 5], 2), [9,6];
is_deeply beggars([1, 2, 3, 4, 5], 3), [5, 7, 3];