Skip to content

Instantly share code, notes, and snippets.

@mfikes
mfikes / README.md
Last active September 17, 2018 18:01
loaded-namespaces REPL tool

If you'd like to know the namespaces currently loaded by ClojureScript, it is easy to get some insight using a macro:

(defmacro loaded-namespaces []
  `(remove nil? [~@(map (fn [ns] `(when (cljs.core/exists? ~ns) '~ns))
                     (sort (keys (:cljs.analyzer/namespaces @cljs.env/*compiler*))))]))

This command uses this gist to set up this macro along with a REPL:

@gfredericks
gfredericks / frobnicate.clj
Last active March 1, 2019 11:35
Self-contained clojure file with deps
#!/usr/bin/env bash
#! top-of-file comments can be written using more #! lines, which
#! is a valid comment in both clojure and bash
":";# alternately this works too
#! The construction below uses cross-language syntactic hackery to
#! specify the -Sdeps arg in a part of the file that's interpreted
#! by clojure as clojure syntax (i.e., not a line comment), so it
@mfikes
mfikes / README.md
Last active December 12, 2019 12:08
ClojureScript parameter type inference

This is a demo of experimental work into ClojureScript parameter type inference.

The work is in a branch, and if you'd like to try out any of this yourself, you can start up a REPL based on the experimental branch using the following command, and play along at home:

clj -Srepro -Sdeps '{:deps {github-mfikes/gist-1e2341b48b882587500547f6ba19279d {:git/url "https://gist.github.com/mfikes/1e2341b48b882587500547f6ba19279d" :sha "55d6c6e9a1c4fc9b58fec74aef1af4aba57bad2a"}}}' -m cljs.main -co @compile-opts.edn -re node -r

To date, all ClojureScript inference "flows" in a certain direction, essentially from primitive values or type-hinted locals to their use sites. For example, consider this expression:

@mfikes
mfikes / example.cljs
Last active September 2, 2018 14:59
(defn include? [var]
(-> var meta :include))
(defn build-map [sym->var]
(->> sym->var
(keep (fn [[sym var]]
(when (include? var)
var])))
(into {})))
@uwo
uwo / .gitignore
Last active September 17, 2018 17:06
osxclip
.cpcache
.nrepl-port
@mfikes
mfikes / deps.edn
Last active May 30, 2018 09:31
Try running this in your terminal: clojure -Sdeps '{:deps {github-mfikes/e600e965da916699a703c9b1f1ff6d3b {:git/url "https://gist.github.com/mfikes/e600e965da916699a703c9b1f1ff6d3b" :sha "aa946e43d193fa719544057b0561b03ce2a72977"}}}' -m cljs.main -m hello-clojure
{:paths ["."]
:deps {org.clojure/clojurescript
{:git/url "https://github.com/clojure/clojurescript"
:sha "08beee84b05f911a6bb0eff0dd1235c201b26c70"}}}
@robert-stuttaford
robert-stuttaford / build.clj
Last active August 1, 2018 14:18
Basic nREPL / cljs compile / cljs repl / figwheel script for use with clj cli command: `clj build.clj TASK`
(require '[cljs.build.api :as api]
'[clojure.string :as string]
'[figwheel-sidecar.repl-api :as figwheel]
'[figwheel-sidecar.components.nrepl-server :as figwheel.nrepl])
(def source-dir "src")
(def compiler-config
{:main 'app.main
:output-to "target/app.js"

Code Splitting

NOTE: This gist uses the master branch of ClojureScript. Clone ClojureScript and from the checkout run ./script/bootstrap and ./script/uberjar. This will produce target/cljs.jar which you can use to follow this guide.

As client applications become larger it becomes desirable to load only the code actually required to run a particular logical screen. Previously ClojureScript :modules compiler option permitted such code splitting, but this feature only worked under :advanced compilation and users would still have to manage loading these splits. :modules also required manual

@noisesmith
noisesmith / download.cljs
Created May 25, 2017 21:47
create a download of some data in a clojurescript repl (eg. figwheel)
(defn download
; adapted from https://stackoverflow.com/a/3665147
[data filename encoder]
(let [el (.createElement js/document "a")
data-str (js/encodeURIComponent (encoder data))]
(doto el
(.setAttribute "href" (str "data:text/plain;charset=utf-8,"
data-str))
(.setAttribute "download" filename))
(aset (.-style el) "display" "none")