Invincible Summer |||

What is a life? Two billion heartbeats. Perhaps four thousand books. And how many keystrokes?

Q is an array-programming language optimised for timeseries. It is implemented as a domain-specific PL embedded in k4, a descendant of APL and Lisp. Q is fast, functional, and brutally terse. You will love it.

Salient features:

  • Implicit iteration Most q primitives have Map built into them and just iterate across arrays. You don’t need to tell q to loop through a list.
  • Iteration operators provide other iteration patterns such as Map/Reduce and Fold.
  • Republic of operators With a rich set of primitives you don’t want to memorise another operator precedence hierarchy. So q doesn’t have one.
  • Prefix notation clarifies the common case where the result of one function is the argument of the next. Instead of writing sum[cos[2+first[list]]] write sum cos 2+first list.
  • First-class objects include tables, dictionaries and lambdas.
  • Application and indexing are the same thing, and the syntax reflects it, a fundamental insight of the language. Discover what this makes possible in your code.
  • Vector literals Q takes vectors seriously. You don’t construct a boolean vector as (true;false;true) but as 101b; the first natural numbers are not (1,2,3) but 1 2 3. You will love the way your q code looks.

Here’s q code to draw the Mandelbrot Set in ASCII, adapted from Michal Wallace.

s:{(.[-]x*x),2*prd x}            / complex square (x is R,Im pair)
m:{floor sqrt sum x*x}           / magnitude (distance from origin)

d: 120 60                        / dimensions of the picture
t: -88 -30                       / camera translation
f: reciprocal 40 20              / scale factor

c: (,/:\:) . f * t + til each d  / complex plane near mandelbrot set
z: d # enlist 0 0                / 3d array of zeroes in same shape
mb: c+ (s'')@                    / Mandelbrot: s(z) + c
r: 1 _ 8 mb\z                    / collect 8 times

o: " 12345678"@ sum 2<m'''[r]    / "color" by how soon point "escapes"

-1 "\n"sv flip o;                / transpose and print the output

Here’s the result. (Compare solutions in other languages at Rosetta Code.)

Mandelbrot Set in ASCII

Save q tables to file and you get kdb+, for two decades the world’s fastest column-store timeseries database. Q includes SQL-like queries built from functional equivalents of greater power and generality.

Q is implemented in C, runs close to the metal, and is bewilderingly fast. For most of its life it has been restricted mostly to financial institutions willing to pay a premium for extreme speed.

I’ve been blessed with access to a q licence and because of its elegance and economy it has become my go-to language for hacking and solving problems.

Now q is becoming more widely accessible through cloud-based services, and the kdb+ Personal Edition is available for free download. You might want to try it yourself.

Up next The return of the king
Latest posts Y q? The return of the king Clarity, rigour and Rory Stewart The World’s End Book Club Remembering Bel Macdonald Barts hearts and faces Summer on wheels All that jazz: The librarian’s song Sandals on their way home A short history of the Australian Flat White Cycling glove, slightly foxed Untoward occurrence at embassy poetry reading To Go to Lvov The founding of Iverson College The pot-boy’s story Prisoners of our own device How green is my valley The ghost in the shell Policing protests in Glasgow Why don’t we do it in the road? The democracy of narcissism The rest is silence Bergh Apton revisited What makes a language flourish? A duty of care Crispbread economics Learning vector programming The Underground The Post Office scandal A plea for simplicity BoJo the clown