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
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)
View demo.clj
(ns enlivez.demo
(:require [enlivez.core :as ez]
[datascript.core :as d]))
(ez/deftemplate new-item []
:state {:db/id self
new-todo ""}
[:input {:value new-todo
:on-change (doto [[:db/add self ::new-todo (-> % .-target .-value)]] prn)}]
package doubetrouble;
import java.util.concurrent.Callable;
public class Main implements Callable<Object> {
private static class Constants {
final static Object constant;
static {


Frequently I want to use reductions only to quickly realize that I'm not interested in the successive values of the state.

A simple example is to imagine one wants to increment a number represented by a sequence of its binary digits:

(inc '()) is (1)
(inc '(1)) is (0 1) ; yes the list is inversed, the lowest significant bit is the first item
(inc '(0 1)) is (1 1)
View maze-generation
(require '[clojure.string :as s])
(defn north-of [[row col]] [(dec row) col])
(defn south-of [[row col]] [(inc row) col])
(defn west-of [[row col]] [row (dec col)])
(defn east-of [[row col]] [row (inc col)])
(defn neighbours [rows cols cell]
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"

Monotonic logic is eventually consistent. Eventual Consistency ( has clear limits. It doesn't matter while you are pure. It matters when you go impure (FP). So boundaries must be clear. Boundaries implies coordination. Impure snapshots are expensive.

Monotonicity is ~ relativity. Impure is ~ absolute

View lambda-sim.clj
(defn lambda-model
"Crude model. rps is requests per second, len is a function that returns the
execution length in seconds, until is the number of seconds to run the simulation,
spawn is a function returning the number of ping sub requests, duty is the max age
for a container, keep-alive is the number of seconds before decommissioning a
Returns a map with: the number of user perceived cold starts and the number of
billable seconds."
[{:keys [rps len until spawn duty keep-alive]
:or {rps 10
View ssh-client.clj
(ns ssh-repl.client
(:require [ :as io]))
(defn connect
([user host repl-port {:as options
:keys [ssh-port repl-host key password]
:or {ssh-port 22
repl-host "localhost"}}]
(let [client (doto (org.apache.sshd.client.SshClient/setUpDefaultClient) .start)
session (-> client (.connect user host ssh-port) (doto .await) .getSession)]