Operators
abs
Use abs
to calculate the absolute value of a given number
.
Basic syntax
To calculate the absolute value of a number
, use the following syntax:
(abs number)
(abs number)
Arguments
Use the following argument to specify the number
for which you want to calculate the absolute value using the abs
Pact function.
Argument | Type | Description |
---|---|---|
number | decimal or integer | Specifies the number for which to calculate the absolute value. |
Return values
The abs
function returns the absolute value of the number
as a decimal
or integer
, depending on the input type.
Examples
The following example calculates the absolute value of a decimal number in the Pact REPL:
pact> (abs (- 10.5 23.7))13.2
pact> (abs (- 10.5 23.7))13.2
The following example calculates the absolute value of an integer:
pact> (abs (- 10 23))13
pact> (abs (- 10 23))13
add (+)
Use +
to add numbers, concatenate strings and lists, or merge objects.
Basic syntax
To add numbers, concatenate strings and lists, or merge objects, use the following syntax:
(+ oper1 oper2)
(+ oper1 oper2)
Arguments
Use the following arguments to specify the values for addition, concatenation, or merging using the +
Pact function.
Argument | Type | Description |
---|---|---|
oper1 | integer, decimal, string, [list], or object | Specifies the first operand for addition, concatenation, or merging. |
oper2 | integer, decimal, string, [list], or object | Specifies the second operand for addition, concatenation, or merging. |
Return value
The +
function returns the result of addition for numbers, the concatenated string or list for strings and lists, or the resulting of merging for objects.
Examples
The following examples demonstrate how to use the +
function to add two numbers in the Pact REPL:
pact> (+ 1 2)3 pact> (+ 5.0 20.5)25.5
pact> (+ 1 2)3 pact> (+ 5.0 20.5)25.5
The following examples demonstrate how to use the +
function to concatenate strings and lists in the Pact REPL:
pact> (+ "every" "body")"everybody" pact> (+ [1 2] [3 4])[1 2 3 4]
pact> (+ "every" "body")"everybody" pact> (+ [1 2] [3 4])[1 2 3 4]
The following example demonstrates how to use the +
function to merge objects in the Pact REPL:
pact> (+ { "foo": 100 } { "foo": 1, "bar": 2 }){"bar": 2,"foo": 100}
pact> (+ { "foo": 100 } { "foo": 1, "bar": 2 }){"bar": 2,"foo": 100}
In this example, merging the object fields using the +
function results in the first operand value replacing the corresponding field in the second operand.
and?
Use and?
to apply a logical AND operation to the results of applying a specified value
to application functions func1
and func2
, with short-circuit evaluation.
You can use any data type for the value
argument as long as the two functions take that same data type and return the resulting boolean value for the logical AND operation performed by the and?
function.
By convention, the data type <a>
is used if an argument represents a type-bound parameter like the value
argument in this function:
(defun and?:bool (func1:(<a> -> bool) func2:(<a> -> bool) value:<a>))
(defun and?:bool (func1:(<a> -> bool) func2:(<a> -> bool) value:<a>))
Basic syntax
To apply a logical AND operation to the results of applying a specified value
to the functions func1
and func2
, use the following syntax:
(and? func1 func2 value)
(and? func1 func2 value)
Arguments
Use the following arguments to specify the functions and value
for the and?
operation.
Argument | Type | Description |
---|---|---|
func1 | function x: <a> -> bool | Specifies the first function to apply the specified value to. The result of applying the specified value of type <a> returns a boolean value. |
func2 | function x: <a> -> bool | Specifies the second function to apply the specified value to. The result of applying the specified value of type <a> returns a boolean value. |
value | <a> | Specifies the value to apply to both func1 and func2 functions. |
Return values
The and?
function returns a boolean value based on the result of applying value
to func1
and func2
with the logical AND operation.
Examples
The following example demonstrates how to use the and?
function in the Pact REPL:
pact> (and? (> 20) (> 10) 15)false
pact> (and? (> 20) (> 10) 15)false
In this example, the and?
function applies the value 15 to the function (> 20)
, with the result being true
because 20 > 15
is true.
The function then applies the value of 15 to the (> 10)
function, with the result being false because 10 > 15
is false.
The result from the and?
function, therefore, is false
because the second condition is false.
and
Use and
to perform a boolean logic AND operation with short-circuiting.
Basic syntax
To perform a boolean logic AND operation between two boolean values oper1
and oper2
, use the following syntax:
(and oper1 oper2)
(and oper1 oper2)
Arguments
Use the following arguments to specify the boolean values for the and
operation.
Argument | Type | Description |
---|---|---|
oper1 | bool | Specifies the first boolean value for the AND operation. |
oper2 | bool | Specifies the second boolean value for the AND operation. |
Return values
The and
function returns a boolean value based on the result of the AND operation between the input boolean values.
Examples
The following example demonstrates how to use the and
function to perform a boolean AND operation between the values true
and false
in the Pact REPL:
pact> (and true false)false
pact> (and true false)false
The following example illustrates using the and
function to evaluate two expressions to determine whether an account string is valid:
(and (>= (length account) 3) (<= (length account) 256))
(and (>= (length account) 3) (<= (length account) 256))
In this example, both expressions must evaluate to true for an account string to be valid.
bitwise-and (&)
Use &
to compute the bitwise AND operation between the first integer oper1
value and the second integer oper2
value.
Basic syntax
To compute the bitwise AND operation between oper1
and oper2
, use the following syntax:
(& oper1 oper2)
(& oper1 oper2)
Arguments
Use the following arguments to specify the values for bitwise AND operation using the &
Pact function.
Argument | Type | Description |
---|---|---|
oper1 | integer | Specifies the first operand. |
oper2 | integer | Specifies the second operand. |
Return value
The &
function returns the result of the bitwise AND operation between oper1
and oper2
.
Examples
The following examples demonstrate how to use the &
function to perform bitwise AND manipulation of integer values in a Pact REPL:
pact> (& 2 3)2 pact> (& 5 -7)1
pact> (& 2 3)2 pact> (& 5 -7)1
bitwise-or (|)
Use |
to compute the bitwise OR operation between the first integer oper1
value and the second integer oper2
value.
Basic syntax
To compute the bitwise OR operation between the oper1
and oper2
integer values, use the following syntax:
(| oper1 oper2)
(| oper1 oper2)
Arguments
Use the following arguments to specify the integers for the bitwise OR operation using the |
Pact function.
Argument | Type | Description |
---|---|---|
oper1 | integer | Specifies the first integer for the OR operation. |
oper2 | integer | Specifies the second integer for the OR operation. |
Return value
The |
function returns the result of the bitwise OR operation as an integer.
Examples
The following examples demonstrate how to use the |
function to perform bitwise OR manipulation between two integers in a Pact REPL:
pact> (| 2 3)3 pact> (| 5 -7)-3
pact> (| 2 3)3 pact> (| 5 -7)-3
bitwise-reverse (~)
Use ~
to reverse all bits in the provided integer.
Basic syntax
To reverse all bits in an integer x
, use the following syntax:
(~ x)
(~ x)
Arguments
Use the following argument to specify the integer for bit reversal using the ~
Pact function.
Argument | Type | Description |
---|---|---|
x | integer | Specifies the integer for which to reverse all bits. |
Return value
The ~
function returns the result of reversing all bits in the provided integer.
Examples
The following example demonstrates how to use the ~
function to reverse all bits in the integer 15
:
pact> (~ 15)-16
pact> (~ 15)-16
ceiling
Use ceiling
to round up the value of a specified decimal value
to the nearest integer or to a specified precision prec
as a decimal.
Basic syntax
To round up the value of a decimal value
to the nearest integer, use the following syntax:
(ceiling value)
(ceiling value)
To round up the value of a decimal value
to a specified precision
as a decimal, use the following syntax:
(ceiling value precision)
(ceiling value precision)
Arguments
Use the following arguments to specify the decimal value
and optional precision
for the ceiling
Pact function.
Argument | Type | Description |
---|---|---|
value | decimal | Specifies the decimal value to round up. |
precision | integer | Specifies the precision to round the specified value to (optional). |
Return values
The ceiling
function returns the rounded-up value as an integer or as a decimal based on the input and precision.
Examples
The following example rounds up a decimal value to the nearest integer in the Pact REPL:
pact> (ceiling 3.5)4
pact> (ceiling 3.5)4
The following example rounds up a decimal value to a precision of 2 decimal places:
pact> (ceiling 100.15234 2)100.16
pact> (ceiling 100.15234 2)100.16
In this example, ceiling
rounds up the decimal value 100.15234
to a precision of 2 decimal places, resulting in 100.16
.
dec
Use dec
to convert a specified integer value
to a decimal value.
This function can be useful if you need to work with decimal values in Pact but have integer inputs.
Basic syntax
To convert a specified integer value
to a decimal value, use the following syntax:
(dec value)
(dec value)
Arguments
Use the following argument to specify the integer for the dec
Pact function.
Argument | Type | Description |
---|---|---|
value | integer | Specifies the integer to cast to a decimal value. |
Return values
The dec
function returns the specified integer as a decimal value.
Example
The following example demonstrates how to use the dec
function:
pact> (dec 3)3.0
pact> (dec 3)3.0
divide (/)
Use /
to divide the first argument oper1
by the second argument oper2
.
Note that you can use this function to divide integer values or decimal values.
However, you should use the same type for both oper1
and oper2
values.
Basic syntax
To divide oper1
by oper2
, use the following syntax:
(/ oper1 oper2)
(/ oper1 oper2)
Arguments
Use the following arguments to specify the values for division using the /
Pact function.
Argument | Type | Description |
---|---|---|
oper1 | integer or decimal | Specifies the value of the dividend. |
oper2 | integer or decimal | Specifies the divisor. |
Return value
The /
function returns the result of dividing oper1
by oper2
.
Examples
The following examples demonstrate how to use the /
function to divide two values in a Pact REPL:
pact> (/ 10.0 2.0)5.0 pact> (/ 8 3)2
pact> (/ 10.0 2.0)5.0 pact> (/ 8 3)2
equal (=)
Use =
to return true if the first argument oper1
is equal to the second argument oper2
.
Basic syntax
To check if oper1
is equal to oper2
, use the following syntax:
(= oper1 oper2)
Arguments
Use the following arguments to specify the values for comparison using the =
Pact function.
Argument | Type | Description |
---|---|---|
oper1 | integer, decimal, string, time, bool, object, list, modref, guard | Specifies the first value for comparison. |
oper2 | integer, decimal, string, time, bool, object, list, modref, guard | Specifies the second value for comparison. |
Return value
The =
function returns a boolean value indicating whether oper1
is equal to oper2
.
Examples
The following example demonstrates how to use the =
function to compare two integer values to check if the first value is equal to the second value:
pact> (= 5 5)true
pact> (= 5 5)true
The following example demonstrates how to use the =
function to compare two decimal values to check if the first value is equal to the second value:
pact> (= 3.14 2.71)false
pact> (= 3.14 2.71)false
The following example demonstrates how to use the =
function to compare two string values to check if the first string is equal to the second string:
pact> (= "hello" "hello")true
pact> (= "hello" "hello")true
The following example demonstrates how to use the =
function to compare two time values to check if the first time is equal to the second time:
pact> (= (time "2023-06-05T10:00:00Z") (time "2023-06-05T10:00:00Z"))true
pact> (= (time "2023-06-05T10:00:00Z") (time "2023-06-05T10:00:00Z"))true
The following example demonstrates how to use the =
function to compare two object values to check if the first object is equal to the second object:
pact> (= { "name": "Alice", "age": 30 } { "name": "Alice", "age": 26 })false
pact> (= { "name": "Alice", "age": 30 } { "name": "Alice", "age": 26 })false
The following example demonstrates how to use the =
function to compare two list values to check if the first list is equal to the second list:
pact> (= [1, 2, 3] [1, 2, 3])true
pact> (= [1, 2, 3] [1, 2, 3])true
You can also the =
function to evaluate variables and expressions.
For example:
(enforce (= amount 1.0) "Mint can only be 1")
(enforce (= amount 1.0) "Mint can only be 1")
exp
Use exp
to calculate the exponential function of the specified value
.
Basic syntax
To calculate the exponential function of a value, use the following syntax:
(exp value)
Arguments
Use the following argument to specify the value for the exp
Pact function:
Argument | Type | Description |
---|---|---|
value | integer or decimal | Specifies the value for which to calculate the exponential function. |
Return values
The exp
function returns the exponential function of the specified value.
Examples
The following example demonstrates how to use the exp
function to calculate the exponential value for three and round the result of this calculation to precision of six decimal places:
pact> (round (exp 3) 6)20.085537
pact> (round (exp 3) 6)20.085537
floor
Use floor
to round down the value of a decimal value
to an integer, or to a specified precision
number of decimal places.
The floor
function is useful for situations where you need to round down decimal values in Pact contracts.
Basic syntax
To round down a decimal value to an integer, use the following syntax:
(floor value)
(floor value)
To round down a decimal value to a specified precision, use the following syntax:
(floor value precision)
(floor value precision)
Arguments
Use the following arguments to specify the decimal value and precision for the floor
Pact function:
Argument | Type | Description |
---|---|---|
value | decimal | Specifies the decimal value to round down. |
precision | integer | Specifies the precision to round down to for the resulting decimal value (optional). |
Return values
The floor
function returns the rounded-down value of the specified decimal:
- If only
value
is provided, it returns an integer. - If
precision
is provided, it returns a decimal with the specified precision.
Examples
The following example demonstrates how to use the floor
function to round down the 3.5
decimal value to the nearest integer:
pact> (floor 3.5)3
pact> (floor 3.5)3
The following example demonstrates how to use the floor
function to round down the decimal value 100.15234
to a decimal value with a precision of two decimal places:
pact> (floor 100.15234 2)100.15
pact> (floor 100.15234 2)100.15
The following example uses the floor function in an expression for calculating a royalty payout:
(royalty-payout:decimal (floor (* sale-price royalty-rate) (fungible::precision)))
(royalty-payout:decimal (floor (* sale-price royalty-rate) (fungible::precision)))
greater-than-equal (>=)
Use >=
to returns true if the first argument oper1
is greater than or equal to the second argument oper2
.
Basic syntax
To check if oper1
is greater than or equal to oper2
, use the following syntax:
(>= oper1 oper2)
(>= oper1 oper2)
Arguments
Use the following arguments to specify the values for comparison using the >=
Pact function.
Argument | Type | Description |
---|---|---|
oper1 | integer, decimal, string, or time | Specifies the first value for comparison. |
oper2 | integer, decimal, string, or time | Specifies the second value for comparison. |
Return value
The >=
function returns a boolean value indicating whether oper1
is greater than or equal to oper2
.
Examples
The following example demonstrates how to use the >=
function to compare two integer values to check if the first value (1
) is greater than or equal to the second value (3
):
pact> (>= 1 3)false
pact> (>= 1 3)false
The following example demonstrates how to use the >=
function to compare two decimal values to check if the first value (5.24
) is greater than or equal to the second value (2.52
):
pact> (>= 5.24 2.52)true
pact> (>= 5.24 2.52)true
The following example demonstrates how to use the >=
function to compare two string values to check if the first value (abc
) is greater than or equal to the second value (def
):
pact> (>= "abc" "def")false
pact> (>= "abc" "def")false
greater-than (>)
Use >
to return true if the first argument oper1
is greater than the second argument oper2
.
Basic syntax
To check if oper1
is greater than oper2
, use the following syntax:
(> oper1 oper2)
(> oper1 oper2)
Arguments
Use the following arguments to specify the values for comparison using the >
Pact function.
Argument | Type | Description |
---|---|---|
oper1 | integer, decimal, string, or time | Specifies the first value for comparison. |
oper2 | integer, decimal, string, time | Specifies the second value for comparison. |
Return value
The >
function returns a boolean value indicating whether oper1
is greater than oper2
.
Examples
The following example demonstrate how to use the >
function to compare two integer values to check if the first value (1
) is greater than the second value (3
):
pact> (> 1 3)false
pact> (> 1 3)false
The following example demonstrates how to use the >
function to compare two decimal values to check if the first value (5.24
) is greater than the second value (2.52
):
pact> (> 5.24 2.52)true
pact> (> 5.24 2.52)true
The following example demonstrates how to use the >
function to compare two string values to check if the first value (abc
) is greater than the second value (def
):
pact> (> "abc" "def")false
pact> (> "abc" "def")false
less-than-equal ( <= )
Use <=
to return true if the first oper1
argument is less than or equal to the second oper2
argument.
Basic syntax
To check if oper1
is less than or equal to oper2
, use the following syntax:
(<= oper1 oper2)
(<= oper1 oper2)
Arguments
Use the following arguments to specify the values for comparison using the <=
Pact function.
Argument | Type | Description |
---|---|---|
oper1 | <a[integer,decimal,string,time]> | Specifies the first value for comparison. |
oper2 | <a[integer,decimal,string,time]> | Specifies the second value for comparison. |
Return value
The <=
function returns a boolean value indicating whether oper1
is less than or equal to oper2
.
Examples
The following example demonstrates how to use the <=
function to compare two integer values to check if the first value (1
) is less than or equal to the second value (3
):
pact> (<= 1 3)true
pact> (<= 1 3)true
The following example demonstrates how to use the >=
function to compare two decimal values to check if the first value (5.24
) is less than or equal to the second value (2.52
):
pact> (<= 5.24 2.52)false
pact> (<= 5.24 2.52)false
The following example demonstrates how to use the >=
function to compare two string values to check if the first value (abc
) is less than or equal to the second value (def
):
pact> (<= "abc" "def")true
pact> (<= "abc" "def")true
ln
Use ln
to compute the natural logarithm of a specified value
.
Basic syntax
To compute the natural logarithm of a value, use the following syntax:
(ln value)
(ln value)
Argument
Use the following argument to specify the value for which you want to compute the natural logarithm using the ln
Pact function.
Argument | Type | Description |
---|---|---|
value | integer or decimal | Specifies the value for which you want to compute the natural logarithm. |
Return value
The ln
function returns the natural logarithm of the specified value.
Examples
The following example demonstrates how to use the ln
function to computer the natural logarithm for the value of 60 and round the result to 6 decimal places:
pact> (round (ln 60) 6)4.094345
pact> (round (ln 60) 6)4.094345
log
Use log
to compute the logarithm of the speified value
with the specified base
.
Basic syntax
To compute the logarithm of a specified value
with the specified base
, use the following syntax:
(log base value)
(log base value)
Arguments
Use the following arguments to specify the base
and value
for which you want to compute the logarithm using the log
Pact function.
Argument | Type | Description |
---|---|---|
base | integer or decimal | Specifies the base of the logarithm. |
value | integer or decimal | Specifies the value for which you want to compute the logarithm. |
Return value
The log
function returns the logarithm of value
with base base
.
Examples
The following example demonstrates how to use the log
function to computer the logarithm of 256 with base 2:
pact> (log 2 256)8
pact> (log 2 256)8
less-than ( < )
Use <
to return true if the first oper1
argument is less than the second oper2
argument.
Basic syntax
To check if oper1
is less than oper2
, use the following syntax:
(< oper1 oper2)
(< oper1 oper2)
Arguments
Use the following arguments to specify the values for comparison using the <
Pact function.
Argument | Type | Description |
---|---|---|
oper1 | integer, decimal, string, or time | Specifies the first value for comparison. |
oper2 | integer, decimal, string, time | Specifies the second value for comparison. |
Return value
The <
function returns a boolean value indicating whether oper1
is less than oper2
.
Examples
The following example demonstrates how to use the <
function to compare two integer values to check if the first value (1
) is less than the second value (3
)::
pact> (< 1 3)true
pact> (< 1 3)true
The following example demonstrates how to use the <
function to compare two decimal values to check if the first value (5.24
) is less than the second value (2.52
):
pact> (< 5.24 2.52)false
pact> (< 5.24 2.52)false
The following example demonstrates how to use the <
function to compare two string values to check if the first value (abc
) is less than the second value (def
):
pact> (< "abc" "def")true
pact> (< "abc" "def")true
mod
Use mod
to compute the remainder of oper1
divided by oper2
.
Basic syntax
To compute the remainder of oper1
divided by oper2
, use the following syntax:
(mod oper1 oper2)
Arguments
Use the following arguments to specify the integers for which you want to compute the remainder using the mod
Pact function.
Argument | Type | Description |
---|---|---|
oper1 | integer | Specifies the dividend. |
oper2 | integer | Specifies the divisor. |
Return value
The mod
function returns the remainder of the division of oper1
by oper2
.
Examples
The following example demonstrates how to use the mod
function to compute the remainder when 13 is divided by 8:
pact>(mod 13 8)5
pact>(mod 13 8)5
multiply (*)
Use *
to multiply the first oper1
argument by the second oper12
argument.
Note that you can use this function to multiply integer values or decimal values.
However, you should use the same type for both oper1
and oper2
values.
Basic syntax
To multiply oper1
by oper2
, use the following syntax:
(* oper1 oper2)
(* oper1 oper2)
Arguments
Use the following arguments to specify the values for multiplication using the *
Pact function.
Argument | Type | Description |
---|---|---|
oper1 | integer or decimal | Specifies the first multiplier. |
oper2 | integer or decimal | Specifies the second multiplier. |
Return value
The *
function returns the result of multiplying oper1
by oper2
.
Examples
The following example demonstrates how to use the *
function to multiply two decimal values:
pact> (* 0.5 10.0)5.0
pact> (* 0.5 10.0)5.0
The following example demonstrates how to use the *
function to multiply two integer values:
pact> (* 3 5)15
pact> (* 3 5)15
These examples illustrate how to use the *
function to perform multiplication operations in Pact, facilitating arithmetic calculations with both integer and decimal values.
not-equal (!=)
Use !=
to return true if the first oper1
argument does not equal the second oper2
argument.
This function allows you to write conditional logic based on whether two values are not equal.
Basic syntax
To check if oper1
does not equal oper2
, use the following syntax:
(!= oper1 oper2)
(!= oper1 oper2)
Arguments
Use the following arguments to specify the values for comparison using the !=
Pact function.
Argument | Type | Description |
---|---|---|
oper1 | integer, string, time, decimal, bool, list, object, keyset, guard, or module | Specifies the first value for comparison. |
oper2 | integer, string, time, decimal, bool, list, object, keyset, guard, or module | Specifies the second value for comparison. |
Return value
The !=
function returns true if oper1
does not equal oper2
, otherwise false.
Examples
The following example demonstrates how to use the !=
function to check whether two strings are not equal:
pact> (!= "hello" "goodbye")true
pact> (!= "hello" "goodbye")true
In the following example, the !=
function ensures that the sender
and receiver
accounts are not equal to prevent the sender initiating a transfer from also being the receiver of the transfer:
(enforce (!= sender receiver) "sender cannot be the receiver of a transfer")
(enforce (!= sender receiver) "sender cannot be the receiver of a transfer")
not?
Use not?
to apply a logical NOT operation to the results of applying a specified value
to an application function.
You can use any data type for the value
argument as long as the app
function takes that same data type and returns the resulting boolean value for the logical NOT operation performed by the not?
function.
By convention, the data type <a>
is used if an argument represents a type-bound parameter like the value
argument in this function:
Basic syntax
To apply a logical NOT operation to the results of applying a specified value
to an application function app
, use the following syntax:
(not? app value)
(not? app value)
Arguments
Use the following arguments to specify the application function and the value to be applied using the not?
Pact function.
Argument | Type | Description |
---|---|---|
app | function x: <a> -> bool | Specifies the application function to apply the specified value to. The result of applying the specified value returns a boolean value. |
value | <a> | Specifies the value to be applied to the application function. |
Return value
The not?
function returns a boolean value representing the logical negation of the result of applying the value to the application function.
Examples
The following example demonstrates how to use the not?
function in the Pact REPL:
pact> (not? (> 20) 15)false
pact> (not? (> 20) 15)false
In this example, the application function is (> 20)
and the value is 15
. Because the expression 20 > 15
evaluates to true
, and not?
negates this value, the not?
function returns false
.
not
Use not
to compute the boolean negation of a specified value
.
Basic syntax
To compute the boolean negation of the specified value
, use the following syntax:
(not value)
(not value)
Argument
Use the following argument to specify the boolean value for which you want to compute the negation using the not
Pact function.
Argument | Type | Description |
---|---|---|
value | bool | Specifies the expression to evaluate that returns the boolean value to be negated. |
Return value
The not
function returns the boolean negation of the input value.
Examples
The following example demonstrates how to use of the not
function in the Pact REPL:
pact> (not (> 1 2))true
pact> (not (> 1 2))true
In this example, the expression (> 1 2)
evaluates to false
, and the not
function negates this value, resulting in true
.
or?
Use or?
to apply a logical OR operation to the results of applying a specified value
to application functions func1
and func2
, with short-circuit evaluation.
You can use any data type for the value
argument as long as the two functions take that same data type and return the resulting boolean value for the logical OR operation performed by the or?
function.
By convention, the data type <a>
is used if an argument represents a type-bound parameter like the value
argument in this function.
Basic syntax
To apply a logical OR operation to the results of applying a value to two application functions, use the following syntax:
(or? func1 func2 value)
(or? func1 func2 value)
Arguments
Use the following arguments to specify the functions and the value
to be applied using the or?
Pact function.
Argument | Type | Description |
---|---|---|
func1 | function x: <a> -> bool | Specifies the first function to apply the specified value to. The result of applying the specified value returns a boolean value. |
func2 | function x: <a> -> bool | Specifies the second function to apply the specified value to. The result of applying the specified value returns a boolean value. |
value | <a> | Specifies the value to apply to both func1 and func2 functions. |
Return value
The or?
function returns a boolean value representing the logical OR operation after evaluating the results from applying the specified value to the two application functions.
Examples
The following example demonstrates how to use the or?
function in the Pact REPL:
pact> (or? (> 20) (> 10) 15)true
pact> (or? (> 20) (> 10) 15)true
In this example, the or?
function applies the value 15 to the function (> 20)
, with the result being true
because 20 > 15
is true.
Because the function performs short-circuit evaluation on the results, the or?
function returns true
because the first condition is true.
or
Use or
to apply a logical OR operation with short-circuit evaluation.
Basic syntax
To perform a logical OR operation with short-circuit evaluation, use the following syntax:
(or oper1 oper2)
(or oper1 oper2)
Arguments
Use the following arguments to specify the boolean values for which you want to perform the logical OR operation using the or
Pact function.
Argument | Type | Description |
---|---|---|
oper1 | bool | Specifies the first expression to evaluate that returns the boolean value to perform the logical OR operation on. |
oper2 | bool | Specifies the second expression to evaluate that returns the boolean value to perform the logical OR operation on. |
Return value
The or
function returns a boolean value based on the logical OR operation of the input values.
Examples
The following example demonstrates how to use the or
function in the Pact REPL:
pact> (or (> 20 10) (> 10 15))true
pact> (or (> 20 10) (> 10 15))true
In this example, the or
function evaluates the expressions (> 20 10)
and (> 10 15)
.
The boolean value returns for the first expression is true
because 20 > 15
is true.
Because the or
function performs short-circuit evaluation on the results, the function returns true
because the first expression is true.
power-of (^)
Use ^
to raises the oper1
argument to the power of the oper2
argument.
Basic syntax
To raise oper1
to the power of oper2
, use the following syntax:
(^ oper1 oper2)
(^ oper1 oper2)
Arguments
Use the following arguments to specify the base and exponent for raising to a power using the ^
Pact function.
Argument | Type | Description |
---|---|---|
oper1 | integer or decimal | Specifies the base value. |
oper2 | integer or decimal | Specifies the exponent value. |
Return value
The ^
function returns the result of raising oper1
to the power of oper2
.
Examples
The following example demonstrates how to use the ^
function to raise 2
to the power of 3
in a Pact REPL:
pact> (^ 2 3)8
pact> (^ 2 3)8
shift
Use shift
to perform a bitwise shift operation on the integer oper1
by oper2
bits.
If oper2
is positive, this function shifts oper1
to the left.
If oper2
is negative, the function shifts oper1
to the right.
Right shifts perform sign extension on signed number types, filling the top bits with 1 if oper1
is negative and with 0 otherwise.
Basic syntax
To shift the integer oper1
by oper2
bits, use the following syntax:
(shift oper1 oper2)
(shift oper1 oper2)
Arguments
Use the following arguments to specify the integer values to be shifted using the shift
Pact function.
Argument | Type | Description |
---|---|---|
oper1 | integer | Specifies the integer value to be shifted. |
oper2 | integer | Specifies the number of bits to shift oper1 by. |
Return value
The shift
function returns the result of shifting oper1
by oper2
bits.
Examples
The following example demonstrates how to use the shift
function to shift the integer 255 to the left by 8 bits:
(shift 255 8)65280
(shift 255 8)65280
The following example demonstrates how to use the shift
function to shift the integer 255 to the right by 1 bit:
(shift 255 -1)127
(shift 255 -1)127
The following example demonstrates how to use the shift
function to shift the negative integer -255 to the left by 8 bits:
(shift -255 8)-65280
(shift -255 8)-65280
The following example demonstrates how to use the shift
function to shift the negative integer -255 to the right by 1 bit:
(shift -255 -1)-128
(shift -255 -1)-128
These examples illustrate how to use the shift
function to perform bitwise shift operations on integers in Pact, either to the left or to the right, with sign extension for right shifts on signed numbers.
sqrt
Use sqrt
to compute the square root of the given value
.
Basic syntax
To calculate the square root of a value, use the following syntax:
(sqrt value)
(sqrt value)
Arguments
Use the following argument to specify the value for which to compute the square root using the sqrt
Pact function.
Argument | Type | Description |
---|---|---|
value | integer or decimal | Specifies the value that you want to compute the square root for. |
Return value
The sqrt
function returns the square root of the specified value.
The return type depends on the type of the input value and whether the square root for the input value is a whole number.
Examples
The following example demonstrates how to use the sqrt
function to calculate the square root of the integer value 25 that returns an integer value:
pact> (sqrt 25)5
pact> (sqrt 25)5
The following example calculates the square root of the decimal value 144.0 that returns a decimal value:
(sqrt 144.0)12.0
(sqrt 144.0)12.0
The following example calculates the square root for 48 and rounds the result to four decimal places:
(round (sqrt 48) 4)6.9282
(round (sqrt 48) 4)6.9282
This example illustrates how to use the sqrt
function to compute the square root of a value in Pact, producing either an integer
or a decimal
result.
subtract (-)
Use -
to negate a value
or to subtract oper2
from oper1
.
Basic syntax
To negate value
, use the following syntax:
(- value)
(- value)
To subtract oper2
from oper1
, use the following syntax:
(- oper1 oper2)
(- oper1 oper2)
Arguments
Use the following arguments to specify the values for negation or subtraction using the -
Pact function.
Argument | Type | Description |
---|---|---|
value | integer or decimal | Specifies the value to be negated. |
oper1 | integer or decimal | Specifies the value to be subtracted from. |
oper2 | integer or decimal | Specifies the value to subtract from oper1 . |
Return value
The -
function returns the negation of the specified value
, or the result of subtracting oper2
from oper1
.
Examples
The following example demonstrates how to use the -
function to negate a value in a Pact REPL:
pact> (- 1.0)-1.0
pact> (- 1.0)-1.0
The following example demonstrates how to use the -
function to subtract integer values in a Pact REPL:
pact> (- 3 2)1
pact> (- 3 2)1
xor
Use xor
to compute the bitwise exclusive OR (xor) operation between two integer arguments.
Basic syntax
To compute the bitwise XOR operation between two integers, use the following syntax:
(xor oper1 oper2)
(xor oper1 oper2)
Arguments
Use the following arguments to specify the integers for the bitwise XOR operation using the xor
Pact function.
Argument | Type | Description |
---|---|---|
oper1 | integer | Specifies the first integer for the XOR operation. |
oper2 | integer | Specifies the second integer for the XOR operation. |
Return value
The xor
function returns the result of the bitwise XOR operation as an integer.
Examples
The following examples demonstrate how to use the xor
function to compute the bitwise XOR operation between two integers:
(xor 127 64)63 (xor 5 -7)-4
(xor 127 64)63 (xor 5 -7)-4