3 ms·
I don't use FP practices in Python much, but if I did I'd define the filter outside the map, like so: begins_with_a = lambda x: x.startswith('a') alist
by LyndsySimon 12y ago
I don't use FP practices in Python much, but if I did I'd define the filter outside the map, like so:
begins_with_a = lambda x: x.startswith('a')
alist = map(foo, filter(begins_with_a, words))
- nilved 12y agoEven with named functions, Python's use of global functions instead of methods for iterators force you to read the expression from the inside out. I think Lisp languages nailed this with their threading macros, which allow natural left-to-right reading, but Ruby's strategy is better than Python's, too, while maintaining very similar syntax. ;; clojure (let [begins-with-a #(.startsWith % "a") foo #(do-some-stuff-with %)] (-> words (filter begins-with-a) (map foo)))) # ruby words.filter { |e| e.start_with?(?a) }.map { |e| foo(e) } It doesn't really make sense for things like `len` and `map` to be global functions in object-oriented languages.