Live data from Hacker News

Functional Python Programming

docs.python.org

61–70 of 105 posts

Re: Functional Python Programming

#62

Fun to work on doing FP in languages that don't really support it, but in my view a language has to be built for FP for it to be a practical option in any real applications. Several obvious reasons for Python being a poor lang in which to do FP: - mutable data structures - no built-in function composition - limited support for HOF - no tail call optimization (AFAIK) - performance in general isn't great and I imagine…

It doesn’t have to be all or nothing though. I’ve definitely seen (and written) functional Python in ‘real applications’ sprinkled here and there in an otherwise normal codebase.

In fact, I’d wager that any Python programmer worth their salt would suggest using a list comprehension and a lambda over a loop in most data transformation situations. Also currying comes in handy often once you have it in your toolkit.

It’s a bit silly to call two very mature libraries that are part of the Python standard library a ‘fun project’ just because the language itself supports multiple programming paradigms.

Re: Functional Python Programming

#63
post #54
post #50

Earlier quoted context omitted.

import pandas as pd import functools ( pd.Series(range(10)) .apply(lambda x: x * 10) .where(lambda x: x % 2 == 0) .pipe(lambda s: functools.reduce(lambda x, y : x + y, s)) )

Readability isn't the best. Also what you present here is method chaining and not functional pipes.

Is the difference mostly syntactic?

Re: Functional Python Programming

#64
post #21

shameless plug: I maintain a small library to do functional pipes. You can write: ( range(10) | Map(lambda x: x * 10) | Filter(lambda x: x % 2 == 0) | Reduce(lambda a, b: a + b) ) instead of: x = range(10) x = map(lambda x: x * 10, x) x = filter(lambda x: x % 2 == 0, x) x = reduce(lambda a, b: a + b, x) and more. https://tandav.github.io/pipe21/

This looks cool! I always wanted something similar for pandas, because I found it very elegant in dplyr in R. AFAIK they do have a `.pipe` method now but it could definitely be better in the future.

Re: Functional Python Programming

#65

Earlier quoted context omitted.

Then what is the point?

Readability. If you're using the same name everywhere, then your code is really hard to understand for other devs: val = my_list.reduce(foobar) What is even going on here?

one option is to define your function inline, directly above the line where you would have liked to write the complex anonymous function. I guess if you are worried about namespace pollution you can 'del' the symbol afterward.

Re: Functional Python Programming

#66
post #8

Sadly, Python is a pretty poor functional language. The core of functional programming is about avoiding mutable states , not much about anonymous functions or passing functions as data. To do proper functional programming in Python, there should be IMO: - a way to enforce non-mutable variables/objects; - non-mutable collections; - proper support for recursion and tail-recursion optimization; - a better syntax for an…

Syntactically maybe, but I find it has a quite workable functional subset. Integers, floats, tuples, named tuples, and frozensets are all immutable, functions are values, etc. E.g.: https://joypy.osdn.io/notebooks/Derivatives_of_Regular_Expre... -or- https://github.com/calroc/xerblin/blob/master/xerblin/btree.... It's not fantastic, but it's not that bad.

I love both python and functional programming and I do write functional style python routinely. One barrier I hit is there is no immutable dict type. There is MappingProxyType but it's insufficient. I prefer using custom dataclass like objects built with pydantic and type checked with mypy. It's better than dict and can be made immutable but requires tons of boilerplate code which is a bit unpythonic.

Re: Functional Python Programming

#67
post #54
post #50

Earlier quoted context omitted.

import pandas as pd import functools ( pd.Series(range(10)) .apply(lambda x: x * 10) .where(lambda x: x % 2 == 0) .pipe(lambda s: functools.reduce(lambda x, y : x + y, s)) )

Readability isn't the best. Also what you present here is method chaining and not functional pipes.

What difference does it make? They're conceptually the same thing. You're mapping immutable data to map/reduce/filter like pure functions to get new data.

Re: Functional Python Programming

#68
post #21

shameless plug: I maintain a small library to do functional pipes. You can write: ( range(10) | Map(lambda x: x * 10) | Filter(lambda x: x % 2 == 0) | Reduce(lambda a, b: a + b) ) instead of: x = range(10) x = map(lambda x: x * 10, x) x = filter(lambda x: x % 2 == 0, x) x = reduce(lambda a, b: a + b, x) and more. https://tandav.github.io/pipe21/

Hmm, I've never seen code like the latter so it's unclear what problem this solves. I'd just write the pandas code snippet given below. Possibly with polars to make it lazy.

Re: Functional Python Programming

#70
post #8

Sadly, Python is a pretty poor functional language. The core of functional programming is about avoiding mutable states , not much about anonymous functions or passing functions as data. To do proper functional programming in Python, there should be IMO: - a way to enforce non-mutable variables/objects; - non-mutable collections; - proper support for recursion and tail-recursion optimization; - a better syntax for an…

Agreed, Python is designed for something other than functional programming, so a functional programming enthusiast would get better mileage out of something else. Even if you were to write all of your Python programs against the grain in a functional style, you'd still need to operate in a community of modules that don't provide referential transparency.

Even worse when some modules have global mutable state like matplotlib and co.
Post reply on HN