Skip to content

Instantly share code, notes, and snippets.

View lnostdal's full-sized avatar
🎧
Clojure, PostgreSQL, Linux

Lars Rune Nøstdal lnostdal

🎧
Clojure, PostgreSQL, Linux
View GitHub Profile
@lnostdal
lnostdal / ping_alarm.sh
Last active April 25, 2016 16:41
Simple Linux shell script to make a looping sound as long as your Internet connectivity is down
#!/bin/bash
## NOTE: Make sure `bell.oga' actually exists and adjust accordingly.
for (( ; ; ))
do
ping -w 5 -c 1 8.8.8.8 &> /dev/null
if [ $? -ne 0 ]; then
paplay /usr/share/sounds/freedesktop/stereo/bell.oga
;;; Add this in your project.clj to enable direct linking
;;
;; :jvm-opts ["-Dclojure.compiler.direct-linking=true"]
;; ..and now observe how this changes things:
; CIDER 0.11.0snapshot (package: 20160119.836) (Java 9-ea, Clojure 1.9.0-master-SNAPSHOT, nREPL 0.2.12)
user> (defn a [] (println "a"))
@lnostdal
lnostdal / atomically_rename.clj
Last active August 27, 2021 10:12
Move a file atomically in Clojure
;;; Move a file atomically in Clojure.
;; This will also replace the target file if it exists since REPLACE_EXISTING is included in the options at the end.
;; user.dir == current working directory (on Linux at least).
(let [source-filename (str (System/getProperty "user.dir") "/source.txt")
target-filename (str (System/getProperty "user.dir") "/target.txt")
source-file (java.nio.file.Paths/get (java.net.URI/create (str "file://" source-filename)))
target-file (java.nio.file.Paths/get (java.net.URI/create (str "file://" target-filename)))]
(java.nio.file.Files/move source-file target-file
(import 'com.google.common.collect.EvictingQueue)
(let [queues (atom {})]
(defn ohlc-circular-cache [size seconds ^String id ^Fn get-ohlc-data-fn]
"`size`: Size of circular buffer.
`seconds`: Time frame of data series. I.e. number of seconds between OHLC .open and .close.
`id`: Unique ID string used to identify this circular buffer.
`get-ohlc-data-fn`: (fn [timestamp] ...) Get the most recent data series from timestamp. If timestamp is NIL get as much data as possible. In both cases the data returned will include the most recently closed OHLC entry (candle). Note that `get-ohlc-data-fn` will only be called once – so if stale data is returned you must deal with this either there or in code that depends on OHLC-CIRCULAR-CACHE directly or indirectly."
(let [seconds (long seconds)
^com.google.common.collect.EvictingQueue
queue (locking queues
@lnostdal
lnostdal / cachebuilder.clj
Last active July 14, 2024 14:21
Guava: CacheBuilder / CacheLoader for compute cache type thing and similar
;; This stuff is pretty useful in many contexts.
...
(:import (com.google.common.cache CacheBuilder CacheLoader))
...
quantataraxia.core> (with (-> (CacheBuilder/newBuilder)
(.weakKeys)
@lnostdal
lnostdal / MapDifference.clj
Created April 9, 2018 15:41
Clojure: guava HashBiMap + MapDifference
quantataraxia.core> (let [m1 (HashBiMap/create {1 :a 2 :b 3 :c})
m2 (HashMap. {:a 1 :b 2})
m1inv (.inverse m1)]
(with (Maps/difference m1inv m2)
(.removeAll (.keySet m1inv) (.keySet (.entriesOnlyOnLeft it)))
(pprint (into {} m1))
(pprint (into {} m1inv))))
{1 :a, 2 :b}
{:a 1, :b 2}
@lnostdal
lnostdal / bitmex.clj
Last active April 14, 2018 20:38
Bitmex API key signature generation in Clojure
;;; www.Quanto.ga
;;;;;;;;;;;;;;;;;
;; Test from https://www.bitmex.com/app/apiKeysUsage#Full-sample-calculation
(letfn [(sign [^String api-secret ^String s]
(let [h (com.google.common.hash.Hashing/hmacSha256 (.getBytes api-secret))]
(.toString (.hashString h s com.google.common.base.Charsets/UTF_8))))]
(let [apiKey "LAqUlngMIQkIUjXMUreyu3qn"
apiSecret "chNOOS4KvNXR_Xq4k4c9qsfoKWvnDecLATCRlcBwyKDYnWgO"]
;; Simple GET
(let [f (future
(try
(Thread/sleep 10000)
(catch Throwable e
(println e)
(println "isInterrupted:" (.isInterrupted (Thread/currentThread)))
(println "isAlive:" (.isAlive (Thread/currentThread)))
(println "interrupted:" (Thread/interrupted)))))]
(Thread/sleep 500)
(future-cancel f)
@lnostdal
lnostdal / async_&_transducers.clj
Created May 18, 2018 07:50
how to use transducer with clojure.core.async
;; I keep forgetting how to do this stuff for some reason, so here goes:
(let [c (async/chan 1 (map #(* % 2)))]
(async/go
(loop []
(when-let [e (async/<! c)]
(println "async/go, e:" e)
(recur))))
(async/>!! c 1)
(async/>!! c 2))
@lnostdal
lnostdal / fast_local_mutation.clj
Last active July 13, 2024 16:58
Clojure: Performance of with-local-vars vs. atom vs. volatile vs. unsynchronized-mutable field
;; Performance of with-local-vars vs. atom vs. volatile vs. unsynchronized-mutable field.
(definterface IOObject
(setVal [new-val])
(getVal [])
(oswap [f])
(oswap [f x])
(oswap [f x y])
(oswap [f x y z]))