Is “map” a loop?

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

    Your question turns on the issue of classification. At least under one interpretation, asking whether map is a loop is like asking whether map is a subset of "Loop". Framed in this way, I think the answer is no. Although map and Loop have many things in common, there are important differences.

    • Loop controls: Chas. Owens makes a strong case that Perl loops are subject to loop controls like next and last, while map is not.
    • Return values: the purpose of map is its return value; with loops, not so much.

    We encounter relationships like this all the time in the real world -- things that have much in common with each other, but with neither being a perfect subset of the other.

     -----------------------------------------
    |Things that iterate?                     |
    |                                         |
    |      ------------------                 |
    |     |map()             |                |
    |     |                  |                |
    |     |          --------|----------      |
    |     |          |       |          |     |
    |     |          |       |          |     |
    |      ------------------           |     |
    |                |                  |     |
    |                |              Loop|     |
    |                 ------------------      |
    |                                         |
     -----------------------------------------
    

提交回复
热议问题