sub my_map { my \$sub = shift; my @out; for my \$element ( @_ ) { push @out, \$sub->( \$element ); } return @out; } ##```## sub square { \$_[0] * \$_[0] } my @squares = my_map( \&square, 1..3 ); # @squares is now ( 1, 4, 9 ) ##``````## my @squares = my_map( sub { \$_[0]*\$_[0] }, 1..3 ); ##``````## my @squares = my_map sub { \$_[0]*\$_[0] }, 1..3; ##``````## sub my_map { my \$sub = shift; my @out; for ( @_ ) { push @out, \$sub->(); # \$sub will get its arg from \$_ } return @out; } ##``````## my @squares = my_map sub { \$_ * \$_ }, 1..3; ##``````## my @squares = map { \$_ * \$_ } 1..3; # BLOCK form of map ##``````## sub twice { \$_ * 2 } ##``````## my @evens = map &twice, 1..10; # EXPR form of map ##``````## my @evens = my_map \&twice, 1..10; ##``````## sub my_map (\&@_) { my \$sub = shift; my @out; push @out, \$sub->() for @_; return @out; } ##``````## my @evens = my_map &twice, 1..10; ##``````## sub my_map (&@) { my \$sub = shift; my @out; push @out, \$sub->() for @_; return @out; } ##``````## my @evens = my_map { \$_ * 2 } 1..3; ##``````## my @evens = my_map \$_ * 2, 1..10; # splat! ##``````## my @ints = 1..3; my @evens = my_map { \$_ *= 2 } @ints; print "@evens\n"; print "@ints\n"; __END__ 2 3 6 2 3 6 ##``````## sub my_grep (&@) { my \$sub = shift; my @out; \$sub->() && push @out, \$_ for @_; return @out; } ```