Jelly
A tacit golfing language with its own 256-character code page. Functions are glued without naming their arguments, and the gluing rules depend on how many arguments each piece takes.
Dennis Mitchell, 2015 · Turing complete: yes · ●●● hard · esolangs.org
The idea
Dennis Mitchell made Jelly in 2015. He was one of the most active golfers on Code Golf Stack Exchange, and Jelly quickly became one of the languages that wins most often there. It is implemented in Python and inspired by J, the array language from the APL family.
Read the story
Jelly is tacit: you almost never name a variable or an argument. A program is a chain of functions, and the way the chain is evaluated depends on the arity of each piece: how many arguments it takes. A nilad takes none (a constant), a monad takes one, a dyad takes two. A fixed set of rules decides, from the pattern of arities, what gets applied to what.
To make every symbol count, Jelly has its own code page of 256 characters, each one byte: ASCII letters and digits, plus accented letters, Greek letters and symbols like þ, € and Ɱ. A Jelly answer of 5 "characters" is scored as 5 bytes.
The machine
- Atoms are the built-in functions:
R(range 1..n),S(sum),P(product),!(factorial),²(square),+,×, and hundreds more. Most of them work on whole lists at once, as in J. - Quicks are modifiers that take atoms and make new ones:
/reduces a list with a dyad,€maps over each element,þbuilds a table. - Links are lines of the program. The last line is the main link; others can be called from it.
- Chains. Inside a link, atoms are read left to right and grouped by arity. For a chain of monads the rule is simple: apply them in order. Mixing in dyads and nilads brings in rules for which value goes on which side.
- Input and output. Command-line arguments become the main link's arguments. The final value is printed.
- Strings. A literal starts with
“. Different closing characters make an ordinary string or a compressed one; compressed strings index into a built-in dictionary of English words, which is why Jelly Hello Worlds are so short.
Example
The sum of 1 to n:
The sum of 1 to n:
RS
With the argument 10: R turns 10 into [1, 2, …, 10], and S sums it. Two monads in a row are applied in order, so the result is 55. Two bytes.
The sum of squares is one byte more: R²S gives 385 for 10, since ² squares each element.
Factorial is a single atom, !, and a plain Hello World is a string literal left open at the end of the program, where it closes by itself:
“Hello, World!
These were traced by hand from the atom definitions; there is no Jelly implementation here. The reference interpreter (and the online service Try It Online) will run them.
Patterns it teaches
- Code golf: the state of the art. Huge built-in libraries, a custom code page, compressed strings.
- Combinators: tacit programming is combinator programming. The chain rules play the role that S, K and friends play in Unlambda: they route arguments without naming them.
- Encoding tricks: a private 256-symbol code page, so that every byte is a different instruction.
Try this
- What does
R×do with argument 4? Look up the rule for a dyad inside a monadic chain first. - Write the product of 1 to n in two bytes, then in one.
- Hard: write a link that tests whether a number is prime, using only atoms you can find in the Jelly wiki on GitHub.
Further
- esolangs.org/wiki/Jelly
- The reference implementation and its wiki (atoms, quicks, chain rules, code page) on GitHub, under DennisMitchell/jellylanguage.
- Related: GolfScript, Joy.
See also
APL-like
Golfing languages
Source esolangs.org (CC0) · Edit on esolangs.org