Is there something like 'accumulate' in mit-scheme or C++?

Discussion in 'Perl Misc' started by Peng Yu, Jul 16, 2010.

  1. Peng Yu

    Peng Yu Guest

    Hi,

    I use the following command to OR the values in an array. I'm
    wondering what is the best way to do so. A function similar to
    'accumulate' in mit-scheme or C++, may be helpful. But I don't find it
    in perl.

    my @array=(0,0,1);
    my $result=0;
    map { $result = $result || $_} @array;
    print $result, "\n";

    Regards,
    Peng
     
    Peng Yu, Jul 16, 2010
    #1
    1. Advertising

  2. Peng Yu

    Peng Yu Guest

    On Jul 15, 9:12 pm, Ben Morrow <> wrote:
    > Quoth Peng Yu <>:
    >
    > > Hi,

    >
    > > I use the following command to OR the values in an array. I'm
    > > wondering what is the best way to do so. A function similar to
    > > 'accumulate' in mit-scheme or C++, may be helpful. But I don't find it
    > > in perl.

    >
    > > my @array=(0,0,1);
    > > my $result=0;
    > > map { $result = $result || $_} @array;
    > > print $result, "\n";

    >
    > The usual term for this operation is 'reduce'; you can find it in
    > List::Util. Note that you need to explicitly provide an initial value:
    >
    >     use List::Util qw/reduce/;
    >
    >     my $result = reduce { $a || $b } 0, @array;
    >
    > In this case you could also use 'first':
    >
    >     use List::Util qw/first/;
    >
    >     my $result = first { $_ } 0, @array;


    #!/usr/bin/env perl

    use strict;
    use warnings;
    use List::Util;

    my @array=1..10;
    my $result=List::Util::reduce { $a + $b } @array;
    print $result, "\n";
    ##########

    I got the following warnings for the above problem. But the code from
    the manual works. I'm wondering what is wrong in the above code?

    Name "main::b" used only once: possible typo at ./main.pl line 8.
    Name "main::a" used only once: possible typo at ./main.pl line 8.


    my $result = List::Util::reduce { $a > $b ? $a : $b } 1..10;#this
    works
     
    Peng Yu, Jul 16, 2010
    #2
    1. Advertising

  3. Peng Yu

    David Bouman Guest

    On Jul 16, 3:27 am, Peng Yu <> wrote:

    > I use the following command to OR the values in an array. I'm
    > wondering what is the best way to do so. A function similar to
    > 'accumulate' in mit-scheme or C++, may be helpful. But I don't find it
    > in perl.


    What about using a grep in scalar context?

    --
    David.
     
    David Bouman, Jul 16, 2010
    #3
  4. Peng Yu

    David Bouman Guest

    On Jul 16, 3:27 am, Peng Yu <> wrote:

    > I use the following command to OR the values in an array. I'm
    > wondering what is the best way to do so. A function similar to
    > 'accumulate' in mit-scheme or C++, may be helpful. But I don't find it
    > in perl.


    What about using a grep in scalar context?

    --
    David.
     
    David Bouman, Jul 16, 2010
    #4
  5. Peng Yu

    Guest

    On Thu, 15 Jul 2010 18:27:06 -0700 (PDT), Peng Yu <> wrote:

    >Hi,
    >
    >I use the following command to OR the values in an array. I'm
    >wondering what is the best way to do so. A function similar to
    >'accumulate' in mit-scheme or C++, may be helpful. But I don't find it
    >in perl.
    >
    >my @array=(0,0,1);
    >my $result=0;
    >map { $result = $result || $_} @array;
    >print $result, "\n";
    >
    >Regards,
    >Peng


    I'm just wondering which would be faster:
    $result ||= $_ and last for ( @array );
    or
    $result |= $_ and last for ( @array );

    -sln
     
    , Jul 16, 2010
    #5
    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. Brian K. Michalk

    64 bit add and accumulate with MMX

    Brian K. Michalk, Sep 20, 2003, in forum: Perl
    Replies:
    6
    Views:
    653
    Terje Mathisen
    Sep 20, 2003
  2. Replies:
    8
    Views:
    734
    Grant Edwards
    Oct 10, 2007
  3. fft1976
    Replies:
    80
    Views:
    1,720
    magicus
    Jul 28, 2009
  4. Jean-Paul Calderone

    Re: If Scheme is so good why MIT drops it?

    Jean-Paul Calderone, Jul 22, 2009, in forum: Python
    Replies:
    5
    Views:
    335
    Дамјан ГеоргиевÑки
    Jul 28, 2009
  5. Hendrik van Rooyen

    Re: If Scheme is so good why MIT drops it?

    Hendrik van Rooyen, Jul 29, 2009, in forum: Python
    Replies:
    3
    Views:
    283
Loading...

Share This Page