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 | |
by Laurent_R (Canon) on Oct 26, 2013 at 22:45 UTC |