Skip to content

Instantly share code, notes, and snippets.

View borkdude's full-sized avatar

Michiel Borkent borkdude

View GitHub Profile
@borkdude
borkdude / bipe
Last active April 9, 2024 21:21
vipe for babashka - see https://github.com/juliangruber/vipe
#!/usr/bin/env bash
# temp file
t=/tmp/bipe.$$.txt
touch $t
# read from stdin
if [ ! -t 0 ]; then
cat > $t
@borkdude
borkdude / router.clj
Last active April 9, 2024 15:03
Small ring router using core.mach in babashka
(require '[clojure.core.match :refer [match]]
'[clojure.string :as str]
'[hiccup2.core :refer [html]]
'[org.httpkit.server :as server])
(defn router [req]
(let [paths (vec (rest (str/split (:uri req) #"/")))]
(match [(:request-method req) paths]
[:get ["users" id]] {:body (str (html [:div id]))}
:else {:body (str (html [:html "Welcome!"]))})))
@borkdude
borkdude / gallery.cljs
Last active March 30, 2024 17:36 — forked from yogthos/gallery.cljs
script to download walpapers from windowsonearth.org, adapted for nbb
@borkdude
borkdude / uuidv1.clj
Created September 15, 2023 09:18
UUID v1 in babashka / Clojure
;; based on code from https://www.baeldung.com/java-uuid
;; translated to Clojure by ChatGPT, minus the hex numbers which I evaluated in jshell
(defn get-64-least-significant-bits-for-version1 []
(let [random (java.util.Random.)
random-63-bit-long (bit-and (.nextLong random) 4611686018427387903)
variant-3-bit-flag -9223372036854775808]
(bit-or random-63-bit-long variant-3-bit-flag)))
(defn get-64-most-significant-bits-for-version1 []
@borkdude
borkdude / pw.clj
Created August 1, 2022 21:25
Playwright browser + bb chrome devtools
;; run npx playwright install to install a browser
(require '[babashka.deps :as deps])
(deps/add-deps
'{:deps {tatut/devtools {:git/url "https://github.com/tatut/clj-chrome-devtools"
:git/sha "cc96255433ca406aaba8bcee17d0d0b3b16dc423"}
org.babashka/spec.alpha {:git/url "https://github.com/babashka/spec.alpha"
:git/sha "1a841c4cc1d4f6dab7505a98ed2d532dd9d56b78"}}})
@borkdude
borkdude / projects.md
Last active January 30, 2024 16:25
clj-kondo, jet, edamame, sci, babashka

Overview of recent projects and how they relate:

  • clj-kondo: Clojure linter, compiled with GraalVM for fast startup. While implementing clj-kondo I realized that analyzed code could not only be linted, but also interpreted. This gave rise to sci.

  • jet: converts between JSON, EDN and Transit. Supports minimal query language in the spirit of jq. While implementing the query language for jet, I realized that using normal Clojure instead of a DSL was better for most Clojure users. This gave rise to sci.

  • edamame: EDN parser with location metadata and configurable dispatch table. This way of parsing is inspired by rewrite-clj, but it skips the intermediate node representation. This parser was extracted from sci.

@borkdude
borkdude / rewrite-if.clj
Last active December 25, 2023 21:12
Rewrite if with rewrite-clj and babashka
#!/usr/bin/env bb
(require '[rewrite-clj.zip :as z])
(def code "(defn foo [] (if true :a :b))")
(defn rewrite-if [code]
(let [zip (z/of-string code)]
(loop [zloc zip]
(if (z/end? zloc)
@borkdude
borkdude / tictactoe.cljs
Created December 14, 2023 18:57
squint_react_tictactoe.cljs
(require '["react" :as react])
(require '["react-dom" :as rdom])
(def empty-board [[\- \- \-]
[\- \- \-]
[\- \- \-]])
(def init-state {:board empty-board :player \X})
(defn get-board-cell
@borkdude
borkdude / aoc23_01.cljs
Last active December 7, 2023 19:53
AOC 2023 day 3 in squint + TC39 Tuples
;; Helper functions:
;; (fetch-input year day) - get AOC input
;; (append str) - append str to DOM
;; (spy x) - log x to console and return x
;; original solution:
;; https://github.com/russmatney/advent-of-code/blob/master/src/_2023/_03/core.clj
(require '["https://unpkg.com/@bloomberg/record-tuple-polyfill" :as tc39])
@borkdude
borkdude / aoc_ui.cljs
Last active December 5, 2023 16:15
AOC ui boilerplate
(require '[clojure.string :as str])
#_(assoc-in! (js/document.querySelector "#compiledCode") [:style :display] :none)
(when-not (js/document.querySelector "#aoc_token")
(let [create-element (fn create-element [tag attributes]
(let [element (js/document.createElement tag)]
(doseq [[key value] attributes]
(aset element key value))
element))