Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is "map" a loop?

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 like a loop (does O(n) work, can be easily re-written by an equivalent loop, and sort of fits the common definition = "a sequence of instructions that is continually repeated").

On the other hand, map is not usually listed among Perl's control structures, of which loops are a subset of. E.g. http://en.wikipedia.org/wiki/Perl_control_structures#Loops

So, what I'm looking for is a formal reason to be convinced of one side vs. the other. So far, the former (it is a loop) sounds a lot more convincing to me, but I'm bothered by the fact that I never saw "map" mentioned in a list of Perl loops.

like image 603
DVK Avatar asked Jun 11 '10 03:06

DVK


People also ask

Is map function same as for loop?

map generates a map object, for loop does not return anything. syntax of map and for loop are completely different. for loop is for executing the same block of code for a fixed number of times, the map also does that but in a single line of code.

Is array map a loop?

Array. prototype. map() is a built-in array method for iterating through the elements inside an array collection in JavaScript. Think of looping as a way to progress from one element to another in a list, while still maintaining the order and position of each element.

Is map faster than for loop JavaScript?

Under these specific circumstances, if you need the benefit of around half a second of performance per-10,000,000 elements in Chrome you might be better off using a for loop for now. However, on other platforms / environments or other circumstances, map might still be faster, and in fact it may be faster in the future.

Is map slower than for loop JavaScript?

Even with these simple tests, loops are almost three times faster.


2 Answers

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.

like image 180
Carson Myers Avatar answered Sep 21 '22 03:09

Carson Myers


No, it is not a loop, from my perspective.

Characteristic of (perl) loops is that they can be broken out of (last) or resumed (next, redo). map cannot:

map { last } qw(stack overflow);  # ERROR!  Can't "last" outside a loop block 

The error message suggests that perl itself doesn't consider the evaluated block a loop block.

like image 37
pilcrow Avatar answered Sep 20 '22 03:09

pilcrow