Working with some numerical test functions

Here are some Clojure functions for numerical tests.

 

user=> (zero? 0)
true
user=> (zero? 0.0)
true
user=> (zero? (/ 0 1))
true
user=> (zero? (/ 1 1))
false
user=> (zero? (inc -1))
true
user=> (zero? "a")

ClassCastException java.lang.String cannot be cast to java.lang.Number  clojure.lang.Numbers.isZero (Numbers.java:92)
user=> (zero? :a)

ClassCastException clojure.lang.Keyword cannot be cast to java.lang.Number  clojure.lang.Numbers.isZero (Numbers.java:92)
user=> (zero? 3)
false
;; note to self: Find out what a "radix" is
user=> (zero? 2r000)
true
;; hex notation
user=> (zero? 0x0)
true

user=> (neg? -1)
true
user=> (neg 0)
CompilerException java.lang.RuntimeException: Unable to resolve symbol: neg in this context, compiling:(/tmp/form-init455595109663875917.clj:1:1) 
;; I guess there are no PUAs allowed in the Clojure community
;; kidding; that was just a typo

user=> (neg? 0)
false
user=> (neg? 1)
false
user=> (neg? -0.1)
true
user=> (neg? 0.1)
false
user=> (neg? -(/ 5 2))

CompilerException clojure.lang.ArityException: Wrong number of args (2) passed to: core/neg?--inliner--4573, compiling:(/tmp/form-init455595109663875917.clj:1:1) 
;; You learn something new every day
user=> (neg? (/ -5 2))
true
user=> (neg? (/ 5 2))
false
;; granted, this is somewhat artificial; I can see the dang minus hyphen
;; let's create some vars
user=> (def x -1)
#'user/x
user=> (def y 1)
#'user/y
user=> (neg? x)
true
user=> (neg? y)
false
user=> (pos? x)
false
user=> (pos? y)
true
user=> (pos? 0)
false
user=> (pos? (/ 5 2))
true
user=> (pos? (/ -5 2))
false
user=> (pos? -5)
false
user=> (pos? 5)
true
user=> (pos? 5.22)
true
user=> (pos? -5.22)
false
user=> (def z 2)
#'user/z
user=> (even? z)
true
user=> (even? y)
false
user=> y
1
user=> (odd? y)
true
user=> (odd? :clojure-devs)

IllegalArgumentException Argument must be an integer: :clojure-devs  clojure.core/even? (core.clj:1372)
;; I kid, as ever
;; moving on.....
user=> (odd? z)
false
user=> (odd? (/ 5 2))

IllegalArgumentException Argument must be an integer: 5/2  clojure.core/even? (core.clj:1372)
user=> (odd? 5.0)

IllegalArgumentException Argument must be an integer: 5.0  clojure.core/even? (core.clj:1372)

;; from the clojure docs:
user=> (filter odd? (range 15))
(1 3 5 7 9 11 13)
user=> (filter even? (range 15))
(0 2 4 6 8 10 12 14)

;; according to https://clojure.org/reference/reader, 2r101010 is equal to 42
user=> (Long/parseLong "101010" 2)
42
user=> (odd? 2r101010)
false
user=> (even? 2r101010)
true

You’re welcome.

 

There are 345 words in this article.