Added type coercing to the right integer for the platform

This commit is contained in:
Guillermo Vaya 2013-10-11 08:35:32 +02:00
parent f61d702788
commit 34275fab60
2 changed files with 13 additions and 8 deletions

View File

@ -23,6 +23,8 @@
;;;; to make functional programming slightly easier. ;;;; to make functional programming slightly easier.
;;;; ;;;;
(import [hy._compat [long-type]]) ; long for python2, int for python3
(defn _numeric-check [x] (defn _numeric-check [x]
(if (not (numeric? x)) (if (not (numeric? x))
(raise (TypeError (.format "{0!r} is not a number" x))))) (raise (TypeError (.format "{0!r} is not a number" x)))))
@ -97,11 +99,13 @@
(defn instance? [klass x] (defn instance? [klass x]
(isinstance x klass)) (isinstance x klass))
(defn integer [x]
"Return Hy kind of integer"
(long-type x))
(defn integer? [x] (defn integer? [x]
"Return True if x in an integer" "Return True if x in an integer"
(if-python2 (isinstance x (, int long-type)))
(isinstance x (, int long))
(isinstance x int)))
(defn iterable? [x] (defn iterable? [x]
"Return true if x is iterable" "Return true if x is iterable"
@ -213,8 +217,7 @@
(= n 0)) (= n 0))
(def *exports* ["cycle" "dec" "distinct" "drop" "drop_while" "empty?" (def *exports* ["cycle" "dec" "distinct" "drop" "drop_while" "empty?"
"even?" "filter" "float?" "inc" "even?" "filter" "float?" "inc" "instance?" "integer" "integer?"
"instance?" "integer?" "iterable?" "iterate" "iterator?" "neg?" "iterable?" "iterate" "iterator?" "neg?" "none?" "nth"
"none?" "nth" "numeric?" "odd?" "pos?" "remove" "repeat" "numeric?" "odd?" "pos?" "remove" "repeat" "repeatedly" "second"
"repeatedly" "second" "string?" "take" "take_nth" "take_while" "string?" "take" "take_nth" "take_while" "zero?"])
"zero?"])

View File

@ -171,6 +171,8 @@
(assert-true (integer? 0)) (assert-true (integer? 0))
(assert-true (integer? 3)) (assert-true (integer? 3))
(assert-true (integer? -3)) (assert-true (integer? -3))
(assert-true (integer? (integer "-3")))
(assert-true (integer? (integer 3)))
(assert-false (integer? 4.2)) (assert-false (integer? 4.2))
(assert-false (integer? None)) (assert-false (integer? None))
(assert-false (integer? "foo"))) (assert-false (integer? "foo")))