GolfScript Built-insBelow is a detailed description of all built-ins along with examples for all different applications of that built-in.These "functions" don't really have arguments but they do typically consume a fixed number of items off the stack. Coerce indicates the two arguments are coerced to the same type, order indicates that if the first arg's type priority is less than the second arg's then swap those args. ~args: 1Bitwise not for integers. 5~ -> -6Evaluate for strings and blocks. "1 2+"~ -> 3 {1 2+}~ -> 3Dump elements for arrays. [1 2 3]~ -> 1 2 3 `args: 1The inverse of ~, generates a string that if evaluated returns the original. Gets its name from python where `...` has a similar effect. 1` -> "1" [1 [2] 'asdf']` -> "[1 [2] \"asdf\"]" "1"` -> "\"1\"" {1}` -> "{1}" !args: 10 [] "" {} yield 1, everything else 0 1! -> 0 {asdf}! -> 0 ""! -> 1 @args: 3desc:Rotates the top 3 elements of the stack so that the 3rd down is now on top. 1 2 3 4 @ -> 1 3 4 2 #args:Not actually a built in variable, it is part of the syntax ignoring everything until newline. $args: 1 or 2If arg is an integer, copys nth item from top of $tack. 1 2 3 4 5 1$ -> 1 2 3 4 5 4For arrays (including strings) a $ort is performed. 'asdf'$ -> "adfs"For blocks, sort by some mapping. [5 4 3 1 2]{-1*}$ -> [5 4 3 2 1] +args: coerceAdds two numbers or concatenate 5 7+ -> 12 'asdf'{1234}+ -> {asdf 1234} [1 2 3][4 5]+ -> [1 2 3 4 5] -args: coerceNote the way - is parsed in the first example. 1 2-3+ -> 1 -1 1 2 -3+ -> 1 -1 1 2- 3+ -> 2 [5 2 5 4 1 1][1 2]- -> [5 5 4] *args: order* can mean many things, the choice of behavior is determined by the type. Multiplication 2 4* -> 8Execute a block a certain number of times, note the order of operands does not matter because these are automatically ordered first. 2 {2*} 5* -> 64Array/string repeat [1 2 3]2* -> [1 2 3 1 2 3] 3'asdf'* -> "asdfasdfasdf"Join [1 2 3]','* -> "1,2,3" [1 2 3][4]* -> [1 4 2 4 3] 'asdf'' '* -> "a s d f" [1 [2] [3 [4 [5]]]]'-'* -> "1-\002-\003\004\005" [1 [2] [3 [4 [5]]]][6 7]* -> [1 6 7 2 6 7 3 [4 [5]]]Fold. Symbol choice for fold comes from ruby golf trick: eval [1,2,3,4,5]*"+". [1 2 3 4]{+}* -> 10 'asdf'{+}* -> 414 /args: order/ can also mean many things based on operand types, usually the opposite of * Division 7 3 / -> 2Split around matches with a second array [1 2 3 4 2 3 5][2 3]/ -> [[1] [4] [5]] 'a s d f'' '/ -> ["a" "s" "d" "f"]Split into groups of a specified size [1 2 3 4 5] 2/ -> [[1 2] [3 4] [5]]Unfold, a slightly complicated function. It is pretty much a while loop except that before each condition test, the top of stack is duplicated, and if that condition check passes the current top of stack is collected into an array. 0 1 {100<} { .@+ } / -> 89 [1 1 2 3 5 8 13 21 34 55 89]Each. Execute a block over all elements. [1 2 3]{1+}/ -> 2 3 4 %args: orderModulus 7 3 % -> 1Split array around another array, like /, but remove empty results. 'assdfs' 's'% -> ["a" "df"] 'assdfs' 's'/ -> ["a" "" "df" ""]Select elements who's index is 0 mod arg, this is essentially to python's array indexing [::number], so like python if it is negative the result will be reversed. [1 2 3 4 5] 2% -> [1 3 5] [1 2 3 4 5] -1% -> [5 4 3 2 1]Map [1 2 3]{.}% -> [1 1 2 2 3 3] |args: coerceBitwise/setwise or 5 3 | -> 7 &args: coerceBitwise/setwise and [1 1 2 2][1 3]& -> [1] ^args: coerceBitwise xor/symmetric set difference [1 1 2 2][1 3]^ -> [2 3] { }Block creation, note that this is not actually a built in variable, instead it is part of the syntax.'Raw string, only \ and ' can be escaped'\n' -> "\\n" ' \' ' -> " ' " "Escaped string"\n" -> "\n" "\144" -> "d" [ ]You might be surprised to learn that [ ] are not built into the syntax. Instead [ marks the stack size when it is executed, and a ] slices the stack back at where it was marked. Anytime the stack is made smaller where it was marked, the [ is moved appropriately. In this example you might expect the result to be 1 2 [], but during the swap those two values are temporarily removed from the stack, causing the [ to move.1 2 [\] -> [2 1] \args: 2Swap top two elements of the stack 1 2 3 \ -> 1 3 2 :args: 1*Assign the value of the top of the stack to the variable immediately after the :. This is built into the syntax not a built-in variable. Does not consume top of stack. 1:a a -> 1 1 1:0;0 -> 1 ;args: 1Pop and discard top of stack. 1 2 3; -> 1 2 <args: orderFor same types, perform less than comparison, pushing 1 for true, 0 for false. Otherwise select elements in an array whose index is less than the integer specified. This is like python's [:n:] usage. 3 4 < -> 1 "asdf" "asdg" < -> 1 [1 2 3] 2 < -> [1 2] {asdf} -1 < -> {asd} >args: orderFor same types, perform greater than comparison, pushing 1 for true, 0 for false. Otherwise select elements in an array whose index is greater than or equal to the integer specified. This is like python's [n::] usage. Note: for mixed types order does matter with these types of operations. 3 4 > -> 0 "asdf" "asdg" > -> 0 [1 2 3] 2 > -> [3] {asdf} -1 > -> {f} =args: orderFor same types, perform equal to comparison, pushing 1 for true, 0 for false. Otherwise select element in an array whose index is equal to the integer specified. If the index is out of bounds it adds nothing to the stack. This is similar python's [n] usage. 3 4 = -> 0 "asdf" "asdg" = -> 0 [1 2 3] 2 = -> 3 {asdf} -1 = -> 102 ,args: 1 or 2Create an array of n elements starting at 0. 10, -> [0 1 2 3 4 5 6 7 8 9]Get the size of an array 10,, -> 10If the argument is a block, take another argument and perform a map, select all original elements in the array whose result was true. 10,{3%}, -> [1 2 4 5 7 8] .args: 1Duplicate top of stack. 1 2 3. -> 1 2 3 3 ?args: orderRaise an integer to a power. 2 8? -> 256What is the index of this element? -1 for none existent. 5 [4 3 5 1] ? -> 2Find. Which element satisfies this condition first? [1 2 3 4 5 6] {.* 20>} ? -> 5 (args: 1De-increment a number. 5( -> 4Uncons for arrays. Uncons is stolen from lisp, it takes the first element out of the array and places the remaining array along with the first element on the stack. [1 2 3]( -> [2 3] 1 )args: 1Increment a number. 5) -> 6Uncons from the right for arrays. [1 2 3]) -> [1 2] 3 and or xorargs: 2Lazy boolean. Implemented as: {1$if}:and; {1$\if}:or; {\!!{!}*}:xor; 5 {1 0/} or -> 5 5 {1 1+} and -> 2 0 [3] xor -> [3] 2 [3] xor -> 0 Convert to a string and print. pargs: 1Convert to a string using ` (inspect) and print followed by variable n. Same deal as ruby. Implemented as: {`puts}:p; nargs: 0Implemented as: "\n":n; putsargs: 1Convert to a string and print followed by variable n. Same deal as ruby. Implemented as: {print n print}:puts; randargs: 1Push random integer up but not including. 5 rand --> 2 doargs: 1A do ... while loop. Execute a block, pop value, if this is true then continue. 5{1-..}do -> 4 3 2 1 0 0 while untilargs: 2Execute a condition block, pop a value, if true/false execute body block. 5{.}{1-.}while -> 4 3 2 1 0 0 5{.}{1-.}until -> 5 ifargs: 3Pop condition value (first arg). If true execute first, otherwise execute second. 1 2 3 if -> 2 0 2 {1.} if -> 1 1 absargs: 1-2 abs -> 2 zipargs: 1 Transpose array rows with columns. [[1 2 3][4 5 6][7 8 9]]zip -> [[1 4 7] [2 5 8] [3 6 9]] ['asdf''1234']zip -> ["a1" "s2" "d3" "f4"] baseargs: 2Unsigned base conversion, second arg is radix. [1 1 0] 2 base -> 6 6 2 base -> [1 1 0] |