CJam
GolfScript rebuilt in Java, with characters as a type, more one-letter commands and fewer surprises.
aditsu, 2014 · Turing complete: yes · ●●○ moderate · esolangs.org
Tested with 8 known programs · Hello World on Try It Online: identical · Test method
Run it
The idea
CJam was made by aditsu in 2014 as a successor to GolfScript. The programs look much the same: a row of one-character commands that work on a stack, with the meaning of each command depending on the types it finds. For years it was one of the strongest languages on the code golf site.
Read the story
The changes are practical. Characters are a type of their own, so a string is an array of characters and 'a is a character literal. The preset variables give cheap constants: A is 10, S a space, N a newline, W is −1. Many letters became commands: q reads all input, l a line, i makes an integer, p prints a value as code.
The machine
One stack and 26 variables. At the end the whole stack is printed, element after element.
| Op | Numbers | Arrays and strings | Blocks |
|---|---|---|---|
+ - | Add, subtract | Join, remove elements | |
* | Multiply | Repeat; join with a separator | Run n times; fold over an array |
/ % | Divide, remainder | Split, chunks; every nth (W% reverses) | For each; map |
, | Range 0 … n−1 | Length | Filter an array |
( ) | Step down, up | Take off the first, the last | |
= < > | Compare | Index (it wraps around), take, drop | |
_ ; \ @ $ | Duplicate, drop, swap, rotate three, copy the nth | Sort with $ | Sort by a key |
~ | Bitwise not | Evaluate a string, dump an array | Run it |
? g h w | If, do while, while |
:+ folds + over an array, :i maps i over it, and 2f* multiplies every element by 2.
Example
The string is left on the stack and printed at the end.
The string is left on the stack and printed at the end.
q~ reads the input and evaluates it to 10, ) makes it 11, , the range 0 to 10, and :+ adds them.
The standard quine. _ copies the block and ~ runs one copy, which pushes _~. A block prints as its source in braces.
Fibonacci: 2$ copies the number below the top, + adds, eight times. ] gathers the stack into an array and S* joins it with spaces.
This implementation is a subset. It has the core commands above and stops with a reason on anything else, such as floating point numbers, the e, m and . families of commands or the rarer letters, rather than guess what the Java interpreter would do.
Patterns it teaches
- Code golf: one character per idea, and types that pick the meaning.
- Concatenative: a program is functions applied to a stack in a row.
- Stack machines: input is read on request, output is the stack.
- Quine: a block prints as its own source, so a quine is seven characters.
Try this
- Print the squares of 0 to 9, separated by spaces.
- Reverse each word of a line of input.
- Hard: print the factorial of the number in the input.
Further
- esolangs.org/wiki/CJam
- The reference interpreter, in Java, with the full list of commands
- Related: GolfScript
See also
Golfing languages
Stack-based
Write your own in Run Convert a program from CJam
Implementation
JavaScript interpreter, source and command line
A JavaScript interpreter, used in the browser and on the command line. Id: cjam.
Command line
shib run cjam -e 'q' --input 'a cat'shib manual cjamSource
- Module
langs/tio/ports.js- Interpreter
langs/tio/cjam.js
Docs: Kernel instructions · The .shib format · Command line · Adding a language
Source esolangs.org (CC0) · Edit on esolangs.org