Make babashka-compatible

This commit is contained in:
Michiel Borkent 2022-03-13 16:46:24 +01:00
parent d0d6fdf581
commit 8ba809a2cd
3 changed files with 70 additions and 24 deletions

3
deps.edn Normal file
View file

@ -0,0 +1,3 @@
;; deps.edn to use this library as a git dependency for babashka
{:paths ["src/clj"]
:deps {riddley/riddley {:mvn/version "0.1.12"}}}

View file

@ -15,10 +15,11 @@
#?(:clj [clojure.pprint :as pp])
[clojure.string :as s]
[clojure.walk :as walk]
#?(:clj [riddley.walk :as riddley]))
#?(:clj (:import [com.rpl.specter Util MutableCell])))
#?(:bb [clojure.walk :as riddley]
:clj [riddley.walk :as riddley]))
#?@(:bb []
:clj [(:import [com.rpl.specter Util MutableCell])]))
(def NONE ::NONE)
@ -82,7 +83,9 @@
(defn intern* [ns name val]
(throw (ex-info "intern not supported in ClojureScript" {}))))
#?(
#?(:bb
(defmacro fast-object-array [i]
`(object-array ~i))
:clj
(defmacro fast-object-array [i]
`(com.rpl.specter.Util/makeObjectArray ~i)))
@ -101,7 +104,8 @@
(if (= platform :cljs)
`(p/select* ~this ~@args)
`(let [~hinted ~this]
(.select* ~hinted ~@args)))))
(#?(:bb p/select*
:clj .select*) ~hinted ~@args)))))
:cljs
(defn exec-select* [this vals structure next-fn]
(p/select* ^not-native this vals structure next-fn)))
@ -115,7 +119,8 @@
(if (= platform :cljs)
`(p/transform* ~this ~@args)
`(let [~hinted ~this]
(.transform* ~hinted ~@args)))))
(#?(:bb p/transform*
:clj .transform*) ~hinted ~@args)))))
:cljs
(defn exec-transform* [this vals structure next-fn]
@ -214,13 +219,19 @@
(set_cell [cell x])))
#?(:cljs
#?(:bb
(defrecord MutableCell [x])
:cljs
(deftype MutableCell [^:volatile-mutable q]
PMutableCell
(set_cell [this x] (set! q x))))
#?(
#?(:bb
(defn mutable-cell
([] (mutable-cell nil))
([v] (MutableCell. (volatile! v))))
:clj
(defn mutable-cell
([] (mutable-cell nil))
@ -232,7 +243,10 @@
([init] (MutableCell. init))))
#?(
#?(:bb
(defn set-cell! [^MutableCell c v]
(vreset! (:x c) v))
:clj
(defn set-cell! [^MutableCell c v]
(.set c v))
@ -242,7 +256,10 @@
(set_cell cell val)))
#?(
#?(:bb
(defn get-cell [^MutableCell c]
@(:x c))
:clj
(defn get-cell [^MutableCell c]
(.get c))
@ -290,8 +307,10 @@
(defn do-compiled-traverse* [apath structure]
(reify #?(:clj clojure.lang.IReduce :cljs cljs.core/IReduce)
(#?(:clj reduce :cljs -reduce)
[this afn]
(#?(:clj .reduce :cljs -reduce) this afn (afn)))
[this afn]
#?(:bb (reduce afn (afn) this)
:default
(#?(:clj .reduce :cljs -reduce) this afn (afn))))
(#?(:clj reduce :cljs -reduce)
[this afn start]
(let [cell (mutable-cell start)]
@ -308,6 +327,9 @@
))))
#?(
:bb
(defn- call-reduce-interface [^clojure.lang.IReduce traverser afn start]
(reduce afn start traverser))
:clj
(defn- call-reduce-interface [^clojure.lang.IReduce traverser afn start]
(.reduce traverser afn start)
@ -322,8 +344,10 @@
(let [traverser (do-compiled-traverse* apath structure)]
(reify #?(:clj clojure.lang.IReduce :cljs cljs.core/IReduce)
(#?(:clj reduce :cljs -reduce)
[this afn]
(#?(:clj .reduce :cljs -reduce) this afn (afn)))
[this afn]
#?(:bb (reduce afn (afn) this)
:default
(#?(:clj .reduce :cljs -reduce) this afn (afn))))
(#?(:clj reduce :cljs -reduce)
[this afn start]
(let [res (call-reduce-interface traverser afn start)]

View file

@ -8,7 +8,8 @@
#?(:clj (:use [com.rpl.specter.macros :only [defnav defrichnav]]
[com.rpl.specter.util-macros :only [doseqres]]))
(:require [com.rpl.specter.impl :as i]
#?(:clj [clojure.core.reducers :as r])))
#?@(:bb []
:clj [[clojure.core.reducers :as r]])))
(defn not-selected?*
@ -103,7 +104,10 @@
structure))
#?(:clj clojure.lang.PersistentArrayMap)
#?(:clj
#?(:bb
(all-transform [structure next-fn]
(non-transient-map-all-transform structure next-fn {}))
:clj
(all-transform [structure next-fn]
(let [k-it (.keyIterator structure)
v-it (.valIterator structure)
@ -185,10 +189,13 @@
:else
(->> structure
(r/map next-fn)
(r/filter not-NONE?)
(into empty-structure))))))
#?(:bb (into empty-structure
(comp (map next-fn) (filter not-NONE?))
structure)
:clj (->> structure
(r/map next-fn)
(r/filter not-NONE?)
(into empty-structure)))))))
#?(:cljs default)
@ -255,7 +262,10 @@
#?(:clj clojure.lang.PersistentArrayMap)
#?(:clj
#?(:bb
(map-vals-transform [structure next-fn]
(map-vals-non-transient-transform structure {} next-fn))
:clj
(map-vals-transform [structure next-fn]
(let [k-it (.keyIterator structure)
v-it (.valIterator structure)
@ -282,7 +292,10 @@
array
)]
(clojure.lang.PersistentArrayMap. array)))))
#?(:clj
#?(:bb
(map-keys-transform [structure next-fn]
(map-keys-non-transient-transform structure {} next-fn))
:clj
(map-keys-transform [structure next-fn]
(let [k-it (.keyIterator structure)
v-it (.valIterator structure)
@ -505,7 +518,10 @@
structure
(updater structure next-fn))))
#?(
#?(:bb
(defn vec-count [v]
(count v))
:clj
(defn vec-count [^clojure.lang.IPersistentVector v]
(.length v))
@ -557,7 +573,10 @@
(assoc v i newe))))))
#?(
#?(:bb
(defn transient-vec-count [v]
(count v))
:clj
(defn transient-vec-count [^clojure.lang.ITransientVector v]
(.count v))