Skip to content

Instantly share code, notes, and snippets.

View cgrand's full-sized avatar

Christophe Grand cgrand

View GitHub Profile
(ns mutabots)
(defn map [f]
(fn [p1]
(fn
([] (p1))
([x] (p1 (f x))))))
(defn filter [pred]
(fn [p1]
@cgrand
cgrand / demo.clj
Last active July 4, 2019 10:00
EZ todo
(ns enlivez.demo
(:require [enlivez.core :as ez]
[datascript.core :as d]))
(ez/deftemplate new-item []
:state {:db/id self
new-todo ""}
[:li
[: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 {

Carry

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)
@cgrand
cgrand / demo.clj
Last active April 26, 2019 06:47
For toolsmithes really caring about isolation
;; Clojure 1.6.0
=> *clojure-version*
{:major 1, :minor 6, :incremental 0, :qualifier nil}
=> (def env1 (create-clojure-env))
#'net.cgrand.quarantine/env1
=> (env1 *in* *out*)
clojure.core=> *clojure-version*
{:major 1, :minor 8, :incremental 0, :qualifier nil}
clojure.core=>
(ns net.cgrand.decay
"Exponentially decaying lists. See http://awelonblue.wordpress.com/2013/01/24/exponential-decay-of-history-improved/
for background and http://clj-me.cgrand.net/2013/02/12/decaying-lists-log-scale-for-lists/ for documentation")
;; PRNG, formulas straight from java.util.Random javadoc
(defn- seed ^long [^long n]
(bit-and (unchecked-multiply n 0x5DEECE66D)
(unchecked-dec (bit-shift-left 1 48))))
(defn- next-seed ^long [^long seed]
(require '[clojure.string :as s])
;; Maze GENERATION
(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
cgrand / tron.bot.clj
Created May 21, 2013 09:23
Fork it and paste your TRON bot
;

Monotonic logic is eventually consistent. Eventual Consistency (https://www.voltdb.com/blog/2016/07/21/6265/) 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

@cgrand
cgrand / ednrepl.clj
Last active March 28, 2018 19:00
What if a REPL out was a stream of EDN forms?
;; some sample interaction with a clojure socket repl whose output stream consists only of valid edn forms.
;; the edn stream is not meant for the end user but for the client UI.
;; it demoes several features:
;; • prompt, out, err, eval and exceptions are demultiplexed; unstructred ones (err & out) are just text but the others give data.
;; • elided content (because data too deep or wide) is replaced by a tagged literal (which optionally contains the expr to evaluate to get more items)
;; this last point makes possible navigation
;; lines are prefixed by > or < to tell what is sent to the repl (>) and what is received from it (<)