Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re^2: map sub to list?

by pldanutz (Acolyte)
on Sep 09, 2013 at 17:14 UTC ( #1053048=note: print w/replies, xml ) Need Help??

in reply to Re: map sub to list?
in thread map sub to list?

I've tried removing the prototype, of course; but actually the main cause of the problem was relying on an argument as opposed to $_, right? I prefer functional style as much as possible, coming from a Lisp background and all that :)

Replies are listed 'Best First'.
Re^3: map sub to list?
by Laurent_R (Canon) on Sep 09, 2013 at 17:50 UTC

    Yes. Assuming you want to keep the overkill of a function, you could either change the relevant line to:

    sub f{ return $_ + 1 };

    or pass the $_ as an argument to the function:

    use strict; use warnings; sub f{ return $_[0] + 1 }; my @a = (1, 2, 3); print join " ", map (f($_), @a);

    I have added the join because you like functional programming. But the more common way to do such things is illustrated in the following Perl one-liner:

    $ perl -e 'print join " ", map {$_ + 1} 1..4' 2 3 4 5

    Talking of functional programming, note that the block of code after the map (the {$_ + 1} part) can be regarded as an anonymous function being applied by map onto each input element.

      Cool. The actual code I had was more complex and warranted a function. I posted a simplified snippet as is customary in such venues :) A problem I have with the no-parens style is that I can't tell how associativity works (between print, join, map in your case)

        You may think of it as a data pipeline from right to left. The 1..4 operator produces a list of values fed as an argument to map; map itself produces here another list of values (2 to 5) fed as argument to join; join produces a string made of the numbers separated by spaces, fed to print. Even though the syntax is quite different, this dataflow programming style of construct is very much in the spirit of the various dialects of Lisp and some other functional languages. You may add parens if you feel more comfortable with them, but with a bit of experience you will probably end up agreeing with my view that the syntax is actually clearer without the parens. If you know Unix and shell programming, think of the | operator (except that, with Perl, you have to read pipeline from right to left).

        Oh, and BTW, once you are a bit more used to Perl (this is probably a bit too early at this point), read Mark-Jason Dominus' book "Higher Order Perl" (PDF available for free on his site), in which he shows that Perl has many features of functional languages, including call back functions, first-class citizen functions, anonymous subroutines, closures, function factories, currying, etc.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2022-10-04 11:03 GMT
Find Nodes?
    Voting Booth?
    My preferred way to holiday/vacation is:

    Results (16 votes). Check out past polls.