Hacker News new | comments | show | ask | jobs | submit login
Functional Programming in Python [pdf] (oreilly.com)
83 points by happy-go-lucky 5 months ago | hide | past | web | favorite | 26 comments

Shameless plug, if you are interested in functional programming in Python:


(PR welcome).

Readers may be interested in the functional superset of Python called Coconut (http://coconut-lang.org). Playing with it casually has been great fun.

Very interesting, thanks. Wondering if Cython would have been a better compilation target. It would still stay usable from CPython.

Wow, it has a Mypy integration, too. Have you had occasion to look at the compiled output? Is it idiomatic and readable Python?

Is Python fully capable and suitable for functional programming?

It's quite possible to write code in a functional style in Python. Map pure lambdas over lazy, infinite sequences all you want. However, Python won't stop you writing code with spaghetti-like flow control and rampant side effects. Self-discipline is required. If you want to write only functional code, then you're definitely better off with a more rigid language that explicitly disallows mutation and side-effects.

However, if you subscribe to the idea that many real-world programs are naturally expressed as "a functional core inside an imperative shell"^1, then Python's stylistic flexibility might make it a good, practical choice.

1. https://gist.github.com/kbilsted/abdc017858cad68c3e7926b0364...

What about if you subscribe to the idea of an imperative core inside a functional shell?

why would you do such a thing?


Not quite. Sometimes its' easier to express the low level details of an algorithm in an imperative way, yet retain the ability to compose components together to make a larger system -- particularly when you consider the difficulty of testing at the system level.

Not really no. You can use the idioms but FP python is not. Guido has declared that there will never be tail call optimization to name one glaring incompatibility.

You can do functional programming in Python, but it's not pleasant. Python lacks a lot of functional niceties - first-class composition, first-class immutable data structures, tail-call optimisation and multiline lambdas

On the contrary, I find it quite pleasant despite (or because of) those facts. Multi-line lambdas I'm particularly glad to be rid of.

Define "fully" and/or... well judge for yourself: https://github.com/dbrattli/oslash/wiki/Functors,-Applicativ...

It depends what you need. It obviously has mutable state and loose typing but it has first class functions and you don't need to use classes so I would say yes it is capaable but I wouldn't pick it for a functional language.

No. It has a lot of functional support, but it's not ideal if you want to write clean, exclusively functional code. What it is amazing at is mixing the paradigms, using whatever styles fit your immediate need the best.

Yes. A function is an object just like any other. What more do you need?

If you say macros, I'd argue that's not quite the same as "functional".

Lots of easy handling of structures immutably. Which Python has a good bit of for sure, with comprehension especially. But I could use more. :)

I wish everything immutablejs gave me was built in to Python. :)

Comparing with immutablejs, there are 4 broad categories that match: lists, dicts, sets, and iterators.

Tuple slicing and concatenation gives you the "immutable" behavior for ordered data. For sets and dicts, one could create a subclass that override add/insert/set methods to return a modified copy. Iterators already have excellent support.

I'm not an immutablejs user, but my first skim of the docs doesn't make it clear what's missing from Python. If the immutable subclasses would be valuable, I'll gladly make a module for it. There's already frozenset. A frozendict didn't seem as useful (according to core devs).

Plus, I forgot that sets have nice operators for union, intersection, etc. So you don't need to override anything there.

> What more do you need?

Tail call elimination, so you don't have the tiny recursion that Python locks you too.

Real lambdas instead of the arbitrarily limited one's Python has.

Sane name spaces, no hacks like "nonlocal", immutable bindings by default, more fluid syntax, functors, encapsulation, tail call elimination.

You're using terms like "sane" and "hacks" that have negative connotations without acknowledging the benefit those design decisions provided over the history of the language.

Language design involves trade-offs. For example, tail call elimination would obfuscate tracebacks and make the interpreter more complex.

Need is different than want. You might want tail call elimination, but you don't need it to write elegant functional style code.

It also lacks multimethods, or pattern matching which mean you need classes for polymorphism. Which makes it not suitable for for imo

No, not really :D

This is also a good addition for functional programming in Python:


Applications are open for YC Summer 2018

Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact