Skip to content

Instantly share code, notes, and snippets.

Avatar

Mike Fikes mfikes

View GitHub Profile
@mfikes
mfikes / gist:3a160a1504debd31e5771736256ca022
Last active Sep 8, 2019
Serializing CLJS Bean instances via transit preserving meta
View gist:3a160a1504debd31e5771736256ca022
$ clj -Sdeps '{:deps {org.clojure/clojurescript {:mvn/version "1.10.520"} com.cognitect/transit-cljs {:mvn/version "0.8.256"} com.cognitect/transit-js {:mvn/version "0.8.861"} cljs-bean {:mvn/version "1.4.0"}}}' -m cljs.main -re node -r
ClojureScript 1.10.520
(require '[cognitect.transit :as t]
         '[cljs-bean.core :refer [->clj]]
         '[cljs-bean.transit])
nil
cljs.user=> (defn roundtrip [x]
              (let [w (t/writer :json
                        {:handlers (cljs-bean.transit/writer-handlers)
View direct-field-access.txt
cljs.user=> (set! *print-fn-bodies* true)
true
cljs.user=> (defrecord Foo [x])
cljs.user/Foo
cljs.user=> (defn bar [^cljs.user/Foo foo] #(:y foo))
#'cljs.user/bar
cljs.user=> (bar (assoc (->Foo 1) :y 3))
#object[Function "function (){
return new cljs.core.Keyword(null,"y","y",-1757859776).cljs$core$IFn$_invoke$arity$1(foo);
}"]
View compare.txt
$ plk -Sdeps '{:deps {cljs-bean {:mvn/version "0.2.0"}}}'
ClojureScript 1.10.520
cljs.user=> (simple-benchmark [o #js {:a 1 :b 2 :c 3}]
#_=> (let [{:keys [a b c]} (js->clj o :keywordize-keys true)]
#_=> (+ a b c))
#_=> 1e6)
[o #object[cljs.tagged-literals.JSValue]], (let [{:keys [a b c]} (js->clj o :keywordize-keys true)] (+ a b c)), 1000000 runs, 3077 msecs
nil
cljs.user=> (require '[cljs-bean.core :refer [bean]])
nil
View perf.md
$ cat src/foo/core.cljs
(ns foo.core)

(prn (time (reduce + (map inc (map inc (range (* 1024 1024)))))))
$ clj -Sdeps '{:deps {org.clojure/clojurescript {:mvn/version "1.10.516"}}}' -m cljs.main -O advanced -c foo.core
$ node out/main.js
View bar.cljs
(ns foo.bar
(:require foo.core)
#_(:require-macros foo.core))
(defn try-me []
(foo.core/m))
View core.clj
(ns foo.core)
(defmacro m []
`(foo.core/f))
View wrap-fn-object.txt
cljs.user=> (def my-inc (with-meta inc {:a 2}))
#'cljs.user/my-inc
cljs.user=> (my-inc 17)
18
cljs.user=> (js/cljs.user.my_inc 17)
cljs.user.my_inc is not a function. (In 'cljs.user.my_inc((17))', 'cljs.user.my_inc' is an instance of MetaFn)
cljs.user=> (def my-inc' (fn [x] (my-inc x)))
#'cljs.user/my-inc'
cljs.user=> (js/cljs.user.my_inc_SINGLEQUOTE_ 17)
View foo.txt
ClojureScript 1.10.439
cljs.user=> (defprotocol IFoo
(delete [_]))
false
cljs.user=> (defrecord Foo []
IFoo
(delete [_] 1))
cljs.user/Foo
cljs.user=> (delete (->Foo))
1
View inf.md

Old behavior:

cljs.user=> (defn f [x] (when (even? x) (inc x)))
#'cljs.user/f
cljs.user=> (let [x (f 2)] (if x (+ x "a")))
                                 ^
WARNING: cljs.core/+, all arguments must be numbers, got [#{number clj-nil} string] instead at line 1
"3a"
View ex-data.txt
ClojureScript 1.10.439
cljs.user=> (require '[clojure.spec.alpha :as s])
nil
cljs.user=> (require '[clojure.spec.test.alpha :as st])
nil
cljs.user=> (defn foo [x])
#'cljs.user/foo
cljs.user=> (s/fdef foo :args (s/cat :x int?))
cljs.user/foo
cljs.user=> (st/instrument)