Skip to content

Instantly share code, notes, and snippets.

View cgrand's full-sized avatar

Christophe Grand cgrand

View GitHub Profile
; 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]
@cgrand
cgrand / foami.clj
Created September 22, 2017 16:02 — forked from ztellman/foami.clj
(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]
@cgrand
cgrand / doc.clj
Created September 14, 2017 15:05
Documenting specs inline
(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))
@cgrand
cgrand / scope.md
Last active September 6, 2017 08:17
Introspectable js scope
// 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}; }})
@cgrand
cgrand / bp.clj
Created September 1, 2017 12:32
pseudo code for back pressure bridging
(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)]
;; 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
@cgrand
cgrand / kein.sh
Last active May 31, 2017 14:11
Launch a plain clojure repl according to project.clj without leiningen (most of the time)
#!/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)

While writing a new reader I used clojure.lang.LispReader code as the reference and I discovered there are two type of spaces:

  • pure whitespace (anything that matches #"[ \t\n\r,]+"),
  • unvalued stuff (any sequence of: whitespace, comments, discard (#_), elided conditionals).

This leads to some quirks. For example in namespaced map syntax is:

namespaced-map:
 "#:" (?! whitespace) unvalued-stuff symbol whitespace map

That's the kind of code that I'd like to make work across lumo & planck (and ideally any bootstrapped)

Is this unreasonable? Do you see any objection or technical issue with that?

  (def repl
    (letfn [(rep []
              (println (str (ns-name *ns*)) "=>")
              (read (fn [form ex]
 (if ex
(ns unrepl.evented-reader
(:require [goog.string :as gstring]))
(defprotocol Stream
(read! [stream])
(unread! [stream])
(on-more [stream f]
"f is a function of two arguments: stream and an array where to push values.
f will be called as soon as input is available.
f returns true when no additional input is required."))