Skip to content

Instantly share code, notes, and snippets.

View cgrand's full-sized avatar

Christophe Grand cgrand

View GitHub Profile
@cgrand
cgrand / main.clj
Last active December 9, 2022 21:47
(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

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
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 / 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 / set-game.clj
Last active November 15, 2021 16:42
the SET game in clojure.spec
;; the SET game in clojure.spec
;; inspired by https://github.com/jgrodziski/set-game
(require '[clojure.spec :as s])
(s/def ::shape #{:oval :diamond :squiggle})
(s/def ::color #{:red :purple :green})
(s/def ::value #{1 2 3})
(s/def ::shading #{:solid :striped :outline})
(s/def ::card (s/keys :req [::shape ::color ::value ::shading]))
@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 / restrict-map.clj
Created May 29, 2012 10:15
Restricting nested maps to keys of interest
;; I could have used a closed dispatch (aka cond) but you may find this version more enjoyable
;; the spec format is the one provided by BG
(defprotocol Selector
(-select [s m]))
(defn select [m selectors-coll]
(reduce conj {} (map #(-select % m) selectors-coll)))
(extend-protocol Selector
@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 / join.clj
Created October 14, 2017 16:00
Join transducer
(defn join
"Joins pairs returned by nested transducers based on their key (first item).
Each nested transducer must returns pairs made of a strictly increasing key and a value.
Emits pairs made of the join key and a vector of the joined values (nil when none).
Values in the vector appears in the same order as the xform that produced them."
([] identity)
([xform] xform)
([xform1 xform2]
(fn [rf]
(let [vq1 (volatile! clojure.lang.PersistentQueue/EMPTY)
@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() {