Since , he has helped to develop the core Clojure language and many Clojure open source projects. Chas writes about Clojure, software development practices, entrepreneurship, and other passions at cemerick. Brian Carper is a professional programmer in the field of psychological research.
He uses Clojure for data analysis and web development. Christophe Grand is an independent consultant, based near Lyon, France. He tutors, trains and codes primarily in Clojure. A participant in developing the core Clojure language, he also authored the Enlive and Moustache libaries and is a contributor to Counterclockwise, the Clojure IDE for Eclipse.
Christophe writes on Clojure at clj-me. Clojure is a dynamic functional language for the JVM, recently released in version 1. Clojure offers a new set of programming techniques for robust code and rapid development.
In particular, it has new solutions for multicore computing. Whether you make the shift to Clojure or stick to Java, learning about this new language will challenge your assumptions about the best way to design software. Clojure is a new language for the JVM.
Join Kobo & start eReading today
Like Groovy, Jython, and JRuby , it offers dynamism, conciseness, and seamless interoperability with Java. Clojure is a dialect of Lisp, recently released in version 1. Developers often dismiss Lisp as impractical, perhaps because of its distinctive syntax, its ascetic simplicity, or the academic uses it's often applied to. Clojure is set to break that curse. Rich Hickey designed the language to make it easy and practical to take on the same sorts of problems you handle with Java, more robustly and with less code. Any new programming language, no matter how good, has to find its killer app to break through into widespread use.
Clojure's killer app is parallel programming for multicore CPUs, which are now the major route to increased processing power. With its immutable datatypes , lockless concurrency, and simple abstractions, Clojure makes multithreading simpler and more robust than in Java. I'll describe some of Clojure's distinctive features and show how applying lessons from the language can make your Java code more elegant and less buggy.
I hope that you come away wanting to learn more. Clojure code looks quite different from Java code, for a simple reason. In Clojure, code is data; it is built from exactly the same lists and vectors as any other data structure. The consistent homoiconic syntax makes it easier for both programmers and programs to understand and manipulate the code.
So, the function definition in Listing 1 is nothing but a list marked with parentheses , holding a vector marked with square brackets and another list. The list syntax in the first line defines the function. After the function name, a vector holds the parameters.
Clojure Basics for Java Developers
And in the last line, the list syntax invokes the function of multiplication, applied to three operands. Clojure's minimalist syntax becomes quite readable with even a little practice. Support for Clojure in all major development environments -- including NetBeans, IntelliJ, and Eclipse, as well as vi and Emacs -- makes it even easier to handle.
Figure 1 shows an example from VimClojure, where paired parentheses are matched by color. The function extracts lower-case characters from a string: get-lower "AbCd" is "bd". In fact, because of its lightweight syntax, a Clojure program is often simpler than the equivalent Java. The Java getLower function in Listing 2, for example, has twice the brackets and four times the code as the Clojure function.
In Java, like other languages, code is transformed into an abstract syntax tree as part of compilation. Java allows access to this structure's classes, fields, and methods using reflection, but the access is read-only, and there is no access to the method implementations. Clojure macros, in contrast, can freely manipulate the syntax tree, letting you implement functionality that ordinary code can't handle.
With macros, you can transform code elements while conditioning, wrapping, and deferring evaluation.
Clojure programming study_00
Here's a familiar and painful example. To process data from a reader or stream in Java, you must jump through some hoops, as shown in Listing 3. Most of the code in Listing 3 is boilerplate, wrapping around the part of the design that varies, namely the data processing. Java often, as here, fails to capture the constant features in reusable code. The essence of good software engineering is knowing what is likely to change and what is not, and Clojure makes it easy to create reusable new code structures with macros, such as the one in Listing 4 from the core library.
The macro receives a vector bindings , which holds a symbol, and a reader or stream. In the second line, the symbol is bound to the reader.
Code as data
The macro also receives the body , code that's wrapped in the try statement. In the last two lines, the macro is executed, calling println as its "data processing" logic. A macro rearranges the code after it is parsed but before it is executed; the actual execution of the body occurs only when the macro is invoked, as with any function. Together with Clojure's dynamic typing and unchecked exceptions, this macro makes the Clojure code not only more reusable, but also more readable.
Clojure macros have some resemblance to C macros: Both rearrange code in a preprocessing step, before execution. But whereas the C preprocessor works on the code as a string, Clojure reuses the expressiveness of the programming language itself to manipulate the code as a data structure. Java can similarly call Clojure code. Clojure can also inherit from Java classes, and vice versa. Java has built-in support for multithreading.
See a Problem?
But concurrency in Java is still difficult. Without locks in the right places, race conditions corrupt data; with locks in the wrong places, starvation and deadlock slow or stop threads.
- Concurrency with pure functional programming.
- Straight on Till Morning: The Life of Beryl Markham.
- Part 1 Getting started!
- Encephalopathy and Nitrogen Metabolism in Liver Failure?
- Best Books for Learning Clojure Programming | Hacker News Books!
- Books & Videos.
In practice, most developers write single-threaded applications or let application servers manage the threads. But when a single application must decompose a problem into parallel lines of execution and coordinate a solution, there's little choice but to write explicitly multithreaded code. Read about three concurrency anti-patterns sure to lead to deadlock in Obi Ezechukwu's High Performance Java blog.
Multicore chips make this more urgent. But with today's CPUs, true concurrency is needed to keep multiple cores running at their fullest. With Clojure's pure functional programming and with special multithreading constructs, threadsafe code becomes much easier. By default, Clojure functions are pure functions: they receive parameters and pass back a return value, with no changes to any accessible state. Different state requires a new object. For example, we can define a map marked with curly braces , then add a key to the map with assoc :.
Related Clojure Programming: Practical Lisp for the Java World
Copyright 2019 - All Right Reserved