Menu

Operators

aditsu

CJam has many predefined operators (or functions, or built-ins in GolfScript parlance) that use the stack and the variables. Most operators use postfix notation (i.e. they operate on the parameters previously pushed on the stack), but there are a couple of infix operators too (they use one or more parameters from the stack and a parameter that follows the operator). The main operators are named with a single character - either a special character or a lowercase letter - but other operators use 2 characters.
Many operators have the same meaning as in GolfScript, but there are a few differences too.

Here is a list of predefined operators in ASCII order:
! - boolean NOT
# - power, find index
$ - copy from stack, sort
% - mod, split, map
& - bitwise AND, set intersection, if-then
( - decrement, uncons from left
) - increment, uncons from right
* - multiply, join, repeat, fold (reduce)
+ - add, concat
, - range, length, filter
- - subtract, remove
. - vectorize
/ - divide, split, each
: - set variable, quick map, quick fold
; - pop and discard
< - compare (less), slice before
= - compare (equal), get array item, find value
> - compare (greater), slice from
? - if (ternary)
@ - rotate top 3 elements on the stack
[ - start array
\ - swap top 2 elements on the stack
] - end array
^ - bitwise XOR, symmetric difference
_ - duplicate top element on the stack
` - string representation
a - wrap in array
b - base conversion
c - convert to char
d - convert to double
e followed by numeric literal - scientific notation
f - for loop, map with extra parameter
g - do-while loop that pops the condition, signum, get from url
h - do-while loop that leaves the condition on the stack
i - convert to integer
j - memoized recursion
l - read line
m followed by numeric literal - subtract/remove (same as "-" but avoids the need for a space after it)
o - print
p - print string representation and newline
q - read the whole input
r - read token (whitespace-separated)
s - convert to string (char array)
t - set array item
w - while loop
z - zip (transpose), abs
| - bitwise OR, set union, if-else
~ - bitwise NOT, eval, dump array

"Extended" 2-character operators:
e! - unique permutations
e# - line comment (not really an operator)
e% - string formatting a la printf
e& - logical AND
e* - repeat each array item
e< - min of 2 values
e= - count occurrences
e> - max of 2 values
e[ - pad array to the left
e\ - swap 2 array items
e] - pad array to the right
e_ - flatten array
e` - RLE encode
ea - command-line args
ed - debug (show stack contents)
ee - enumerate array
el - lowercase
er - transliteration (element replacement)
es - timestamp (milliseconds from the epoch)
et - local time
eu - uppercase
ew - overlapping slices
e| - logical OR
e~ - RLE decode

"Math" 2-character operators:
m! - factorial, permutations with duplicates
m* - cartesian product, cartesian power
m< - bit shift, rotate left
m> - bit shift, rotate right
mC - arccos
mF - factorization with exponents
mL - log with base
mO - round with precision
mQ - integer square root
mR - random choice
mS - arcsin
mT - arctan
m[ - floor
m] - ceil
ma - atan2
mc - cos
md - divmod
me - exp
mf - factorization
mh - hypot
ml - ln (natural logarithm)
mo - round
mp - prime
mq - sqrt
mr - random number, shuffle
ms - sin
mt - tan

More operators to be implemented later.


Related

Wiki: Differences from GolfScript
Wiki: Home
Wiki: Syntax