- Clojure - Libraries
- Clojure - Automated Testing
- Clojure - Applications
- Clojure - Concurrent Programming
- Clojure - Java Interface
- Clojure - Databases
- Clojure - Reference Values
- Clojure - Macros
- Clojure - Watchers
- Clojure - Agents
- Clojure - StructMaps
- Clojure - Metadata
- Clojure - Atoms
- Clojure - Date & Time
- Clojure - Destructuring
- Clojure - Predicates
- Clojure - Regular Expressions
- Clojure - Sequences
- Clojure - Exception Handling
- Clojure - Namespaces
- Clojure - Maps
- Clojure - Vectors
- Clojure - Sets
- Clojure - Lists
- Clojure - Strings
- Clojure - File I/O
- Clojure - Recursion
- Clojure - Numbers
- Clojure - Functions
- Clojure - Decision Making
- Clojure - Loops
- Clojure - Operators
- Clojure - Variables
- Clojure - Data Types
- Clojure - REPL
- Clojure - Basic Syntax
- Clojure - Environment
- Clojure - Overview
- Clojure - Home
Clojure Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Clojure - Operators
An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations.
Clojure has the following types of operators −
Arithmetic operators
Relational operators
Logical operators
Bitwise operators
Note − In Clojure, operators and operands work in the following syntax manner.
Syntax
(operator operand1 operand2 operandn)
For example,
Example
(+ 1 2)
The above example does an arithmetic operation on the numbers 1 and 2.
Arithmetic Operators
Clojure language supports the normal Arithmetic operators as any language. Following are the Arithmetic operators available in Clojure.
Operator | Description | Example |
---|---|---|
+ | Addition of two operands | (+ 1 2) will give 3 |
− | Subtracts second operand from the first | (- 2 1) will give 1 |
* | Multippcation of both operands | (* 2 2) will give 4 |
/ | Division of numerator by denominator | (float (/ 3 2)) will give 1.5 |
inc | Incremental operators used to increment the value of an operand by 1 | inc 5 will give 6 |
dec | Incremental operators used to decrement the value of an operand by 1 | dec 5 will give 4 |
max | Returns the largest of its arguments | max 1 2 3 will return 3 |
min | Returns the smallest of its arguments | min 1 2 3 will return 1 |
rem | Remainder of spaniding the first number by the second | rem 3 2 will give 1 |
Relational Operators
Relational operators allow comparison of objects. Following are the relational operators available in Clojure.
Operator | Description | Example |
---|---|---|
= | Tests the equapty between two objects | (= 2 2) will give true |
not= | Tests the difference between two objects | (not = 3 2) will give true |
< | Checks to see if the left object is less than the right operand | (< 2 3) will give true |
<= | Checks to see if the left object is less than or equal to the right operand | (<= 2 3) will give true |
> | Checks to see if the left object is greater than the right operand | (> 3 2) will give true |
>= | Checks to see if the left object is greater than or equal to the right operand | (>= 3 2) will give true |
Logical Operators
Logical operators are used to evaluate Boolean expressions. Following are the logical operators available in Groovy.
Operator | Description | Example |
---|---|---|
and | This is the logical “and” operator | (or true true) will give true |
or | This is the logical “or” operator | (and true false) will give false |
not | This is the logical “not” operator | (not false) will give true |
The following code snippet shows how the various operators can be used.
Bitwise Operators
Clojure provides four bitwise operators. Following are the bitwise operators available in Clojure.
Sr.No. | Operator & Description |
---|---|
1 |
bit-and This is the bitwise “and” operator |
2 |
bit-or This is the bitwise “or” operator |
3 |
bit-xor This is the bitwise “xor” or Exclusive ‘or’ operator |
4 |
bit-not This is the bitwise negation operator |
Following is the truth table showcasing these operators.
p | q | p&q | p | q | p ^ q |
---|---|---|---|---|
0 | 0 | 0 | 0 | 0 |
0 | 1 | 0 | 1 | 1 |
1 | 1 | 1 | 1 | 0 |
1 | 0 | 0 | 1 | 1 |
Operator Precedence
As is the case with LISPs in general, there is no need to worry about operator precedence. This is one of the benefits of S-Expressions and prefix notation. All functions evaluate left to right and inside out. The operators in Clojure are just functions, and everything is fully parenthesized.
Advertisements