Skip to content

Instantly share code, notes, and snippets.

Avatar

Henry Widd henryw374

View GitHub Profile
@henryw374
henryw374 / kroki-read.bb
Last active Jul 4, 2022
encode and decode files as kroki url diagrams https://kroki.io/#try
View kroki-read.bb
#!/usr/local/bin/bb
(import '[java.io ByteArrayOutputStream])
(import '[java.util Base64]) ;
(import '[java.util.zip Inflater]) ;
(defn uncompress [source-bytes]
(let [inflater (doto (Inflater.)
(.setInput source-bytes))
outputStream (new ByteArrayOutputStream (count source-bytes))
@henryw374
henryw374 / adhoc_file_server.bb
Last active Jul 5, 2022
babashka adhoc file server
View adhoc_file_server.bb
#!/usr/bin/env bb
; run the script from any dir to serve its files
(require '[babashka.deps])
(def deps '{:deps {io.github.babashka/http-server
{:git/sha "b38c1f16ad2c618adae2c3b102a5520c261a7dd3"}}})
(babashka.deps/add-deps deps)
@henryw374
henryw374 / same_thread_executor.clj
Created Jun 8, 2022
clojure executorservice concurrency testing
View same_thread_executor.clj
(ns same-thread-executor
"handy in unit tests to have things happen straight away"
(:import (java.util.concurrent ExecutorService Future)))
(defn executor []
(reify ExecutorService
(^Future submit [_ ^Callable f]
(let [r (f)]
(reify Future
(get [_] r))))))
View kaocha-coverage-repl.clj
(slingshot.slingshot/try+
(kaocha.plugin.cloverage/cloverage-main-hook (kaocha.repl/config))
(catch :kaocha/early-exit m
(prn m)))
@henryw374
henryw374 / reagent_interval.cljs
Last active Jun 9, 2022
clojure reagent interval subscription
View reagent_interval.cljs
(ns com.widdindustries.reagent-interval
"functions to create reagent reactions (ie ratoms) whose value changes
as an argument function is called periodically"
(:require [reagent.ratom :as ratom]
[reagent.core :as r]))
(defn interval-async
"create ratom whose value will initially be initial-state,
and thereafter may change as f is called with the 'state' atom every delay-ms"
[f delay-ms initial-state]
View clojure-delete-on-close-fileinput-stream.clj
(ns delete-on-close-fileinput-stream)
(defn create-fis [^java.io.File f]
(proxy [java.io.FileInputStream] [f]
(close []
(try
(proxy-super close)
(finally
(.delete f))))))
@henryw374
henryw374 / parsing-offset-names.clj
Last active Dec 15, 2021
java.time offset zonedatetime
View parsing-offset-names.clj
; demonstrating that offset names such as EST, CEST etc are not always parsed properly into ZonedDateTime.
; EST, CEST are defined as fixed offsets from UTC so are not ambiguous in that respect, although apparently the same abbreviation
; is used more than once for the same thing in some cases! Ideally they could be parsed to OffsetDateTime,
; but I can't find a way to get that working. Comment if you know how ;-)
; old parse api
(->
(SimpleDateFormat. "M/d/y, H:m z")
(.parse "3/26/2020, 14:00 EST"))
@henryw374
henryw374 / safe_subs.clj
Created Nov 30, 2021
clojure safe subs substring
View safe_subs.clj
(ns com.widdindustries.safe-subs)
(defn subs-safe
"like clojure.core/subs but more forgiving"
([^String s start] (subs-safe s start (count s)))
([^String s start end]
(when s
(let [end (if (> end (count s))
(count s)
View clojure-log4j2-influx.clj
(ns my.ns
(:require [com.widdindustries.log4j2.log-api :as log]
[com.widdindustries.log4j2.config :as config]
[com.widdindustries.InfluxDbProvider :as influx]
[com.widdindustries.influx-data :as influx-data]
[com.widdindustries.log4j2.log-impl :as log-impl])
(:import [org.apache.logging.log4j.message MapMessage]
[org.apache.logging.log4j Level]
[org.influxdb InfluxDB InfluxDBFactory InfluxDB$ConsistencyLevel]
[org.influxdb.dto Query]))
View timelines.cljc
(ns com.widdindustries.timelines
"create timelines which are (possibly infinite) sequences of maps with a ::time key.
Multiple timelines can be merged into a single timeline
Zero dependency - BYO time lib
Demo in comment at the end
")