Skip to content

Instantly share code, notes, and snippets.

@reborg
reborg / rich-already-answered-that.md
Last active February 23, 2024 13:09
A curated collection of answers that Rich gave throughout the history of Clojure

Rich Already Answered That!

A list of commonly asked questions, design decisions, reasons why Clojure is the way it is as they were answered directly by Rich (even when from many years ago, those answers are pretty much valid today!). Feel free to point friends and colleagues here next time they ask (again). Answers are pasted verbatim (I've made small adjustments for readibility, but never changed a sentence) from mailing lists, articles, chats.

How to use:

  • The link in the table of content jumps at the copy of the answer on this page.
  • The link on the answer itself points back at the original post.

Table of Content

@henryw374
henryw374 / clojure-delete-on-close-fileinput-stream.clj
Created February 2, 2022 15:29
clojure fileinputstream delete
(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 / tanstack-importmap-demo.js
Created November 7, 2023 16:50
tanstack react-router no build step
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>demo</title>
</head>
<body>
<div id="app" style="min-height:100%">
Hello world!
@jdf-id-au
jdf-id-au / transit-connection.cljc
Last active September 10, 2023 09:32
Sketch for connecting henryw374/time-literals to transit
(ns transit-connection
"Connect time-literals to transit."
(:require [time-literals.read-write]
#?(:cljs [java.time :refer [Period
LocalDate
LocalDateTime
ZonedDateTime
OffsetTime
Instant
OffsetDateTime
(ns com.widdindustries.kaocha-cljs2-ns-pattern-hook
"the only way I can find to filter kaocha-cljs2 tests by pattern. see comment block for usage"
(:require [kaocha.core-ext :as core-ext]
[kaocha.load :as load]))
(defn ns-filter [plan]
;(def plan plan)
;(println "About to start loading!")
(update plan :kaocha.test-plan/tests
(ns datascript-to-datomic-util
(:require [datascript :as d]))
;;;; a utility to help with a datomic-datascript roundtrip process involving:
;;; 1. export some data from a datomic database and transact into a datascript instance.
;;; 2. perform one or more transactions against datascript.
;;; 3. transact the sum of all changes made against datascript back into datomic in a single tx
;;; this namespace contains two public functions:
;;; listen-for-changes: listen to datascript transactions and build up a record of changes
@sunng87
sunng87 / reflection.clj
Created November 20, 2015 10:04
clojure: access private field/method via reflection
(defn invoke-private-method [obj fn-name-string & args]
(let [m (first (filter (fn [x] (.. x getName (equals fn-name-string)))
(.. obj getClass getDeclaredMethods)))]
(. m (setAccessible true))
(. m (invoke obj args))))
(defn private-field [obj fn-name-string]
(let [m (.. obj getClass (getDeclaredField fn-name-string))]
(. m (setAccessible true))
(. m (get obj))))
(ns protocol-proxy
"for when you have an object foo, which satisfies some protocols and you want to make adhoc changes to
one or more of the protocol methods, but just on foo.
Can be handy for testing.
"
(:refer-clojure :exclude [proxy])
(:require [clojure.string :as string]))
;(remove-ns 'protocol-proxy)
@jjttjj
jjttjj / deps.edn
Last active March 4, 2023 19:03 — forked from jdf-id-au/transit-connection.cljc
Sketch for connecting henryw374/time-literals to transit
{:paths ["."]
:deps {time-literals/time-literals {:mvn/version "0.1.5"}
com.cognitect/transit-clj {:mvn/version "0.8.319"}
com.cognitect/transit-cljs {:mvn/version "0.8.256"}
}}
(ns contiguous-spans
"takes pre-sorted dates and returns contiguous periods contained therein.
see comment block for demo
"
(:require [tick.core :as t]))
(defn contiguous-spans
"takes pre-sorted dates and returns contiguous periods contained.
dates a and b are contiguous if a plus span-length is equal to b "