Skip to content

Instantly share code, notes, and snippets.

@fmnoise
Forked from mfikes/cljs-graaljs.md
Created July 19, 2018 08:53
Show Gist options
  • Save fmnoise/526327c4f30b89acb0fb12bcebc17087 to your computer and use it in GitHub Desktop.
Save fmnoise/526327c4f30b89acb0fb12bcebc17087 to your computer and use it in GitHub Desktop.
Try ClojureScript Graal.js

First, you need to install GraalVM and set things up so that the java installed with GraalVM is picked up in your path. See http://www.graalvm.org for more details.

With that in place, you can manage the Truffle languages installed.

Here you can see that I've already installed Python and Ruby:

$ gu list
ComponentId              Version             Component name
----------------------------------------------------------------
python                   1.0.0-rc3           Graal.Python
ruby                     1.0.0-rc3           TruffleRuby

Let's add R to the mix

$ gu install R
Downloading: Component catalog
Processing component archive: Component R
Downloading: Component R
Installing new component: FastR (org.graalvm.R, version 1.0.0-rc3)
...

Let's set up to use the latest SHA in my fork of ClojureScript that adds Graal.js support by creating a deps.edn file with

{:deps {org.clojure/clojurescript {:git/url "https://github.com/mfikes/clojurescript"
                                   :sha "51e0e3f355ad922607ba7013b402979338794634"}}}

Now let's start up a ClojureScript REPL, specifying graaljs as our REPL environment (instead, of say node, browser, or nashorn), and calculate the sum from 1 to 100 using ClojureScript, R, Ruby, Python, and JavaScript:

$ clj --main cljs.main --repl-env graaljs --repl
Checking out: https://github.com/mfikes/clojurescript at 51e0e3f355ad922607ba7013b402979338794634
cljs.user=> (reduce + (range 1 101))
5050
cljs.user=> (.eval js/Polyglot "R" "sum(1:100)")
5050
cljs.user=> (.eval js/Polyglot "ruby" "(1..100).reduce(:+)")
5050
cljs.user=> (.eval js/Polyglot "python" "sum(x for x in range(1, 101))")
Error: Operation is not allowed for: /private/tmp/graaljs
cljs.user=> (.eval js/Polyglot "python" "sum(x for x in range(1, 101))")
5050
cljs.user=> (.eval js/Polyglot "js" "(100 * (100 + 1)) / 2")
5050

For some reason, Python fails on first use, but works subsequent times. ¯\(ツ)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment