Hello Monks,

I'm reading Higher Order Perl and one of the examples is an "imap" function which acts like map only it takes a block and an iterator.

sub imap { my ($transform, $it) = @_; return Iterator { my $next = NEXTVAL($it); return unless defined $next; return $transform->($next); } }

That code may as well be in greek because it makes no sense to me. So I figured I'd try to write my own map function to see how it works.

#!/usr/bin/env perl use 5.010; use strict; use warnings; sub foo(&@) { my $coderef = shift; my @params = @_; my @ret; while(@params) { push @ret, $coderef->($_); } return @ret; } my @a = ( "1", "2", "3", "4", ); my @b = foo { $_ ** 2 } @a;

But that produces the error "Use of uninitialized value $_ in exponentiation (**) at coderef.pl line 19."

I know when I pass parameters to a subroutine or closure they are passed in in @_, so I tried to shift the parameter off @_, but that produces the same error (based on my understanding of perl, this second attempt should work... "Should" is funny word).

my @b = foo { $_ = shift; $_ ** 2 } @a; my @b = foo { my $var = shift; $var ** 2 } @a;

What gives? How does map/grep work it's magic? And why doesn't my attempt work? I realize that all of the useful map functions have probably already been written, so this is more of an exercise in understanding, how could I write my own map function?

Thanks Monks!

Edit: Seems my problem was with my use of while instead of for, I thought while set $_, but I guess not.

Here's the version that works:

#!/usr/bin/env perl use 5.010; use strict; use warnings; sub foo(&@) { my $coderef = shift; my @params = @_; my @ret; for(@params) { push @ret, $coderef->(); } return @ret; } my @a = ( "1", "2", "3", "4", ); my @b = foo { $_ ** 2 } @a;

In reply to [Solved] How does map work? by three18ti

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.