10 ms·
Also: functional programming feels better in JS than in Python. On the other hand, it's nicer to write for-loops in Python.
by mvn9 6y ago
Also: functional programming feels better in JS than in Python. On the other hand, it's nicer to write for-loops in Python.
- BossingAround 6y agoI really love the array.map( e -> e.doStuff() ) syntax. You can do similar in Java and other JVM languages of course. Not sure whether you can do that in Python.
- magnio 6y agoThere is map in Python, but nowhere near as nice as in JavaScript IMO.
- JimDabell 6y agoYou 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]