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. Advertisements

  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. Advertisements

  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. Advertisements

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. =?UTF-8?B?0JTQsNC80ZjQsNC9INCT0LXQvtGA0LPQuNC10LLR

    Is there something like Zoe in Python

    =?UTF-8?B?0JTQsNC80ZjQsNC9INCT0LXQvtGA0LPQuNC10LLR, Oct 14, 2004, in forum: Python
    Replies:
    4
    Views:
    429
    Miki Tebeka
    Oct 14, 2004
  2. Peter Gordon
    Replies:
    2
    Views:
    429
    Peter Hansen
    Nov 14, 2004
  3. Mike Meng
    Replies:
    8
    Views:
    763
    gene tani
    Jun 2, 2006
  4. Replies:
    8
    Views:
    960
    Grant Edwards
    Oct 10, 2007
  5. fft1976
    Replies:
    80
    Views:
    2,170
    magicus
    Jul 28, 2009
  6. 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:
    448
    Дамјан ГеоргиевÑки
    Jul 28, 2009
  7. 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:
    381
  8. dmitrey
    Replies:
    5
    Views:
    653
    Jussi Piitulainen
    Apr 20, 2012
Loading...