New Operators in CJam 0.6.5¶
Vector operators¶
.
can be applied before a binary operation to vectorise it so that it performs operations element-wise.
[1 2 3] [7 6 2] .+ -> [8 8 5]
[1 2 3] [7 6 2] .* -> [7 12 6]
[1 2 3] [7 6 2] .# -> [1 64 9]
[1 2 3] [7 6 2] .e< -> [1 2 2]
[1 2 3] [7 6 2] .\ -> [7 1 6 2 2 3]
Since .
is just a modifier, it can be chained.
[[1 2 3] [7 6 2]] [[8 9 1] [2 0 3]] ..+ -> [[9 11 4] [9 6 5]]
w
– while loops¶
Takes two blocks – a condition followed by a loop body – and performs a while loop. The condition is tested for before each loop iteration, and is consumed like g
.
For example,
1 {_5<} {_'X*N+o)} w ;
prints a triangle of X
s by incrementing a number until it is no longer less than 5.
X
XX
XXX
XXXX
mQ
– integer square root¶
Take an integer or double and finds the square root, truncated to int.
99 mQ -> 9
100 mQ -> 10
3.14 mQ -> 1
mR
– random choice¶
Given an array, chooses a (pseudo-)random element, for example:
[1 2 3 4] mR -> 2
e%
– string formatting a la printf¶
Takes a format string and an element/array and performs string formatting using printf.
"(%d, %d)" [1 2] e% -> (1, 2)
"%.08f" 3.14 e% -> 3.14000000
e*
– repeat each item¶
Given an array A
and a number n
, returns a new array which consists of every element of A
repeated n
times.
[1 2 3] 5 e* -> [1 1 1 1 1 2 2 2 2 2 3 3 3 3 3]
["abc" "def"] 3 e* -> ["abc" "abc" "abc" "def" "def" "def"]
e\
– swap array items¶
Given an array and two indices, swaps two elements of the array at the specified indices.
[0 1 2 3 4] 1 4 e\ -> [0 4 2 3 1]
e=
– count occurrences¶
Given an array and an element, counts the number of times the element appears in the array.
[0 0 1 0 2 2] 0 e= -> 3
[[0 1] [1 0] [1 1] [0 1]] [0 1] e= -> 2
e!
– permutations¶
Given an array, returns an array consisting of all unique permutations, in lexicographical order.
[3 1 2] e! -> [[1 2 3] [1 3 2] [2 1 3] [2 3 1] [3 1 2] [3 2 1]]
"BAA" e! -> ["AAB" "ABA" "BAA"]
If given a number, an implicit call to ,
(range) is performed first.
3 e! -> [[0 1 2] [0 2 1] [1 0 2] [1 2 0] [2 0 1] [2 1 0]]
m!
– factorial and permutations with duplicates¶
Given an array, returns an array consisting of all possible permutations. Unlike e!
, this is formed by applying each permutation in lexicographical order to the array, and hence the result may not necessarily be sorted.
[3 1 2] m! -> [[3 1 2] [3 2 1] [1 3 2] [1 2 3] [2 3 1] [2 1 3]]
"BAA" m! -> ["BAA" "BAA" "ABA" "AAB" "ABA" "AAB"]
Given a number, finds the factorial of the number. Note that the number is truncated, so this is not equivalent to the gamma function.
5 m! -> 120
3.14 m! -> 6
e_
– flatten¶
Flattens an array completely, no matter how nested it is.
[[1 2 3] [[4] [[5] [6]]]] e_ -> [1 2 3 4 5 6]
e`
and e~
– run length encode/decode¶
e`
encodes a sequence using run-length encoding, which keeps counts of the number of times the same element appears in a row.
"AAAABCCCAAC" e` -> [[4 'A] [1 'B] [3 'C] [2 'A] [1 'C]]
[[1 0] [1 0] [1 1] [1 1] [1 1]] e` -> [[2 [1 0]] [3 [1 1]]]
e~
does the opposite, and decodes a run-length encoding.
[[4 'A] [1 'B] [3 'C] [2 'A] [1 'C]] e~ -> "AAAABCCCAAC"
[[2 [1 0]] [3 [1 1]]] e~ -> [[1 0] [1 0] [1 1] [1 1] [1 1]]
ew
– overlapping slices¶
Given an array and a slice length, returns all overlapping slices of the array with the given length.
[1 2 3 4 5 6] 3 ew -> [[1 2 3] [2 3 4] [3 4 5] [4 5 6]]
e[
and e]
– left/right array padding¶
Pads an array to a given length with a specified element.
"abc" 10 '0 e[ -> "0000000abc"
[1 2 3 4] 7 0 e[ -> [0 0 0 1 2 3 4]
"abc" 10 '0 e] -> "abc0000000"
[1 2 3 4] 7 0 e] -> [1 2 3 4 0 0 0]
m*
with array and number – Cartesian power¶
Given an array and a number, returns the Cartesian product of the array repeated the given number of times. Alternatively, this is all arrays of the given length consisting of elements from the specified array. Elements can be repeated.
[0 1] 3 m* -> [[0 0 0] [0 0 1] [0 1 0] [0 1 1] [1 0 0] [1 0 1] [1 1 0] [1 1 1]]
"AAB" 2 m* -> ["AA" "AA" "AB" "AA" "AA" "AB" "BA" "BA" "BB"]
ee
– enumerate¶
Given an array, returns an array of [index element]
pairs.
"ABCDEFG" ee -> [[0 'A] [1 'B] [2 'C] [3 'D] [4 'E] [5 'F] [6 'G]]
&
and |
for blocks – “if” with only “then” or only “else”¶
&
checks if a condition is truthy, and if so executes the block.
100 0 {3+} & -> 100
100 1 {3+} & -> 103
|
checks if a condition is falsy, and if so executes the block.
100 0 {3+} | -> 103
100 1 {3+} | -> 100