Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / jsonista-streaming.clj
Last active September 30, 2022 10:17
demo reading and writing json with clojure jsonista library in a streaming style
(ns streaming
"read-write large json arrays with jsonista - techniques from
https://cassiomolin.com/2019/08/19/combining-jackson-streaming-api-with-objectmapper-for-parsing-json/
comment at end shows running/testing code"
(:require [jsonista.core :as j])
(:import (com.fasterxml.jackson.databind ObjectMapper)
(com.fasterxml.jackson.core JsonToken JsonGenerator JsonParser)
(java.io OutputStream ByteArrayOutputStream ByteArrayInputStream InputStream)))
@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"}
}}
@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
@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.

@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)
@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

@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))))