Clojure and ClojureScript

At today’s thoughtbot dev discussion in Boston, @benorenstein talked to us about Clojure and ClojureScript. Ben drew from the slides used in Stu Halloway’s Clojure in 10 Big Ideas. My notes follow.

Clojure:

  • Integrate tightly with host platforms
    • JVM
    • JavaScript
  • Dynamic typing. Optionally will type check?
  • commas are whitespace (and optional)
  • typically read from bottom up (must be defined before used)

Clojure In Ten Big Ideas

  1. edn (Eden)
  • Looks like JSON
  • has data structures
    • list ( )
    • vector
    • map { }
    • set #{ }
  • clojure programs are written in edn. “Written in Data”
    • programs that write programs.
    • programs that modify programs
  • built in tags
    • formats : dates, uuid
    • can define their own formats (call this function)
  1. Persitent Data Structures
  • immutable
  • Structural sharing
  • return new versions, reusing same parts
  • maintain performance
  1. Unified Succession model
  • common way of handling mutation
  • like git history
  • change is encapsulated by constructors. Build new things using new
  • atoms for atomic updates
  1. Sequences
  • almost everything is a sequence
  • first, rest, cons
  • take, drop
  • higher order functions: every? not-every? not-any?)
  • map, filter, reduce
  • sequences are lazy and often infinite - iterated, cycle, repeat
  1. Protocols
  • Interfaces
  • can extend interfaces, namespaced - like Refinements but they work.
  1. ClojureScript
  • Outputs JavaScript
  • JavaScript is tailored for minification via Google Closure (no relation)
    Compiler
  • Can get a ClojureScript repl in the browser
  1. Reducers
  • Composing sequences
  • Not certain of the differences between lazy sequeces and reducers
  • Reducers: Lazy, unordered, and parallel. That might be the difference?
  1. Datomic
  • Datalog is the query language
  • Lazily load the entire database.
  • Query the database, not the connection
  • Can query and join across databases.
  • When transactions return you get the new database, the old database and
    the diff.
  • can travel in time in the databse.
  1. Core.async
  • in ClojureScript gives you what looks like blocking threads in the
    browser.

My Thoughts

I found Clojure to be more readable than I had anticipated. That may be because I’ve been becoming more familiar with the functional programming paradigm because I recall looking at a Clojure program a couple of years ago and running away scared. The smaller programs we looked at were plagued by )))))))))))))), but Ruby just has a different version of that noise in the form of several consecutive end statements.

The programs themselves being just another bit of data that the program could operate on was interesting. I found Datomic to be of the most interest and wouldn’t mind playing around with that some.

If you’re comfortable with object oriented programming and looking to expand your horizons a bit, I think picking up a functional language such as Haskell or Clojure would be very beneficial - even if you never get paid to program in one of those. Clojure’s immediate usefulness via ClojureScript make it a really good candidate for this type of interest, I think.

I thought Ben did a good job of showing the neat constructs (such as the -> macro) to alleviate the wall-of-parenthesis problem. This helps ease my fear of lisp.

Thanks for the link of presentation on youtube. I never understand how to read Clujure’s code at all from before until I watched the presentation. I think of picking up another functional programming to learn as my hobby: Elixir, Go, or Clojure.

  1. Do you have any ideas on these different between them?
  2. Do they fall in the same category and solve the same problem?
  3. From your experiences, what kinds of apps will you use one of these languages over Ruby?

As someone who has championed lisps to other programmers for a few years now, concerns about all the parens almost always comes up early on, but it’s actually a total non-issue in practice:

  1. That mass of trailing parens that Derek mentioned very quickly fades into the background as you learn to ignore them (just like Ruby’s ends, as he pointed out).

  2. Precedence rules become extremely straightforward.

  3. Writing your program in lisp’s own data structures (lists, represented by elements inside parentheses) lets you do some pretty incredible things with macros.

Elixir is a little more Ruby-like, so if you’re looking for a quick pivot into functional programming from the Ruby world, that might be a comfortable choice. I haven’t played with it yet, so I can’t comment on it much beyond that.

Clojure is more mature and much more popular, so there will be more libraries and community support. Because it has Lisp heritage, you can take advantage of decades of writing about functional programming in Lisp while learning Clojure.

Go is not a functional programming language. It’s powerful and popular, but will not teach you functional programming concepts.

1 Like