Last active
February 23, 2022 16:14
-
-
Save zoren/efa6b2888755ab2a56eb71db1ed7a430 to your computer and use it in GitHub Desktop.
surprises in Clojure land
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; some surprises found while learning Clojure | |
;; min is for numbers | |
(min #inst "2018" #inst "2019") ; Execution error (ClassCastException) at clojure-test.core/eval8469 (form-init524028722621051551.clj:2589) . | |
; java.util.Date cannot be cast to java.lang.Number | |
;; macros are not values | |
(apply or (list false true)) ; Syntax error compiling at (Untitled-1:2:1). | |
; Can't take value of a macro: #'clojure.core/or | |
;; index-of can return index less than from index | |
(clojure.string/index-of "abc" "d" 9) ; => nil | |
(clojure.string/index-of "abc" "" 9) ; => 3 | |
;; evaluation order, sets and maps are evaluated in undefined order | |
#{(/ 1 0) 1 1} ; Syntax error reading source at (REPL:2:15). | |
; Duplicate key: 1 | |
;; reader and side effects | |
(def a (atom 0)) | |
(defn inca [] (swap! a inc)) | |
#{(inca) (inca)} ; Syntax error reading source at (REPL:3:17). | |
; Duplicate key: (inca) | |
(def inca2 inca) | |
#{(inca) (inca2)} ; => #{1 2} | |
;; more reader side effects | |
(def a 1) | |
(def b 1) | |
#{a b} ; Execution error (IllegalArgumentException) at user/eval21063 (REPL:-1). | |
; Duplicate key: 1 | |
#{(rand) (rand)} ; Syntax error reading source at (REPL:48:17) . | |
;; non equal NaNs | |
(not (= ##NaN ##NaN)) ; => true | |
(not= ##NaN ##NaN) ; => false | |
;; read more here: https://github.com/jafingerhut/batman/blob/master/doc/ubuntu-18.04.2-jdk-openjdk-11.0.3-clojure-1.10.1.txt | |
;; seqs and collections | |
(seq? []) ; => false | |
(sequential? []) ; => true | |
(sequential? {}) ; => false | |
(coll? {}) ; => true | |
(java.util.HashMap.) ; => {} | |
(coll? (java.util.HashMap.)) ; => false | |
;; insts are Dates | |
(type #inst "2018") ; => java.util.Date | |
;; sorted-maps | |
(instance? java.util.SortedMap (sorted-map)) ; => false | |
;; integers | |
(type 9223372036854775807) ; => java.lang.Long | |
(type 9223372036854775808) ; => clojure.lang.BigInt | |
(inc 9223372036854775807) ; throws java.lang.ArithmeticException | |
(inc' 9223372036854775807) ; => 9223372036854775808N | |
;; big numbers and infinity | |
(< 1e308M ##Inf) ; => true | |
(< 1e309M ##Inf) ; => false | |
(inc' ##Inf) ; => ##Inf | |
;; lists | |
(list? (map identity ())) ; => false | |
(list? ()) ; => true | |
;; big decimals | |
(/ 31M 360M) | |
;; Execution error (ArithmeticException) at java.math.BigDecimal/divide (BigDecimal.java:1723). | |
;; Non-terminating decimal expansion; no exact representable decimal result. | |
(binding [*math-context* java.math.MathContext/DECIMAL128] | |
(/ 31M 360M)) ; => 0.08611111111111111111111111111111111M | |
;; numbers and equality | |
(= 1 1M) ; => false | |
(== 1 1M) ; => true | |
;; repeat | |
(repeat 3/2 3) ; => (3) | |
(repeat 1.9999999999999999 1) ; => (1 1) | |
;; also | |
1.9999999999999999 ; => 2.0 | |
;; from Joy of Clojure | |
(let [r (range 1e9)] | |
[(first r) (last r)]) ; => [0 999999999] | |
(let [r (range 1e9)] | |
[(last r) (first r)]) ; java.lang.OutOfMemoryError | |
;; case | |
(case :get (:get :head) 302 307) ; => 302 | |
;; comparing characters | |
(= \А \A) ; => false | |
;; subtracting | |
('- 1000 1001) ; => 1001 | |
(-' 1000 1001) ; => -1 | |
;; comparing NaN's | |
(= ##NaN ##NaN) ; => false | |
(== ##NaN ##NaN) ; => false | |
(compare 0.0 ##NaN) ; => 0 | |
(compare ##NaN ##NaN) ; => 0 | |
(compare ##NaN 99N) ; => 0 | |
;; comparing maps | |
(compare {} {}) ; => 0 | |
(compare {:x 5} {:x 5}) ; Execution error (ClassCastException) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment