4 ms·
You can do that in most languages. But map() (in either language) has the semantics of applying a transformation to each item in a collection to produce a new
by JimDabell 6y ago
You can do that in most languages.
But map() (in either language) has the semantics of applying a transformation to each item in a collection to produce a new collection, so you wouldn’t really want to use map() to call something like doStuff(). That's what normal loops and forEach() are for. If you aren’t transforming the collection into a new collection, map() is the wrong choice; you’re telling anybody reading it that you’re doing something that you aren’t.
But assuming doStuff() just has a bad name and is actually transformational, then the most direct translation would be to use map() and a lambda:
new_array = map(lambda e: e.doStuff(), array)
However it’s more idiomatic to use a list comprehension:
new_array = [e.doStuff() for e in array]