Skip to content

Commit

Permalink
1.2.2
Browse files Browse the repository at this point in the history
  • Loading branch information
caumond committed May 21, 2024
1 parent f9dcabd commit 24c50b5
Show file tree
Hide file tree
Showing 12 changed files with 173 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/commit_validation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ jobs:
# this is to fix GIT not liking owner of the checkout dir, See https://github.com/actions/runner/issues/2033
chown -R $(id -u):$(id -g) $PWD
- name: Base image
uses: docker://hephaistox/gha-automaton-core:1.2.1
uses: docker://hephaistox/gha-automaton-core:1.2.2
2 changes: 1 addition & 1 deletion bb.edn
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
;; The file is updated automatically
{:deps {org.clojars.hephaistox/automaton-build #:mvn{:version "1.4.2"}}
{:deps {org.clojars.hephaistox/automaton-build #:mvn{:version "1.4.3"}}
:paths []
:tasks
{:requires
Expand Down
8 changes: 4 additions & 4 deletions deps.edn
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{:aliases
{:bb-deps {:extra-deps {org.clojars.hephaistox/automaton-build
#:mvn{:version "1.4.2"}}}
:build {:extra-deps {org.clojars.hephaistox/automaton-build
#:mvn{:version "1.4.2"}}}
{:bb-deps {:extra-deps {org.clojars.hephaistox/automaton-build #:mvn{:version
"1.4.3"}}}
:build {:extra-deps {org.clojars.hephaistox/automaton-build #:mvn{:version
"1.4.3"}}}
:cljs-deps {:extra-deps {binaryage/devtools #:mvn{:version "1.0.7"}
com.yetanalytics/colossal-squuid {:mvn/version
"0.1.5"}
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<packaging>jar</packaging>
<groupId>org.clojars.hephaistox</groupId>
<artifactId>automaton-core</artifactId>
<version>1.2.1</version>
<version>1.2.2</version>
<name>automaton-core</name>
<dependencies>
<dependency>
Expand Down
17 changes: 17 additions & 0 deletions src/clj/automaton_core/utils/call_limit.cljc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
(ns automaton-core.utils.call-limit "Limit the number of calls of a function")

(def ^:private n "Number of call per key" (atom {}))

(defn allow-one-only-call
"For key `k`, allow one call only of function `f`.
Following calls will throw an exception."
[f k]
(if (pos? (get @n k 0))
(throw (ex-info (str "More than one call to " k " detected") {}))
(do (swap! n update k (fnil inc 0)) (f))))

(defn remove-fn-call
"Removes a call so next `allow-one-only-call` will be successful.
Use it when the previous call scope of `allow-one-only-call` is lost. Especially useful for tests."
[k]
(when (pos? (get @n k 0)) (swap! n update k dec)))
28 changes: 28 additions & 0 deletions src/cljc/automaton_core/utils/map.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,31 @@
(->> m
(map (fn [[k v]] [k (get translation v k)]))
(into {})))

(defn get-key-or-before
"Returns the key if it exists in the sorted-map
Note this is not an efficient way
Params:
* `m` the sorted map
* `n`"
[^clojure.lang.PersistentTreeMap m n]
(->> m
keys
(take-while (partial >= n))
last))

(defn get-key-or-after
"Returns the key if it exists in the sorted-map
Note this is not an efficient way
Params:
* `m` the sorted map
* `n`"
[^clojure.lang.PersistentTreeMap m n]
(->> m
keys
(take-while (partial <= n))
last))
18 changes: 18 additions & 0 deletions src/cljc/automaton_core/utils/numbers.cljc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
(ns automaton-core.utils.numbers
"Helpers for manipulating numbers in the same way between clj and cljs")

(defn check-val-in-range
"Returns `nil` if `val` is in the range `[min;max[`.
Otherwise, returns `val` itself."
[min max val]
(cond
(> min val) val
(<= max val) val
:else nil))


(defn check-vals-in-range
"Check if `vals` are between the range of integers (i.e. `[min;max[`.
Returns the value if it exceeds the range."
[min max vals]
(filter (partial check-val-in-range min max) vals))
14 changes: 14 additions & 0 deletions src/cljc/automaton_core/utils/sequences.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,17 @@
* `pred` pred is a function returning true when called on the element to return the sequence of"
[coll pred]
(let [idx? (fn [i a] (when (pred a) i))] (first (keep-indexed idx? coll))))

(defn position-by-values
"Returns a map which associates each value of the vector to the positions where it happens.
Params:
* `v`"
[v]
(reduce (fn [res i]
(update res
(nth v i)
(fn [previous]
(if (empty? previous) [i] (conj previous i)))))
{}
(range (count v))))
39 changes: 39 additions & 0 deletions test/cljc/automaton_core/utils/map_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -233,3 +233,42 @@
(is (= {} (sut/translate-keys nil {})))
(is (= {} (sut/translate-keys {} nil)))
(is (= {} (sut/translate-keys nil nil)))))

(deftest get-key-or-before-test
(testing "Test if latest key is returned, even if doesn't exist"
(is (nil? (sut/get-key-or-before (into (sorted-map)
{1 :a
32 :b
2 :c})
0)))
(is (= 1
(sut/get-key-or-before (into (sorted-map)
{1 :a
32 :b
2 :c})
1)))
(is (= 1
(sut/get-key-or-before (into (sorted-map)
{1 :a
32 :b
2 :c})
1.3)))
(is (= 2
(sut/get-key-or-before (into (sorted-map)
{1 :a
32 :b
2 :c})
2)))
(is (= 2
(sut/get-key-or-before (into (sorted-map)
{1 :a
32 :b
2 :c})
30)))
(is (= 32
(sut/get-key-or-before (into (sorted-map)
{1 :a
32 :b
2 :c})
32)))
(is (nil? (sut/get-key-or-before (into (sorted-map) {}) 32)))))
26 changes: 26 additions & 0 deletions test/cljc/automaton_core/utils/numbers_test.cljc
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
(ns automaton-core.utils.numbers-test
(:require
[automaton-core.utils.numbers :as sut]
#?@(:cljs [[cljs.test :refer-macros [testing is deftest]]]
:clj [[clojure.test :refer [testing is deftest]]])))

(deftest check-val-in-range-test
(testing "In the range integer are accepted"
(is (nil? (sut/check-val-in-range 1 100 10)))
(is (nil? (sut/check-val-in-range 10 100 10))))
(testing "In the range floats are accepted"
(is (nil? (sut/check-val-in-range 1.0 100.0 10.0)))
(is (nil? (sut/check-val-in-range 10.0 100.0 10.0))))
(testing "Out of range integer are accepted"
(is (= -1 (sut/check-val-in-range 0 100 -1)))
(is (= 100 (sut/check-val-in-range 1 100 100)))
(is (= 0 (sut/check-val-in-range 1 100 0))))
(testing "Out of range floats are accepted"
(is (= 100.0 (sut/check-val-in-range 1.0 100.0 100.0)))
(is (= 0.0 (sut/check-val-in-range 1.0 100.0 0.0)))
(is (= 110.0 (sut/check-val-in-range 10.0 100.0 110.0)))))


(deftest check-vals-in-range-test
(testing "In the range integer are accepted"
(is (empty? (sut/check-vals-in-range 1 100 [10 1 99])))))
23 changes: 23 additions & 0 deletions test/cljc/automaton_core/utils/sequences_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,26 @@
(is (= 3 (sut/index-of [1 2 3 :foo] #{:foo}))))
(testing "Element not found in the sequence"
(is (nil? (sut/index-of [1 2 3] :foo)))))

(deftest position-by-values-test
(testing "Non vector are ok"
(is (= {1 [0]
2 [1]
3 [2]}
(sut/position-by-values '(1 2 3)))))
(testing "Empty vectors"
(is (= {} (sut/position-by-values [])))
(is (= {} (sut/position-by-values nil))))
(testing "Return vectors"
(is (every? vector?
(-> (sut/position-by-values [1 2 3 1 1 2])
vals))))
(testing "Happy path"
(is (= {1 [0 3 4]
2 [1 5]
3 [2]}
(sut/position-by-values [1 2 3 1 1 2])))
(is (= {1 [0]
2 [1]
3 [2]}
(sut/position-by-values [1 2 3])))))
2 changes: 1 addition & 1 deletion version.edn
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
;; Last generated version, note a failed push consume a number
{:version "1.2.1"}
{:version "1.2.2"}

0 comments on commit 24c50b5

Please sign in to comment.