Sunderam Goplalan wrote:Hi John,
Coming from a Java Background, when I learnt Java Script, I liked many features that Java script offers that are not available in Java. For instance, the power comes from Java Script being a
dynamically typed language and functions are first class objects. i.e. they can be passed around seamlessly. I believe the same is true in Clojure. And JavaScript is a functional language too,
like Clojure.
Even though Clojure and Java Script may not be directly comparable as they solve different goals, would it be reasonably accurate to say Clojure SHARES most of the features of JavaScript?
If NOT, how is Clojure fundamentally different from JavaScript?
Thanks
GS
Hello Sunderam,
JavaScript is taking over the world it seems and its a language that is useful to know for all programmers. There are common concepts in place between the two languages, although I'd suggest that the design and thinking process is quite different. There is also ClojureScript to consider. ClojureScript allows you to write Clojure where otherwise you would write client-side JavaScript. So just as JavaScript is heading into the server side with nodejs, Clojure is venturing into the client side with ClojureScript.
Probably the most obvious difference is in the data structures core to each of the languages. Clojure has sequences (immutable lists, vectors, maps and sets) and I am not aware of constructs in the JavaScript language that provide similar immutable data structures.
One common design approach in Clojure is to create a very clear data structure (often a big map) and define functions that act upon that data structure. With good design you can infer much of the concepts about an application by first looking at the data structure. If all the functions access the data structure without changing its state, then there is greater certainty as to what a function will do. In mathematical terms, given the same data in to a function,
you should get the same result out. This is also true when your data structure is immutable.
For those times when you do need to change state, this can be easily managed in Clojure under the hood using software transactional memory (STM). STM is kind of like having an in-memory transactional database for your data, so that only one function can change state at any one time. Even when you do make a change, you are really creating a new data structure. This new data structure is efficient as it points back to original and only contains changes.
On a much smaller note, I personally find the Clojure syntax more elegant and concise compared to JavaScript, especially when writing non-blocking code.
Hope this gives you a few ideas, there is so much more detail to go into (you could write a book on it)
Thanks
John.