Neato
This is clever and worth reading, and I didn’t know about some of the 3.12 changes. Overall though I think the author has some pain in store. Using functions like map in Python result in Python iterators which are mutable objects (they consume and throw away an element of the sequence on each iteration) and this often causes hassles unless you convert them to lists (which burns memory and maybe does unnecessary computation). Also, Python’s type checking stuff (at least Mypy) are mostly a bug catching feature. They aren’t like a real type system though: mistyped programs can still get through, and properly typed ones can get flagged. It’s better than nothing and I use it, but it’s nowhere near e.g. Haskell.
I don’t think I would call this functional. Python is decidedly not at all functional - there’s no way to declare arbitrary functions inline, no chaining of map/filter etc.
But the static types are definitely welcome. I didn’t know about the
type
keyword. Apparently it makes it support forward references.like everything in python, to achieve functional you must first
import functional
You’re still limited by lambda expressions though. And in general the language is still statement based, not expression based. You can’t do
a = if foo then x else y
type things (except for the one-off and backwardsx if foo else y
; they were so close!).“a = x if foo else y” is a perfectly cromulent statement!
It’s not. In functional languages there’s no special case like this. All if-elses are expressions. It’s far superior. For example how do you do this with Python’s if-else expression?
let x = if foo { let y = bar(); baz(); y } else { z }
x = foo(y:=bar(), baz(), y) or z
should work assuming foo bar and baz are functions being called?if this is setting y to the effect of bar() + running baz after, then:
x = [bar(), baz()][0] or z
might work
and if you need y to be defined for later use:
x = [(y:=bar()), baz()][0] or z
but thats from memory, not sure if that will even run as written.
if I get to a real computer I'll try that with an actual if statement instead of a bastardized ternary.
foo
isn’t a function, it’s a bool. But in any case, as you can see the answer is “with terrible hacks”. Python is not a functional language. It is imperative.Yeah, never said it was, just that if you really want to emulate that style you mostly can.
Does it have higher-order functions? Yes, therefore you can use it to do functional programming.
Everything else is syntactic sugar.
Functional programming doesn’t just mean higher order functions. There’s a range of other features that it implies.
This is quite cool and a very interesting read. I’m not sure if it’s something I would want to use in a real project, because it looks like you’re basically inventing a new language within Python’s type system itself, which is a little weird. That said, I feel like I learned a lot from reading this, and it was a very interesting demonstration of what you can do with Python’s type-checking system.