Skip to content

Commit b66cd76

Browse files
committed
Merge pull request schani#24 from jolby/add_imeta_and_other_fns
Added more core fns ported from JS impl
2 parents 24470f6 + 6ef1360 commit b66cd76

File tree

4 files changed

+68
-7
lines changed

4 files changed

+68
-7
lines changed

src/cljc/cljc/core.cljc

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,10 @@ reduces them without incurring seq initialization"
848848
false
849849
(satisfies? ISet x)))
850850

851+
(defn ^boolean associative?
852+
"Returns true if coll implements Associative"
853+
[x] (satisfies? IAssociative x))
854+
851855
(defn ^boolean sequential?
852856
"Returns true if coll satisfies ISequential"
853857
[x] (satisfies? ISequential x))
@@ -867,6 +871,22 @@ reduces them without incurring seq initialization"
867871
(defn ^boolean chunked-seq?
868872
[x] (satisfies? IChunkedSeq x))
869873

874+
(defn ^boolean distinct?
875+
"Returns true if no two of the arguments are ="
876+
([x] true)
877+
([x y] (not (= x y)))
878+
([x y & more]
879+
(if (not (= x y))
880+
(loop [s #{x y} xs more]
881+
(let [x (first xs)
882+
etc (next xs)]
883+
(if xs
884+
(if (contains? s x)
885+
false
886+
(recur (conj s x) etc))
887+
true)))
888+
false)))
889+
870890
(defn- accumulating-seq-count [coll]
871891
(loop [s (seq coll) acc 0]
872892
(if (counted? s) ; assumes nil is counted, which it currently is
@@ -935,6 +955,14 @@ reduces them without incurring seq initialization"
935955
([o k not-found]
936956
(-lookup o k not-found)))
937957

958+
(defn find
959+
"Returns the map entry for key, or nil if key not present."
960+
[coll k]
961+
(when (and coll
962+
(associative? coll)
963+
(contains? coll k))
964+
[k (-lookup coll k)]))
965+
938966
(defn assoc
939967
"assoc[iate]. When applied to a map, returns a new map of the
940968
same (hashed/sorted) type, that contains the mapping of key(s) to
@@ -960,6 +988,31 @@ reduces them without incurring seq initialization"
960988
(recur ret (first ks) (next ks))
961989
ret))))
962990

991+
(defn with-meta
992+
"Returns an object of the same type and value as obj, with
993+
map m as its metadata."
994+
[o meta]
995+
(-with-meta o meta))
996+
997+
(defn meta
998+
"Returns the metadata of obj, returns nil if there is no metadata."
999+
[o]
1000+
(when (satisfies? IMeta o)
1001+
(-meta o)))
1002+
1003+
(defn peek
1004+
"For a list or queue, same as first, for a vector, same as, but much
1005+
more efficient than, last. If the collection is empty, returns nil."
1006+
[coll]
1007+
(-peek coll))
1008+
1009+
(defn pop
1010+
"For a list or queue, returns a new list/queue without the first
1011+
item, for a vector, returns a new vector without the last item.
1012+
Note - not the same as next/butlast."
1013+
[coll]
1014+
(-pop coll))
1015+
9631016
(defn disj
9641017
"disj[oin]. Returns a new set of the same (hashed/sorted) type, that
9651018
does not contain key(s)."

test/clojurec/core_test.clj

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,8 @@
422422
(recur (rest l)))))
423423
[13 0 10]))
424424
(is (= (core-run '(print (-count nil))) [0]))
425+
(is (= (core-run '(let [v ["A" "B" "C"] vm (with-meta v {:a 33})] (pr ((meta vm) :a))))
426+
[33]))
425427
(is (= (core-run '(pr (filter #(> % 3) (list 1 2 3 4 5))))
426428
['(4 5)]))
427429
(is (= (core-run '(do
@@ -559,7 +561,11 @@
559561
(is (= (core-run '(pr ((set '(1 2 3)) 1)
560562
((set '(1 2 3)) 4)
561563
((set '(1 2 3)) 4 5)))
562-
[1 nil 5]))))
564+
[1 nil 5]))
565+
(is (= (core-run '(pr
566+
(distinct? 1 2 3 1)
567+
(distinct? 1 2 3)))
568+
[false true]))))
563569

564570
(deftest functions
565571
(testing "functions"

test/clojurec/persistent_hash_map_test.clj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
(= (get m2 f2 "NOT FOUND") "Baz")
3232
(= (get m3 2 "NOT FOUND") "V2"))))))
3333

34-
(is (= [100 100 true true false nil false 99 true]
34+
(is (= [100 100 true true true false nil 3 false 99 true]
3535
(clojurec.core/run-expr
3636
'clojurec.core-test true
3737
'(loop [m1 cljc.core.PersistentHashMap/EMPTY
@@ -53,9 +53,11 @@
5353
(pr (count m1)
5454
(count m2)
5555
(= m1 m2)
56+
(associative? m1)
5657
(contains? m1 3)
5758
(contains? m1 333)
5859
(get m1 333)
60+
(first (find m1 3))
5961
(contains? (dissoc m1 3) 3)
6062
(count (dissoc m1 3))
6163
(every? boolean v))))))))))

test/clojurec/vector_test.clj

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@
3838
(core-run '(let [v1 (conj [] 123)
3939
v100 (loop [i 1 v v1]
4040
(if (< i 100) (recur (inc i) (conj v i)) v))]
41-
(print (-peek v1))
42-
(print (count (-pop v1)))
43-
(print (-peek v100))
44-
(print (count (-pop v100)))
41+
(print (peek v1))
42+
(print (count (pop v1)))
43+
(print (peek v100))
44+
(print (count (pop v100)))
4545
(print (count (loop [i 1 v v100]
4646
(if (< i 100)
47-
(recur (inc i) (-pop v))
47+
(recur (inc i) (pop v))
4848
v)))))))))
4949

5050
(testing "PersistentVector assoc"

0 commit comments

Comments
 (0)