From e8d7715693fb99e06867d14fbffd7c73075f2dbb Mon Sep 17 00:00:00 2001 From: janos erdos Date: Fri, 3 Nov 2023 23:57:40 +0100 Subject: [PATCH] use eduction instead of sequence --- src/stencil/merger.clj | 11 ++++------- test/stencil/merger_test.clj | 4 ++-- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/src/stencil/merger.clj b/src/stencil/merger.clj index f810b788..1b38840a 100644 --- a/src/stencil/merger.clj +++ b/src/stencil/merger.clj @@ -36,7 +36,7 @@ (defn- ->action-inside-parser [chars-and-tokens-to-append] (let [expected-close-tag-chars (volatile! (seq close-tag)) buffer-nonclose-chars-only (new java.util.ArrayList) - buffer-all-read (new java.util.ArrayList)] + buffer-all-read (new java.util.ArrayList)] (fn self ([] (when (seq buffer-all-read) @@ -86,17 +86,15 @@ (do (.add buffer token) self)))))))) -(defn cleanup-runs!!! [] +(defn- parser-trampoline [] (fn [rf] - ;; handler fn returns a fn (like trampoline) or a collection (let [handler (volatile! (->action-parser []))] (fn ([acc] (rf (reduce rf acc (@handler)))) ([acc token] (let [result (@handler token)] (if (fn? result) - (do (vreset! handler result) - acc) + (do (vreset! handler result) acc) (reduce rf acc result)))))))) (defn- unmap-text-nodes [] @@ -116,8 +114,7 @@ (rf (rf acc new-node) x))))))))) (defn cleanup-runs [tokens-seq] - (sequence (comp map-text-nodes (cleanup-runs!!!) (unmap-text-nodes)) - tokens-seq)) + (eduction (comp map-text-nodes (parser-trampoline) (unmap-text-nodes)) tokens-seq)) (defn- map-token [token] (:action token token)) diff --git a/test/stencil/merger_test.clj b/test/stencil/merger_test.clj index b68aab9f..f9a6132d 100644 --- a/test/stencil/merger_test.clj +++ b/test/stencil/merger_test.clj @@ -66,8 +66,8 @@ (testing "text token has full expression" (with-redefs [map-action-token map-action-token'] (are+ [x expected-literal expected-parsed] - [(= expected-literal (binding [*only-includes* true] (doall (cleanup-runs x)))) - (= expected-parsed (binding [*only-includes* false] (doall (cleanup-runs x))))] + [(= expected-literal (binding [*only-includes* true] (vec (cleanup-runs x)))) + (= expected-parsed (binding [*only-includes* false] (vec (cleanup-runs x))))] [{:text "{%=1%}"}] [{:text "{%=1%}"}]