5 ms·
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
by saeranv 6y 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).
- RussianCow 6y agoI'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.
- vizzier 6y agoAnd 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.
- nightski 6y agoHaving read plenty of python numerical code, I'm not sure "easy to parse and understand" is what exactly comes to mind.
- empthought 6y agoMost of the problem comes from Pandas which is of course R-inspired.
- disgruntledphd2 6y agoYup, 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.
- valzam 6y agoThere 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).
- dragonwriter 6y ago> 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.
- valzam 6y agoThanks for the explanation, that makes sense!
- dragonwriter 6y ago> 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.
- NashHallucinate 6y agoA 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.
- dragonwriter 6y agoYes, 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.)
- syntonym2 6y agoYou might be interested in coconut [0] which extends python with functional concepts and compiles to python. [0] http://coconut-lang.org/ http://coconut-lang.org/