Skip to content

Instantly share code, notes, and snippets.

@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))
(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))))
@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 / 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)
@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.

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