Operators
Most of the operators have more than one overload and act differently depending on arguments on stack. Arguments additionaly can be ordered or coerced. Types in Calcscript have priority, from the lowest to highest: number, array, string and block. Ordering takes two arguments from the stack, sorts them according to their priority and places them back on stack. Coercing converts arguments to be of the same type - argument with lower priority will be converted to match the second one.
When converting to boolean value,
0
,
[]
,
''
and
{}
are considered falsy. Everything else is truthy.
+
Arguments: 2, coerced
Arguments | Result | Example | ||
---|---|---|---|---|
number | number | Arithmetic addition of arguments | 2 3+ | 5 |
array | array | Array concatenation | [1 2][3 4]+ | [1 2 3 4] |
string | string | Text concatenation | 'test''this'+ | 'testthis' |
block | block | Block concatenation | 'test'{123}+ | {test 123} |
-
Arguments: 2, coerced
Arguments | Result | Example | ||
---|---|---|---|---|
number | number | Arithmetic substraction of arguments | 2 3- | -1 |
array | array | Removes from first array elements that are in second | [1 2][3 1]- | [1] |
*
Arguments: 2, ordered
Arguments | Result | Example | ||
---|---|---|---|---|
number | number | Arithmetic multiplication of arguments | 2 3* | 6 |
array | number | Creates array that is a multiple copy of original | 2[1 2]* | [1 2 1 2] |
array | array | Join array using second array as separator | [1 2 3][4 5]* | [1 4 5 2 4 5 3] |
string | array | Join array using separator | [1 2]','* | '1,2' |
string | string | Use first string as an array and second as separator | 'test'','* | 't,e,s,t' |
block | number | Executes block given number of times | 2{3}* | 3 3 3 |
block | array | Fold, reduce array to single value using block | [1 2 3 4]{+}* | 10 |
/
Arguments: 2, ordered
Arguments | Result | Example | ||
---|---|---|---|---|
number | number | Arithmetic division of arguments | 2 3/ | 0.667 |
array | number | Split into groups | [1 2 3 2 4]2/ | [[1 2][3 2][4]] |
array | array | Split array around matches | [1 2 3 2 4][2]/ | [[1] [3] [4]] |
block | array | Execute block for each argument in array | [1 2 3]{.}/ | 1 1 2 2 3 3 |
block | block | Unfold, while first block yields true execute second block and gather results into array | 0 1 {20<}{.@+} / | 13 [1 1 2 3 5 8 13] |
%
Arguments: 2, ordered
How to input: Long click on
*
Arguments | Result | Example | ||
---|---|---|---|---|
number | number | Arithmetic modulo of arguments | 7 3% | 1 |
array | number | Take every n-th element from array | [1 2 3 4 5]2% | [1 3 5] |
string | string | Split string around matches, drop empty elements | 't,e,s,t'','% | ['t' 'e' 's' 't'] |
block | array | Map. Execute block for each argument in array and gather results | [1 3 5]{2*}% | [2 6 10] |
$
Arguments: 1
How to input: Long click on
+
Arguments | Result | Example | ||
---|---|---|---|---|
number | Get n-th element from stack | 1 2 3 4 2$ | 2 | |
array | Sort array elements | [5 3 1 6 2]$ | [1 2 3 5 6] | |
string | Sort letters in string | 'test'$ | 'estt' | |
block | array | Sort array using block as mapping | [5 3 1 6 2]{-1*}$ | [6 5 3 2 1] |
,
Arguments: 1
How to input: Long click on
;
Arguments | Result | Example | ||
---|---|---|---|---|
number | Create array from 0 to given number | 5, | [0 1 2 3 4] | |
array | Get length of an array | [0 1 2 3 4], | 5 | |
block | array | Filter. Get elements from array that return true when executed by block | [0 1 2 3 4]{2%}, | [1 3] |
~
Arguments: 1
How to input: Long click on
-
Arguments | Result | Example | |
---|---|---|---|
number | Bitwise negate given number | 5~ | -6 |
array | Pop array values onto stack | [1 2 3]~ | 1 2 3 |
string | Evaluate string as script | '1 2+'~ | 3 |
block | Execute block | {1 2+}~ | 3 |
?
Arguments: 2, ordered
How to input: Long click on
.
Arguments | Result | Example | ||
---|---|---|---|---|
number | number | Raise number to given power | 2 8? | 256 |
array | number | Get index of element | [1 2 3 4]2? | 1 |
block | array | Get element that first passes given condition | [1 2 3 4]{2%}? | 1 |
|
Arguments: 2, ordered
How to input: Long click on
7
Arguments | Result | Example | ||
---|---|---|---|---|
number | number | Bitwise OR | 2 8| | 10 |
array | array | Set union | [1 2 3][2 4]| | [1 2 3 4] |
&
Arguments: 2, ordered
How to input: Long click on
8
Arguments | Result | Example | ||
---|---|---|---|---|
number | number | Bitwise AND | 2 8& | 0 |
array | array | Set intersection | [1 2 3][2 4]& | [2] |
^
Arguments: 2, ordered
How to input: Long click on
9
Arguments | Result | Example | ||
---|---|---|---|---|
number | number | Bitwise XOR | 2 8^ | 10 |
array | array | Set symmetric difference | [1 2 3][3 4]^ | [1 2 4] |
<
Arguments: 2, ordered
How to input: Long click on
[
Arguments | Result | Example | ||
---|---|---|---|---|
number | number | Compare values | 2 8< | 1 |
array | number | Get first n elements of an array | [1 2 3]2< | [1 2] |
string | string | Compare values | 'test''abc'< | 0 |
>
Arguments: 2, ordered
How to input: Long click on
]
Arguments | Result | Example | ||
---|---|---|---|---|
number | number | Compare values | 2 8> | 0 |
array | number | Remove first n elements from an array | [1 2 3]2> | [3] |
string | string | Compare values | 'test''abc'> | 1 |
=
Arguments: 2, ordered
How to input: Long click on
=
Arguments | Result | Example | ||
---|---|---|---|---|
number | number | Compare values | 2 8= | 0 |
array | number | Get element of given index, -1 if not found | [1 2 3]2= | 3 |
string | string | Compare values | 'test''abc'= | 0 |
(
Arguments: 1
How to input: Long click on
{
Arguments | Result | Example | |
---|---|---|---|
number | Decrement value | 8( | 7 |
array | Extract first element from array | [1 2 3]( | 1[2 3] |
)
Arguments: 1
How to input: Long click on
}
Arguments | Result | Example | ||
---|---|---|---|---|
number | Increment value | 8) | 9 | |
array | Extract last element from array | [1 2 3]( | 3[1 2] |
`
Arguments: 1
How to input: Long click on
⇧
Arguments | Result | Example | |
---|---|---|---|
any | Convert to string | 8` | '8' |
!
Arguments: 1
How to input: Long click on
:
Arguments | Result | Example | |
---|---|---|---|
any | Negate value. 0, [], '' and {} will result in 1, otherwise 0 | 8! | 0 |
.
Arguments: 1
Arguments | Result | Example | |
---|---|---|---|
any | Duplicate value on top of the stack | [1 2 3]. | [1 2 3][1 2 3] |
@
Arguments: 3
How to input: Long click on
0
Arguments | Third argument | Result | Example | |||
---|---|---|---|---|---|---|
any | any | any | Rotate arguments on top of the stack. Extract third argument to the top. | 1 2 3@ | 2 3 1 |
\
Arguments: 2
How to input: Long click on
/
Arguments | Result | Example | ||
---|---|---|---|---|
any | any | Swap two arguments on top of the stack. | 1 2 3\ | 1 3 2 |
;
Arguments: 1
Arguments | Result | Example | |
---|---|---|---|
any | Remove top of the stack. | 1 2 3; | 1 2 |
[
Arguments: 0
Arguments | Result | Example | |
---|---|---|---|
(none) | Marks position of the stack for slicing. | [1 2 3] | [1 2 3] |
]
Arguments: 0
Arguments | Result | Example | |
---|---|---|---|
(none) | Slices stack where it has been marked. | [1 2 3] | [1 2 3] |
abs
Arguments: 1
Arguments | Result | Example | |
---|---|---|---|
number | Returns absolute value of an number. | -3abs | 3 |
and
Arguments: 2
Arguments | Result | Example | ||
---|---|---|---|---|
any | any | Lazy and - returns first argument if it's falsy otherwise returns second. | 4 {1.} and | 1 1 |
or
Arguments: 2
Arguments | Result | Example | ||
---|---|---|---|---|
any | any | Lazy or - returns first argument if it's truthy otherwise returns second. | 4 {1.} or | 4 |
xor
Arguments: 2
Arguments | Result | Example | ||
---|---|---|---|---|
any | any | Exclusive or - returns one of the arguments
that is truthy. If both are truthy
returns 0 .
|
4 {1.} xor | 1 0 |
do
Arguments: 1
Arguments | Result | Example | |
---|---|---|---|
block | Executes block, pops a value from the stack, if it's truthy then repeat. | 5{1-..}do | 4 3 2 1 0 0 |
if
Arguments: 3
Arguments | Result | Example | |||
---|---|---|---|---|---|
any | any | any | If first argument is truthy then execute second, otherwise execute third. | 5 1 {2 0/}if | 1 |
while
Arguments: 2
Arguments | Result | Example | ||
---|---|---|---|---|
block | block | Executes first block (condition). If result is truthy then execute second. Repeat. | 5{.}{1-.}while | 4 3 2 1 0 0 |
until
Arguments: 2
Arguments | Result | Example | ||
---|---|---|---|---|
block | block | Executes first block (condition). If result is falsy then execute second. Repeat. | 5{.}{1-.}until | 5 |
rand
Arguments: 1
Arguments | Result | Example | |
---|---|---|---|
number | Returns a random number from 0 up
to (but not including) given number.
|
5rand | 3 |
zip
Arguments: 1
Arguments | Result | Example | |
---|---|---|---|
array | Swap array rows with columns. | [[1 2][3 4][5 6]]zip | [[1 3 5][2 4 6]] |
pi
Arguments: 0
Arguments | Result | Example | |
---|---|---|---|
(none) | Returns a value of π. | pi | 3.141 |
sin
Arguments: 1
Arguments | Result | Example | |
---|---|---|---|
number | Returns a math sine value for given number. | 0.3sin | 0.296 |
cos
Arguments: 1
Arguments | Result | Example | |
---|---|---|---|
number | Returns a math cosine value for given number. | 0.3cos | 0.955 |
tan
Arguments: 1
Arguments | Result | Example | |
---|---|---|---|
number | Returns a math tangent value for given number. | 0.3tan | 0.309 |
asin
Arguments: 1
Arguments | Result | Example | |
---|---|---|---|
number | Returns an inverse math sine value for given number. | 0.296asin | 0.3 |
acos
Arguments: 1
Arguments | Result | Example | |
---|---|---|---|
number | Returns an inverse math cosine value for given number. | 0.3acos | 1.266 |
atan
Arguments: 1
Arguments | Result | Example | |
---|---|---|---|
number | Returns an inverse math tangent value for given number. | 0.995atan | 0.782 |
sinh
Arguments: 1
Arguments | Result | Example | |
---|---|---|---|
number | Returns a math hyperbolic sine value for given number. | 0.3sinh | 0.304 |
cosh
Arguments: 1
Arguments | Result | Example | |
---|---|---|---|
number | Returns a math hyperbolic cosine value for given number. | 0.3cos | 1.045 |
tanh
Arguments: 1
Arguments | Result | Example | |
---|---|---|---|
number | Returns a math hyperbolic tangent value for given number. | 0.3tanh | 0.291 |
e
Arguments: 0
Arguments | Result | Example | |
---|---|---|---|
(none) | Returns a value of E. | e | 2.718 |
ln
Arguments: 1
Arguments | Result | Example | |
---|---|---|---|
number | Returns a value of natural logarithm. | e ln | 1 |
log
Arguments: 2
Arguments | Result | Example | ||
---|---|---|---|---|
number | number | Returns a value of logarithm for given number and base. | 32 2log | 5 |
exp
Arguments: 1
Arguments | Result | Example | |
---|---|---|---|
number | Raises E to a given power. Equivalent of e x? . |
2exp | 7.389 |
sum
Arguments: 1
Arguments | Result | Example | |
---|---|---|---|
array | Returns a sum for all numbers in array. | [1 2 3] sum | 6 |
base
Arguments: 2
Arguments | Result | Example | ||
---|---|---|---|---|
number | number | Convert number to different base. | 6 2 base | [1 1 0] |
array | number | Interprets an array as a number in given base. | [1 1 0] 2 base | 6 |
hex
Arguments: 1
Arguments | Result | Example | |
---|---|---|---|
number | Converts a number to a hexadecimal string. | 123hex | 'x7b' |
bin
Arguments: 1
Arguments | Result | Example | |
---|---|---|---|
number | Converts a number to a binary string. | 123bin | 'b1111011' |
oct
Arguments: 1
Arguments | Result | Example | |
---|---|---|---|
number | Converts a number to an octal string. | 123oct | 'o173' |
dec
Arguments: 1
Arguments | Result | Example | |
---|---|---|---|
string | Converts a string from hexadecimal, binary or octal format to a number. | 'xcafe'dec | 51966 |