core: add Pair.reverse and Map.reverse

This commit is contained in:
hellerve 2019-05-05 11:49:30 +02:00
parent 9b9b0df90e
commit 58e280c553
4 changed files with 17 additions and 1 deletions

View File

@ -276,6 +276,9 @@
@"{"
m)]
(String.append &res " }")))
(defn reverse [m]
(from-array &(Array.copy-map &Pair.reverse &(to-array m))))
)
(deftype (SetBucket a) [entries (Array a)])

View File

@ -33,4 +33,7 @@
(defn > [p1 p2]
(PairRef.> &p1 &p2))
(doc reverse "reverses a `Pair` `p` such that its first member is its second member and vice versa.")
(defn reverse [p]
(Pair.init @(Pair.b p) @(Pair.a p)))
)

View File

@ -197,6 +197,11 @@
(Map.get &{(Pair.init 1 2) 3} &(Pair.init 1 2))
"Pairs work as keys"
)
(assert-equal test
&{1 @"hi" 2 @"bye"}
&(Map.reverse &{@"hi" 1 @"bye" 2})
"reverse works"
)
(assert-equal test
1
(Set.length &(Set.put (Set.create) "1"))

View File

@ -21,4 +21,9 @@
"comparison works with (Ref (Pair String Int)) I")
(assert-false test
(< &(Pair.init @"a" 2) &(Pair.init @"a" 1))
"comparison works with (Ref (Pair String Int)) II"))
"comparison works with (Ref (Pair String Int)) II")
(assert-equal test
&(Pair.init 2 @"a")
&(Pair.reverse &(Pair.init @"a" 2))
"reverse works")
)