// assuming b is a local
// rewrite
function(a) { return a + b; }
// to
Object.defineProperty(function(a) { return a + b; }, "$scope", {value: function() { return {"b": b}; }})
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; with the setting that values are indented by two if they are at the start of a line | |
=> (doseq [w (range 20)] | |
(prn 'width w 'non-strict) | |
(render (best-layout (spans '{1 2 3 (a b) 5 6}) w)) | |
(newline) | |
(prn 'width w 'strict) | |
(render (best-layout (spans '{1 2 3 (a b) 5 6}) w true)) | |
(newline)) | |
width 0 non-strict | |
{1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
=> (doseq [w (range 32)] | |
(prn 'width w) | |
(render (best-layout (spans '(1 2 3 4 (a b c) 5 6)) w)) | |
(newline)) | |
width 0 | |
width 1 | |
width 2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn join | |
"Joins pairs returned by nested transducers based on their key (first item). | |
Each nested transducer must returns pairs made of a strictly increasing key and a value. | |
Emits pairs made of the join key and a vector of the joined values (nil when none). | |
Values in the vector appears in the same order as the xform that produced them." | |
([] identity) | |
([xform] xform) | |
([xform1 xform2] | |
(fn [rf] | |
(let [vq1 (volatile! clojure.lang.PersistentQueue/EMPTY) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; ported from https://en.wikipedia.org/wiki/De_Bruijn_sequence#Algorithm | |
(defn- de-bruijn "generate de-bruijn sequences of size n and with elements in 0..k (k excluded)." [k n] | |
(let [a (into [] (repeat (* k n) 0)) | |
db (fn db [a t p] | |
(if (> t n) | |
(into a (when (zero? (mod n p)) (subvec a 1 (inc p)))) | |
(let [a (assoc a t (nth a (- t p))) | |
a (db a (inc t) p)] | |
(reduce | |
(fn [a j] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns foami.core | |
"FOreign Asynchronous Mechanism Interop" | |
(:require [clojure.core.async :as async])) | |
(defn put! | |
"Takes a `ch`, a `msg`, a single arg function that when passed `true` enables backpressure | |
and when passed `false` disables it, and a no-arg function which, when invoked, closes the | |
upstream source." | |
[ch msg backpressure! close!] | |
(let [status (atom :sending] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defmacro doc | |
"Returns a spec which acts in all points like the provided spec, | |
expect for describe/form where the docstring appears." | |
[docstring spec] | |
`(doc-impl ~docstring '~spec (delay (spec/spec ~spec)))) | |
(defn doc-impl [docstring form delayed-spec] | |
(reify spec/Spec | |
(conform* [_ x] (spec/conform* @delayed-spec x)) | |
(unform* [_ y] (spec/unform* @delayed-spec y)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(a/go-loop [async-tcp-connections-to-chans ; "foreign" -> ch | |
buffered ; ch -> data | |
pressurized] ; ch -> foreign | |
(let [cnx (select (keys async-tcp-connections-to-chans)) | |
ch (async-tcp-connections-to-chans cnx) | |
data (read-data cnx) | |
async-tcp-connections-to-chans (dissoc async-tcp-connections-to-chans cnx) | |
buffered (assoc buffered ch data) | |
pressurized (assoc pressurized ch cnx)] | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; it's very rough (no line info displayed yet, no return value, no filter etc.) | |
;; but I believe this is the most comprehensive tracing for Clojure | |
user=> (trace #(reduce + (range 5))) | |
> user$eval144$fn__145 / invoke | |
> clojure.lang.Var / getRawRoot | |
< clojure.lang.Var / getRawRoot | |
> clojure.lang.Var / getRawRoot | |
< clojure.lang.Var / getRawRoot | |
> clojure.lang.Var / getRawRoot |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# launch a clojure plain repl but with options and classpath matching project.clj | |
# Except when project.clj changes (and on first launch), lein is not called. | |
CODE=' | |
(let [p (leiningen.core.project/read) | |
args (@(var leiningen.core.eval/get-jvm-args) p) | |
cp (with-out-str (leiningen.classpath/classpath p))] | |
(print "ARGS=\"") | |
(apply print args) |