Destructuring Clojure with Javascript

Aravind Baskaran
HackerNoon.com
7 min readOct 2, 2016

--

Clojure is a language I am learning as I work on awesome stuff at Swym Corporation. When I came on board last year, I had no clue what it meant unless it was the thing that causes problems in Javascript for loops. Turns out it is pretty close to Javascript.

Source — http://java.ociweb.com/mark/clojure/article.html

Coming to Clojure. It is a beautiful language. Weird at first, but it grows on you. Functional languages somehow tend to be. In this post, I am going to talk about a feature shared by many languages, but with specific details in Clojure and Javascript with their joys and pains. Destructuring. Why Clojure? — that’s a post for another time.

Destructuring makes assignments easier, which could instead take multiple lines of code.

Simple destructuring,

To put it simply, load variables from vectors and arrays quickly without repeating a word more or less.

One stark between Clojure and JS, is the immutatablity of data-structures. Like variable assignment is available, but not necessarily good to use. This plays heavily when understanding Clojure, I thought of it as a handicap initially. But as time went by, the intuitiveness showed itself.

Going a step deeper into vectors, especially useful when you don’t want to list out arguments and destructure as they come

Clojure matches with it

Picking default values is simple, without all those flimsy if checks from yore.

Same in Clojure

Objects and nested structures

This is where the real fun lies. Very powerful for functions which take configurations and optional parameters by simplifying the assignments.

Compared to Javascript, Clojure has some guns, like :keys and :strs binding forms to bunch the destructuring together. Warning — Entering a little more complicated territory

The usage of :keys binding form and regular destructure is a good way to use rather than only one. Renaming is supported with :as binding form at a higher level

Now time for some overkill. Lets see which combination of the above breaks the REPL.

JS Overkill — Very hard to find this case — renaming with internal destructuring

Clojure Overkill — Was far easier to find. Trying to destructure a vector inside an object while destructuring the object.

Destructuring makes for a more intuitive code and easier to write functions, read functional programming. Comparing the two, it makes Javascript’s functional origins clearer. A new point to wonder —Does the Javascript prototype object fall under a functional programming paradigm?

Unfortunately for Javascript users, destructuring is not fully safe to use on all browser based JITs, but if you know your deployment, go for it.

Do let me know your thoughts and how you would use them, and please improve anything I may have written wrong.

References

Javascript
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator

Clojure
Very useful to read this one first — http://blog.brunobonacci.com/2014/11/16/clojure-complete-guide-to-destructuring/
http://clojure.org/guides/destructuring
https://gist.github.com/john2x/e1dca953548bfdfb9844

P.S: PHP sucks, Javascript is beautiful, Java is consistent, CSS is beautiful again, Objective C is weird, Python is sensitive(read whitespace), C#/.Net is a mutant and so on. All personal opinions.

P.P.S : The gists I used for reference

Hacker Noon is how hackers start their afternoons. We’re a part of the @AMI family. We are now accepting submissions and happy to discuss advertising & sponsorship opportunities.

If you enjoyed this story, we recommend reading our latest tech stories and trending tech stories. Until next time, don’t take the realities of the world for granted!

--

--

Aravind Baskaran
HackerNoon.com

Engineering @ Swym and other stuff. Opinions are only mine, when not useful.