Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8134905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T10:13:11+00:00 2026-06-06T10:13:11+00:00

Some time ago I was asked the strange question how would I implement map

  • 0

Some time ago I was asked the “strange” question how would I implement map with grep.
Today I tried to do it, and here is what came out. Did I squeeze everything from Perl, or there are other more clever hacks?

#!/usr/bin/env perl
use strict;
use warnings;
use 5.010;

sub my_map(&@) {
    grep { $_= $_[0]->($_) } @_[1..$#_];
}

my @arr = (1,2,3,4);

#list context
say (my_map sub {$_+1}, @arr);
#scalar context
say "".my_map {$_+1} @arr;
say "the array from outside: @arr";
say "builtin map:", (map {$_+1} @arr);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T10:13:12+00:00Added an answer on June 6, 2026 at 10:13 am

    Are you sure they didn’t ask how to implement grep with map? That’s actually useful sometimes.

    grep { STMTs; EXPR } LIST
    

    can be written as

    map { STMTs; EXPR ? $_ : () } LIST
    

    (With one difference: grep returns lvalues, and map doesn’t.)

    Knowing this, one can compact

    map { $_ => 1 } grep { defined } @list
    

    to

    map { defined ? $_ => 1 : () } @list
    

    (I prefer the “uncompressed” version, but the “compressed” version is probably a little faster.)


    As for implementing map using grep, well, you can take advantage of grep‘s looping and aliasing properties.

    map { STMTs; EXPR } LIST
    

    can be written as

    my @rv;
    grep { STMTs; push @rv, EXPR } LIST;
    @rv
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I asked a question some time ago on java 2d pathfinding... Pathfinding 2D Java
This question was asked quite some time ago, and while it covers possible solutions
I asked another SOAP related question some time ago, which asks for a working
This is an extension to a question which I asked some time ago. I
I've already asked question similar to this some time ago, I thought I solved
Some time ago I asked a question about nested loops on SO and as
I'v asked these question some time ago: Multiple inheritance casting from base class to
this is somewhat the same question as I've asked some time ago: How to
I asked this question about using the system-drawables in an Android-app some time ago:
some time ago someone already asked this question and a few answers were given

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.