Is “map” a loop?

后端 未结 15 1827
自闭症患者
自闭症患者 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:11

    I think of map as more akin to an operator, like multiplication. You could even think of integer multiplication as a loop of additions :). It's not a loop of course, even if it were stupidly implemented that way. I see map similarly.

    0 讨论(0)
  • 2020-12-14 15:14

    map is a higher level concept than loops, borrowed from functional programming. It doesn't say "call this function on each of these items, one by one, from beginning to end," it says "call this function on all of these items." It might be implemented as a loop, but that's not the point -- it also might be implemented asynchronously -- it would still be map.

    Additionally, it's not really a control structure in itself -- what if every perl function that used a loop in its implementation were listed under "loops?" Just because something is implemented using a loop, doesn't mean it should be considered its own type of loop.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-14 15:14

    FM's and Dave Sherohman's answers are quite good, but let me add an additional way of looking at map.

    map is a function which is guaranteed to look at every element of a structure exactly once. And it is not a control structure, as it (itself) is a pure function. In other words, the invariants that map preserves are very strong, much stronger than 'a loop'. So if you can use a map, that's great, because you then get all these invariants 'for free', while if you're using a (more general!) control structure, you'll have to establish all these invariants yourself if you want to be sure your code is right.

    And that's really the beauty of a lot of these higher-order functions: you get many more invariants for free, so that you as a programmer can spend your valuable thinking time maintaining application-dependent invariants instead of worrying about low-level implementation-dependent issues.

    0 讨论(0)
  • 2020-12-14 15:15

    "Loop" is more of a CS term rather than a language-specific one. You can be reasonably confident in calling something a loop if it exhibits these characteristics:

    • iterates over elements
    • does the same thing every time
    • is O(n)

    map fits these pretty closely, but it's not a loop because it's a higher-level abstraction. It's okay to say it has the properties of a loop, even if it itself isn't a loop in the strictest, lowest-level sense.

    0 讨论(0)
  • 2020-12-14 15:15

    It all depends on how you look at it...

    On the one hand, Perl's map can be considered a loop, if only because that's how it's implemented in (current versions of) Perl.

    On the other, though, I view it as a functional map and choose to use it accordingly which, among other things, includes only making the assumption that all elements of the list will be visited, but not making any assumptions about the order in which they will be visited. Aside from the degree of functional purity this brings and giving map a reason to exist and be used instead of for, this also leaves me in good shape if some future version of Perl provides a parallelizable implementation of map. (Not that I have any expectation of that ever happening...)

    0 讨论(0)
提交回复
热议问题