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
(defrecord Foo [state] | |
clojure.lang.IFn | |
(invoke [this] | |
(println "my state is" state))) | |
;; Try it out! | |
((->Foo "foo")) | |
my state is foo |
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
(extend-type Object | |
Closable | |
(.close [o] o)) | |
(defn closable-jetty [server] | |
(reify Closable (close [_] (.stop server)))) | |
(defn start [] | |
(let [system (promise)] | |
(future |
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 run-once | |
(d/function '{:lang "clojure" | |
:requires [[datomic.api :as d]] | |
:params [db db-fn] | |
:code (when-not (d/pull db [:data.migration/done] db-fn) | |
[[db-fn] | |
[:db/add db-fn :data.migration/done "datomic.tx"]])})) | |
(def schema-tx | |
[{:db/ident :data.migration/run-once |
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 either-try | |
([body] `(try [~body] (catch Throwable e# [nil e#]))) | |
([body pred] | |
`(try [~body] (catch Throwable e# (if (~pred e#) [nil e#] (throw e#)))))) | |
(defn risky-op [] (throw (ex-info "Oops" {:error-type :my-specific-error}))) | |
(let [[result error] (either-try (risky-op) #(-> % ex-data :error-type (= :my-specific-error)))] | |
(if error | |
(println "got" error) |
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
(ns executor) | |
(defrecord GraphqlResult [errors data]) | |
(defn result | |
([v] (result nil v)) | |
([e v] (->GraphqlResult e v))) | |
(defn result? [v] (instance? GraphqlResult 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
(go (try (<! (throw (ex-info "a" {}))) (catch Exception e nil) (finally (println "here") (throw (ex-info "b" {}))))) | |
=> | |
here | |
here | |
Exception b {} |
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
(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))) |
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 |
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 |
OlderNewer