Skip to content

Instantly share code, notes, and snippets.

@henryw374
henryw374 / tidy-ns-requires.clj
Last active May 14, 2019 13:06
clojure clojurescript namespace require tidy
(ns tidy-requires)
(defn tidy-requires [[ _ & requires]]
(cons :require (sort-by first requires)))
(comment
(tidy-requires '(:require [foo]
[bar] ))
@henryw374
henryw374 / index-by.cljc
Created May 16, 2019 10:30
clojure index-by
(ns corr.core
(:require [medley.core :as m]))
(defn index-by [f coll]
(->> coll
(group-by f)
(m/map-vals (fn [xs]
(assert (= 1 (count (take 2 xs))))
(first xs)))))

hard sell intro key points we know clojure let's leverage that ie functions. ideally pure ones across the repos, we should follow conventions unless there's a good reason not to what problems are there? duplicated code every project has some about of setup boilerplate

(ns ftp-client
(:require
[clojure.java.io :as io]
[clojure.tools.logging :as log]
[miner.ftp :as ftp])
(:import [java.nio.file Path Paths Files FileSystem FileSystems]
[java.nio.file.attribute FileAttribute]
[java.io File]))
(defn remove!
@henryw374
henryw374 / com_widdindustries_statsatom.clj
Last active November 9, 2020 15:21
drop-in-replacement for clojure.core/atom which records count of updates and attempts to update
(ns com-widdindustries-statsatom
"A drop-in replacement for clojure.core/atom
The difference is that this record the following stats, held in the atom's metadata:
- number of times updated via swap!/swap-vals! and
- number of attempts made to do those updates
Clearly these stats themselves need to be atomically updated so there's some overhead added there.
"
(:refer-clojure :exclude [atom])
@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)))
(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
")
(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]))
@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 / 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"))