3 ms·
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
by xwowsersx 3y ago
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's even worse when relying heavily on recursion, etc
Still, a fun project!
- jgilias 3y agoIt 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.
- xwowsersx 3y agoNo, it doesn't need to be all or nothing, but the scenario I was directing my point at was where people try to "do FP in lang X", not just sprinkling in functional concepts here are there. At this point, almost every language has some functional programming concepts in it, but I wasn't addressing that. I could've been clearer, but I've seen many cases where people would rather be programming in Haskell or some other functional language and then when "forced" to use some other language, like Python, they try to essentially recreate Haskell in that language and my point is that this really doesn't work or, at the very least, it's more trouble than it's worth and the resulting codebase is a complete mess because you've tried your hardest to do FP in a language that ultimately isn't suited for it. You are absolutely right though that it needs not be all or nothing. To the extent you can rely on some of these constructs and enable equational reasoning by avoiding mutation, etc. you'll be better off, no doubt (I do this too in Python and other languages because wherever I can make it easier to reason about my code, I will do so). I'm just cautioning against trying to do something akin to pure FP in Python.
- jgilias 3y agoThis I agree with completely! I’m a big believer of “when in Rome”. As in, try to be idiomatic in whatever language you need to use. I once saw a Python codebase where someone had tried very hard to write Java in Python.. That was equally horrible!
- xwowsersx 3y agoExactly. Java in Python sounds scary :)
- pjmlp 3y agoScheme is one of the few FP languages with required TCO on the language standard, and alongside Lisp, Caml Light, Standard ML, OCaml, F#, Scala, supports mutable data structures.
- xwowsersx 3y agoRight. I've used Scala pretty extensively and even Scala, which obviously has much better support for FP than Python, is a pretty unpleasant experience if you try to turn it into "Haskell on the JVM", in my opinion.