Where could I find a good example of implementing a system in functional vs OO style

StackOverflow https://stackoverflow.com/questions/23252018

  •  08-07-2023
  •  | 
  •  

Question

I have started experimenting with Clojure, and I am impressed by what it can do. However, I don't think I have really 'got' functional programming design. My background is in Smalltalk, and that colours how I think about design. I would like to really understand how FP differs from OO, and one approach would be to have a good side-by-side comparison of a solution.

I don't think the problem needs to be that complex. Maybe it is would be enough to show how FP would implement the typical bank account problem?

Does anyone know of a suitable resource?

Was it helpful?

Solution

I think that most books on Clojure will have something like a simple bank account program, which I view as a little database in which states get updated. For example, Halloway and Bedra's Programming Clojure has a program that keeps track of music recordings. When you read through a description of that program, you'll immediately see how to map it into an OO solution. However, I don't think you're going to "get" FP from such a simple example. Programming Clojure also has functional snake game. You could compare that to an OO snake game. (I'm not promoting Programming Clojure; it's a worthwhile book but I think it has a number of flaws. It just happens to be the Clojure book that I know best. A lot of people seem to like it, though.)

I'd suggest another strategy, maybe as a supplement to yours, for grokking FP: Read something introductory that immerses you in an FP worldview. My favorite is Friedman and Felleisen's The Little Schemer. It uses Scheme, not Clojure, but the ideas translate. There is a Clojure website devoted to it, but I recommend working through the original book in the way that it recommends, covering parts of the page as you go. I think the book that gave me an understanding of parts of FP that go beyond The Little Schemer, extending to much of what you find in the Clojure world, was Bird's Introduction to Functional Programming using Haskell. I feel funny suggesting a Haskell book, though. Why not a Clojure book? Well, maybe one of the Clojure books will have the same effect of giving you a pure baptism in the FP Way of Thinking, but the Clojure books I've read (P.C, The Joy of Clojure), though very good books, are somehow less immersive--maybe because they're more practical. On the other hand, P.C (and probably some other Clojure books) is (are) largely targeted toward OO programmers, so that might actually be what you want.

OTHER TIPS

A few random observations on Clojure from a Smalltalker's perspective.

  • The Clojure data structures will remind you of Smalltalk's collection classes, but immutability/persistence takes a bit of getting used to.
  • You already use do and its relatives to abbreviate control structure. You'll find that the sequence library (map, reduce, and the rest), with laziness offer you another level of power and concision.
  • You're already used to writing your code in little pieces. The pieces will get smaller and there will be more of them.

But

  • Clojure is less pure than Smalltalk: not everything is an object.
  • Nothing like the wonderful Smalltalk browser is standard.

If you like Smalltalk, you'll love Clojure.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top