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 / fuzzy_compare_floating_point_numbers.clj
Last active July 19, 2018 14:05
fuzzy comparing floating point numbers
;; This turns out to be very, very useful when working with floating point numbers in some cases.
;; E.g. if you switch some of your input from ^long to ^double your calculations might actually shift ever so slightly
;; which will make comparing old vs. new results tricky.
;; You'll need this in your project.clj file:
[com.google.guava/guava "LATEST"]
;; ..and this somewhere useful:
@lnostdal
lnostdal / ehlers_super_smoother_in_clojure.clj
Last active August 2, 2018 21:55
Ehlers' Super Smoother in Clojure
;; Based on this talk https://www.youtube.com/watch?v=BR5pDiPYsnw
;;
;; NOTE: I've not yet tested these for correctness(!)
(defn double-finite-or-zero ^double [^double n]
(if (Double/isFinite n)
n
0.0))
@lnostdal
lnostdal / clojure_big_files.clj
Created June 21, 2018 08:27
Dealing with big files from Clojure via GC finalize and laziness via map/line-seq.
;; https://www.Quanto.ga/
;; NOTE: The normal and sane(!) way to do this would be to use an outer WITH-OPEN
;; – or even better put your stuff in a database!
(deftype GCedResource ;; Clojure WITH-OPEN macro (sort of?) on drugs. Bonus: It will work in context of laziness.
[resource]
Object
@lnostdal
lnostdal / map_vs_keyword_as_fn.clj
Created June 18, 2018 09:20
Clojure performance: using Map as Fn vs. using Keyword as Fn
;; https://www.Quanto.ga/
(def ^:const exchange-use-dedicated-sl-and-tp-orders?
{:backtest false
:bitmex true
:oneb false})
quantataraxia.core> (do
@lnostdal
lnostdal / clojure.core.async.deadlocks.clj
Last active November 7, 2018 12:30
clojure.core.async: how to deal with deadlocks
;;; https://www.Quanto.ga
;; clojure.core.async: how to deal with deadlocks
;;
;; If you have a channel that in the "reader" or consumer also sends
;; to itself you might end up with a deadlock in certain cases. Especially
;; if you don't want to use buffered channels -- perhaps because it is
;; impossible to predict how much of a buffer you will need:
@lnostdal
lnostdal / upmap.clj
Last active July 13, 2024 16:58
Clojure unordered concurrency test: MAP vs. PMAP vs. UPMAP
;; www.Quanto.ga
;;
;; cp/upmap is from https://github.com/TheClimateCorporation/claypoole
quantataraxia.core> (do
(println "MAP:")
(println (time (doall (map (fn [x] (Thread/sleep x) x)
(range 500 50 -9)))))
(println "PMAP:")
@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]))
@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))
(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 / 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