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.