Skip to content

Instantly share code, notes, and snippets.

(defun insert-parentheses-backward ()
"Insert parentheses around the sexp near point.
Move parentheses backward by sexp if used repeatedly."
(interactive)
(cond ((string-match-p "\\\w" (char-to-string (char-after)))
(forward-char) (insert-parentheses-backward))
((equal (char-before) 41)
(backward-sexp) (insert-parentheses-backward))
((equal (char-after) 40)
(if (equal (char-before) 40)
#[proc_macro_attribute]
pub fn instrument_err(args: TokenStream, item: TokenStream) -> TokenStream {
let input: ItemFn = syn::parse_macro_input!(item as ItemFn);
let args = syn::parse_macro_input!(args as AttributeArgs);
// these are needed ahead of time, as ItemFn contains the function body _and_
// isn't representable inside a quote!/quote_spanned! macro
// (Syn's ToTokens isn't implemented for ItemFn)
let ItemFn {
attrs,
@nivekuil
nivekuil / crux
Created September 30, 2020 08:19
(put {:crux.db/id {[:user/id 1] :login-details} :user/id 1 :user/name "foo" :user/password "bar"})
(put {:crux.db/id {[:user/id 1] :favorites} :user/id 1 :user/favorite-food "pie" :user/favorite-animal "cow"})
(defn attr [ident attr]
(dissoc (crux/entity (db) {ident attr}) :crux.db/id))
(defn entity [ident]
(let [[table id] ident]
(dissoc
@nivekuil
nivekuil / scylla.clj
Last active March 16, 2021 23:37
crux scylla/cassandra doc store
(ns app.crux.scylla
(:require [crux.codec :as codec]
[crux.db]
[crux.document-store :as ds]
[crux.io :refer [with-nippy-thaw-all]]
[crux.system :as sys]
[crux.memory]
[taoensso.nippy :as nippy]
[taoensso.timbre :as log]
[qbits.alia :as alia]
@nivekuil
nivekuil / router.cljc
Last active April 8, 2021 12:00
Reitit routes to fulcro legacy routing tree
["/user" {:name ::user-main
:target 'app.ui.root/UserMain}
["/" {:target-ident [:user-main :screen]}] ;; put it here, because reitit will append collections
["/:uuid"
{:name ::user
:target 'app.ui.user/User
:target-ident [:user/id :param/uuid]
:target-router :router/user
:parameters {:path [:map [:uuid uuid?]]}}]]
@nivekuil
nivekuil / sort.clj
Created April 11, 2021 03:54
merge two sorted vecs clojure
(defn merge-two-vecs
"Takes a keyfn and two sorted vecs, peek-smallest, and returns one sorted vec.
e.g. (merge-two-vecs identity [5 3 1] [6 4 2])"
[keyfn xs ys]
(let [keyfn (comp keyfn peek)]
(loop [acc (transient [])
xs xs
ys ys]
(let [x (keyfn xs) y (keyfn ys)]
@nivekuil
nivekuil / pathom-remote.cljs
Created September 29, 2022 01:36
basic pathom3 remote
(ns app.client.pathom-remote
(:require [com.wsscode.pathom3.plugin :as p.plugin]
[com.fulcrologic.fulcro.algorithms.transit :as fulcro-transit]
[com.wsscode.pathom3.connect.runner :as pcr]
[com.wsscode.pathom3.interface.async.eql :as p.a.eql]
[com.wsscode.pathom3.connect.foreign :as pcf]
[com.wsscode.transito :as transito]
[promesa.core :as p]
[com.fulcrologic.fulcro.algorithms.tx-processing :as txn]
[taoensso.timbre :as log]
@nivekuil
nivekuil / randomizer.cljc
Created January 10, 2023 10:41
weighted randomizer
;; port of https://blog.bruce-hill.com/a-faster-weighted-random-choice
(defn weighted-randomizer [weights]
(let [N (count weights)
avg (/ (reduce + weights) N)
aliases (volatile! (vec (repeat N [1 nil])))
{:keys [smalls bigs]} (->> (map-indexed
(fn [idx weight]
(if (< weight avg)
[[idx (/ weight avg)] :smalls]
@nivekuil
nivekuil / x.clj
Created September 4, 2023 19:02
electric odoyle
(ns app.todo-list
(:require contrib.str
[hyperfiddle.electric :as e]
[hyperfiddle.electric-dom2 :as dom]
[hyperfiddle.electric-ui4 :as ui]
[odoyle.rules :as o]
[missionary.core :as y])
#?(:clj (:import [clojure.lang IFn IDeref])))
(defn rule->subject [*s whats then]
@nivekuil
nivekuil / animated.clj
Last active October 6, 2023 18:22
animated value in electric
(def timer*
(->> e/<clock ;; produce time
(y/sample e/-get-system-time-ms) ;; label time
(y/reductions (fn([] {:t (e/-get-system-time-ms) :dt 0})
([acc next] ;; produce difference
{:t (e/-get-system-time-ms)
:dt (- next (:t acc))})))))
(defn pid [<setpoint {:keys [<force error-threshold k <clock]
:or {<force (y/watch (atom 10))