Map.to-array and Set.to-array functions, equality for non-ref Pairs

This commit is contained in:
Joel Kaasinen 2018-12-07 21:24:36 +02:00
parent 30b9ffbde6
commit 31824bcadb
3 changed files with 48 additions and 1 deletions

View File

@ -225,6 +225,12 @@
(set! m (put m k v))))
m))
(doc to-array "Convert Map to Array of Pairs")
(defn to-array [m]
(kv-reduce (fn [arr k v] (Array.push-back arr (Pair.init-from-refs k v)))
[]
m))
(defn str [m]
(let [res (kv-reduce (fn [s k v]
(String.join @"" &[s @" " (prn @k) @" " (prn @v)]))
@ -343,6 +349,10 @@
(set! init (f init e))))))
init))
(doc to-array "Convert Set to Array of elements")
(defn to-array [s]
(reduce (fn [arr elt] (Array.push-back arr @elt)) [] s))
(defn str [set]
(let [res (reduce (fn [s e] (String.join @"" &[s @" " (prn e)]))
@"{"

View File

@ -4,8 +4,19 @@
(defn init-from-refs [r1 r2]
(Pair.init @r1 @r2))
(defn = [p1 p2]
(and (= (Pair.a &p1) (Pair.a &p2))
(= (Pair.b &p1) (Pair.b &p2))))
(defn /= [p1 p2]
(not (Pair.= p1 p2)))
)
(defmodule PairRef
(defn = [p1 p2]
(and (= (Pair.a p1) (Pair.a p2))
(= (Pair.b p1) (Pair.b p2))))
)
(defn /= [p1 p2]
(not (PairRef.= p1 p2))))

View File

@ -100,6 +100,17 @@
&(str &(Map.from-array &[(Pair.init @"hi" @"bye")]))
"stringification works II"
)
(assert-equal test
&[(Pair.init 1 2)]
&(Map.to-array &(Map.put (Map.create) &1 &2))
"Map.to-array works 1"
)
(assert-equal test
2
(Array.length &(Map.to-array &(Map.from-array &[(Pair.init 1 2)
(Pair.init 3 4)])))
"Map.to-array works 2"
)
(assert-equal test
"{ 1 12 3 34 }"
&(str &(Map.endo-map (fn [k v] (+ @v (* 10 @k)))
@ -123,6 +134,11 @@
&(Map.vals &{1 1 2 1 3 2})
"vals works"
)
(assert-equal test
1
(Set.length &(Set.put (Set.create) "1"))
"length works"
)
(assert-equal test
2
(Set.length &(Set.put (Set.put (Set.create) "1") "2"))
@ -179,4 +195,14 @@
"{ @\"hi\" @\"bye\" }"
&(str &(Set.from-array &[@"hi" @"bye"]))
"stringification works"
)
(assert-equal test
&[1]
&(Set.to-array &(Set.put (Set.create) &1))
"Set.to-array works 1"
)
(assert-equal test
2
(Array.length &(Set.to-array &(Set.from-array &[1 2])))
"Set.to-array works 2"
))