Skip to content

Instantly share code, notes, and snippets.

@mszajna
mszajna / Deadlock.java
Created July 17, 2023 15:23
Apache httpclient 4.x deadlocking with virtual threads
import java.util.concurrent.Executors;
import java.io.IOException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class Deadlock {
public static void deadlock(int n) throws IOException {
var request = new HttpGet("https://google.com");
try (var client = HttpClients.createDefault();
{:deps {org.clojure/tools.namespace {:mvn/version "0.2.11"}}
:aliases {:rebel {:extra-deps {com.bhauman/rebel-readline {:mvn/version "0.1.4"}}
:main-opts ["-m" "rebel-readline.main"]}
:rebels {:extra-deps {com.bhauman/rebel-readline-cljs {:mvn/version "0.1.4"}
org.clojure/clojurescript {:mvn/version "1.10.597"}}
:main-opts ["-m" "rebel-readline.cljs.main"]}}}
@mszajna
mszajna / def-quirks.clj
Created August 25, 2021 10:43
Quirks of Clojure def
; Clojure is not an interpreted language. Every top level form is compiled before it's run.
; If a form attempts to refer to an undefined variable, compilation fails.
(let [] ; ignore the useless 'let' for now - we'll get back to why it's there later.
(inc a))
;=> Syntax error compiling at (REPL:1:1).
;=> Unable to resolve symbol: a in this context
(def a 1)
; Now that the variable is defined, compilation of the form below works fine
(let []
(defmacro with-open* [bindings & body]
(let [[sym exp & others] bindings]
(if sym
`(let [~sym ~exp
result# (try
(with-open* [~@others] ~@body)
(catch Throwable t#
(try
(. ~sym close)
(catch Throwable t2#
@mszajna
mszajna / scoped_repl.clj
Last active March 24, 2021 22:31
Proof of concept for REPL sessions with scoped context
(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 []
@mszajna
mszajna / letfn.clj
Last active November 9, 2020 17:42
; 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 November 2, 2020 19:33
Bootstrapping def
(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 November 2, 2020 14:18
Clojure def as a macro
(defmacro mydef [n v] `(intern *ns* (quote ~(vary-meta n merge (meta &form))) ~v))
(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
(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