From 10558896c186c9010eb1c1ffb9e64e8a0bee9fe8 Mon Sep 17 00:00:00 2001 From: "kurt.sys" Date: Mon, 24 Jul 2017 09:38:14 +0200 Subject: [PATCH 1/4] make eager-fn user-definable + rename manage-with-courage -> manage-eager-conditions + add manage-with function --- src/special/core.cljc | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/special/core.cljc b/src/special/core.cljc index 9c452eb..0cf7c68 100644 --- a/src/special/core.cljc +++ b/src/special/core.cljc @@ -2,7 +2,7 @@ (defonce ^:dynamic *-special-condition-handlers-* {}) -(defn- manage-with-courage +(defn- manage-eager-conditions "Takes an eager function f and an \"inlined\" map of conditions to handlers. Returns a function in which these conditions are managed. @@ -24,17 +24,19 @@ (binding [*-special-condition-handlers-* (merge *-special-condition-handlers-* restarts)] (apply f args)))) +(defn- eager [v] + (pr-str v) + v) + (defn- eagerize "Turns a lazy function into an eager function, at the run-time cost of using pr-str to fully realize the function result." - [f] + [eager-fn f] (fn [& args] - (let [res (apply f args) - _ (pr-str res)] - res))) + (eager (apply f args)))) -(defn manage +(defn manage-with "Takes a function f and an \"inlined\" map of conditions and keywords. Returns a function in which these conditions are managed. @@ -43,9 +45,10 @@ f is allowed to be lazy, but the result must be finite, as it will always be fully realized. In other words: manage returns an eager function." + [f eager-fn & restarts] + (apply manage-eager-conditions (eagerize eager-fn f) restarts)) - [f & restarts] - (apply manage-with-courage (eagerize f) restarts)) +(def manage (partial manage-with eager)) (defn condition "Raise a condition c with optional value v and optionally an \"inlined\" map of conditions to handlers. From d6061306cb9695cfc8ba8038bbb266cc0b72b8b8 Mon Sep 17 00:00:00 2001 From: "kurt.sys" Date: Mon, 24 Jul 2017 10:01:02 +0200 Subject: [PATCH 2/4] implement multimethod instead of fn-pass --- src/special/core.cljc | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/special/core.cljc b/src/special/core.cljc index 0cf7c68..55e08c0 100644 --- a/src/special/core.cljc +++ b/src/special/core.cljc @@ -24,7 +24,9 @@ (binding [*-special-condition-handlers-* (merge *-special-condition-handlers-* restarts)] (apply f args)))) -(defn- eager [v] + +(defmulti eager (fn [type _] type)) +(defmethod eager :default [_ v] (pr-str v) v) @@ -32,11 +34,11 @@ "Turns a lazy function into an eager function, at the run-time cost of using pr-str to fully realize the function result." - [eager-fn f] + [eager-type f] (fn [& args] - (eager (apply f args)))) + (eager eager-type (apply f args)))) -(defn manage-with +(defn manage-as "Takes a function f and an \"inlined\" map of conditions and keywords. Returns a function in which these conditions are managed. @@ -45,10 +47,10 @@ f is allowed to be lazy, but the result must be finite, as it will always be fully realized. In other words: manage returns an eager function." - [f eager-fn & restarts] + [f & restarts] (apply manage-eager-conditions (eagerize eager-fn f) restarts)) -(def manage (partial manage-with eager)) +(def manage (partial manage-as :default)) (defn condition "Raise a condition c with optional value v and optionally an \"inlined\" map of conditions to handlers. From 0d55a85b335d922773dc18cabb4c9476b8df12bd Mon Sep 17 00:00:00 2001 From: "kurt.sys" Date: Mon, 24 Jul 2017 10:29:54 +0200 Subject: [PATCH 3/4] implement multimethods and pass-fn --- src/special/core.cljc | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/special/core.cljc b/src/special/core.cljc index 55e08c0..b416d96 100644 --- a/src/special/core.cljc +++ b/src/special/core.cljc @@ -24,19 +24,21 @@ (binding [*-special-condition-handlers-* (merge *-special-condition-handlers-* restarts)] (apply f args)))) - (defmulti eager (fn [type _] type)) +(defmethod eager ::default [_ v] + (pr-str v) v) (defmethod eager :default [_ v] - (pr-str v) - v) + (eager ::default v)) (defn- eagerize "Turns a lazy function into an eager function, at the run-time cost of using pr-str to fully realize the function result." - [eager-type f] + [eager-method f] (fn [& args] - (eager eager-type (apply f args)))) + (if (keyword? eager-method) + (eager eager-method (apply f args)) + (eager-method (apply f args)) ))) (defn manage-as "Takes a function f and an \"inlined\" map of conditions and keywords. @@ -47,10 +49,15 @@ f is allowed to be lazy, but the result must be finite, as it will always be fully realized. In other words: manage returns an eager function." - [f & restarts] + [type f & restarts] + (apply manage-eager-conditions (eagerize type f) restarts)) + +(defn manage-with + [eager-fn f & restarts] (apply manage-eager-conditions (eagerize eager-fn f) restarts)) -(def manage (partial manage-as :default)) +(def manage (partial manage-with #(do (pr-str %) %))) +;; or (def manage (partial manage-as ::default)) (defn condition "Raise a condition c with optional value v and optionally an \"inlined\" map of conditions to handlers. From df0fae2bd4db966a32860d6f0cdfb6c8318e576b Mon Sep 17 00:00:00 2001 From: "kurt.sys" Date: Mon, 24 Jul 2017 12:42:40 +0200 Subject: [PATCH 4/4] add ::postwalk eagerization --- src/special/core.cljc | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/special/core.cljc b/src/special/core.cljc index b416d96..5a6a6e6 100644 --- a/src/special/core.cljc +++ b/src/special/core.cljc @@ -1,4 +1,5 @@ -(ns special.core) +(ns special.core + (:require [clojure.walk :as walk])) (defonce ^:dynamic *-special-condition-handlers-* {}) @@ -25,6 +26,8 @@ (apply f args)))) (defmulti eager (fn [type _] type)) +(defmethod eager ::postwalk [_ v] + (walk/postwalk (constantly nil) v) v) (defmethod eager ::default [_ v] (pr-str v) v) (defmethod eager :default [_ v]