Menu

Extended operators

aditsu

Extended operators

e! (exclamation)

Unique permutations

Arity: 1 • Notation: postfix • Syntax: arg1 e!

Generates all the distinct permutations of an array. If the array has duplicate elements, some of its permutations could end up being identical, but any such duplicate permutations are excluded. If a number n is given instead of an array, it is treated as a range from 0 to n-1. The permutations are generated in lexicographic order.

arg1 Result Example Result
array or string Unique permutations of arg1 "foo"e! ["foo" "ofo" "oof"]
numeric Permutations of 0…arg1-1 2e! [[0 1] [1 0]]

e% (percent)

String formatting

Arity: 2 • Notation: postfix • Syntax: arg1 arg2 e%

Formats a string using a template (format string) and an argument or array of arguments to be inserted into the string. Works like the printf functions in various languages. The full behavior is not specified at this point, but you can assume that the intersection of C's and java's printf behaviour is included (i.e. the parts they agree on).

arg1 arg2 Result Example Result
string non-array arg1 formatted with arg2 as argument "x=%.2f"2.5e% "x=2.50"
string array or string arg1 formatted with arg2 as arguments "(%d-%d)"[2 3]e% "(2-3)"
"hi %s!"["Jon"]e% "hi Jon!"
non-array string arg2 formatted with arg1 as argument 7"%03d"e% "007"
non-string array string arg2 formatted with arg1 as arguments ["a"3]"%s=%d"e% "a=3"

e& (ampersand)

Logical AND

Arity: 2 • Notation: postfix • Syntax: arg1 arg2 e&

Gets the logical AND of two values. Rather than a 0/1 result, it returns the value that decided the result. Practically, it returns the first value if it is false (0 or empty), or the second value if the first value is true. If the result is a block, it is executed. Note that currently the first value can not be a block, because CJam doesn't have automatic boolean evaluation for blocks (yet).

arg1 arg2 Result Example Result
non-block anything arg1 if arg1 is false, else arg2 5{1 3+}e& 4
"""foo"e& ""

e* (asterisk)

Repeat each array item

Arity: 2 • Notation: postfix • Syntax: arg1 arg2 e*

Given an array (or string) and a number, it repeats each item of the array, in place.

arg1 arg2 Result Example Result
array or string numeric arg1 with each item repeated arg2 times "foo"2e* "ffoooo"
numeric array or string arg2 with each item repeated arg1 times 3[2 1]e* [2 2 2 1 1 1]

e< (less than)

Minimum of 2 values

Arity: 2 • Notation: postfix • Syntax: arg1 arg2 e<

Compares two compatible values and returns the smaller one. Arrays/strings are compared lexicographically.

arg1 arg2 Result Example Result
numeric or char numeric or char min(arg1, arg2) (by numeric value) 3 2.5e< 2.5
array or string array or string min(arg1, arg2) (lexicographically) "12""3"e< "12"

e= (equals)

Count occurrences

Arity: 2 • Notation: postfix • Syntax: arg1 arg2 e=

Given an array/string and another value, it counts how many times the value appears in the array (i.e. how many array items are equal to that value).

arg1 arg2 Result Example Result
array or string anything No. of occurrences of arg2 in arg1 "foo"'oe= 2
non-array array or string No. of occurrences of arg1 in arg2 3[1 2]e= 0

e> (greater than)

Maximum of 2 values

Arity: 2 • Notation: postfix • Syntax: arg1 arg2 e>

Compares two compatible values and returns the larger one. Arrays/strings are compared lexicographically.

arg1 arg2 Result Example Result
numeric or char numeric or char max(arg1, arg2) (by numeric value) 64'Ae> 'A
array or string array or string max(arg1, arg2) (lexicographically) "foo""bar"e> "foo"

e[ (left square bracket)

Pad array to the left

Arity: 3 • Notation: postfix • Syntax: arg1 arg2 arg3 e[

Pads an array/string to the left with a given value, up to a given size. If the array is not shorter than that size, it is left alone.

arg1 arg2 arg3 Result Example Result
array or string integer anything arg1 padded to the left with arg3 up to length arg2 "hi"5'*e[ "***hi"

e\ (backslash)

Swap 2 array items

Arity: 3 • Notation: postfix • Syntax: arg1 arg2 arg3 e\

Swaps 2 items of an array/string, based on their positions (indices). The indices wrap around.

arg1 arg2 arg3 Result Example Result
array or string integer integer swap arg1[arg2] with arg1[arg3] "hello"0 9e\ "oellh"
integer array or string integer swap arg2[arg1] with arg2[arg3] -1[2 3 4]1e\ [2 4 3]
integer integer array or string swap arg3[arg1] with arg3[arg2] 0 1["x""y"]e\ ["y""x"]

e] (right square bracket)

Pad array to the right

Arity: 3 • Notation: postfix • Syntax: arg1 arg2 arg3 e]

Pads an array/string to the right with a given value, up to a given size. If the array is not shorter than that size, it is left alone.

arg1 arg2 arg3 Result Example Result
array or string integer anything arg1 padded to the right with arg3 up to length arg2 "hi"5'!e] "hi!!!"

e_ (underscore)

Flatten array

Arity: 1 • Notation: postfix • Syntax: arg1 e_

Expands any nested arrays in a given array. Note that strings are arrays too.

arg1 Result Example Result
array or string flattened arg1 ["hi"['!1[2 3.4]]]e_ ['h'i'!1 2 3.4]

e` (backtick)

RLE encode

Arity: 1 • Notation: postfix • Syntax: arg1 e`

Compresses an array/string using run-length encoding, converting it to an array of [length item] pairs.

arg1 Result Example Result
array or string RLE-compressed arg1 "hiiiii"e` [[1'h][5'i]]

ea

Command-line arguments

Arity: 0 • Syntax: ea

Pushes an array of all the command-line arguments. The array is empty if there were no arguments given.

ed

Debug (show stack contents)

Arity: 0 • Syntax: ed

Prints the string representation of the current contents of the stack as an array, on a separate line. It does not modify the stack.

Example Result
"foo"ed"bar"
Stack: ["foo"]
foobar

ee

Enumerate array

Arity: 1 • Notation: postfix • Syntax: arg1 ee

Enumerates an array/string, converting it to an array of [index item] pairs.

arg1 Result Example Result
array or string array of [i arg1[i]] pairs "hi"ee [[0'h][1'i]]

el

Convert to lowercase

Arity: 1 • Notation: postfix • Syntax: arg1 el

Converts a character or string to lowercase.

arg1 Result Example Result
char lowercase arg1 'Ael 'a
string lowercase arg1 "Hi"el "hi"

er

Transliteration (element replacement)

Arity: 3 • Notation: postfix • Syntax: arg1 arg2 arg3 er

Replaces elements of an array/string, using an array of values to be replaced and another array of values to use instead. The 2nd and 3rd argument can be non-array values, in which case they are treated as a single-element array containing that value. Elements not present in arg2 are left alone, and if arg3 has fewer elements than arg2, the last element of arg3 will be used for the remaining values.

arg1 arg2 arg3 Result Example Result
array or string anything anything replace elements of arg2 with corresponding elements of arg3 in arg1 "hello""lo"'yer "heyyy"

es

Timestamp

Arity: 0 • Syntax: es

Pushes the current time, represented as the number of milliseconds that have passed since the Unix epoch (1970-01-01 00:00:00 UTC).

et

Local time

Arity: 0 • Syntax: et

Pushes an array containing the current year, month, date, hour, minute, second, millisecond, day of week (Sunday=0, Saturday=6) and timezone offset (in milliseconds) in the default timezone.

eu

Convert to uppercase

Arity: 1 • Notation: postfix • Syntax: arg1 eu

Converts a character or string to uppercase.

arg1 Result Example Result
char uppercase arg1 'aeu 'A
string uppercase arg1 "Hi"eu "HI"

ew

Overlapping slices

Arity: 2 • Notation: postfix • Syntax: arg1 arg2 ew

Given an array/string and a number, returns an array of all the slices of the given length from the given array (starting from every possible position).

arg1 arg2 Result Example Result
array or string integer arg1 slices of length arg2 "hello"3ew ["hel""ell""llo"]
integer array or string arg2 slices of length arg1 2[5 4 3]ew [[5 4][4 3]]

e| (pipe)

Logical OR

Arity: 2 • Notation: postfix • Syntax: arg1 arg2 e|

Gets the logical OR of two values. Rather than a 0/1 result, it returns the value that decided the result. Practically, it returns the first value if it is true (non-zero or not empty), or the second value if the first value is false. If the result is a block, it is executed. Note that currently the first value can not be a block, because CJam doesn't have automatic boolean evaluation for blocks (yet).

arg1 arg2 Result Example Result
non-block anything arg1 if arg1 is true, else arg2 0{1 3+}e| 4
5"foo"e| 5

e~ (tilde)

RLE decode

Arity: 1 • Notation: postfix • Syntax: arg1 e~

Decompresses an array of [length item] pairs using run-length encoding.

arg1 Result Example Result
array RLE-decompressed arg1 [[1'h][5'i]]e~ "hiiiii"