Skip to content

Instantly share code, notes, and snippets.

@nivekuil
nivekuil / fasthash.cljs
Created January 11, 2024 13:42
fast hash types
(deftype IdAttr [id attr ^:mutable ^number _hasheq]
IEquiv
(-equiv
[this that]
(boolean
(or (identical? this that)
(and (instance? IdAttr that)
(= id (.-id ^IdAttr that))
(= attr (.-attr ^IdAttr that))))))
@nivekuil
nivekuil / flowdiff.cljc
Last active January 9, 2024 06:01
missionary flow managing effects from diffs
;; Essentially we want to make a tee, passing through diffs for rendering but also taking advantage of missionary's supervision
;; for mount/unmount effects. Thus the diffs have to have a concept of corresponding to an identity. 3rd try is the charm?
(def sender (y/observe (fn [!] (def send! !) #(prn "sender dead"))))
(defn boot [flow]((y/reduce {}nil flow) prn prn))
(def !state (atom 0))
(def flow (y/ap
(let [diff (y/?= sender)
res (y/?> (y/observe (fn[!]
(dom/div
(let [!cs (atom [0 1 2 3 4]) cs (e/watch !cs)]
(e/for [c cs]
(ui/button
(e/fn [] (reset! !cs (vec (remove #{c} cs))))
(dom/props {:class "fade-in"})
(dom/text "REMOVE " c)))
(ui/button
(e/fn [] (swap! !cs conj (inc (peek cs))))
(dom/text "ADD"))))
@nivekuil
nivekuil / gist:f3698345ac51f6adf8039b475288461a
Created October 14, 2023 18:59
clojurescript relative time
#?(:cljs (def relative-time-formatter
(js/Intl.RelativeTimeFormat. "en" #js{:numeric "auto"})))
(defn timeago [time]
#?(:clj (str time)
:cljs (let [seconds-ago (t/seconds (t/between (t/now) time))
[divide unit] (condp #(> %1 (abs %2)) seconds-ago
60 [1 "second"]
3600 [60 "minute"]
86400 [3600 "hour"]
[86460 "day"])]
@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))
@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 / 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 / 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 / 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 / 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?]]}}]]