Skip to content

Commit

Permalink
Implement cljs.core/int and cljs.core/long
Browse files Browse the repository at this point in the history
  • Loading branch information
the-kenny authored and David Nolen committed Apr 20, 2012
1 parent ef0d525 commit 27e0bc8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
4 changes: 2 additions & 2 deletions devnotes/corelib.org
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ macro currently expands into extend call
* init-proxy
* DONE instance?
does what?
* int
* DONE int
* int-array
* DONE integer?
* DONE interleave
Expand Down Expand Up @@ -314,7 +314,7 @@ does what?
* load-string
* loaded-libs
* locking
* TODO long
* DONE long
* TODO long-array
* TODO longs
* DONE loop
Expand Down
10 changes: 10 additions & 0 deletions src/cljs/cljs/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -943,6 +943,16 @@ reduces them without incurring seq initialization"
(Math/floor q)
(Math/ceil q)))

(defn int
"Coerce to int by stripping decimal places."
[x]
(fix x))

(defn long
"Coerce to long by stripping decimal places. Identical to `int'."
[x]
(fix x))

(defn mod
"Modulus of num and div. Truncates toward negative infinity."
[n d]
Expand Down
9 changes: 9 additions & 0 deletions test/cljs/cljs/core_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,15 @@
(assert (= [[1 1] [1 2] [1 3] [2 1] [2 2] [2 3]]
(map #(%) (for [i [1 2] j [1 2 3]] (fn [] [i j])))))

(assert (= 42 (int 42.5)))
(assert (integer? (int 42.5)))

(assert (= 42 (long 42.5)))
(assert (integer? (long 42.5)))

(assert (= -1 (int -1.5)))
(assert (= -9 (long -9.8)))

(assert (= 2 (:b {:a 1 :b 2})))
(assert (= 2 ('b '{:a 1 b 2})))
(assert (= 2 ({:a 1 :b 2} :b)))
Expand Down

0 comments on commit 27e0bc8

Please sign in to comment.