@@ -618,12 +618,12 @@ v/table-viewer
618
618
; ; also calls `clerk/mark-preserve-keys`. This tells Clerk to leave
619
619
; ; the keys of the map as-is.
620
620
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.
627
627
628
628
(def literal-viewer
629
629
{:pred sicmutils.expression/literal?
@@ -749,6 +749,44 @@ v/table-viewer
749
749
Moving --> Crash
750
750
Crash --> [*]" )
751
751
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
+
752
790
; ; ## ⚙️ Customizations
753
791
754
792
; ; Clerk allows easy customization of visibility, result width and
0 commit comments