Wrestling with the Integers
Clojure provides three different sets of operations for integer types:
- The unchecked operators
- The default operators
- The promoting operators
The following table gives a sampling of these operator types.
Unchecked | Default | Promoting |
|---|---|---|
unchecked-add | + | +’ |
unchecked-subtract | - | -’ |
unchecked-multiply | * | *’ |
unchecked-inc | inc | inc’ |
unchecked-dec | dec | dec’ |
The unchecked operators correspond exactly with primitive math in Java. They are fast but terrifically dangerous in that they can overflow silently and give incorrect answers. In Clojure, the unchecked operators should be used only in the rare situation that overflow is the desired behavior or when performance is paramount and you are certain overflow is impossible or irrelevant.
(unchecked-add ... |
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access