Over a million developers have joined DZone.

Reaction to “Clojure: Towards The Essence Of Programming” from a Scala perspective

DZone's Guide to

Reaction to “Clojure: Towards The Essence Of Programming” from a Scala perspective

· Java Zone
Free Resource

Just released, a free O’Reilly book on Reactive Microsystems: The Evolution of Microservices at Scale. Brought to you in partnership with Lightbend.

Just watched Howard Lewis Ship’s Clojure: Towards The Essence Of Programming, which is an excellent introduction to Clojure and how it addresses the problems in Java. As a Scala fan, I thought it would be a good idea to see how one could use Scala to do the same.

Ceremony vs Essence

Howard uses the example below to show how much ceremony (code with little real info) is required, hiding the essence (the real intention) when using Java to sort a list of Stock objects (a portfolio) either using different properties such as open values or last trade values:

 public static void sortByOpen(List<Stock> portfolio) {
//ceremony: a lot of code but very little real info
Comparator<Stock> c = new Comparator<Stock>() {
public int compare(Stock o1, Stock o2) {
return o1.getOpen() - o2.getOpen(); //essence: the beef is here
Collections.sort(portfolio, c);

The Clojure version is:

//a Stock object is just a map with each field as a key. ":open" is the key to get the open value.
(sort-by :open portfolio)

In Scala, it can be as simple as the Clojure version:

//full version: using an anonymous function instead of a comparator object
portfolio.sortBy((s: Stock) => s.open)

//simpified version: sortBy is expecting the argument to be a function
// (Stock) => SOME-RESULT-TYPE, so we don't need to declare the
//type for "s".
portfolio.sortBy((s) => s.open)

//final version: as "s" only appears once in the function body, just use
//an underscore and get rid of the parameter declaration.

The Clojure version is like the final Scala version in the first attempt because it doesn’t need typing information in the code. In Scala, the compiler infer it from the code.

In addition, in Clojure the object is just a map so people can refer to a property directly with its name without referring to the object. In Scala, we must use something to denote the object in the form of obj.property. However, the underscore syntax eliminates the need to declare that object, so the code is still very succinct.

The benefits of the Scala approach are:

  • Compile time checking: if the property name mismatches the Stock class, in the Scala the compiler will tell us immediately.
  • Runtime performance: it is a lot faster to access the property by offset than by looking up a key.

The cost we pay is mainly the learning curve: we must learn how Scala infers the typing information so that we know what to type and what to NOT type and learn the meaning of the underscore in an anonymous function.

Form is structure

I may not be fully understanding this example. Howard uses the Fahrenheit conversion, f =9*c/5+32, to show that in Clojure the form (the look on the surface) is the same as the structure (the actual relationship):

//the look is the same as the actual syntax tree
(* 9 c)

I definitely think the Java or Scala version is much simpler and more concise:


This is, after all, what we call DSL. Arithmetics has its rules of precedence and associativity, allowing us to write in a very succinct form to express more complex structure.

Read Eval Print Loop

Scala has it too. I like it very much too.

Functional programming

Howard that moves onto functional programming with examples using filter, reduce, map, for comprehension, stream. Obviously, Scala can do all these in a similar fashion. For example, to get the last trade total value of the portfolio, we can map each stock to its last trade value and then use reduce to sum them all up. In Clojure:

//#() means anonymous function. % refers to the one and only parameter (the stock).
(reduce + (map #(* (% :last-trade) (% :shares)) portfolio))

In Scala:

//+ in Scala is not a function, but a method. So we can't use it directly;
//instead, use an anonymous function with underscores again.

There is not much difference. However, I think the significance is much deeper. The question is not whether functional programming is supported or not, but how much it is intended to be used in place of procedural programming. Scala allows both styles of programming, which may or may not be a good thing, depending on how you view it. If you’re committed to the functional programming style for concurrency, testability, predictability, then using a language that is less capable of procedural programming is actually a good thing. If you’re more comfortable with procedural programming and just using functional programming in a smaller scale, then a hybrid language would be better.

Extending the language with DSL

Then Howard moves onto extending Clojure with DSL. One example he uses is building a DOM tree with a DSL.

(defview root-index
:html [
:head [
:title [ "Cascade Blog" ]
:body [
:h1 [ "Cascade Blog" ]
:ul { :class "recent-postings" } [
(template-for [posting (recent-postings env)]
:li [
(render-link env show-posting (posting :id) (posting :title))

As an exercise I implemented a similar DOM DSL in Scala:

//the DSL is implemented in the Html object
import Html._

//using the DSL to define a DOM tree
p @@ ("style" -> "color: red") containing (
"This is an ",
b containing ("interesting"),
"DOM tree"

They aren’t that different except that in Clojure each list item is separate from its neighbors by being enclosed in its own () but in Scala we must use a comma to separate the list items. So, choose your own poison: either tolerate a million ()’s or the ugly commas in the DSL.

Another difference is that in Clojure text can be freely mixed in the DOM tree as there is no typing restriction, while in Scala text is magically converted into a TextNode object. Again, the latter provides structure, compile-time protection and runtime performance, but we must learn the implicit conversion mechanism to understand what is going on.

Just to be complete, the DOM DSL is implemented in Scala as below:

//a DOM node
class Node

//a text node
case class TextNode(val text: String) extends Node

//an element node
case class Element(val name: String,
val attrs: Map[String, String],
val children: Seq[Node]) extends Node {
def this(name: String) = this (name, Map(), List())

//we can use symbols as method names, but @ is a reserved keyword in Scala,
//so I used @@ as the name of the method that adds attributes to the element.
def @@(attrPairs: (String, String)*) =
//just add each pair to the map
Element(name, attrPairs.foldLeft[Map[String, String]](Map())(_ + _), children)

//add the child nodes (elements or text nodes) to this element
def containing(children: Node*) = Element(name, attrs, children)

object Html {
//convert strings into text nodes automatically
implicit def string2TextNode(s: String) = TextNode(s)
def p = new Element("p")
def b = new Element("b")

Howard also shows the need for lazy evaluation so that some code is only executed on demand. For example, the DSL code to generate each is repeatedly called in a loop. In Clojure lazy evaluation is implemented with macros which generate well-form code instead of arbitrary text. This seems to be more powerful (able to do more) than the call-by-name mechanism Scala, although I don’t know if/when this extra power is really needed.


From http://agileskills2.org/blog/2011/08/28/reaction-to-clojure-towards-the-essence-of-programming-from-a-scala-perspective/

Strategies and techniques for building scalable and resilient microservices to refactor a monolithic application step-by-step, a free O'Reilly book. Brought to you in partnership with Lightbend.


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}