Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View malcolmsparks's full-sized avatar

Malcolm Sparks malcolmsparks

View GitHub Profile

If you're using clojure.tools.logging with SLF4J.

It's often useful to 'switch on' logging for a given web request, or during the evaluation of a given form, etc.

Add this to your logback.xml configuration:

  <turboFilter class="ch.qos.logback.classic.turbo.MDCFilter">
    <MDCKey>logging</MDCKey>
    <Value>on</Value>
@malcolmsparks
malcolmsparks / thoughts-on-yada.adoc
Last active January 22, 2023 19:24
Thoughts on yada

Thoughts on yada

Frameworks

Let’s define a framework as any library that contains one or more functions that accept callbacks.

Web frameworks are great for beginner developers who need to get stuff done. But they ultimately force you into a corner. In most applications, experienced developers need to retain control. Libraries are better.

@malcolmsparks
malcolmsparks / dom.clj
Created February 17, 2021 08:13
XML DOM builder in Clojure
(defmacro dom [doc parent el]
(let [docsym (gensym "doc")
parentsym (gensym "parent")
elsym (gensym "el")]
`(let [~parentsym ~parent
[ens# enm#] ~(first el)
~docsym ~doc
~elsym (.createElementNS ~docsym ens# enm#)]
~@(concat
(for [item (next el)]
@malcolmsparks
malcolmsparks / backup-photos.clj
Last active June 29, 2021 19:10
A babashka script for sorting and de-duping a photo collection on Linux.
#_( ;; Allow this script to be executed directly
"exec" "bb" -o "--classpath" "." "$0" "$@"
)
;; Copyright © 2020, Malcolm Sparks
;; Permission is hereby granted, free of charge, to any person obtaining a copy
;; of this software and associated documentation files (the “Software”), to deal
;; in the Software without restriction, including without limitation the rights
;; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
@malcolmsparks
malcolmsparks / vertx.clj
Created April 19, 2020 08:38
Starting a Vertx web server and handler
(defmethod ig/init-key ::http-server
[_ {:keys [vertx port]}]
(let [server
(..
vertx
(createHttpServer
(..
(new HttpServerOptions)
(setPort port))))]
(..
@malcolmsparks
malcolmsparks / charset.clj
Created March 29, 2020 18:01
Parsing the RFC 7231 Accept-Charset header with reap
;; Accept-Charset = *( "," OWS ) ( ( charset / "*" ) [ weight ] ) *( OWS
;; "," [ OWS ( ( charset / "*" ) [ weight ] ) ] )
(let [parser
(let [charset-with-weight
(p/sequence-group
(p/alternatives
(p/pattern-parser
(re-pattern charset))
(p/pattern-parser
(re-pattern (re/re-str \*))))
(ns juxt.play
(:require [clojure.set :as set])
)
;;#{[0 1] [1 1]}
;;what about if we're at [1 1]
;; Copyright © 2014-2017, JUXT LTD.
(ns yada.dev.upload
(:require
[byte-streams :as bs]
[bidi.bidi :refer [RouteProvider tag]]
[bidi.vhosts :refer [uri-for redirect]]
[clojure.tools.logging :refer :all]
[clojure.java.io :as io]
[schema.core :as s]
@malcolmsparks
malcolmsparks / gist:16e5618ebb759f15f7fc938eacd5c9f4
Last active May 28, 2019 14:28
json path parsing in clojure
(ns juxt.jsonschema.formula
(:require [clojure.set :as set])
)
(defn partition-into-ranges-iter
"Find consecutive number sequences. O(n)"
[coll]
(loop [[x & xs] (sort coll)
subsequent 0
curr []
@malcolmsparks
malcolmsparks / reset.clj
Created May 6, 2018 08:56
Reset on HUP signal
(defn reset-on-hup []
(let [bindings (get-thread-bindings)]
(sun.misc.Signal/handle
(sun.misc.Signal. "HUP")
(reify sun.misc.SignalHandler
(handle [_ signal]
(with-bindings bindings
(println (reset))))))))