Transitioning an App from Java to Clojure (Notes of a Talk)

239 views
Skip to first unread message

Timothy Washington

unread,
Jul 27, 2012, 2:43:44 PM7/27/12
to clo...@googlegroups.com
Hi all, I recently gave a talk at a Toronto Dev Shop, The Jonah Group. It just detailed my experiences, transitioning a Java app to a Clojure app, and the benefits that accrued to me. I thought it would be nice to share with the Clojure community. Below is an approximate outline of my talk. Use or improve it as you see fit. 


Ah, Clojure & me


Overview

This talk is about Clojure, and the benefits of the functional paradigm as I saw it. And this is from someone coming from the Enterprise Java world. But there's a twist. There's already a lot of technical information available to you on the web. You are all very senior people, and I won't bore you all with the details of the Clojure language itself. Instead, I thought a personal account might be more useful.

I'll give you my guiding principles for my own software craft. Then try to give a gist of how I see computing in general. This is all to give you some kind of context. Context for look at how I transformed a Java application that I created, into a much more powerful and maintainable Clojure version.


Programmer's Progress

I'm principally a web-app developer, building business process software. But really I'm passionate about technology. They key phrase I like to throw around is that I really like "extracting greater expressive power from my software tools". About 11 years ago, I began my career as a Java / OO programmer. From there, I developed an appreciation for the power of Design Patterns. The next natural steps for me were to design my own language (DSL) and do it's parser implementation. Since then, I've really began to see the expressive powers that functional programming provides. And for about the past 2 years, I've considered myself a Clojure developer. In short, I have a guiding principle of maximizing expressive power. Ideally, I want to program as quickly as I think.


Concept of Computation

What does it mean to compute? Turns out this is a complicated question. From what I can tell, to compute is an actualization (or concrete version) of a mathematical function. We are swimming in a universe of functions in math, statistics and the natural world. The lights in here are a function of certain inputs: ie, the amount of electricity and whether the switch is engaged.

I'll briefly mention what Alan Turing had to say. Then I'll briefly describe how LISPs interpreted that model of computation. What I want to setup, is an OO languages approach to computing a thing, versus a functional (or LISP) approach to computing a thing. Ultimately, this should give you some perspective on my software refactoring from OO (Java) to a Lisp (Clojure).
  • Alan Turing: Ideas of computation can be found in i) his contributions to the concept of an algorithm and ii) his description of a turing machine.
  • Algorithm: An algorithm is just a step-by-step procedure for calculations. Those steps are expressed as a finite list of well-defined instructions for calculating a function.
  • Turing Machine: This is a theoretical computer, that consumes i) an infinite ribbon of tape and ii) can perform read and write operations on the tape and iii) alters it's own state. This is a gross simplification. But the concept is that a Turing machine with the correct minimal set of operations can calculate anything that is computable, no matter the complexity (anything for which there is a function).


Object-Oriented's approach to computing: OO is actually a programming paradigm, based on what Alan Kay called the "Actors Model". This definition is derived from early versions of Smalltalk, and rules 5 and 6 show Smalltalk's Lisp heritage. Kay remarked as such, noting that rules 4-6 would mutate as Smalltalk developed. 
  1. EverythingIsAnObject.
  2. Objects communicate by sending and receiving messages (in terms of objects).
  3. Objects have their own memory (in terms of objects).
  4. Every object is an instance of a class (which must be an object).
  5. The class holds the shared behaviour for its instances (in the form of objects in a program list)
  6. To eval a program list, control is passed to the first object and the remainder is treated as its message.

LISP's approach to computing: Created by John McCarthy, Lisp was originally created as a kind of practical mathematical notation. Lists were the major data structures and functions were just another value passed into a list.
  1. Probably one of the defining qualities of Lisp, is that it can be written in itself (code is data). This is what makes on-the-fly change of your running code (ie macros) possible. Basically you can think of your source file as an AST that gets loaded into the running system. And you can change that ast before it's loaded or while it's loaded.
  2. Function is a first class value in the system
  3. Recursion


Bookkeeping System in Java (OO)

I have a bookkeeping application that I originally wrote in Java. It used a custom XML/Java binding framework that I built ( like XMLBeans, but better ;) within that tool, I built an XPath interpreter to find nodes. To interact with the system, I built a shell that ran a custom language I designed. All this, with an XML database in the back-end. In retrospect, it had pretty advanced mechanisms. But the result of all that was that the system was very highly stacked, and was becoming difficult to maintain. 


Bringing in Clojure (LISP)

In 2010, I rewrote most of that bookkeeping app in about 1/10th the amount of Clojure code. First, I replaced the code that implemented the parser generated code that handled the custom languages. After a while, I realized that I didn't need XML (now spitting out to MongoDB) or that custom language (using regular functions and macros for domain specific stuff). In other words, the expressive power that I gained, and the concepts inherent in the Clojure language itself (sequences, immutable data, etc), have really altered the way I look at data
 processing and computing in general. I'd had some taste of functional approaches with javascript and ruby, but this was an order of magnitude better.

s-expressions (code is data) meant I didn't need XML as an extra data transfer language between my systems
  • [x] XML / Java binding framework
  • [x] XPath module

functions and macros removed need for a separate DSL and library
  • [x] Language Design
  • [x] Custom DSL implementation
  • [x] Custom Shell to run language implementation




Tim Washington 

nicola...@gmail.com

unread,
Jul 27, 2012, 3:14:13 PM7/27/12
to clo...@googlegroups.com
Hi,

Great notes. I like a lot.

A few (mostly technical) comments:


> Concept of Computation
>
> What does it mean to compute? Turns out this is a complicated question. From
> what I can tell, to compute is an actualization (or concrete version) of a
> mathematical function. We are swimming in a universe of functions in math,
> statistics and the natural world. The lights in here are a function of
> certain inputs: ie, the amount of electricity and whether the switch is
> engaged.
>
In all fairness, this is already an opiniated view of computations.
(It is the view of computations for a functional programmer.)
On top of that, the usual maths functions, do not treat well of partiality.
(non-termination, exceptions...)


> Alan Turing: Ideas of computation can be found in i) his contributions to
> the concept of an algorithm and ii) his description of a turing machine.
> Algorithm: An algorithm is just a step-by-step procedure for calculations.
> Those steps are expressed as a finite list of well-defined instructions for
> calculating a function.
> Turing Machine: This is a theoretical computer, that consumes i) an infinite
> ribbon of tape and ii) can perform read and write operations on the tape and
> iii) alters it's own state. This is a gross simplification. But the concept
> is that a Turing machine with the correct minimal set of operations can
> calculate anything that is computable, no matter the complexity (anything
> for which there is a function).

Anything that is computable is different that anything for which there
is a function, at least in classic mathematics.

2 simple arguments:
- some functions are not decidable. (halting problem, for example.)
However, in classic mathematics
such a function exists. You can even prove its value on some inputs.
- There is an uncountable number of functions from natural numbers to
natural numbers, and a countable
number of programs taking a natural number to a natural number. (A
program is a list of character.) So there
is more functions than computable functions (and, uncountably,
infinitely so.).

There is a branch of mathematics, constructive mathematics, that
exclude things that you can not construct
with a program.


> LISP's approach to computing: Created by John McCarthy, Lisp was originally
> created as a kind of practical mathematical notation. Lists were the major
> data structures and functions were just another value passed into a list.
>
> Probably one of the defining qualities of Lisp, is that it can be written in
> itself (code is data). This is what makes on-the-fly change of your running
> code (ie macros) possible.
macros are not on the fly change of your running code.

Best,

Nicolas.

Timothy Washington

unread,
Jul 27, 2012, 5:29:15 PM7/27/12
to clo...@googlegroups.com
Hey Nicolas, 

Thanks for the feedback and corrections. I was trying to hone in on OO and Lisp's model of what it means to compute. While this just served as a backdrop for the conceptual and code differences between Bkeeping's Java and Clojure versions, I can see a much deeper analysis happening here ( ooh, this is fun :). 

There is indeed an infinite number of functions, or relationships between natural numbers. I don't think that means that that any one of those relationships is not computable because it is within the range of infinite functions. The countable parts of a program can still accept an infinite amount of input, like Turing's machine. So the best I can say is that all functions (relationships between natural numbers) can be computable, but humans may or may not have figured out a way to represent all natural numbers. 

But really, I meant for the term function, to give a philosophical prism for human understanding and reasoning about the relationship between things in the natural world. So any of the relationships between any of the infinite natural numbers, should be computable. Then, what mechanisms do we get for crunching those numbers, in OO vs. LISP. Feel free to respond, correct, etc. 


Cheers 

nicola...@gmail.com

unread,
Jul 28, 2012, 6:40:40 AM7/28/12
to clo...@googlegroups.com
> There is indeed an infinite number of functions, or relationships between
> natural numbers. I don't think that means that that any one of those
> relationships is not computable because it is within the range of infinite
> functions. The countable parts of a program can still accept an infinite
> amount of input, like Turing's machine. So the best I can say is that all
> functions (relationships between natural numbers) can be computable, but
> humans may or may not have figured out a way to represent all natural
> numbers.

Some of these functions/relations are known to be not computable. For
example a function taking
the "source code" of a program (in any Turing-complete language) and
returning 1 if the
program stops or 0 if it runs forever. (the halting problem
http://en.wikipedia.org/wiki/Halting_problem )

This functions is mathematically perfectly well defined, but it can be
proved it can not be computed.
(No Turing machine can compute this function. It is important to
understand it is not a complexity
problem. It does not mean such a program would be too slow to be
useful. It really means such a program
can not exist.)

Timothy Washington

unread,
Jul 28, 2012, 8:41:39 AM7/28/12
to clo...@googlegroups.com
Very interesting. Thanks for the insight. 


--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply all
Reply to author
Forward
0 new messages