cljs compatible

This commit is contained in:
Nathan Marz 2016-06-07 00:49:52 -04:00
parent 68ac32ef56
commit a4c941b744
3 changed files with 39 additions and 33 deletions

View file

@ -90,6 +90,7 @@
(let [data [1 2 3 4 5 6 7 8 9 10]] (let [data [1 2 3 4 5 6 7 8 9 10]]
(run-benchmark "filter a sequence" 1000000 (run-benchmark "filter a sequence" 1000000
(doall (filter even? data)) (doall (filter even? data))
(filterv even? data)
(select [ALL even?] data) (select [ALL even?] data)
(select-any (filterer even?) data) (select-any (filterer even?) data)
)) ))

View file

@ -7,7 +7,7 @@
) )
(:use [com.rpl.specter.protocols :only (:use [com.rpl.specter.protocols :only
[select* transform* collect-val]] [select* transform* collect-val]]
[com.rpl.specter.util-macros :only [doseqres]] #+clj [com.rpl.specter.util-macros :only [doseqres]]
) )
(:require [com.rpl.specter.protocols :as p] (:require [com.rpl.specter.protocols :as p]
[clojure.walk :as walk] [clojure.walk :as walk]
@ -22,12 +22,6 @@
(def NONE ::NONE) (def NONE ::NONE)
#+clj
(def kw-identical? identical?)
#+cljs
(def kw-identical? keyword-identical?)
(defn spy [e] (defn spy [e]
(println "SPY:") (println "SPY:")
(println (pr-str e)) (println (pr-str e))
@ -590,8 +584,8 @@
;; amazingly doing this as a macro shows a big effect in the ;; amazingly doing this as a macro shows a big effect in the
;; benchmark for getting a value out of a nested map ;; benchmark for getting a value out of a nested map
(defmacro compiled-traverse* #+clj
[path result-fn structure] (defmacro compiled-traverse* [path result-fn structure]
`(let [tfns# (transform-fns-field ~path) `(let [tfns# (transform-fns-field ~path)
ex# (executors-field tfns#)] ex# (executors-field tfns#)]
((traverse-executor-field ex#) ((traverse-executor-field ex#)
@ -602,6 +596,19 @@
~structure) ~structure)
)) ))
#+cljs
(defn compiled-traverse* [path result-fn structure]
(let [tfns (transform-fns-field path)
ex (executors-field tfns)]
((traverse-executor-field ex)
(params-field path)
(params-idx-field path)
(selector-field tfns)
result-fn
structure)
))
(defn compiled-select* [path structure] (defn compiled-select* [path structure]
(let [res (mutable-cell (transient [])) (let [res (mutable-cell (transient []))
result-fn (fn [structure] result-fn (fn [structure]
@ -613,44 +620,44 @@
)) ))
(defn compiled-select-one* [path structure] (defn compiled-select-one* [path structure]
(let [res (mutable-cell ::NOTHING) (let [res (mutable-cell NONE)
result-fn (fn [structure] result-fn (fn [structure]
(let [curr (get-cell res)] (let [curr (get-cell res)]
(if (kw-identical? curr ::NOTHING) (if (identical? curr NONE)
(set-cell! res structure) (set-cell! res structure)
(throw-illegal "More than one element found in structure: " structure) (throw-illegal "More than one element found in structure: " structure)
)))] )))]
(compiled-traverse* path result-fn structure) (compiled-traverse* path result-fn structure)
(let [ret (get-cell res)] (let [ret (get-cell res)]
(if (kw-identical? ret ::NOTHING) (if (identical? ret NONE)
nil nil
ret ret
)))) ))))
(defn compiled-select-one!* [path structure] (defn compiled-select-one!* [path structure]
(let [res (mutable-cell ::NOTHING) (let [res (mutable-cell NONE)
result-fn (fn [structure] result-fn (fn [structure]
(let [curr (get-cell res)] (let [curr (get-cell res)]
(if (kw-identical? curr ::NOTHING) (if (identical? curr NONE)
(set-cell! res structure) (set-cell! res structure)
(throw-illegal "More than one element found in structure: " structure) (throw-illegal "More than one element found in structure: " structure)
)))] )))]
(compiled-traverse* path result-fn structure) (compiled-traverse* path result-fn structure)
(let [ret (get-cell res)] (let [ret (get-cell res)]
(if (kw-identical? ::NOTHING ret) (if (identical? NONE ret)
(throw-illegal "Found no elements for select-one! on " structure)) (throw-illegal "Found no elements for select-one! on " structure))
ret ret
))) )))
(defn compiled-select-first* [path structure] (defn compiled-select-first* [path structure]
(let [res (mutable-cell ::NOTHING) (let [res (mutable-cell NONE)
result-fn (fn [structure] result-fn (fn [structure]
(let [curr (get-cell res)] (let [curr (get-cell res)]
(if (kw-identical? curr ::NOTHING) (if (identical? curr NONE)
(set-cell! res structure))))] (set-cell! res structure))))]
(compiled-traverse* path result-fn structure) (compiled-traverse* path result-fn structure)
(let [ret (get-cell res)] (let [ret (get-cell res)]
(if (kw-identical? ret ::NOTHING) (if (identical? ret NONE)
nil nil
ret ret
)))) ))))
@ -669,7 +676,7 @@
[compiled-path structure] [compiled-path structure]
(->> structure (->> structure
(compiled-select-any* compiled-path) (compiled-select-any* compiled-path)
(kw-identical? NONE))) (identical? NONE)))
(defn selected?* (defn selected?*
[compiled-path structure] [compiled-path structure]
@ -680,7 +687,7 @@
walker (fn this [structure] walker (fn this [structure]
(if (pred structure) (if (pred structure)
(let [r (continue-fn structure)] (let [r (continue-fn structure)]
(if-not (kw-identical? r NONE) (if-not (identical? r NONE)
(set-cell! ret r)) (set-cell! ret r))
r r
) )

View file

@ -1,15 +1,13 @@
(ns com.rpl.specter.util-macros) (ns com.rpl.specter.util-macros)
(defmacro doseqres [backup-res-kw [n aseq] & body] (defmacro doseqres [backup-res [n aseq] & body]
(let [platform (if (contains? &env :locals) :cljs :clj) `(reduce
idfn (if (= platform :clj) 'identical? 'keyword-identical?)] (fn [curr# ~n]
`(reduce (let [ret# (do ~@body)]
(fn [curr# ~n] (if (identical? ret# ~backup-res)
(let [ret# (do ~@body)] curr#
(if (~idfn ret# ~backup-res-kw) ret#
curr# )))
ret# ~backup-res
))) ~aseq
~backup-res-kw ))
~aseq
)))