12 ms·
The case for dynamic, functional programming
- functional_pyth 4y agoWould have been a stronger case if Julia, Erlang/Elixir, or Clojure/Racket was used. Python is one of the worst dynamic/FP languages I've ever had the displeasure to utilize. I think the OP needs to step out of their comfort zone of Python.
- qsort 4y agoThe point of the article isn't to say that python is better, all it's saying is that a more functional style leads to cleaner code. This is irrespective of python's technical merits. With a dynamic, functional language you could enjoy all of this simplicity. But you don't even need that to benefit from this perspective today! You can adopt the techniques of functional programming right now in your favorite language (such as Python, used above). What's important is your mindset and how you approach problems, minimizing state and maximizing clarity in the code you write.
- z9znz 4y agoI suspect the point of the article was to be seen again and remind people of his book.
- jeremya 4y agoWhether or not that is the case, surely the substance of the article more worthy of your analysis than something as intangible as the authors internal motivations? Assuming positive intent is worthwhile frame of reference that keeps my world looking a bit brighter and something I always hope other do for me.
- z9znz 4y agoFair enough. My comment above was unnecessary. I did elsewhere in a top level comment go into detail about why I thought TFA wasn't good. https://news.ycombinator.com/item?id=33722399 https://news.ycombinator.com/item?id=33722399
- bslatkin 4y agoI agree that another language that's better for functional programming would make this more compelling. Python has a lot of limitations in this area. I think all of those alternatives you listed (Julia, Clojure, etc) include turn-offs for Python programmers that are deal breakers. So I'm seeking another language that appeals to Python programmers but has more of the attributes that are amenable to functional programming.
- z9znz 4y agoIMO, Ruby is a better general purpose "scripting" language like Python that has cleaner and more consistent features - including ones that lend themselves to functional approaches. Outside of web development it doesn't have the same appeal as Python due to the larger Python ecosystem... (many companies think they want to and will do "data science!") Also, in my experience, Pythonistas tend to be more resistent to paradigm changes. So often I have heard, "Why would you want to do that?", or "This works fine as it is.". Not that I'm a fan of list comprehensions in Python (I think they're awkward and ugly compared to Ruby collection operations), but Python codebases I have had the displeasure of working in had lots of nested loops, mutations everywhere, copy-pasted code, and 30+ line methods. Trying to encourage single-responsibility, composability, less OOP, and more pure functions is like shoveling water uphill. With both Ruby and Python you do need to be a bit thoughtful about unnecessary collection copies (which you would tend to favor when writing pure functions); but often you have a good sense of how large a volume of data you are handling and where copies will be particularly slow or heavy to do. When necessary, you can have an impure function that mutates input data, and at least in Ruby you can warn callers of the impending mutation by adding the ! to the function name. update_order!(order).
- revskill 4y agoAgree. But Python is not good example for me. I think Python developers are genius because they could get things done in a language with 1-line lambda !
- Jtsummers 4y agoSingle expression lambdas. Lambdas can span multiple lines but they specifically contain a single expression, no statements. And since Python isn't really an expression oriented language this is still pretty constraining.
- bjd2385 4y agoWait where is self.count set, and is it 0 or 1?
- bslatkin 4y agoFixed now, thanks!
- xedrac 4y ago> With Python it's much easier to become proficient and remember everything you need to know. That translates to more time being productive writing code instead of reading documentation Until your python program reaches a certain size where the lack of type safety and other protections becomes a real liability. Contrast that to something like Rust, where it's easy to have confidence in very large code bases.
- falcolas 4y agoAs someone who's worked on a huge Python program (10M+ LOC), type safety was the last thing on our mind when maintaining it. Refactoring might not have been quite as simple, but there are plenty of tools available for refactoring Python code, and we were never afraid of doing it. Though, working on a project right now in Java, the type system isn't exactly saving us from refactoring failures, because the type system doesn't ensure that the String or Integer or <low level interface> being passed in is the right one. Of course, I'm sure we're just "doing it wrong".
- yakshaving_jgt 4y agoThere’s a significant delta between Java types and Haskell types.
- falcolas 4y agoHow do you know someone's a Haskell programmer? They insert themselves into every discussion about typing to point out how bad all programming languages that aren't Haskell are at types.
- yakshaving_jgt 4y agoI could just as well have said OCaml. Or Idris. Or PureScript. Or Elm. The point isn't Haskell. The point is not Java.
- rowanG077 4y agoYou picked one of the best dynamic languages and compare to literally the bottom barrel of the statically typed language. And then you complain when someone points this out. This seems to me like you are acting in bad faith.
- linguae 4y agoJudging by the title, I was expecting to read about the advantages of dynamically-typed functional programming languages like Scheme and Clojure over statically-typed functional languages like Standard ML and Haskell. It seems to me that large segments of the functional programming community love static typing, and the research community has done (and continues to do) a lot of work applying type theory to the design of programming languages. However, there are still many advocates of dynamically-typed functional programming languages, generally from the Lisp family. I don’t think of functional programming when I use Python, even though it’s possible to program in a functional style. I’m also unaware of any Python development environments that offer Smalltalk or Common Lisp levels of interactivity, though this is less of a criticism of the language and more of a matter of tooling.
- falcolas 4y agoYup, I would love to see a bit more love for Lisp style languages. They're incredibly powerful and it's a shame they're relegated to the fringes of professional programming.
- bslatkin 4y ago100% agree with both of these comments. I'd love to see a resurgence of interest in this area.
- kaveh808 4y agoWhat we need are examples of Lisp-style languages leading to big wins. Clojure did that to some extent, but unfortunately in the Common Lisp world successful commercial or technical projects are few and far between. Our best bet is probably making interesting advances in the open source field, until something takes off, which is largely a factor of luck.
- AtlasBarfed 4y agoMy biggest critique of the Lisp crowd. I mean, look at what gets made in Go, Go is barely 10 years old. Go is a worse Java, not as egregious an example of "worse is better" as Javascript, but still... a backwards step outside of its threading model. For a language with such raw purported power, it is lacking in databases, operating systems, management software, games, etc. AI used to be it's killer app, but it seems the latest AI revolution isn't functional programming based. One of the issues is that Lisp makes an individual programmer very powerful. The Paul Graham Lisp essay that gets brought up CONSTANTLY is an example of that. He built an entire ecommerce site by himself, but it was incomprehensible to the people that bought it from him and they rewrote it in some more pedestrian language. Lisp lends itself to ivory tower constructions of abstractions that are ridiculously powerful to the one person that wrote them: the programmer, but the rest of the world will just end up reinventing their own wheels. It takes a higher IQ that the person that wrote code to understand it about as well as the original author. But if LISP people are all high IQ, then the people capable of reading other Lisp code is of such high IQ that it falls apart. I look at people like the Jepsen guy who uses clojure to test about the most difficult thing to test in the world: concurrent distributed systems, and other ultrasmart people and understand that FP is powerful. It also has advantages in true heavy concurrent programming, although FP seems to be squandering this. I think it goes back to the operating environment. If Lisp machines were the killer apps they seemed, the lisp people need a novel OS + GUI + IDE + REPL computing environment to visually sell themselves, kind of like what smalltalk did back in the day.
- z9znz 4y agoUnfortunately this toy example isn't even pure functional because it raises an exception as a way of providing an alternative return from the function. A better example, if one wanted to keep a single function pure and still keep (two) states would be to return a tuple of (current_count, is_expired). Further, since it uses zero as the implicit boundary to determine when the counter is complete by counting down from the provided value, the function should be named countdown_timer() or something more descriptive. Honestly I'm not sure who this little article was written for. The references to C++ were irrelevant. The OOP Python example, however, was a pretty common example of how Python people write code. It's a shame, really. We should (edit: not) have to deprogram people to get them to choose something more like the latter "functional" approach over the former pointlessly-OOPy example.
- nlitened 4y agoWhy do you consider raising an exception not being pure functional? In my opinion, it is exactly equivalent to propagating error return value up the call chain until it is handled (but is faster if no exception is thrown, and contains a stack trace of where the original error value was produced for programmer's convenience).
- jerf 4y agoIf you take that view, yes, it's functional. However, if you take that sort of view, everything is functional. Writing to the screen can be viewed as having returned instructions to write to the screen, combined with an interpreter that performs it. Writing to the network is like having returned an instruction to write the network, along with an interpreter to do it. (And a certain laissez faire attitude about laziness, perhaps.) This view is not necessarily wrong. There is a certain truth to it; for one thing it's not a terrible gateway to understanding how "pure functional" languages interact with the real world in practice, because this is pretty close to how they do it. However, it is useless, because it means every language is a pure functional language, and the utility of a term is its ability to split the universe under discussion into multiple pieces. Or to put it another way, a decision procedure that puts 100% of the items under decision into the same category conveys exactly zero bits of information. So for a definition of "pure functional" to have any utility, it needs to exclude some things. Exceptions are borderline. Technically Haskell does indeed have them, but it has a complicated relationship with them. It is certainly the case that we are discussing something that is not in the function signature anymore, which people generally consider not "pure functional". But generally it definitely has the "well, we could pretend that we returned a special value and had a special interpreter wrapped around it for every call" copout feel to it in general.
- bmitc 4y agoI'm not sure what Python has to do with functional programming, even remotely. It basically contains none of the typical building blocks for functional programming (immutable data, real lambdas, pattern matching, sane scoping, etc.). Python has weird scoping, weird lambdas, and fully on mutability. Regarding static versus dynamic functional programming, I think dynamic typing is probably oversold in that most dynamic programs could be static programs with very little effort. Where dynamic types really shine are at boundaries of programs. Dynamic types are very useful for handling messages, I/O, data formats, etc. I actually think an ideal language would be one that is at its core a statically typed functional language, but then it has dynamic types allowed at the above stated boundaries. Then there could be some explicit conversion layer or solution that moves values from one side to the other. Basically all other uses of dynamic typing can be solved in a statically typed language that has anonymous types like anonymous records and anonymous discriminated unions and some sort of "umbrella" or "shared" type such as interfaces, generics, and/or typeclasses, for example.
- somewhereoutth 4y agoIndeed, and I would further argue that business data be dynamically typed, because business data is often messy and changeable, whereas the plumbing that carries it can be statically typed to ensure you are connecting the pipes correctly.
- grumpyprole 4y ago> I would further argue that business data be dynamically typed, because business data is often messy and changeabl Huh? Static types make change and refactoring much easier.
- cardanome 4y agoIt's complicated. Static typing easily leads to a over-specification of types. With dynamic typing the least specific type that will make this work is automatically implied. You can often make huge changes to the underlying data structures and things will just work. Basically: I need a Car Object with these exact fields vs give me a thing with the field "foobar" that contains something that I can concatenate with another things. (Though static languages with structural typing like TS muddle the water here as they can represent some of the power of dynamic languages. That might be part of why TS is so popular. The problem is that types in dynamic languages can easily get super complex without feeling complex while for static languages you might need very advanced features (dependent types and other scary stuff) to represent some cases.) The big downsides when working with dynamic languages: You need to have tests or at least some reliable way to test you program. If not, refactoring can be indeed worse than what you know from static languages. Also static types make it easier to safely make changes to a program without actually understanding most of the code as you can just follow compiler errors while dynamic languages require you to have some model of what is going on in your head. So for people that jump projects a lot, static typing also allows for easier refactoring while dynamic typing allows for more productivity when one is more intimate with the code.
- substation13 4y ago> You can adopt the techniques of functional programming right now in your favorite language (such as Python, used above). No tail call optimization, no expression orientation, mutability by default, no variable shadowing, single expression lambdas, no do-notation, reference semantics everywhere, dog slow function calls... functional programming in Python doesn't work in my experience. Now, there is a great case for functional lisps, like Clojure, since they support all the fancy patterns in cutting-edge Haskell etc. but without an extremely complex compiler. Of course you sacrifice some type-safety.
- bslatkin 4y agoAuthor here: I agree that another dynamic language with all of those attributes would be great.
- throwawaymaths 4y agoElixir. Lets you do stateful declarative stuff (talking to databases, communication, disk) with no fuss. No mutability, single line lambdas, fast function calls. Plus, rediculously easy concurrency, customizable failure domains, first class concurrent testing...
- eru 4y agoMight as well use Erlang?
- bongobingo1 4y agoNo? The syntax and standard library is vastly different in terms of ergonomics. You could argue whether its better but it's undeniably more familiar to non-erlangers. (Alternatively, you already are using Erlang anyway.)
- pjmlp 4y agoOr former Prolog users.
- bitwize 4y agoFunctional programming can be attempted in any language that offers higher-order functions that can close over free variables. In other words, languages isomorphic to untyped lambda calculus with additions. Python, Perl, JavaScript, and Go all qualify. However, it seems to be generally accepted that you're really only doing baby FP unless you also program with types. Types, and the algebras that can be imposed over them, are where the really interesting FP work is done that yields new control structures (of which monads are just one), that are difficult and cumbersome to express in untyped LC, or in a dynamic programming language.
- bslatkin 4y agoAuthor here. Part of my point here is 1) you get a lot of benefit from dynamic languages, and 2) you also get a lot of benefit from functional programming. And 3) you'd get the most benefit from both together. When you add types into the mix then you're taking two steps forward and one step back in terms of simplicity and understandability. What you said is also perpetuating a common reason that people are turned off to FP in general these days: https://twitter.com/haxor/status/1584725493513072640 https://twitter.com/haxor/status/1584725493513072640 "At some point functional programming became equivalent in people's minds to complex type systems (e.g., monads), and that caused the value of Lisp to be lost to history. It's a shame."
- bitwize 4y agoI don't know what you're getting at. Static typing makes your code easier to understand and reason about. It's pretty much an unmitigated win. Dynamic languages may have made some sense back when people wanted to cruft something up and get it running without declaring the type of each object before it is used (which declarations can get verbose and cumbersome in the presence of parameterized types). But today we have powerful computers that can quickly check and infer types in ever more elaborate type systems, and the risks of dynamic languages seem less and less worth the development speed gains. You can see this in perennial language popularity contests. Consider how thoroughly the programming community embraced TypeScript over JavaScript because of the guarantees static type checking provides before your code even runs. And while Lisp used to be the smart kids' favored programming language, it was displaced by Haskell and later by Rust once those languages matured to where people were building real world stuff in them. (As it turns out, baking object lifetimes and ownership into your type system means you don't even need a GC to get Lisp levels of power and abstraction!)
- whateveracct 4y agoNix is my favorite dynamic functional language. So much so that I'll probably use it (via hnix) as a general scripting language in projects that benefit from that.
- mayoff 4y ago> dynamic languages don't require you to "emulate the compiler" in your head On the contrary. Your ‘functional’ timer function takes an argument and applies the - operator to it. You must be sure, every time you call the function, that you pass an argument to which - can be applied. If you don't, your user might see a TypeError. So you, the human author of the code, must ‘emulate the compiler’ by performing type inference and type checking in your head. In my statically-typed language, the compiler ensures that my program only ever passes arguments for which the - operator is applicable. I don't need to ‘emulate the compiler’, because I'm actually using a compiler.
- bslatkin 4y agoI wouldn't do the type checking in my head, I'd just assume it's going to work as intended. If that's not the case, then an error will happen. What's wrong with the user seeing an error? There's always a chance my program will have a bug and fail. So I have to handle that case no matter what and properly deal with unexpected errors, error reporting, etc.
- stank345 4y agoBugs are inevitable but we should strive to have as few as possible. Your program not working right is obviously bad UX (as we've all experienced using buggy software). The reason we have type checking is same reason we have other tooling in our editors to do static analysis like highlighting syntax errors and linting: catching bugs during development is better than finding them in production.
- lgas 4y agoSure, you always have to handle the case that an unexpected error occurs, but every unexpected error that occurs in production has a cost. What that cost is depends on what your software does and how your users are using it, but there's always a cost. You may decide in your particular context that there is no amount or severity of errors that is too costly for your users to encounter, but most successful software endeavours have some limits.
- sidlls 4y ago
- pieresqi 4y agoDynamic typing is great for small to med projects or for prototyping but in bigger codebases it becomes quite messy trying to keep in mind what is what and can lead to logical errors or use of "bad" data. Static typing let's compiler keep tabs on what variable can contain and you can define safe fully defined conversions between types and removes mental overhead of keeping types in mind.
- omginternets 4y agoThis entire post reminds me of that satirical O’Riley book called “upsetting your coworkers with Python”. https://pics.me.me/thumb_using-list-map-lambda-and-filter-on-a-single-line-37387961.png https://pics.me.me/thumb_using-list-map-lambda-and-filter-on...
- BulgarianIdiot 4y agoI'm working on a language/platform that combines pure core (no direct side-effects) with some procedural techniques (allow internal/local mutations) and no shared mutability (100% pass by value, copy-on-write is used to optimize quick deep snapshots of value trees). I'm convinced this is the best combination of OOP and FP. I tried to find other languages like this, but I couldn't. Ironically the closest thing is how an SQL database works. Does my description ring a bell to anyone?
- quickthrower2 4y agoSounds like Haskell but making things that are awful monad stacks of readers, writers, state (yuk) into sublime language features.
- qazxcvbnm 4y agoYes, I agree with your conception. Likewise, I am working on a language with no shared mutability, and something like a pure core and often find myself solving problems that feel like putting a database in the middle of the application. Any links to your language, perhaps?
- sampo 4y agoThis is what Firefox shows me: > Warning: Potential Security Risk Ahead > This site is blocked due to a phishing threat. https://phish.opendns.com/main?url=www.onebigfluke.com%2F2022%2F11%2Fthe-case-for-dynamic-functional.html https://phish.opendns.com/main?url=www.onebigfluke.com%2F202...
- bslatkin 4y agoThanks for reporting that — I have no idea how that could be happening. Site is hosted on Blogger so it's a pretty vanilla setup.