faif / python-patterns

A collection of design patterns/idioms in Python

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Question] Functional Programming Patterns

fkromer opened this issue · comments

Would you accept PRs about functional programming patterns @faif ? Would probably imply some project restructuring (e.g. putting dirs "behavioral" and "structural" into "oop").

I'm a big fan of fp but although you can do it in Python, it is not always considered idiomatic. It's not a bad idea but I'm not sure if it's good to put it in this repo. I'd like to hear some thoughts from the rest of the community.

(...) although you can do it in Python, it is not always considered idiomatic.

As far as I know/learned until now some functional programming in Python is very pythonic. However these kind of patterns are more idioms than higher level patterns and not really suitable for the repo (due to low level).

+1 for functional patterns here.

@fkromer had you something particular in mind?
We should probably make a list to briefly discuss it before actually implementing(or copying).

I'll start proposing "pipeline" candidate

I had the patterns addressed in S. Lott's "Functional Python Programming" in mind. In case you want to look into the book pay attention before doing so: There is a section about patterns and some references in the index but the majority of examples is spread over the whole book. It makes sense to depend on some Python package(s) which implement boilerplate required to implement the actual patterns. As @faif already said: I am not sure if it's a good idea to put this into here.

Recently I started a small project called 30 seconds of functional python programming. Right now it's a collection of Python package API examples running in an interactive Python environment on Binder. Python packages for functional programming have been hardly addresses in the books I've looked into so far. The packages provide lowest level building blocks to adapt a FP style beyond what you can do with Pythons builtin and standard lib stuff. I think it's way more effective to learn new programming language features via exploratory experimentation. I've not pushed everything I've prepared so far but I will do so that you get a better understanding of what the project is aiming for. E.g. I'd like to port the JavaScript examples from Functional Programming Jargon to Python as well. And it would be reasonable to put pattern examples/implementations into specific Python package specific sections as well. Feel free to contribute there as well.

I'll start proposing "pipeline" candidate

Another higher level candidate: Parallel Functional Pipeline Pattern

Perhaps this repo would be a better place to centralize functional programming pattens and concepts in Python.

https://github.com/sfermigier/awesome-functional-python

In my opinion, Python is not a good language for FP (many use cases are not covered), although it has some of the ideas of FP.
So I think FP Pattern should not be included in this repository because this feature is not complete.

[From this page: https://intellipaat.com/community/12542/why-isnt-python-very-good-for-functional-programming]
Below is a list of some functional things which you will miss in Python:

  • Pattern matching
  • Tail recursion
  • Large library of list functions
  • Functional dictionary class
  • Automatic currying
  • A concise way to compose functions
  • Lazy lists
  • The meaning of no pattern matching and no tail recursion in Python is your basic algorithms have to be written imperatively.Recursion is ugly and slow in Python.
  • There is no syntax for currying or composition means that point-free style is about as full of punctuation as explicitly passing arguments.
  • Iterators instead of lazy lists mean that you have to know whether you want efficiency or persistence and to scatter calls to list around if you want persistence. (Iterators are use-once)

A project accepts PRs on functional programming patterns would depend on the project's goals and the maintainers' preferences. If the project's focus is on object-oriented programming, the maintainers may not be interested in adding functional programming patterns or restructuring the project in that way. However, if the maintainers are open to adding functional programming patterns, restructuring the project may make sense, and PRs related to functional programming could be considered. Ultimately, the best course of action would be to reach out to the maintainers and ask for their thoughts on the matter.

I believe that functional programming idioms in any language are good, as long as they are not implemented in an obscure way. Especially the fundamentals such as immutability and pure functions can be very beneficial to Python programmers. So if people are willing to showcase them here, maybe it's a good idea to include them.