Martin McBride
1 min readMar 17, 2020

--

As others have pointed out, simple examples of map, filter and reduce can often be replaced by list comprehensions and reducing function such as sum().

But one of the big advantages of functional programming is lazy evaluation via iterators.

I use Python to create animations. I can use generators and map to create a fairly complex chain of operations, very intuitively. When I run it, each frame of the animation passes through the entire chain before the next one starts. I start to see my output folder filling with final PNG files straight away. I don’t have to wait for the whole animation to finish before I spot that every frame is blank because of a silly coding error (leaving aside the vast amount of memory usage or disk churn required to process all the images through each stage before going on to the next one).

Yes you could use generator comprehensions instead. That gives you lazy evaluation but every comprehension contains a for loop. It might be a single line for loop, but by the time you have 20 of them in a chain it looks pretty repetitive and a recipe for copy and paste bugs.

Horses for courses, but unfortunately simple examples don’t fully illustrate the benefits of map.

--

--

Martin McBride
Martin McBride

No responses yet