r/functionalprogramming • u/redd-sm • Jun 29 '21
Python good examples of functional-like python code that one can study?
Would love to be able to study some real world python code that is written in functional style. Have not come across any. They must exist out there given the interest in functional and interest in python.
Thank you for sharing.
16
Upvotes
View all comments
Show parent comment
3
u/ws-ilazki Jul 01 '21
Haskell's a great language but a lot of what it does and how it does it is more academic or experimental, so IMO you end up learning some weird habits that don't necessarily translate to other languages. (Though they might one day.)
OCaml and Clojure, on the other hand, are functional languages that are more pragmatic. Their language design encourages you toward FP idioms without forcing them by having defaults that make FP the obvious solution, but still allowing "escape hatches" when it makes sense. That makes them more practical for general use IMO, but I think it's also a trait that makes for a good starter FP language, because some problems are harder to solve in an FP way and it just makes more sense to go "fuck it, imperative for this bit".
The issue with languages like JS for learning FP is that, while you can mix imperative and FP in the same way, they default toward imperative idioms, which makes FP sometimes unnatural or clumsy. It's easier to just write a non-FP solution to everything, and that can be a bad thing when learning, because learning FP involves unlearning some existing habits. If the language is (not so) subtly encouraging imperative style it's going to be harder to learn a different way of thinking.
Once you've learned it a bit, though, those wishy-washy not-quite-FP languages like JS can be really nice to use, because they make it easy to take what you've learned about FP and put it to use in a practical way. Lua, for example, is an imperative language that operates a lot like JS (but with different syntax) and is, unfortunately, a terrible language to learn FP in because it lacks even the most basic FP functions like
map. However, good knowledge of FP makes the language much nicer to use because you can take advantage of FP to cut down on a lot of annoying boilerplate code and do some cool stuff. Terrible to learn FP with, great to use with FP style.Basically even if you don't want to work in an FP language you can benefit from learning FP style in one because it encourages a lot of good patterns that will follow you into other languages. :)
Sorry, no suggestions there. With the exception of art stuff (which is primarily a visual medium) I don't get much benefit out of video tutorials. For programming topics I stick to text articles and documentation.