Skip to content

Instantly share code, notes, and snippets.

View cgrand's full-sized avatar

Christophe Grand cgrand

View GitHub Profile
; early days
defonce + :watch
; yesterday
:managed [*app-state (atom {}) :dispose nil]
:bind {:*app-state *app-state}
...
@cgrand
cgrand / merge.cljc
Last active March 8, 2025 11:49
Fast structural merge in ClojureDart
;; ClojureDart has structural merge for maps https://github.com/Tensegritics/ClojureDart/commit/ac6bd2a8dec29e31dd2f598a8009ebdffd036781#diff-7d37216e2a92d1019eb7fe5bf292430220b2fcec4cbc2cd3355c6a4226333680R4714
;; * (merge big-map small-map) and (merge small-map big-map) are equally fast
;; * (merge original-map modified-map) takes time proportional to the number of modified keys
;; * even in worst case, it's faster than `into`
;; BEST CASE
user=> (let [m (zipmap (range 1e6) (range 1e6))] (time (count (merge {:a 1} m))))
"Elapsed time: 287.794583 msecs"
1000001
@cgrand
cgrand / asym.clj
Created November 18, 2024 16:56
->> and -> asymmetries
;; cond-> inside -> is fine
(-> input
(dothis ...)
(cond-> test (maybe do this also ...))
(dothat ...))
;; cond->> inside ->> isn't fine
(->> input
(dothis ...)
(cond->> test (maybe do this also ...)) ; the input seq comes last because of ->> but cond->> expects it first.
@cgrand
cgrand / core.cljc
Last active June 3, 2024 11:10
adhoc parquet layout, random search, no optimization. Hot reload to update.
(ns parquet-layout.core)
(defn destock
([stock] stock)
([stock board]
(let [n (dec (stock board))]
(if (zero? n)
(dissoc stock board)
(assoc stock board n)))))
(ns workshop.main
(:require ["package:flutter/material.dart" :as m]
["package:google_fonts/google_fonts.dart" :as fonts]
#_["example.dart" :as ex]
[cljd.flutter.alpha2 :as f]))
(def normal-text-style
(fonts/GoogleFonts.sourceCodePro
.fontSize 26
.fontWeight m/FontWeight.w700
@cgrand
cgrand / t10s-expressive-datalog.cljc
Last active December 10, 2021 16:17
Putting expressions in datomic-flavored datalog :where clauses. (This is a snippet I had laying around and that I'd like to develop further.)
(ns tensegritics.expressive.datalog
"Putting expressions in datomic-flavored datalog :where clauses.")
;; I (cgrand) believes that datalog literally lacks expressivity because
;; it's not expression-based but clause-based and thus requires a lot of
;; gratuitous naming.
;; Here is an attempt to compiles expressions into clauses.
(defn- grounded? [[e a v]] (and (keyword? a) (not (symbol? v))))
@cgrand
cgrand / doto.clj
Last active November 3, 2021 22:31
A usage of `doto` that I overlooked for years
;; let's say you want to perform an action only when a state is true and returns true/false depending on the action success
;; typically I would write
(if @open ; check state
(do
(.put q bytes) ; action
true)
false)
;; or
@cgrand
cgrand / for.clj
Created July 14, 2021 14:58
ClojureDart's for macro
;; For ClojureDart in a bout of NIH syndrome we rewrote Clojure for
;; it seems to be faster because we don't use concat
(defmacro for
"List comprehension. Takes a vector of one or more
binding-form/collection-expr pairs, each followed by zero or more
modifiers, and yields a lazy sequence of evaluations of expr.
Collections are iterated in a nested fashion, rightmost fastest,
and nested coll-exprs can refer to bindings created in prior
binding-forms. Supported modifiers are: :let [binding-form expr ...],
:while test, :when test.
@cgrand
cgrand / main.dart
Created July 3, 2021 13:19
lazy init
log(String msg, x) {
print(msg);
return x;
}
var a = log("Init A", 1);
var b = log("Init B", 2);
var c = log("Init C", 3);
main() {
@cgrand
cgrand / super.md
Last active June 24, 2021 15:35
super in ClojureDart

Accessing super members in ClojureDart

In ClojureDart deftype (and reify) can extend abstract classes and, as a consequence, we need a way to call super implementations (even on fields because of getters/setters).

For example, this Dart

class WebViewExampleState extends State<WebViewExample> {
  @override
 void initState() {