Skip to content

Instantly share code, notes, and snippets.

@swannodette
swannodette / transitive_foreign.md
Last active June 1, 2020 14:22
Transitive Foreign Dependencies
NOTE: This proposal is dead. A simpler solution was arrived at via the new `:bundle` target.

ClojureScript Support for Foreign Libraries via NPM

Problem Description

The current Webpack solution works reasonable well for application oriented development, but does not address the wider issue of developing ClojureScript libraries that depend on the wider JavaScript ecosystem where the artifacts do not reside in Java-centric distrubtion like Maven (i.e. NPM). Currently a ClojureScript library cannot express these kinds of foreign library dependencies at all. Thus users must constantly redefine basic dependencies from NPM such as React, boilerplate Webpack configuration, boilerplate import/export files, and thus cannot create and share reusable ClojureScript components that depend on this vast ecosystem.

@joastbg
joastbg / lazyio.clj
Last active September 9, 2020 10:00
Lazy IO in Clojure
(ns Experiments.lazyio)
;; Self contained example - Lazy I/O etc
;; (C) Johan Astborg, 2014
(def filename "out.gz")
(defn lazy-gzip-read [file]
"lazy gzip file reader"
(let [zip (java.util.zip.GZIPInputStream. (java.io.FileInputStream. file))
@henryw374
henryw374 / tick_recipes.cljc
Last active June 30, 2021 05:35
juxt tick
;local date to native date (js/Date or java.util.Date.)
(-> d
(cljc.java-time.local-date/at-start-of-day (t/zone "UTC"))
(t/inst))
; native date (js/Date or java.util.Date.) to local date
(-> (java.util.Date.)
t/instant
(cljc.java-time.zoned-date-time/of-instant (t/zone "UTC"))
t/date)
@henryw374
henryw374 / parsing-offset-names.clj
Last active December 15, 2021 11:07
java.time offset zonedatetime
; 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 / kaocha-coverage-repl.clj
Created March 23, 2022 10:28
kaocha coverage repl
(slingshot.slingshot/try+
(kaocha.plugin.cloverage/cloverage-main-hook (kaocha.repl/config))
(catch :kaocha/early-exit m
(prn m)))
@henryw374
henryw374 / same_thread_executor.clj
Created June 8, 2022 08:18
clojure executorservice concurrency testing
(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))))))
@henryw374
henryw374 / safe_subs.clj
Created November 30, 2021 14:01
clojure safe subs substring
(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)
@henryw374
henryw374 / reagent_interval.cljs
Last active June 9, 2022 06:05
clojure reagent interval subscription
(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]
@henryw374
henryw374 / kroki-read.bb
Last active July 4, 2022 17:50
encode and decode files as kroki url diagrams https://kroki.io/#try
#!/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 July 5, 2022 09:51
babashka adhoc file server
#!/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)