Skip to content

Commit

Permalink
use eduction instead of sequence
Browse files Browse the repository at this point in the history
  • Loading branch information
erdos committed Nov 3, 2023
1 parent a287ca5 commit e8d7715
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 9 deletions.
11 changes: 4 additions & 7 deletions src/stencil/merger.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 []
Expand All @@ -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))

Expand Down
4 changes: 2 additions & 2 deletions test/stencil/merger_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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%}"}]
Expand Down

0 comments on commit e8d7715

Please sign in to comment.