3 ms·
> ain’t very good with maps and filters Isn't a comprehension just a map/filter by another syntax? You can re-create them from a list comprehension pretty simp
by ok_dad 5y ago
> ain’t very good with maps and filters
Isn't a comprehension just a map/filter by another syntax? You can re-create them from a list comprehension pretty simply:
`map = lambda data, map_fn : [map_fn(x) for x in data]`
`filter = lambda data, filter_fn : [x for x in data if filter_fn(x)]`
Then, why even have the lambda-translations above since the comprehension is already "Pythonic"? Also, you have functools (https://docs.python.org/3/library/functools.html https://docs.python.org/3/library/functools.html) which you can use for several functional programming paradigms as well.
Functional programming isn't what you call the techniques (map/filter/etc.) it's how you are programming that makes it functional. You can still use functional techniques in Python to make things a bit easier to reason about. A lot of the things people thing about functional programming aren't inherent to the actual functional programming paradigm: like laziness or immutability.
- muxator 5y agoNit: in py3 map() and filter() are lazy (they return iterators instead of lists). You can better mimic their semantics using generator expressions "()" instead of list comprehensions "[]".