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

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?

Replies are listed 'Best First'.
Re^3: map sub to list?
by toolic (Bishop) on Sep 09, 2013 at 17:25 UTC
    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 ($_) }

        Your use of syntax leaves me with the impression that before Perl you only knew Haskell, or some other, lazy / currying functional language.

        The major difference between Perl and these others is that Perl is eager, so any unadorned mention of a function will simply call it. Unless you use built-in functions or fancy syntax tricks, all function parameters are passed in parentheses.

        Creating/returning new functions is quite possible in Perl, but the usual approach is more eager in the sense of passing around values instead of passing around functions that, at some time in the future will be called to produce the final output of the program.

        > Where can I read about functions that have comma and non-comma parsings?

        in my own words:

        BLOCK means { code } in curlies, like map { $_ +1 } 1..3, so no comma

        DB<124> map { $_ +1 } 1..3 => (2, 3, 4)

        EXPR can be any "atomar" code-snippet.

        DB<128> map sqrt , 1..3 => (1, "1.4142135623731", "1.73205080756888") DB<129> map 5 , 1..3 => (5, 5, 5)

        in both cases the list values are passed by setting a special variable (aka default variable) $_

        according to perlglossary

        BLOCK A syntactic construct consisting of a sequence of Perl stat +ements that is delimited by braces. The "if" and "while" statemen +ts are defined in terms of BLOCKs, for instance. Sometimes we als +o say "block" to mean a lexical scope; that is, a sequence of sta +tements that act like a "BLOCK", such as within an eval or a file, +even though the statements arenít delimited by braces. ... expression Anything you can legally say in a spot where a "value" is required. Typically composed of literals, variables, opera +tors, functions, and "subroutine" calls, not necessarily in that +order.

        Cheers Rolf

        ( addicted to the Perl Programming Language)

        f($_) means that you are calling the function, f, and passing it a scalar value contained in the $_ variable. See also perldoc perlsub