Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re: map sub to list?

by BrowserUk (Patriarch)
on Sep 09, 2013 at 16:50 UTC ( #1053042=note: print w/replies, xml ) Need Help??

in reply to map sub to list?

chr defaults to using $_, so use: @a = map f( $_ ), @numbers;.

Or define f() to default to $_:

sub f(_){ $_[0]+1 };

Now my @a = map f, @numbers; works.

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^2: map sub to list?
by pldanutz (Acolyte) on Sep 09, 2013 at 17:15 UTC
    Thanks to everybody who replied! I think I've got it. When writing @a = map f( $_ ), @numbers; this is a case of map BLOCK LIST (as opposed to map EXPR LIST) right?
      No, using the comma makes it a case of EXPR, LIST.

        I can't seem to be able to reply to other replies (too much nesting probably). Perhaps I'll try the chatterbox or a separate question.

        I was trying to understand if the Perl compiler parses "f ($_)" in "map f ($_), @numbers" as a lambda. If it's not a block, but an expression, it must be either an arithmetic expression, a lambda expression etc. My question was from a syntactic/compiler point of view.

        And as to eager evaluation, obviously this is a counterexample, since map f($_) @nums doesn't immediately, eagerly evaluate f on the current $_, but instead passed a lambda to map()

        Where can I read about functions that have comma and non-comma parsings? This confuses me. Also, what expression is f ($_)? Is it a new function, i.e. a shorthand for sub f2 (_) { f ($_) }

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2022-12-08 09:03 GMT
Find Nodes?
    Voting Booth?

    No recent polls found