Is “map” a loop?

后端 未结 15 1982
自闭症患者
自闭症患者 2020-12-14 14:29

While answering this question, I came to realize that I was not sure whether Perl\'s map can be considered a loop or not?

On one hand, it quacks/walks l

15条回答
  •  庸人自扰
    2020-12-14 15:14

    The map function is not a loop in Perl. This can be clearly seen by the failure of next, redo, and last inside a map:

    perl -le '@a = map { next if $_ %2; } 1 .. 5; print for @a'
    Can't "next" outside a loop block at -e line 1.
    

    To achieve the desired affect in a map, you must return an empty list:

    perl -le '@a = map { $_ %2 ? () : $_ } 1 .. 5; print for @a'
    2
    4
    

    I think transformation is better name for constructs like map. It transforms one list into another. A similar function to map is List::Util::reduce, but instead of transforming a list into another list, it transforms a list into a scalar value. By using the word transformation, we can talk about the common aspects of these two higher order functions.

    That said, it works by visiting every member of the list. This means it behaves much like a loop, and depending on what your definition of "a loop" is it might qualify. Note, my definition means that there is no loop in this code either:

    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    my $i = 0;
    FOO:
        print "hello world!\n";
    goto FOO unless ++$i == 5;
    

    Perl actually does define the word loop in its documentation:

       loop
           A construct that performs something repeatedly, like a roller
           coaster.
    

    By this definition, map is a loop because it preforms its block repeatedly; however, it also defines "loop control statement" and "loop label":

       loop control statement
           Any statement within the body of a loop that can make a loop
           prematurely stop looping or skip an "iteration".  Generally you
           shouldn't try this on roller coasters.
    
       loop label
           A kind of key or name attached to a loop (or roller coaster) so
           that loop control statements can talk about which loop they want to
           control.
    

    I believe it is imprecise to call map a loop because next and its kin are defined as loop control statements and they cannot control map.

    This is all just playing with words though. Describing map as like-a-loop is a perfectly valid way of introducing someone to it. Even the documentation for map uses a foreach loop as part of its example:

                   %hash = map { get_a_key_for($_) => $_ } @array;
    
               is just a funny way to write
    
                   %hash = ();
                   foreach (@array) {
                       $hash{get_a_key_for($_)} = $_;
                   }
    

    It all depends on the context though. It is useful to describe multiplication to someone as repeated addition when you are trying to get him or her to understand the concept, but you wouldn't want him or her to continue to think of it that way. You would want him or her to learn the rules of multiplication instead of always translating back to the rules of addition.

提交回复
热议问题