Clojure-operators

提供:Dev Guides
移動先:案内検索

Clojure-オペレーター

  • 演算子*は、特定の数学的または論理的な操作を実行するようコンパイラーに指示する記号です。

Clojureには次のタイプの演算子があります-

  • 算術演算子
  • 関係演算子
  • 論理演算子
  • ビットごとの演算子

-Clojureでは、演算子とオペランドは次の構文で機能します。

構文

(operator operand1 operand2 operandn)

例えば、

(+ 1 2)

上記の例は、数値1と2に対して算術演算を実行します。

算術演算子

Clojure言語は、通常の算術演算子を任意の言語としてサポートします。 Clojureで使用できる算術演算子は次のとおりです。

リンク:/clojure/clojure_arithmetic_operators [例を表示]

Operator Description Example
+ Addition of two operands (+ 1 2) will give 3
Subtracts second operand from the first (- 2 1) will give 1
* Multiplication 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 dividing the first number by the second rem 3 2 will give 1

関係演算子

関係演算子を使用すると、オブジェクトを比較できます。 Clojureで使用できる関係演算子は次のとおりです。

リンク:/clojure/clojure_relational_operators [例を表示]

Operator Description Example
= Tests the equality 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

論理演算子

論理演算子は、ブール式を評価するために使用されます。 以下は、Groovyで使用可能な論理演算子です。

link:/clojure/clojure_logical_operators [例を表示]

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

次のコードスニペットは、さまざまな演算子の使用方法を示しています。

ビット演算子

Clojureは4つのビット演算子を提供します。 Clojureで使用できるビット演算子は次のとおりです。

リンク:/clojure/clojure_bitwise_operators [例を表示]

Sr.No. Operator & Description
1
  • bit-and*

これはビット単位の「and」演算子です

2

bit-or

これはビット単位の「or」演算子です

3

bit-xor

これはビット単位の「xor」または排他的な「or」演算子です

4

bit-not

これはビットごとの否定演算子です

以下は、これらの演算子を示す真理値表です。

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

演算子の優先順位

一般的なLISPの場合と同様に、演算子の優先順位について心配する必要はありません。 これは、S-Expressionsとプレフィックス表記の利点の1つです。 すべての関数は、左から右、裏返しで評価されます。 Clojureの演算子は単なる関数であり、すべてが括弧で囲まれています。