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
5
u/ws-ilazki Jul 01 '21
No need to apologise, ambiguity in English is easy and it's as likely I simply misinterpreted and others didn't. :)
No idea honestly, Python's not my thing. It's probably not that common, but it seems to make a good effort to play nice with non-Coconut users with its code output, various compile options, and general interoperability. Sort of like using TypeScript instead of JS.
OCaml uses a similar type system and the same notation for type signatures, so that's not a big deal either way. Haskell's interesting if you specifically want a more academic approach that strongly enforces pure functions, but the trade-off is that adds tangential complexity in the form of having to deal with monads right out the gate instead of learning actual FP fundamentals first. Plus it's lazy by default which is cool but has some performance and complexity implications that might be a surprise as a newbie.
OCaml is still FP-first, but allows you to intermingle impure code in a way that makes it a bit more practical and familiar. No monads at the start, strict evaluation instead of lazy, just a smoother start experience. Plus the linked book is an amazing introduction to FP concepts that mostly carries over to any language.
Not exactly the language itself, but OCaml's cool as a learning tool because it has some amazing supporting tooling that makes exploratory programming really nice. It has an amazing REPL experience that's good for exploration, almost as good as what you get from a good lisp or Smalltalk. For example, you can inspect practically everything in the REPL using
#show: view function or entire module signatures, inspect objects, view the value and types of variables, etc. And you can get more functionality by installing an alternate REPL calledutopto add autocomplete and some other cool stuff, plus there'socp-browserfor module exploration. Also theocamlcommand used for the REPL works as an interpreter, so you don't even have to compile source files if you don't want to. It understands shebangs so you can add one like you would a python/ruby/perl/etc script, like#!/usr/bin/env ocaml,chmod +xit, and run it without compilation. Fun trick I use a lot to test stuff out early on.Honestly though it doesn't matter if you want to learn with F#, OCaml, Elixir, Erlang, Clojure, Haskell, etc.; just figure out something that appeals to you and has good resources on beginner FP. It's better to start with something that strongly encourages FP idioms, which is why people are suggesting FP-first languages instead of trying to pick up FP via JS or other methods, but it's not an absolute requirement. It just helps push you into the right direction because the language itself is designed to encourage FP style.
I actually started with FP using Clojure and the book that helped me "get" FP was the first third or so of Clojure Programming. But I suggest Functional Programming in OCaml to people because it's an amazingly good book that has good explanations of concepts, exercises, uses an FP-focused language, and is freely available to read.