DEV Community

Discussion on: ELI5 - "Map-filter-reduce"

Collapse
 
idanarye profile image
Idan Arye

When working with map and friends, you usually want to use iterators/generators/enumerators/streams/whatever-they-are-called-in-your-language-of-choice instead of lists. You are only going to iterate them once - it's wasteful to allocate memory for it when these combinators can usually read it one value at a time and "pass" the value they emit to the next combinator.

If we take your Python example:

map(lambda x : x*x, [i for i in range(10)])

This allocates a list to hold your 10 numbers - which is redundant, because map can just read them one at a time. This is better:

map(lambda x : x*x, (i for i in range(10)))

(i for i in range(10)) will not allocate a list for holding all 10 numbers. Instead, it will create an iterator that generates the next number on demand. With 10 numbers the difference is neglectable - but when you need to handle larger lists it can be quite serious, and the syntax for doing it the right way is easy.

This holds for other languages too - each with it's own syntax/api.

Collapse
 
idanarye profile image
Idan Arye

It seems, though, like it doesn't improve things in all languages: stackoverflow.com/q/47653131/794380

Collapse
 
tpoliaw profile image
Peter Holloway • Edited

Even

map(lambda x : x*x, (i for i in range(10)))

is excessive if you're using a comprehension anyway.

(i*i for i in range(10))
Collapse
 
idanarye profile image
Idan Arye

Your version doesn't do the job though...

Thread Thread
 
tpoliaw profile image
Peter Holloway

It doesn't? What's the difference?

Thread Thread
 
idanarye profile image
Idan Arye

The purpose of the original snippet was to demonstrate the map combinator. Your version doesn't do it.