Skip to content

Instantly share code, notes, and snippets.


Christophe Grand cgrand

View GitHub Profile

Quick notes on how the fallback pseudo-type is used in cljd (ClojureDart).

Like cljs, cljd is built on protocols. Like in clj, each protocol is backed by an interface for the fast path. Let's call this interface the direct interface.

Cljd protocols are also backed by a second interface used for extensions to 3rd-party classes. Let's call this interface the extension interface.

Protocols are reified as instances of an IProtocol interface:

// emitted code
View self-hosted

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
cgrand / maze.clj
Created Jan 24, 2011
A maze generator (Wilson's algorithm) which can work with any topography (hextiles, torus variants, teapot etc.)
View maze.clj
; output images (square maze)
; (hex maze)
;; generic Wilson's algorithm implementation
(defn maze
"Returns a random maze carved out of walls; walls is a set of
2-item sets #{a b} where a and b are locations.
The returned maze is a set of the remaining walls."
cgrand / heredoc.clj
Last active Mar 6, 2021
An ugly hacky heredoc for Clojure
View heredoc.clj
(defn heredoc []
(let [delim (.readLine *in*)]
(->> (repeatedly #(.readLine *in*))
(take-while #(not= delim %))
(interpose \newline)
(apply str))))
; The following lines are read (by the reader) as:
; "Look )(\"\\T\na here doc!\n"
View demo.clj
;; this file is a walkthrough of Moustache features, a web framework for Clojure
;; Moustache allows to declare routes, apply middlewares and dispatch on http methods.
;; Moustache is compatible with all frameworks built on Ring, including Compojure
(ns demo
(:use net.cgrand.moustache)
(:use [ring.adapter.jetty :only [run-jetty]])) ;; hmmm Ring without servlets
cgrand / tarjan.clj
Last active Nov 18, 2020
Implementing Tarjan in Clojure, step by step
View tarjan.clj
;; Now, replace the loop by more telling operations.
(defn tarjan
"Returns the strongly connected components of a graph specified by its nodes
and a successor function succs from node to nodes.
The used algorithm is Tarjan's one."
[nodes succs]
(letfn [(sc [env node]
; env is a map from nodes to stack length or nil, nil means the node is known to belong to another SCC
; there are two special keys: ::stack for the current stack and ::sccs for the current set of SCCs
cgrand / gist:564d6e8ad57299f64beb438e4a8b709f
Created Jul 11, 2020 — forked from KingCode/gist:773560f4ab5bf91e660a2a26e581b036
cond-let and cond-let| macros, to leverage bindings between test and result expressions, as well as earlier ones (for cond-let)
View gist:564d6e8ad57299f64beb438e4a8b709f
;; (cond-let
;; (odd? x) [x n] (inc x)
;; (< n 10) [y (inc n)] 10
;; :else n))
;; we want the above to yield
;; (let [x n]
;; (if (odd? x)
;; (inc x)
cgrand / core.cljc
Last active Oct 14, 2020
Mixing macros and code in cljc and supporting clj, cljs and self-hosted cljs, see
View core.cljc
;; SEE:
;; macros and code in a single cljc working across clj, cljs and self-hosted cljs
;; require clojurescript from master
(ns foo.core
#?(:cljs (:require-macros
[net.cgrand.meta-macros :refer [macros no-macros]]
[foo.core :refer [add]])
:clj (:require
cgrand / bot.clj
Created Jun 25, 2013
Fork me and paste your bot. Don't forget to clearly wtate which function to call.
View bot.clj
;; * paste your code
;; * add a comment at the top telling which function to call
cgrand / bots.clj
Created Jun 26, 2013
Fork & paste your bot code here!
View bots.clj