ClojureScript Mainia
July 29, 2015
Planck can now dynamically execute a ClojureScript file, passing command line arguments into a -main
entry point.
Let's say we want to write a script to compute Pythagorean distance.
(ns pythag.core)
(defn square [x]
(* x x))
(defn dist [x y]
(Math/sqrt
(+ (square x)
(square y))))
(defn -main [a b]
(println
(dist (js/parseInt a)
(js/parseInt b))))
Note that, at the bottom we've supplied a main entry point which deals with the I/O and calls our dist
function.
We drop this script into a file src/pythag/core.cljs
, and then we can execute it thusly:
$ planck -c src -m pythag.core 3 4
5
How does this work? When you pass -m
to Planck, it essentially does steps that are analogous to what you would get if you issued these forms at the REPL:
(require 'pythag.core)
((var pythag.core/-main) "3" "4")
And this, as it should, prints 5.