Skip to content

Instantly share code, notes, and snippets.


Maciej Szajna mszajna

View GitHub Profile
View with-open.clj
(defmacro with-open* [bindings & body]
(let [[sym exp & others] bindings]
(if sym
`(let [~sym ~exp]
(with-open* [~@others] ~@body)
(catch Throwable t#
(. ~sym close)
(catch Throwable t2#
mszajna / scoped_repl.clj
Last active Mar 24, 2021
Proof of concept for REPL sessions with scoped context
View scoped_repl.clj
(def ^:dynamic *depth* 0)
(defn repl-env [env]
(with-local-vars [ret nil]
:eval #(var-set ret (eval `(let [~@(mapcat identity env)] ~%)))
:prompt #(printf "%s%s=> " (ns-name *ns*) (apply str (repeat *depth* "="))))
(defmacro cnt []
View letfn.clj
(defn Y* [& fs]
(map (fn [f] (f))
((fn [x] (x x))
(fn [p]
(fn [f]
(fn []
(apply f
mszajna / mydef.clj
Created Nov 2, 2020
Bootstrapping def
View mydef.clj
(let* [list (. clojure.lang.PersistentList creator)
mydef (fn* [&form &env sym val] (list 'clojure.lang.Var/intern `*ns* (list 'quote sym) val))]
(clojure.lang.Var/intern *ns* (quote mydef) mydef)
(. (var mydef) (setMacro)))
(mydef a 1)
=> #'user/a
=> 1
mszajna / def.clj
Created Nov 2, 2020
Clojure def as a macro
View def.clj
(defmacro mydef [n v] `(intern *ns* (quote ~(vary-meta n merge (meta &form))) ~v))
View wrap-completable-future.clj
(defn async? [response]
(instance? java.util.concurrent.CompletionStage response))
(defn- cs-handle [cs f]
(.handle ^java.util.concurrent.CompletionStage cs
(reify java.util.function.BiFunction
(apply [_ v t] (f v t)))))
(defn wrap-completable-future
"Adapts a 1-arity handler, returning either a response map or one wrapped
View response.clj
(defn- ^Function java-fn [f]
(reify java.util.function.Function
(apply [_ x] (f x))))
(defn- to-completion-stage [response]
(if (instance? CompletionStage response)
(CompletableFuture/completedFuture response)))
(defn bind-response
View ring-middleware-cookies-bug.clj
(require 'ring.middleware.cookies)
(defn echo-handler
([request] request)
([request respond raise] (future (respond request)) nil))
(def handler
{:encoder (fn [_] (throw (ex-info "encoder error" {})))}))
View datomic-call.clj
(require '[datomic.api :as d])
; Datomic transaction are ACID which is achieved processing only one at a time.
; For certain workloads it's a good enough model. The built in transaction functions
; offer enforcing uniqueness and a compare-and-swap operation. They work great but
; are often too little for expressing more complex business rules.
; For those, datomic offers transaction functions, that you can install storing the
; source code in the DB.
(def conn (let [url "datomic:mem:call"] (d/create-database url) (d/connect url)))
View core_async_bug.clj
(go (try (<! (throw (ex-info "a" {}))) (catch Exception e nil) (finally (println "here") (throw (ex-info "b" {})))))
Exception b {}