Skip to content

Instantly share code, notes, and snippets.

View borkdude's full-sized avatar

Michiel Borkent borkdude

View GitHub Profile
@borkdude
borkdude / pinball.cljs
Last active November 17, 2023 23:54
Squint pinball
;; Adapted from: https://thegeez.net/2023/03/01/pinball_scittle.html
#_(do #_:clj-kondo/ignore (warn-on-lazy-reusage!))
(defn element [tag id child-of prepend?]
(or (js/document.getElementById id)
(let [elt (js/document.createElement tag)
parent (if child-of (js/document.querySelector child-of)
js/document.body)]
(set! (.-id elt) id)
@borkdude
borkdude / wordle.cljs
Created November 16, 2023 12:18
Squint wordle
(ns wordle)
(def board-state (atom []))
(def counter (atom 0))
(def attempt (atom 0))
(def word-of-the-day (atom "hello"))
(defn write-letter [cell letter]
(set! (.-textContent cell) letter))
@borkdude
borkdude / hoplon_demo.cljs
Last active October 27, 2023 12:28
hoplon demo
(ns view.index
(:require
[hoplon.core :as h]
[javelin.core :as j]))
(defn my-list [& items]
(h/div :class "my-list"
(apply h/ul (map #(h/li (h/div :class "my-list-item" %)) items))))
(defonce clicks (j/cell 0))
@borkdude
borkdude / partial.md
Last active September 11, 2023 14:24
partial.md

Here's a list of things I collected a conversation on partial. Personally I think using partial almost never has a benefit, while there are some downsides to using it. There are exceptional cases where partial has a benefit, e.g. if you want to "wrap" a multi-arity function or a function with an unknown amount of arguments.

Some reasons not to use partial:

@borkdude
borkdude / text_xform.clj
Created September 1, 2017 13:07
Clojure text files transducer
(ns text-xform
(:require [clojure.java.io :as io]
[clojure.string :as str]
[cheshire.core :as json])
(:import [java.io BufferedReader]))
;;;; inspired by https://tech.grammarly.com/blog/building-etl-pipelines-with-clojure
(def db (atom 0))
@borkdude
borkdude / js_parser.clj
Last active August 6, 2023 22:14
Parse JavaScript in Clojure using the Google Closure Compiler
#!/bin/sh
#_(
"exec" "clojure" "-Sdeps" "{:deps {org.clojure/clojurescript {:mvn/version \"1.10.520\"}}}" "$0" "$@"
)
;; running js_parser.clj "function foo(x) { var y = x + 1; }" will print:
;; [{:type :function, :name "foo", :body [{:variable-statement [{:lvalue "y", :initializer {:type :binary-op, :left "x", :operator "+", :right "1"}}]}], :params ["x"]}]
@borkdude
borkdude / 01-README.md
Last active May 4, 2023 03:42
Shadow CLJS require ES Module in script

This shows how to require ES modules in a node script which is itself compiled as an ES module by shadow. I made this to verify if nbb's way of requiring ES modules aligns with other tools that accomplish the same using the CLJS compiler.

Build with npx shadow-cljs compile script. Run with node out/script.js. It will print something like:

#js {:columns 202, :rows 45}
[Function: Spinner]
(defmacro <-
[& טופסים]
(let [[x & טופסים] (reverse טופסים)]
(loop [x x, טופסים טופסים]
(if טופסים
(let [טופס (first טופסים)
מתארגן (if (seq? טופס)
(with-meta `(~(first טופס) ~x ~@(next טופס)) (meta טופס))
(list טופס x))]
(recur מתארגן (next טופסים)))
@borkdude
borkdude / test_args.clj
Created November 8, 2022 12:15
Clojure deftest with arguments
;; {:deps {org.babashka/cli {:mvn/version "0.5.40"}
;; babashka/fs {:mvn/version "0.1.11"}}}
(ns test-args
{:clj-kondo/config '{:lint-as {test-args/deftest clojure.core/defn}}}
(:require
[babashka.cli :as cli]
[clojure.test :as t :refer [is]]))
(def ^:dynamic *test-args* (cli/parse-opts *command-line-args*))
@borkdude
borkdude / scrape_tables.clj
Created October 5, 2020 08:21
Extract HTML tables with babashka and bootleg
(ns scrape
(:require [babashka.pods :as pods]
[clojure.walk :as walk]))
(pods/load-pod "bootleg") ;; installed on path, use "./bootleg" for local binary
(require '[babashka.curl :as curl])
(def clojure-html (:body (curl/get "https://en.wikipedia.org/wiki/Clojure")))