reduce, anyone?

Discussion in 'Perl Misc' started by Ivan Shmakov, Mar 15, 2012.

  1. Ivan Shmakov

    Ivan Shmakov Guest

    Since the days of APL, reduction (or folding) is one of the
    basic operations on ordered collections (lists, arrays, etc.)

    I see that Perl 6 has such an operator [1], but what about
    Perl 5? A quick search on CPAN reveals [2] (though it seems to
    implement APL's scan instead of reduce), but I still wonder if
    there're any other possible choices? (It's not that hard to
    implement such a function, BTW.)

    TIA.

    sub reduce {
    my ($f, $a, @l) = @_;
    foreach $v (@l) { $a = &$f ($a, $v); }
    ## .
    $a;
    }

    sub add {
    my ($a, $b) = @_;
    ## .
    $a + $b;
    }

    sub access {
    my ($h, $k) = @_;
    ## .
    $h->{$k};
    }

    print (reduce (\&add, 0, 1, 2, 3), "\n");
    ## => 6

    print (reduce (\&access, { "a" => { "b" => { "c" => "d" } } },
    qw (a b c)), "\n");
    ## => d

    [1] http://search.cpan.org/~lichtkind/Perl6-Doc-0.36/lib/Perl6/Doc/Overview/Reduce.pod
    [2] http://search.cpan.org/~patch/Operator-Util-0.05/lib/Operator/Util.pm

    --
    FSF associate member #7257
    Ivan Shmakov, Mar 15, 2012
    #1
    1. Advertising

  2. Ivan Shmakov

    Ivan Shmakov Guest

    >>>>> Ben Morrow <> writes:
    >>>>> Quoth Ivan Shmakov <>:


    >> Since the days of APL, reduction (or folding) is one of the basic
    >> operations on ordered collections (lists, arrays, etc.)


    >> I see that Perl 6 has such an operator [1], but what about Perl 5?
    >> A quick search on CPAN reveals [2] (though it seems to implement
    >> APL's scan instead of reduce), but I still wonder if there're any
    >> other possible choices? (It's not that hard to implement such a
    >> function, BTW.)


    > List::Util has the standard implementation.


    Thanks!

    Moreover, it also defines first (), so that I can now use
    first (defined, @a) instead of my own either (@a).

    --
    FSF associate member #7257
    Ivan Shmakov, Mar 16, 2012
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. DAvid

    reduce the CLB

    DAvid, Oct 24, 2004, in forum: VHDL
    Replies:
    2
    Views:
    612
  2. Jack Wright

    Reduce The Size Of Your ASP.NET Output

    Jack Wright, Feb 29, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    428
    Eric Lawrence [MSFT]
    Mar 2, 2004
  3. JDS

    Anyone?? Anyone at all??

    JDS, Sep 26, 2005, in forum: HTML
    Replies:
    2
    Views:
    376
    Montgomery BOO...URNS
    Sep 29, 2005
  4. Steve Williams

    Anyone? Anyone?

    Steve Williams, Sep 16, 2003, in forum: Python
    Replies:
    0
    Views:
    382
    Steve Williams
    Sep 16, 2003
  5. gregarican

    Anyone, anyone...Bueller?

    gregarican, Jun 22, 2007, in forum: Ruby
    Replies:
    2
    Views:
    90
    gregarican
    Jun 22, 2007
Loading...

Share This Page