Skip to content

Instantly share code, notes, and snippets.

View rauhs's full-sized avatar

Andre W. rauhs

  • Bavaria, Germany
View GitHub Profile
(defn decode-param
[^String qs start equal needs-decode?]
(if (true? needs-decode?)
(try
(URLDecoder/decode (.substring qs start equal) "UTF-8")
(catch UnsupportedEncodingException _
(.substring qs start equal)))
(.substring qs start equal)))
(defn handle-query-param
(ns cljs-snippeter
(:require
[clojure.walk :as walk]
[clojure.string :as str]
[clj-http.client :as client]))
(def ^:dynamic *compiling-ns* nil)
(defn compile-form
[form]
(defmacro afor
"Like for but eagerly builds a JS array.
Usually for react consumption."
[[item coll] & body]
`(reduce (fn [neue# ~item]
(.push neue# (do ~@body))
neue#)
(cljs.core/array) ~coll))
(defmacro arfor
(ns services.live-reload
(:require
[cheshire.core :as json]
[immutant.web :as immutant]
[immutant.web.async :as immut-async :refer [send! open?]]
[clojure.tools.logging :as log]
[mount.core :as mount]))
(defonce channels (atom #{}))
@rauhs
rauhs / A.synopsis.md
Last active September 20, 2021 14:22
Configure logback at runtime without XML but using hiccup

Synopsis

So logback uses a configuration framework called Joran. Upon JVM startup it loads the logback.xml file and shoves the XML into the Joran configurator. (See: https://logback.qos.ch/manual/onJoran.html )

This gist does nothing else but:

  1. Take the hiccup, which uses the exact same nodes/attributes as the XML file
  2. Generate XML from it
  3. Shove the entire thing into Joran.
;; Today at 2:05pm: (either future or past)
(.toInstant
(LocalDateTime/of (LocalDate/now) (LocalTime/of 14 05))
(ZoneOffset/UTC))
(defn ms-until+next
"Returns a vector of milliseconds:
1. The ms until the H:M of today. Negative if in the past.
2. The ms until the next H:M
- Equal if in the future."
(defn compute-between
[f]
(fn [rf]
(let [rf ((drop 1) rf)
p (volatile! 0)]
(completing
(fn [acc v]
(let [p0 @p]
(vreset! p v)
(rf (rf acc (f p0 v)) v)))))))
(to-transit [:a :a :aa :aa :aaa :aaa])
;; => "[\"~:a\",\"~:a\",\"~:aa\",\"^0\",\"~:aaa\",\"^1\"]"
;; => keywords >= 2 characters get cached, irregardless of being a map key or not
(to-transit ['a 'a 'aa 'aa 'aaa 'aaa])
;; => "[\"~$a\",\"~$a\",\"~$aa\",\"^0\",\"~$aaa\",\"^1\"]"
;; => same for symbols
(to-transit [{"aaa" "aaa"} {"aaa" "aaa"}
{"aaaa" ""} {"aaaa" ""}])
;; => "[[\"^ \",\"aaa\",\"aaa\"],[\"^ \",\"aaa\",\"aaa\"],[\"^ \",\"aaaa\",\"\"],[\"^ \",\"^0\",\"\"]]"
(ns xyz.abc
"
A map to convert diacrits / accents to latin characters
"
(:require
#?(:cljs [goog.object :as gobj])))
(def latinize-map
(#?(:cljs js-obj
:clj hash-map)

Synopsis

Implement an efficient datastructure that allows FAST access to the nth datom of a datomic index. This allows getting the "most recent post", "most popular post" etc.

;; API: to implement:
(nth-datoms db :avet :post/date 2284) ;; == (drop 2284 (d/datoms ...))
(rdatoms db :avet :post/date 50) ;; 50 last datoms of the index
(rdatoms-skip db :avet :post/date 150 50) ;; skip 150 datoms, get 50.