Last active
March 5, 2018 08:01
-
-
Save darkleaf/313cbe2a2f55775dae35c693315590d3 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
;; This buffer is for Clojure experiments and evaluation. | |
;; Press C-j to evaluate the last expression. | |
;; (ns test1 | |
;; (:require | |
;; [cats.core :as c] | |
;; [cats.monad.either :as either])) | |
;; (defn check-logged-out [] | |
;; (if true | |
;; (either/right) | |
;; (either/left {:error :logged-out}))) | |
;; (defn check-params [params] | |
;; (if true | |
;; (either/right) | |
;; (either/left {:error :wrong-params}))) | |
;; (defn check-authentication [user params] | |
;; (if false | |
;; (either/right) | |
;; (either/left {:error :wrong-login-or-pass}))) | |
;; (defn find-user [params] | |
;; (if true | |
;; (either/right :user) | |
;; (either/left))) | |
;; (defn log-in! [user] | |
;; (assert (= :user user))) | |
;; (time | |
;; (dotimes [_ 1000000] | |
;; (let [params nil] | |
;; (c/mlet [_ (check-logged-out) | |
;; _ (check-params params) | |
;; user (find-user params) | |
;; _ (check-authentication user params)] | |
;; (log-in! user) | |
;; (c/return {:success true}))))) | |
;; (ns test2 | |
;; (:require | |
;; [slingshot.slingshot :as s])) | |
;; (defn check-logged-out [] | |
;; (if true | |
;; nil | |
;; (s/throw+ {:error :logged-out}))) | |
;; (defn check-params [params] | |
;; (if true | |
;; nil | |
;; (s/throw+ {:error :wrong-params}))) | |
;; (defn check-authentication [user params] | |
;; (if false | |
;; nil | |
;; (s/throw+ {:error :wrong-login-or-pass}))) | |
;; (defn find-user [params] | |
;; :user) | |
;; (defn log-in! [user] | |
;; (assert (= :user user))) | |
;; (time | |
;; (dotimes [_ 1000000] | |
;; (let [params nil] | |
;; (s/try+ | |
;; (let [_ (check-logged-out) | |
;; _ (check-params params) | |
;; user (find-user params) | |
;; _ (check-authentication user params)] | |
;; (log-in! user) | |
;; {:success true}) | |
;; (catch Object e e))))) | |
;; (ns test3) | |
;; (defmacro let-cond [bindings & body] | |
;; (if (empty? bindings) | |
;; `(do ~@body) | |
;; (let [binding (take 2 bindings) | |
;; bindings (drop 2 bindings)] | |
;; (if (-> binding first seq?) | |
;; `(if-let [~'% ~(first binding)] | |
;; ~(second binding) | |
;; (let-cond [~@bindings] ~@body)) | |
;; `(let [~@binding] | |
;; (let-cond [~@bindings] ~@body)))))) | |
;; (defn check-logged-out [] | |
;; (if-not true | |
;; {:error :logged-out})) | |
;; (defn check-params [params] | |
;; (if-not true | |
;; {:error :wrong-params})) | |
;; (defn check-authentication [user params] | |
;; (if-not false | |
;; {:error :wrong-login-or-pass})) | |
;; (defn find-user [params] | |
;; (if true | |
;; :user)) | |
;; (defn log-in! [user] | |
;; (assert (= :user user))) | |
;; (time | |
;; (dotimes [_ 1000000] | |
;; (let [params nil] | |
;; (let-cond [(check-logged-out) % | |
;; (check-params params) % | |
;; user (find-user params) | |
;; (check-authentication user params) %] | |
;; (log-in! user) | |
;; {:success true})))) | |
(ns test4) | |
(defn check-logged-out [] | |
(if true | |
nil | |
(throw (java.lang.RuntimeException. "logged-out")))) | |
(defn check-params [params] | |
(if true | |
nil | |
(throw (java.lang.RuntimeException. "wrong-params")))) | |
(defn check-authentication [user params] | |
(if false | |
nil | |
(throw (RuntimeException. "wrong-login-or-pass" nil true true)))) | |
(defn find-user [params] | |
:user) | |
(defn log-in! [user] | |
(assert (= :user user))) | |
(time | |
(dotimes [_ 1000000] | |
(let [params nil] | |
(try | |
(let [_ (check-logged-out) | |
_ (check-params params) | |
user (find-user params) | |
_ (check-authentication user params)] | |
(log-in! user) | |
{:success true}) | |
(catch RuntimeException e e))))) | |
(java.lang.RuntimeException.) | |
This file contains hidden or 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
;; check-* return error map or nil | |
(defn process [params] | |
(or (check-logged-out) | |
(check-params params) | |
(let [user (find-user params)] | |
(or (check-authentication user params) | |
(do (user-session/log-in! user) | |
{:type ::processed :user user}))))) | |
;; --------------------------------- | |
(defn process [params] | |
(let [d-user (delay (find-user params))] | |
(or (check-logged-out) | |
(check-params params) | |
(check-authentication @d-user params) | |
(do (user-session/log-in! @d-user) | |
{:type ::processed :user @d-user})))) | |
;; --------------------------------- | |
;; https://github.com/Engelberg/better-cond | |
(b/defnc process [params] | |
:let [err (or (check-logged-out) | |
(check-params params))] | |
(some? err) err | |
:let [user (find-user params) | |
err (check-authentication user params)] | |
(some? err) err | |
:do (user-session/log-in! user) | |
{:type ::processed :user user})))))) | |
;; --------------------------------- | |
(defmacro let-cond [bindings & body] | |
(if (empty? bindings) | |
`(do ~@body) | |
(let [binding (take 2 bindings) | |
bindings (drop 2 bindings)] | |
(if (-> binding first seq?) | |
`(if-let [~'% ~(first binding)] | |
~(second binding) | |
(let-cond [~@bindings] ~@body)) | |
`(let [~@binding] | |
(let-cond [~@bindings] ~@body)))))) | |
(defn process [params] | |
(let-cond [(check-logged-out) % | |
(check-params params) % | |
user (find-user params) | |
(check-authentication user params) %] | |
(user-session/log-in! user) | |
{:type ::processed :user user})) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment