Skip to content

Commit 27e0bc8

Browse files
the-kennyDavid Nolen
authored andcommitted
Implement cljs.core/int and cljs.core/long
1 parent ef0d525 commit 27e0bc8

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

devnotes/corelib.org

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ macro currently expands into extend call
277277
* init-proxy
278278
* DONE instance?
279279
does what?
280-
* int
280+
* DONE int
281281
* int-array
282282
* DONE integer?
283283
* DONE interleave
@@ -314,7 +314,7 @@ does what?
314314
* load-string
315315
* loaded-libs
316316
* locking
317-
* TODO long
317+
* DONE long
318318
* TODO long-array
319319
* TODO longs
320320
* DONE loop

src/cljs/cljs/core.cljs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -943,6 +943,16 @@ reduces them without incurring seq initialization"
943943
(Math/floor q)
944944
(Math/ceil q)))
945945

946+
(defn int
947+
"Coerce to int by stripping decimal places."
948+
[x]
949+
(fix x))
950+
951+
(defn long
952+
"Coerce to long by stripping decimal places. Identical to `int'."
953+
[x]
954+
(fix x))
955+
946956
(defn mod
947957
"Modulus of num and div. Truncates toward negative infinity."
948958
[n d]

test/cljs/cljs/core_test.cljs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,15 @@
160160
(assert (= [[1 1] [1 2] [1 3] [2 1] [2 2] [2 3]]
161161
(map #(%) (for [i [1 2] j [1 2 3]] (fn [] [i j])))))
162162

163+
(assert (= 42 (int 42.5)))
164+
(assert (integer? (int 42.5)))
165+
166+
(assert (= 42 (long 42.5)))
167+
(assert (integer? (long 42.5)))
168+
169+
(assert (= -1 (int -1.5)))
170+
(assert (= -9 (long -9.8)))
171+
163172
(assert (= 2 (:b {:a 1 :b 2})))
164173
(assert (= 2 ('b '{:a 1 b 2})))
165174
(assert (= 2 ({:a 1 :b 2} :b)))

0 commit comments

Comments
 (0)