Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript to clojure

I am aware of ClojureScript - possibility to compile clojure code to javascript, but is it possible to do the reverse, take some subset of javascript code and translate it back to clojure?

like image 397
Jiri Avatar asked Jul 22 '11 09:07

Jiri


2 Answers

Yes, although it wouldn't really make sense.

Clojure -> JavaScript makes sense because:

  • JavaScript is the only suitable target language for a wide class of web applications
  • It allows effective use of the Google Closure compiler for whole program optimisation
  • Clojure is a great "source" language because of its macro features and great support for defining expressive DSLs

Clojure would be an odd choice for a target language - if you want to run on the JVM platform, it would be more natural to target Java bytecode directly.

JavaScript would also be an odd choice for a source language compiling to Clojure - if you want Clojure code, why would you not just write Clojure directly? In particular, using a (possible subset of) JavaScript would not give you easy access to all the features that make Clojure really compelling (lazy functional programming, concurrency support, macro metaprogramming , persistent data structures etc.)

like image 193
mikera Avatar answered Sep 22 '22 03:09

mikera


Yes, this is definitely possible and a very actionable idea. You could actually use the Rhino Javascript compiler to convert the Javascript to Java classes and could then rig something up to call the Java classes from Clojure. You don't get the source code, but you can leverage the libraries in Clojure code.

like image 33
Marc Avatar answered Sep 22 '22 03:09

Marc