Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: [Solved] How does map work?

by Laurent_R (Canon)
on Oct 26, 2013 at 21:47 UTC ( [id://1059848]=note: print w/replies, xml ) Need Help??


in reply to [Solved] How does map work?

Although the code below is quite similar to what BrowserUk has posted, I post here two versions of the "my_map" function that I wrote a couple of weeks ago for a tutorial that I am in the course of writing in French on functional programming in Perl. The first one works similarly to Perl's map function in the sense that if the code block modifies $_, then the original array is modified:

sub my_map (&@){ my $code_ref = shift; my @d ; push @d, $code_ref->($_) for @_; return @d; }

The second one is more like a pure functional version of map having no side-effect on the original array:

sub my_map (&@){ my $code_ref = shift; my @d = @_; $_ = $code_ref->($_) for @d; return @d; }

Replies are listed 'Best First'.
Re^2: [Solved] How does map work?
by LanX (Saint) on Oct 26, 2013 at 21:54 UTC
    may I ask, why you pass $_ as argument?

    Cheers Rolf

    ( addicted to the Perl Programming Language)

      Well, yes, you are right, this is not needed. But this was an example for a tutorial, I wanted it to be explicit to explain clearly what is going on. Thank you for your remark, I need to add at least one example without $_ in my tutorial.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1059848]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (5)
As of 2024-04-25 14:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found