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(lambdax:x*x,[iforiinrange(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(lambdax:x*x,(iforiinrange(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.
When working with
mapand 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:
This allocates a list to hold your 10 numbers - which is redundant, because
mapcan just read them one at a time. This is better:(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.
It seems, though, like it doesn't improve things in all languages: stackoverflow.com/q/47653131/794380
Even
is excessive if you're using a comprehension anyway.
Your version doesn't do the job though...
It doesn't? What's the difference?
The purpose of the original snippet was to demonstrate the
mapcombinator. Your version doesn't do it.