Skip to content

Commit d96a399

Browse files
borkdudemk
authored andcommitted
Evaluator docs for Book of Clerk (#510)
Add Evaluator section to show how to use cherry as an alternative evaluator for `:render-fn`s. --------- Co-authored-by: Martin Kavalar <[email protected]>
1 parent 70948a2 commit d96a399

File tree

2 files changed

+45
-7
lines changed

2 files changed

+45
-7
lines changed

book.clj

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -618,12 +618,12 @@ v/table-viewer
618618
;; also calls `clerk/mark-preserve-keys`. This tells Clerk to leave
619619
;; the keys of the map as-is.
620620

621-
;; In our `:render-fn`, which is called in the browser, we will receive
622-
;; this map. Note that this is a quoted form, not a function. Clerk
623-
;; will send this form to the browser for evaluation. There it will
624-
;; create a `reagent/atom` that holds the selection state. Lastly,
625-
;; `v/inspect-presented` is a component that takes a `wrapped-value`
626-
;; that ran through `v/present` and show it.
621+
;; In our `:render-fn`, which is called in the browser, we will receive this
622+
;; map. Note that this is a quoted form, not a function. Clerk will send this
623+
;; form to the browser for evaluation. There it will create a `reagent/atom`
624+
;; that holds the selection state. Lastly,
625+
;; `nextjournal.clerk.render/inspect-presented` is a component that takes a
626+
;; `wrapped-value` that ran through `v/present` and show it.
627627

628628
(def literal-viewer
629629
{:pred sicmutils.expression/literal?
@@ -749,6 +749,44 @@ v/table-viewer
749749
Moving --> Crash
750750
Crash --> [*]")
751751

752+
;; #### 🧙 Evaluator
753+
754+
;; By default, [SCI](https://github.com/babashka/sci) is used for evaluating `:render-fn` functions in the browser.
755+
756+
;; What follows is an intentionally inefficient but fun way to compute
757+
;; the nth fibonacci number and show how long it took.
758+
759+
(def fib-viewer
760+
{:render-fn '(fn [n opts]
761+
(reagent.core/with-let
762+
[fib (fn fib [x]
763+
(if (< x 2)
764+
1
765+
(+ (fib (dec x)) (fib (dec (dec x))))))
766+
time-before (js/performance.now)
767+
nth-fib (fib n)
768+
time-after (js/performance.now)]
769+
[:div
770+
[:p
771+
(if (= :cherry (-> opts :viewer :render-evaluator))
772+
"Cherry"
773+
"SCI")
774+
" computed the " n "th fibonacci number (" nth-fib ")"
775+
" in " (js/Math.ceil (- time-after time-before) 2) "ms."]]))})
776+
777+
(clerk/with-viewer fib-viewer 25)
778+
779+
;; You can opt into [cherry](https://github.com/squint-cljs/cherry) as an
780+
;; alternative evaluator by setting `{::clerk/render-evaluator :cherry}` via the
781+
;; viewers opts (see [Customizations](#customizations)). The main difference between cherry and SCI
782+
;; for viewer functions is performance. For performance-sensitive code cherry is
783+
;; better suited since it compiles directly to JavaScript code.
784+
785+
(clerk/with-viewer fib-viewer {::clerk/render-evaluator :cherry} 25)
786+
787+
#_(clerk/halt!)
788+
#_(clerk/serve! {:port 7777})
789+
752790
;; ## ⚙️ Customizations
753791

754792
;; Clerk allows easy customization of visibility, result width and

render/deps.edn

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88
io.github.nextjournal/clojure-mode {:git/sha "ac038ebf6e5da09dd2b8a31609e9ff4a65e36852"}
99
thheller/shadow-cljs {:mvn/version "2.23.1"}
1010
io.github.squint-cljs/cherry {;; :local/root "/Users/borkdude/dev/cherry"
11-
:git/sha "ac27030016e5ae8f5280a62eaf81dfe0dc83924b"}}}
11+
:git/sha "ac89d93f136ee8fab91f62949de5b5822ba08b3c"}}}

0 commit comments

Comments
 (0)