Hacker News new | ask | show | jobs
by saeranv 2021 days ago
Question by someone who is ignorant but interested in functional programming: what is the closest equivalent to these functions in Python? (Or correct me if I'm asking the wrong question).

I used to love using lambdas in Python, along with map/reduce/filter but for whatever reason the Python community has turned against it. Map and filter can now be nicely done with list comprehensions, although I still haven't found a decent one-line equivalent for reduce (other then importing functools).

4 comments

There is none and after having used Elixir for a year going back to Python for anything non-trivial input/output parsing feels really cumbersome now.

Other comments have mentioned that functional idioms make code harder to read for devs unfamiliar with the concepts but the pipe operator IMHO has no downsides (I am not even sure what it really has to do with funcational programming, other than that it happens to be used in more functional languages).

> Other comments have mentioned that functional idioms make code harder to read for devs unfamiliar with the concepts but the pipe operator IMHO has no downsides (I am not even sure what it really has to do with funcational programming, other than that it happens to be used in more functional languages).

What it has to do with functional programming is, first, that it's right side operand is a function, and, second, that it's a technique for unrolling the deeply nested function calls common in expression-oriented functional programming without resorting intermediate assignments which are natural in statement-oriented imperative programming but less so for single-user and not independently semantically important values in expression-oriented functional programming.

Thanks for the explanation, that makes sense!
I'm not sure what specific "functions" you're talking about, but Python generally encourages a procedural style of programming as opposed to functional. The rationale is that functional code can be really difficult to read if you aren't already familiar with the idioms and terminology, whereas it's pretty easy to mentally parse and understand a `for` loop. So in that sense, list comprehensions are about as far as Python goes in that direction; there is no syntactic equivalent to the pipe operator, and no way to write reduce or similar operations as succinctly as you can in functional languages.
Having read plenty of python numerical code, I'm not sure "easy to parse and understand" is what exactly comes to mind.
Most of the problem comes from Pandas which is of course R-inspired.
Yup, it's a terrible shame that pandas started off as a base-R clone in Python.

Now, the only time I write base-R like code is in Python, which is pretty weird.

It's also strange as sklearn is beautiful, and in general python libraries are nicer than the equivalents in R, but pandas is a large, warty exception.

And just in brief as to what the pipe generally does without the syntax available. If you have functions x and y which take 1 argument. Where |> is the piping syntax

y(1) |> x would be equivalent to x(y(1)) in python.

> Question by someone who is ignorant but interested in functional programming: what is the closest equivalent to these functions in Python?

The equivalent to the function of the first is python’s lambda syntax, there's no simple syntax providing the same function as the pipe.

A pipe merely "pipes" the output of one function as an input to another. For example, | in bash. In Python this can be done the trivial way (by composing) or by using decorators.
Yes, it can be done the trivial way in most languages. For deep nestung, that's ugly and awkward, which is why some languages have piping/composition operators [or threading macros] (sometimes more than one). Python has no close equivalent of a piping operator or threading macro (decorators don't seem helpful at all here.)
You might be interested in coconut [0] which extends python with functional concepts and compiles to python.

[0] http://coconut-lang.org/