Skip to content

Commit bcf60ce

Browse files
author
dnolen
committed
rename long preds to int and cover all fixed precision integer types
same as Clojure 20f67081
1 parent a274b0b commit bcf60ce

File tree

4 files changed

+71
-29
lines changed

4 files changed

+71
-29
lines changed

src/main/cljs/cljs/core.cljs

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
(ns cljs.core
1010
(:require goog.math.Long
11+
goog.math.Integer
1112
[goog.string :as gstring]
1213
[goog.object :as gobject]
1314
[goog.array :as garray])
@@ -2124,25 +2125,57 @@ reduces them without incurring seq initialization"
21242125
(not (identical? n js/Infinity))
21252126
(== (js/parseFloat n) (js/parseInt n 10))))
21262127

2127-
(defn ^boolean long?
2128-
"Return true if x is an instance of goog.math.Long"
2129-
[x] (instance? goog.math.Long x))
2130-
2131-
(defn ^boolean pos-long?
2132-
"Return true if x is a positive Long"
2133-
[x] (and (instance? goog.math.Long x)
2134-
(not (.isNegative x))
2135-
(not (.isZero x))))
2136-
2137-
(defn ^boolean neg-long?
2138-
"Return true if x is a negative Long"
2139-
[x] (and (instance? goog.math.Long x)
2140-
(.isNegative x)))
2141-
2142-
(defn ^boolean nat-long?
2143-
"Return true if x is a non-negative Long"
2144-
[x] (and (instance? goog.math.Long x)
2145-
(or (not (.isNegative x)) (.isZero x))))
2128+
(defn ^boolean int?
2129+
"Return true if x is an integer"
2130+
[x]
2131+
(or (integer? x)
2132+
(instance? goog.math.Integer x)
2133+
(instance? goog.math.Long x)))
2134+
2135+
(defn ^boolean pos-int?
2136+
"Return true if x is a positive integer"
2137+
[x]
2138+
(cond
2139+
(integer? x) (pos? x)
2140+
2141+
(instance? goog.math.Integer x)
2142+
(and (not (.isNegative x))
2143+
(not (.isZero x)))
2144+
2145+
(instance? goog.math.Long x)
2146+
(and (not (.isNegative x))
2147+
(not (.isZero x)))
2148+
2149+
:else false))
2150+
2151+
(defn ^boolean neg-int?
2152+
"Return true if x is a negative integer"
2153+
[x]
2154+
(cond
2155+
(integer? x) (neg? x)
2156+
2157+
(instance? goog.math.Integer x)
2158+
(.isNegative x)
2159+
2160+
(instance? goog.math.Long x)
2161+
(.isNegative x)
2162+
2163+
:else false))
2164+
2165+
(defn ^boolean nat-int?
2166+
"Return true if x is a non-negative integer"
2167+
[x]
2168+
(cond
2169+
(integer? x)
2170+
(or (not (neg? x)) (zero? x))
2171+
2172+
(instance? goog.math.Integer x)
2173+
(or (not (.isNegative x)) (.isZero x))
2174+
2175+
(instance? goog.math.Long x)
2176+
(or (not (.isNegative x)) (.isZero x))
2177+
2178+
:else false))
21462179

21472180
(defn ^boolean contains?
21482181
"Returns true if key is present in the given collection, otherwise

src/main/cljs/cljs/spec.cljc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,11 +384,11 @@ specified, return speced vars from all namespaces."
384384
(gen/fmap mkdate#
385385
(gen/large-integer* {:min st# :max et#}))))))
386386

387-
(defmacro long-in
387+
(defmacro int-in
388388
"Returns a spec that validates longs in the range from start
389389
(inclusive) to end (exclusive)."
390390
[start end]
391-
`(spec (and c/long? #(long-in-range? ~start ~end %))
391+
`(spec (and c/int? #(int-in-range? ~start ~end %))
392392
:gen #(gen/large-integer* {:min ~start :max (dec ~end)})))
393393

394394
(defmacro instrument

src/main/cljs/cljs/spec.cljs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,9 +1124,18 @@
11241124
(let [t (inst-ms inst)]
11251125
(c/and (<= (inst-ms start) t) (< t (inst-ms end))))))
11261126

1127-
(defn long-in-range?
1127+
(defn int-in-range?
11281128
"Return true if start <= val and val < end"
11291129
[start end val]
1130-
(c/and (long? val)
1131-
(.lessThanOrEqual (goog.math.Long.fromNumber start) val)
1132-
(.lessThan val (goog.math.Long.fromNumber end))))
1130+
(cond
1131+
(integer? val) (and (<= start val) (< val end))
1132+
1133+
(instance? goog.math.Long val)
1134+
(c/and (.lessThanOrEqual start val)
1135+
(.lessThan val end))
1136+
1137+
(instance? goog.math.Integer val)
1138+
(c/and (.lessThanOrEqual start val)
1139+
(.lessThan val end))
1140+
1141+
:else false))

src/main/cljs/cljs/spec/impl/gen.cljs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,10 @@ gen-builtins
8989
(let [simple (simple-type-printable)]
9090
{number? (one-of [(large-integer) (double)])
9191
integer? (large-integer)
92-
long? (large-integer)
93-
pos-long? (large-integer* {:min 1})
94-
neg-long? (large-integer* {:max -1})
95-
nat-long? (large-integer* {:min 0})
92+
int? (large-integer)
93+
pos-int? (large-integer* {:min 1})
94+
neg-int? (large-integer* {:max -1})
95+
nat-int? (large-integer* {:min 0})
9696
;float? (double)
9797
string? (string-alphanumeric)
9898
ident? (one-of [(keyword-ns) (symbol-ns)])

0 commit comments

Comments
 (0)