Skip to content

Instantly share code, notes, and snippets.

View mhuebert's full-sized avatar

Matt Huebert mhuebert

View GitHub Profile
@mhuebert
mhuebert / x.clj
Last active November 9, 2016 14:29
Macros that require macros, in ClojureScript. Remember, in your .clj file: use `:require` with `:include-macros true`
(ns app.x
(:require [app.y :refer [y-macro] :include-macros true]))
(defmacro x-macro [& args]
(y-macro args))
;; how can we use macros from one .clj namespace in another .clj namespace?
;; take a close look at the :require statement above. Two important points:
;; - it must say :require, not :require-macros
@mhuebert
mhuebert / as-vec.clj
Last active April 28, 2017 14:26
Return vectors from a Clojure spec regular expression generator
(ns spec-test.as-vec
(:require [clojure.spec :as s :include-macros true]
[cljs.spec.impl.gen :as gen]
[clojure.test.check.generators]))
(defn as-vec
"Wrap the generator of expr to return a vector"
[expr]
(s/with-gen expr #(gen/bind (s/gen expr)
(comp gen/return vec))))
@mhuebert
mhuebert / pyrgiometry.clj
Created July 10, 2017 21:57 — forked from daveliepmann/pyrgiometry.clj
"Πυργεωμετρία ('Pyrgi-ometry')", a Processing sketch using Clojure and Quil. Inspired by artwork in the village of Pyrgi on the Greek island Chios: https://daveliepmann.exposure.so/pyrgi
;; "Πυργεωμετρία ('Pyrgi-ometry')", a Processing sketch using Clojure and Quil
(ns livecode.pyrgi
(:use quil.core))
(def stripe-height 50)
(def canvas-height (* 9 stripe-height))
(def canvas-width (* canvas-height (/ 3 2)))
;; Color conversion, courtesy of Jack Rusher
(defn hex-to-color [hex]
@mhuebert
mhuebert / localstorage.cljs
Last active July 11, 2017 14:24 — forked from daveliepmann/localstorage.cljs
HTML5 localStorage utility functions for ClojureScript. I find it makes for cleaner code when I wrap the native JS.
(ns localstorage)
#_[]
[1
2
3
4]
(defn lorenz-points [p sigma beta dt]
(loop [n 6000
x 0.01
y 0
z 0
out []]
(if (= 0 n)
out
(recur (dec n)
(+ x (* p (- y x) dt))
(require '[maria.eval :as e]
'[goog.object :as gobj]
'[clojure.string :as string])
(defn resolve-to-val [sym]
(let [{the-name :name} (e/resolve-var sym)]
(->> (string/split (str the-name) #"[\./]")
(map munge)
(to-array)
(apply gobj/getValueByKeys js/window))))
S1
tap -> S3
S1default
click -> S1a
tap -> S1b
S1a
go -> S2
back -> S1
S1b
go -> S3
@mhuebert
mhuebert / React Contexts with Reagent.md
Last active October 30, 2020 14:15
React contexts with Reagent

The following provide and consume Reagent components expose React contexts without fuss.

Example

(:require [example.reagent-context :as c])

;; in a component, use `provide` to supply values for contexts:
[c/provide {:app-theme {:color "blue"}}
 ;; consume one context at a time
@mhuebert
mhuebert / Readme.md
Last active March 1, 2024 16:55
material-ui's CSS-in-JS with Reagent. (see https://material-ui.com/customization/css-in-js/)

material-ui allows for customizing CSS via a higher-order component called withStyles. Like many higher-order components which expect render props, the purpose of withStyles is to accept some parameters, and then introduce a new variable into the scope of the component tree.

One succinct and simple way to translate the concept of scope into Clojure is via a custom let macro. Usage of such a macro is demonstrated here:

(:require [material-ui.styles :as m])

(m/let [{:keys [leftPad]} {:leftPad 
                           {:paddingLeft 8}}]
 ;; `leftPad` is now the _className_ associated with {:paddingLeft 8} 
@mhuebert
mhuebert / deps.edn
Last active February 1, 2019 14:43
js-interop
{:deps {chia {:git/url "https://github.com/mhuebert/chia"
:sha "885e84e84b39836a9cb12bc3b90880dc25a11482"}}}