Skip to content

Instantly share code, notes, and snippets.

Avatar

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]
(try
(with-open* [~@others] ~@body)
(catch Throwable t#
(try
(. ~sym close)
(catch Throwable t2#
@mszajna
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]
(clojure.main/repl
:eval #(var-set ret (eval `(let [~@(mapcat identity env)] ~%)))
:prompt #(printf "%s%s=> " (ns-name *ns*) (apply str (repeat *depth* "="))))
@ret))
(defmacro cnt []
View letfn.clj
; https://stackoverflow.com/questions/4899113/fixed-point-combinator-for-mutually-recursive-functions
(defn Y* [& fs]
(map (fn [f] (f))
((fn [x] (x x))
(fn [p]
(map
(fn [f]
(fn []
(apply f
(map
@mszajna
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
a
=> 1
@mszajna
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)
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
(ring.middleware.cookies/wrap-cookies
echo-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" {})))))
=>
here
here
Exception b {}