• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Clojure versus JavaScript

 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 7
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Rancher
Posts: 379
22
Mac OS X Monad Clojure Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JavaScript was originally thrown together in about ten days, according to its creator, and was intended to be a Lisp but the mandate was a syntax familiar to Java developers. So it's more fair to say JavaScript and Clojure both share a subset of the features of the Lisp family, with Clojure having many more of those features.

I would classify JavaScript only loosely as a functional language since it has mutable data and a prototype-based object system, and most JS is written in an imperative OO style. Clojure is fundamentally a functional language, with immutable data structure, no objects (although it supports polymorphism), and lazy sequences (whilst not a prerequisite for a functional language, I consider this extremely important).

As for solving different goals, I think they both play in the same space: ClojureScript lets you write Clojure for the browser, and JavaScript can be used for server-side code with Node.js. Both are becoming general purpose languages for both client and server.

JavaScript is a very loose language, with a lot of surprising behavior and a lot of dark corners - because it was created very quickly and has grown in a rather haphazard manner since. Clojure, by contrast, is pretty tightly defined and very carefully considered - and based on a consistent set of strong abstractions. Clojure is also fairly strongly typed at runtime, unlike JavaScript.

A final, fundamental difference is that Clojure, like other Lisps, is homoiconic, meaning "code is data". That allows for a very powerful macro system and the ability to easily build DSLs - Domain Specific Languages.
 
Sunderam Goplalan
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks John and Sean for the detailed explanation. I can see the differences between the two languages with more clarity
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic