From f82ab31b36eae61f8b551ffefb4e593680223c17 Mon Sep 17 00:00:00 2001 From: Nathan Marz Date: Sat, 4 Jun 2016 21:22:57 -0400 Subject: [PATCH] expand optimized if-path to encompass any sequence of static functions --- CHANGES.md | 2 +- src/clj/com/rpl/specter/impl.cljx | 13 ++++++++----- test/com/rpl/specter/core_test.cljx | 8 ++++++++ 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 10d0107..e545b43 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -2,7 +2,7 @@ * More efficient inline caching for Clojure version, now inline caching is always within 5% of manually precompiled code * Huge performance improvement for ALL transform on maps and vectors * Significant performance improvements for FIRST/LAST for vectors -* Huge performance improvements for `if-path` and `cond-path`, especially for condition path containing a single static function +* Huge performance improvements for `if-path` and `cond-path`, especially for condition path containing only static functions * Eliminated compiler warnings for ClojureScript version * Dropped support for Clojurescript below v1.7.10 * Added :notpath metadata to signify pathedfn arguments that should be treated as regular arguments during inline factoring. If one of these arguments is not a static var reference or a non-collection value, the path will not factor. diff --git a/src/clj/com/rpl/specter/impl.cljx b/src/clj/com/rpl/specter/impl.cljx index e1ada8f..023390c 100644 --- a/src/clj/com/rpl/specter/impl.cljx +++ b/src/clj/com/rpl/specter/impl.cljx @@ -762,11 +762,14 @@ path (and (coll? path) - (= 1 (count path)) - (fn? (first path))) - (first path) - )) - + (every? fn? path)) + (reduce + (fn [combined afn] + (fn [structure] + (and (combined structure) (afn structure)) + )) + path + ))) (defn if-select [structure next-fn then-tester late-then late-else] (let [apath (if (then-tester structure) diff --git a/test/com/rpl/specter/core_test.cljx b/test/com/rpl/specter/core_test.cljx index f88ba7d..bbe6ae4 100644 --- a/test/com/rpl/specter/core_test.cljx +++ b/test/com/rpl/specter/core_test.cljx @@ -400,6 +400,14 @@ (select k m)) )))) +(deftest optimized-if-path-test + (is (= [-4 -2] (select [s/ALL (s/if-path [even? neg?] s/STAY)] + [1 2 -3 -4 0 -2]))) + (is (= [1 2 -3 4 0 2] (transform [s/ALL (s/if-path [even? neg?] s/STAY)] + - + [1 2 -3 -4 0 -2]))) + ) + (defspec multi-path-test (for-all+ [k1 (limit-size 3 gen/keyword)