Re:Anti-Patterns in Python Programming
by LyndsySimon on Wednesday July 09, 2014 @03:16PM
(#8011666)
I don't use FP practices in Python much, but if I did I'd define the filter outside the map, like so:
[ Parent
| on HN ]
Even 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.