Make babashka-compatible (#323)

This commit is contained in:
Michiel Borkent 2022-03-14 18:11:40 +01:00 committed by GitHub
parent d0d6fdf581
commit a64209d582
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 130 additions and 33 deletions

37
.github/workflows/ci.yml vendored Normal file
View file

@ -0,0 +1,37 @@
name: CI
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
fetch_depth: 0
- name: Setup Babashka
uses: turtlequeue/setup-babashka@v1.3.0
with:
babashka-version: 0.7.8
- name: Prepare java
uses: actions/setup-java@v2
with:
distribution: 'zulu'
java-version: '8'
- name: Install clojure tools
uses: DeLaGuardo/setup-clojure@4.0
with:
lein: 2.9.1
- name: Run clj tests
run: bb test:clj
- name: Run cljs tests
run: bb test:cljs
- name: Run babashka tests
run: bb test:bb

2
.gitignore vendored
View file

@ -11,3 +11,5 @@ pom.xml.asc
.lein-failures .lein-failures
.cljs_node_repl .cljs_node_repl
out/ out/
.cpcache
.cache

View file

@ -1,8 +0,0 @@
language: clojure
lein: 2.9.1
cache:
directories:
- $HOME/.m2
script:
- lein test
- lein doo node test-build once

View file

@ -1,4 +1,4 @@
# Specter [![Build Status](https://secure.travis-ci.org/redplanetlabs/specter.png?branch=master)](http://travis-ci.org/redplanetlabs/specter) # Specter
Specter rejects Clojure's restrictive approach to immutable data structure manipulation, instead exposing an elegant API to allow any sort of manipulation imaginable. Specter especially excels at querying and transforming nested and recursive data, important use cases that are very complex to handle with vanilla Clojure. Specter rejects Clojure's restrictive approach to immutable data structure manipulation, instead exposing an elegant API to allow any sort of manipulation imaginable. Specter especially excels at querying and transforming nested and recursive data, important use cases that are very complex to handle with vanilla Clojure.
@ -334,6 +334,10 @@ When using Specter in a project with [clj-kondo](https://github.com/clj-kondo/cl
com.rpl.specter/defrichnav clojure.core/defn}} com.rpl.specter/defrichnav clojure.core/defn}}
``` ```
# Babashka
This library is compatible with [babashka](https://babashka.org/) as of specter 1.1.4 and babashka 0.7.8.
# License # License
Copyright 2015-2020 Red Planet Labs, Inc. Specter is licensed under Apache License v2.0. Copyright 2015-2020 Red Planet Labs, Inc. Specter is licensed under Apache License v2.0.

19
bb.edn Normal file
View file

@ -0,0 +1,19 @@
{:paths ["src/clj"]
:tasks
{test:clj {:doc "Run clj tests with leiningen"
:task (shell "lein test")}
test:cljs {:doc "Run cljs tests with leiningen"
:task (shell "lein doo node test-build once")}
test:bb {:doc "Run bb tests"
:extra-paths ["test"]
:extra-deps {org.clojure/test.check {:mvn/version "0.9.0"}
io.github.cognitect-labs/test-runner
{:git/tag "v0.5.0" :git/sha "b3fd0d2"}
org.clojure/tools.namespace {:git/url "https://github.com/babashka/tools.namespace"
:git/sha "3625153ee66dfcec2ba600851b5b2cbdab8fae6c"}}
:requires ([cognitect.test-runner :as tr])
:task (apply tr/-main
"-d" "test"
*command-line-args*)}}}

View file

@ -15,10 +15,11 @@
#?(:clj [clojure.pprint :as pp]) #?(:clj [clojure.pprint :as pp])
[clojure.string :as s] [clojure.string :as s]
[clojure.walk :as walk] [clojure.walk :as walk]
#?(:clj [riddley.walk :as riddley])) #?(:bb [clojure.walk :as riddley]
:clj [riddley.walk :as riddley]))
#?(:clj (:import [com.rpl.specter Util MutableCell])))
#?@(:bb []
:clj [(:import [com.rpl.specter Util MutableCell])]))
(def NONE ::NONE) (def NONE ::NONE)
@ -82,7 +83,9 @@
(defn intern* [ns name val] (defn intern* [ns name val]
(throw (ex-info "intern not supported in ClojureScript" {})))) (throw (ex-info "intern not supported in ClojureScript" {}))))
#?( #?(:bb
(defmacro fast-object-array [i]
`(object-array ~i))
:clj :clj
(defmacro fast-object-array [i] (defmacro fast-object-array [i]
`(com.rpl.specter.Util/makeObjectArray ~i))) `(com.rpl.specter.Util/makeObjectArray ~i)))
@ -101,7 +104,8 @@
(if (= platform :cljs) (if (= platform :cljs)
`(p/select* ~this ~@args) `(p/select* ~this ~@args)
`(let [~hinted ~this] `(let [~hinted ~this]
(.select* ~hinted ~@args))))) (#?(:bb p/select*
:clj .select*) ~hinted ~@args)))))
:cljs :cljs
(defn exec-select* [this vals structure next-fn] (defn exec-select* [this vals structure next-fn]
(p/select* ^not-native this vals structure next-fn))) (p/select* ^not-native this vals structure next-fn)))
@ -115,7 +119,8 @@
(if (= platform :cljs) (if (= platform :cljs)
`(p/transform* ~this ~@args) `(p/transform* ~this ~@args)
`(let [~hinted ~this] `(let [~hinted ~this]
(.transform* ~hinted ~@args))))) (#?(:bb p/transform*
:clj .transform*) ~hinted ~@args)))))
:cljs :cljs
(defn exec-transform* [this vals structure next-fn] (defn exec-transform* [this vals structure next-fn]
@ -214,13 +219,19 @@
(set_cell [cell x]))) (set_cell [cell x])))
#?(:cljs #?(:bb
(defrecord MutableCell [x])
:cljs
(deftype MutableCell [^:volatile-mutable q] (deftype MutableCell [^:volatile-mutable q]
PMutableCell PMutableCell
(set_cell [this x] (set! q x)))) (set_cell [this x] (set! q x))))
#?( #?(:bb
(defn mutable-cell
([] (mutable-cell nil))
([v] (MutableCell. (volatile! v))))
:clj :clj
(defn mutable-cell (defn mutable-cell
([] (mutable-cell nil)) ([] (mutable-cell nil))
@ -232,7 +243,10 @@
([init] (MutableCell. init)))) ([init] (MutableCell. init))))
#?( #?(:bb
(defn set-cell! [^MutableCell c v]
(vreset! (:x c) v))
:clj :clj
(defn set-cell! [^MutableCell c v] (defn set-cell! [^MutableCell c v]
(.set c v)) (.set c v))
@ -242,7 +256,10 @@
(set_cell cell val))) (set_cell cell val)))
#?( #?(:bb
(defn get-cell [^MutableCell c]
@(:x c))
:clj :clj
(defn get-cell [^MutableCell c] (defn get-cell [^MutableCell c]
(.get c)) (.get c))
@ -290,8 +307,10 @@
(defn do-compiled-traverse* [apath structure] (defn do-compiled-traverse* [apath structure]
(reify #?(:clj clojure.lang.IReduce :cljs cljs.core/IReduce) (reify #?(:clj clojure.lang.IReduce :cljs cljs.core/IReduce)
(#?(:clj reduce :cljs -reduce) (#?(:clj reduce :cljs -reduce)
[this afn] [this afn]
(#?(:clj .reduce :cljs -reduce) this afn (afn))) #?(:bb (reduce afn (afn) this)
:default
(#?(:clj .reduce :cljs -reduce) this afn (afn))))
(#?(:clj reduce :cljs -reduce) (#?(:clj reduce :cljs -reduce)
[this afn start] [this afn start]
(let [cell (mutable-cell 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 :clj
(defn- call-reduce-interface [^clojure.lang.IReduce traverser afn start] (defn- call-reduce-interface [^clojure.lang.IReduce traverser afn start]
(.reduce traverser afn start) (.reduce traverser afn start)
@ -322,8 +344,10 @@
(let [traverser (do-compiled-traverse* apath structure)] (let [traverser (do-compiled-traverse* apath structure)]
(reify #?(:clj clojure.lang.IReduce :cljs cljs.core/IReduce) (reify #?(:clj clojure.lang.IReduce :cljs cljs.core/IReduce)
(#?(:clj reduce :cljs -reduce) (#?(:clj reduce :cljs -reduce)
[this afn] [this afn]
(#?(:clj .reduce :cljs -reduce) this afn (afn))) #?(:bb (reduce afn (afn) this)
:default
(#?(:clj .reduce :cljs -reduce) this afn (afn))))
(#?(:clj reduce :cljs -reduce) (#?(:clj reduce :cljs -reduce)
[this afn start] [this afn start]
(let [res (call-reduce-interface traverser 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]] #?(:clj (:use [com.rpl.specter.macros :only [defnav defrichnav]]
[com.rpl.specter.util-macros :only [doseqres]])) [com.rpl.specter.util-macros :only [doseqres]]))
(:require [com.rpl.specter.impl :as i] (:require [com.rpl.specter.impl :as i]
#?(:clj [clojure.core.reducers :as r]))) #?@(:bb []
:clj [[clojure.core.reducers :as r]])))
(defn not-selected?* (defn not-selected?*
@ -103,7 +104,10 @@
structure)) structure))
#?(:clj clojure.lang.PersistentArrayMap) #?(: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] (all-transform [structure next-fn]
(let [k-it (.keyIterator structure) (let [k-it (.keyIterator structure)
v-it (.valIterator structure) v-it (.valIterator structure)
@ -185,10 +189,13 @@
:else :else
(->> structure #?(:bb (into empty-structure
(r/map next-fn) (comp (map next-fn) (filter not-NONE?))
(r/filter not-NONE?) structure)
(into empty-structure)))))) :clj (->> structure
(r/map next-fn)
(r/filter not-NONE?)
(into empty-structure)))))))
#?(:cljs default) #?(:cljs default)
@ -255,7 +262,10 @@
#?(:clj clojure.lang.PersistentArrayMap) #?(: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] (map-vals-transform [structure next-fn]
(let [k-it (.keyIterator structure) (let [k-it (.keyIterator structure)
v-it (.valIterator structure) v-it (.valIterator structure)
@ -282,7 +292,10 @@
array array
)] )]
(clojure.lang.PersistentArrayMap. 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] (map-keys-transform [structure next-fn]
(let [k-it (.keyIterator structure) (let [k-it (.keyIterator structure)
v-it (.valIterator structure) v-it (.valIterator structure)
@ -505,7 +518,10 @@
structure structure
(updater structure next-fn)))) (updater structure next-fn))))
#?( #?(:bb
(defn vec-count [v]
(count v))
:clj :clj
(defn vec-count [^clojure.lang.IPersistentVector v] (defn vec-count [^clojure.lang.IPersistentVector v]
(.length v)) (.length v))
@ -557,7 +573,10 @@
(assoc v i newe)))))) (assoc v i newe))))))
#?( #?(:bb
(defn transient-vec-count [v]
(count v))
:clj :clj
(defn transient-vec-count [^clojure.lang.ITransientVector v] (defn transient-vec-count [^clojure.lang.ITransientVector v]
(.count v)) (.count v))