Comment

Comment by whalesalad

Disclaimer: as a Clojure developer and functional programming evangelist, I do not want to be too critical of this article. It's a really great post about some incredibly valuable and advanced Clojure features.

That being said, it really bothers me when functional programming evangelists write out such horrible Python code examples. It makes Python look like it's not a functional language. It perpetuates the idea that Python won't let you write elegant and stateless programs that behave in a functional manner.

If the author had not mentioned Python and had instead used it as pseudocode to represent the entirely non-functional way to write something, I would have been okay with it. But calling out Python specifically is just incorrect!

I have rewritten the code in a very pythonic manner that illustrates the functional capabilities of the language:

    even = lambda x: x % 2 == 0

    # alternatively, depending on your religious beliefs:
    def even(x): return x % 2 == 0

    def process(seq):
      return sum(x + 1 for x in seq if even(x))
Note the (parens) for the comprehension instead of [brackets], which creates a generator. The above code is lazy (in Python 3)!

And if we fire up a python repl and play around:

    In [2]: process(range(10))
    Out[2]: 25
I really love Clojure and use it daily, but I find the Python version to be far more legible. It reads more like english.

Anyway, the moral of the story here is that you can do immutable/functional programming with regular ol' Python.

parent whalesalad 9 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton