mirror of
				https://github.com/janet-lang/janet
				synced 2025-10-31 07:33:01 +00:00 
			
		
		
		
	compare functions now work for built ins and 'objects'
This commit is contained in:
		| @@ -692,7 +692,7 @@ | |||||||
|                fyx (f y x)] (- fyx)) |                fyx (f y x)] (- fyx)) | ||||||
|     (compare-primitive x y))) |     (compare-primitive x y))) | ||||||
|  |  | ||||||
| (defn compare-reduce- [op & xs] | (defn compare-reduce- [op xs] | ||||||
|   (var r true) |   (var r true) | ||||||
|   (loop [i :range [0 (- (length xs) 1)] |   (loop [i :range [0 (- (length xs) 1)] | ||||||
|          :let [c (compare (xs i) (xs (+ i 1))) |          :let [c (compare (xs i) (xs (+ i 1))) | ||||||
| @@ -705,27 +705,27 @@ | |||||||
| (defn compare= | (defn compare= | ||||||
|   "Equivalent of '=' but using compare function instead of primitive comparator" |   "Equivalent of '=' but using compare function instead of primitive comparator" | ||||||
|   [& xs] |   [& xs] | ||||||
|   (compare-reduce- := xs)) |   (compare-reduce- = xs)) | ||||||
|  |  | ||||||
| (defn compare< | (defn compare< | ||||||
|   "Equivalent of '<' but using compare function instead of primitive comparator" |   "Equivalent of '<' but using compare function instead of primitive comparator" | ||||||
|   [& xs] |   [& xs] | ||||||
|   (compare-reduce- :< xs)) |   (compare-reduce- < xs)) | ||||||
|  |  | ||||||
| (defn compare<= | (defn compare<= | ||||||
|   "Equivalent of '<=' but using compare function instead of primitive comparator" |   "Equivalent of '<=' but using compare function instead of primitive comparator" | ||||||
|   [& xs] |   [& xs] | ||||||
|   (compare-reduce- :<= xs)) |   (compare-reduce- <= xs)) | ||||||
|  |  | ||||||
| (defn compare> | (defn compare> | ||||||
|   "Equivalent of '>' but using compare function instead of primitive comparator" |   "Equivalent of '>' but using compare function instead of primitive comparator" | ||||||
|   [& xs] |   [& xs] | ||||||
|   (compare-reduce- :> xs)) |   (compare-reduce- > xs)) | ||||||
|  |  | ||||||
| (defn compare>= | (defn compare>= | ||||||
|   "Equivalent of '>=' but using compare function instead of primitive comparator" |   "Equivalent of '>=' but using compare function instead of primitive comparator" | ||||||
|   [& xs] |   [& xs] | ||||||
|   (compare-reduce- :>= xs)) |   (compare-reduce- >= xs)) | ||||||
|  |  | ||||||
| ### | ### | ||||||
| ### | ### | ||||||
|   | |||||||
| @@ -345,9 +345,11 @@ | |||||||
| (assert (= 1 (compare "foo" "bar")) "compare strings (1)") | (assert (= 1 (compare "foo" "bar")) "compare strings (1)") | ||||||
|  |  | ||||||
| (assert (compare< 1 2 3 4 5 6) "compare less than integers") | (assert (compare< 1 2 3 4 5 6) "compare less than integers") | ||||||
|  | (assert (not (compare> 1 2 3 4 5 6)) "compare not greater than integers") | ||||||
| (assert (compare< 1.0 2.0 3.0 4.0 5.0 6.0) "compare less than reals") | (assert (compare< 1.0 2.0 3.0 4.0 5.0 6.0) "compare less than reals") | ||||||
| (assert (compare> 6 5 4 3 2 1) "compare greater than integers") | (assert (compare> 6 5 4 3 2 1) "compare greater than integers") | ||||||
| (assert (compare> 6.0 5.0 4.0 3.0 2.0 1.0) "compare greater than reals") | (assert (compare> 6.0 5.0 4.0 3.0 2.0 1.0) "compare greater than reals") | ||||||
|  | (assert (not (compare< 6.0 5.0 4.0 3.0 2.0 1.0)) "compare less than reals") | ||||||
| (assert (compare<= 1 2 3 3 4 5 6) "compare less than or equal to integers") | (assert (compare<= 1 2 3 3 4 5 6) "compare less than or equal to integers") | ||||||
| (assert (compare<= 1.0 2.0 3.0 3.0 4.0 5.0 6.0) "compare less than or equal to reals") | (assert (compare<= 1.0 2.0 3.0 3.0 4.0 5.0 6.0) "compare less than or equal to reals") | ||||||
| (assert (compare>= 6 5 4 4 3 2 1) "compare greater than or equal to integers") | (assert (compare>= 6 5 4 4 3 2 1) "compare greater than or equal to integers") | ||||||
| @@ -370,9 +372,9 @@ | |||||||
|   @{:type :mynum :v 0 :compare |   @{:type :mynum :v 0 :compare | ||||||
|     (fn [self other] |     (fn [self other] | ||||||
|       (case (type other) |       (case (type other) | ||||||
|       :number (compare-primitive (self :v) other)) |       :number (compare-primitive (self :v) other) | ||||||
|       :table (when (= (get other :type) :mynum) |       :table (when (= (get other :type) :mynum) | ||||||
|                (compare-primitive (self :v) (other :v))))}) |                (compare-primitive (self :v) (other :v)))))}) | ||||||
|  |  | ||||||
| (let [n3 (table/setproto @{:v 3} mynum)] | (let [n3 (table/setproto @{:v 3} mynum)] | ||||||
|   (assert (= 0 (compare 3 n3)) "compare num to object (1)") |   (assert (= 0 (compare 3 n3)) "compare num to object (1)") | ||||||
| @@ -384,5 +386,6 @@ | |||||||
|   (assert (compare= 3 n3 (table/setproto @{:v 3} mynum)) "compare= poly") |   (assert (compare= 3 n3 (table/setproto @{:v 3} mynum)) "compare= poly") | ||||||
|   (assert (deep= (sorted @[4 5 n3 2] compare<) @[2 n3 4 5]) "polymorphic sort")) |   (assert (deep= (sorted @[4 5 n3 2] compare<) @[2 n3 4 5]) "polymorphic sort")) | ||||||
|  |  | ||||||
|  |  | ||||||
| (end-suite) | (end-suite) | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Mike Beller
					Mike Beller