This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{: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"]}}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; 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 [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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 [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defmacro mydef [n v] `(intern *ns* (quote ~(vary-meta n merge (meta &form))) ~v)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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 |
NewerOlder