Skip to content

Commit 2e9c132

Browse files
committed
Added my 4 most useful functions to the libary.
0 parents  commit 2e9c132

File tree

7 files changed

+135
-0
lines changed

7 files changed

+135
-0
lines changed

.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.cljs_node_repl/
2+
.cpcache/
3+
.idea/
4+
.nrepl-port
5+
*.iml
6+
node_modules
7+
out/
8+
*.jar
9+
pom.xml
10+
11+
package-lock.json

bin/kaocha

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/usr/bin/env bash
2+
clojure -M:test -m kaocha.runner "$@"

deps.edn

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{:paths ["src"]
2+
:deps {} ;; This part intentionally left blank
3+
:aliases {:dev {:extra-deps {org.clojure/clojure {:mvn/version "1.10.3"}
4+
org.clojure/clojurescript {:mvn/version "1.11.4"}}}
5+
:test {:extra-paths ["test"]
6+
:extra-deps {lambdaisland/kaocha {:mvn/version "1.63.998"}
7+
lambdaisland/kaocha-cljs {:mvn/version "1.0.107"}
8+
lambdaisland/kaocha-junit-xml {:mvn/version "0.0.76"}
9+
org.clojure/test.check {:mvn/version "1.1.1"}}}
10+
11+
; clojure -M:outdated --upgrade
12+
:outdated {:extra-deps {com.github.liquidz/antq {:mvn/version "1.5.1"}}
13+
:main-opts ["-m" "antq.core"]}
14+
15+
:depstar {:replace-deps {com.github.seancorfield/depstar {:mvn/version "2.1.303"}}
16+
:exec-fn hf.depstar/jar
17+
:exec-args {:sync-pom true
18+
:group-id "taipei.404"
19+
:artifact-id "mate"
20+
:version "0.0.1"
21+
:jar "mate.jar"}}}}
22+
;; Memo for deploying a new release:
23+
;; - Change the version above, then build the jar:
24+
;; clojure -X:depstar
25+
;; - add a tag "v0.x.y" to the latest commit and push to repo
26+
;; - deploy:
27+
;; mvn deploy:deploy-file -Dfile=mate.jar -DpomFile=pom.xml -DrepositoryId=clojars -Durl=https://clojars.org/repo/

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"devDependencies": {
3+
"isomorphic-ws": "^4.0.1",
4+
"ws": "^7.0.1"
5+
}
6+
}

src/mate/core.cljc

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
(ns mate.core
2+
#?(:cljs (:require-macros mate.core))
3+
(:refer-clojure :exclude [group-by])
4+
(:require [clojure.core :as cc]))
5+
6+
(defn seq-indexed
7+
"Returns an indexed sequence from the collection `coll`."
8+
[coll]
9+
(map-indexed vector coll))
10+
11+
(defmacro comp->
12+
"Same as `comp` but with the arguments in reverse order."
13+
[& args]
14+
`(comp ~@(reverse args)))
15+
16+
(defn group-by
17+
"Same as clojure.core/group-by, but with some handy new arities which apply
18+
custom map & reduce operations to the elements grouped together under the same key."
19+
([kf coll]
20+
;(group-by kf identity conj [] coll)
21+
(cc/group-by kf coll))
22+
([kf vf coll]
23+
(group-by kf vf conj [] coll))
24+
([kf vf rf coll]
25+
(group-by kf vf rf (rf) coll))
26+
([kf vf rf init coll]
27+
(->> coll
28+
(reduce (fn [ret x]
29+
(let [k (kf x)
30+
v (vf x)]
31+
(assoc! ret k (rf (get ret k init) v))))
32+
(transient {}))
33+
persistent!)))
34+
35+
(defn index-by
36+
"Returns a hashmap made of `[key value]` pairs from items in the collection `coll`
37+
where the keys are `(kf item)` and the values are `(vf item)`.
38+
`vf` defaults to the identify function."
39+
([kf coll]
40+
(index-by kf identity coll))
41+
([kf vf coll]
42+
(->> coll
43+
(reduce (fn [ret x]
44+
(assoc! ret (kf x) (vf x)))
45+
(transient {}))
46+
persistent!)))

test/mate/core_test.cljc

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
(ns mate.core-test
2+
(:require [clojure.test :refer [deftest testing is are]]
3+
[mate.core :as m]))
4+
5+
(deftest seq-index-test
6+
(is (= (m/seq-indexed [:a :a :b :a :b])
7+
'([0 :a] [1 :a] [2 :b] [3 :a] [4 :b]))))
8+
9+
(deftest comp->-test
10+
(is (= ((m/comp-> inc str) 2) "3")))
11+
12+
(deftest group-by-test
13+
(let [coll [[:a 1] [:a 2] [:b 3] [:a 4] [:b 5]]]
14+
(is (= (m/group-by first coll)
15+
{:a [[:a 1] [:a 2] [:a 4]]
16+
:b [[:b 3] [:b 5]]}))
17+
18+
(is (= (m/group-by first second coll)
19+
{:a [1 2 4]
20+
:b [3 5]}))
21+
22+
(is (= (m/group-by first second + coll)
23+
{:a 7
24+
:b 8}))
25+
26+
(is (= (m/group-by first second + 10 coll)
27+
{:a 17
28+
:b 18}))))
29+
30+
(deftest index-by-test
31+
(let [coll [[:a 1] [:a 2] [:b 3] [:a 4] [:b 5]]]
32+
(is (= (m/index-by first coll)
33+
{:a [:a 4]
34+
:b [:b 5]}))
35+
36+
(is (= (m/index-by first second coll)
37+
{:a 4
38+
:b 5}))))

tests.edn

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#kaocha/v1
2+
{:tests [{:id :unit
3+
:type :kaocha.type/clojure.test}
4+
{:id :unit-cljs
5+
:type :kaocha.type/cljs}]}

0 commit comments

Comments
 (0)