13

Self-Hosted Clojurescript in action - Part 1

 3 years ago
source link: https://blog.klipse.tech/clojurescript/2016/04/04/self-host-part-1.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Self-Hosted Clojurescript in action - Part 1

Apr 4, 2016 • Yehonathan Sharvit

ClojureScript can compile itself

Like the whole clojurescript community, we were excited when David Nolen published on July 29, 2015:

ClojureScript can compile itself.

And actually, it’s quite simple: the cljs.js namespace provides two functions for evaluation and compilation of clojurescript expressions:

  1. eval-str for evaluation of clojurescript expressions
  2. compile-str for compilation of clojurescript expressions

In this article, we are going to let you experiment cljs.js inside KLIPSE.

Evaluation

Let’s start by evaluating (map inc [1 2 3]):

xxxxxxxxxx
(ns my.main
  (:require [cljs.js :as cljs]))
(cljs/eval-str (cljs/empty-state)
               "(ns my.user) (map inc [1 2 3])"
               ""
               {:eval cljs/js-eval}
               identity)
the evaluation will appear here (soon)...

Feel free to play with the second argument to eval-str and see the result of the evaluation.

Here is eval-str documentation and if you are really curious you can also read empty-state documentation.

Compilation

You can also play with compile-str:

xxxxxxxxxx
(cljs/compile-str (cljs/empty-state)
                  "(ns my.user) (map inc [1 2 3])"
                  ""
                  {:eval cljs/js-eval}
                  identity)
xxxxxxxxxx
the evaluation will appear here (soon)...

Vertigo

The funny thing about this article is that KLIPSE itself evaluates the code through cljs.js. Every time I am trying to think about it, I feel dizzy !?!?!

A piece of code that evaluates a piece of code that evaluates a piece of code!

Please share your thoughts (and your feelings) in the comments below.

In an upcoming article, we will demonstrate advanced features of cljs.js like: expression vs. statement, macros, advanced compilation and more…

Clojurescript rocks!

If you enjoy this kind of interactive articles would you consider a (small) donation💸 on Patreon or at least giving a star⭐ for the Klispe repo on Github?

to stay up-to-date with the coolest interactive articles around the world.

Discover more cool interactive articles about javascript, clojure[script], python, ruby, scheme, c++ and even brainfuck!

Give Klipse a Github star to express how much you appreciate Code Interactivity.

Subscribe to the Klipse newsletter:

Feel free to email me [email protected] for getting practical tips and tricks in writing your first interactive blog post.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK