
K programming language (2005) - jxub
http://archive.vector.org.uk/art10010830
======
beagle3
This concise article describes K2 (version 2 of the K language), and, at the
level it describes, little has changed, so it is still a good introduction to
the language 12 years later.

However, beyond the basics, a lot has changed since (current commercial
version is K4, current dev is K6 AFAIK): Dictionaries have become better
integrated in the language, the database layer was rolled into the language,
the integrated (bare bones and milspec-looking but ultra effective) electric
GUI was dropped, http and web (as both client and server) were added to the
core, 64-bit integers, nanosecond timestamps, GUIDs were added as core types,
and probably a few more things I forgot.

If you find this article interesting, you may want to experiment with
JohnEarnest's ok [0], which provides graphical playground (implemented in JS
and runs everywhere) and also read Q for mortals[1] - Q is a syntactic-sugar
version of K4, though still the same language underneath.

[0] [https://github.com/JohnEarnest/ok](https://github.com/JohnEarnest/ok)

[1] [http://code.kx.com/q4m3/](http://code.kx.com/q4m3/)

~~~
throwaway7645
Why remove the GUI stuff?

~~~
RodgerTheGreat
No commercial demand.

The K2 GUI was very handy for programmers, for rapid prototyping, but it is
impractical to use it for making visually polished UIs you would give to a
customer.

If a feature of K doesn't pull its weight, it is removed.

~~~
throwaway7645
Gotcha...I would assume to leave it in for use in interactive data analysis,
but if you REALLY want to keep things small...

~~~
RodgerTheGreat
Arthur starts over completely from scratch with each major release, and abhors
drawing in dependencies. It's a good strategy for traveling light.

------
icen
If you want to try out K, you can either go to Kx and download the 32bit
version of kdb+. \ at the prompt (on its own) will enter K mode. This uses K4.

Alternatively, there's the free and open source Kona, which is an
implementation of K3, the preceding version.

[https://kx.com/download](https://kx.com/download)

[https://github.com/kevinlawler/kona](https://github.com/kevinlawler/kona)

~~~
rkowalick
I think my favorite way of trying K is John Earnest’s JS-based interpreter Ok:

[http://johnearnest.github.io/ok/index.html](http://johnearnest.github.io/ok/index.html)

------
dasmoth
Arthur Whitney seems to have been developing this stuff pretty steadily.
There's a sparse-but-quite-informative website at
[http://kparc.com/](http://kparc.com/) where you could follow the development
of the K5 and K6 dialects. It's been pretty quiet the last year or so, but
there was a comment on HN a month or two back which suggests that K7 could be
on its way.

For playing around with the basic constructs,
[https://github.com/JohnEarnest/ok](https://github.com/JohnEarnest/ok) is nice
(and itself pleasingly concise). Currently targeting the K6 dialect, I think.
It doesn't really have any of the "database" side of things, though.

~~~
throwaway7645
Any news on kOS? I'd love to build a barebones high performance computer with
K running on bare metal.

~~~
dasmoth
Nothing I’ve seen recently. Seemed to be tied to K5, and there’s been at least
one re-write since then.

~~~
scottlocklin
Pretty sure the idea is done; Art's working on other things at the moment.

------
Athas
Does anyone know of a good description of which parts of K's implementation
makes it so fast? I have heard the interpreter itself is quite small, and
easily fits in L1 CPU cache, which helps of course. Are the primitives further
implemented using vector instructions or multi-threading? Does the K
interpreter pattern-recognize compositions of constructs and dispatch to an
optimized implementation, like with APL "idioms"?

~~~
geocar
> I have heard the interpreter itself is quite small which helps of course

It helps a lot more than a lot: Small is everything.

"Main memory" is something like 1000x slower than "L1 CPU cache", so if your
whole program lives in L1 you only pay to receive data, which streams in as
fast as main memory can. How can you possibly go any faster than that?

The interpreter looks a lot like this[1], scan a character, do something with
it. There's no scanning phase, or AST, or very much analysis at all. Being
simple helps keep it small. Writing dense makes it easy to see (without
scrolling) similar code which can be refactored into less space. This is how
small (dense) source code can also help make small object code. This is how
small (dense) source code is fast!

[1]: nsl.com/papers/origins.htm

Once you've done all that, vector instructions and multi-threading can help
eke out a little bit more speed. Recognising a couple characters at a time and
treating them specially can sometimes help as well, but it also can cost a lot
of object size quickly, so there needs to be some balance.

~~~
Athas
You can "go faster" than memory bandwidth by accessing memory more
intelligently (that is, by reducing the amount of memory traffic, possibly by
improving cache behaviour). The classic example is loop tiling for matrix
multiplication. Here you write significantly more code, but reduce accesses to
main memory by a good constant factor. Sadly, with modern architectures, just
keeping things small and simple is rarely the way to peak performance. My
experience from doing high-performance programming (mostly on GPUs) is that
you have a very generous budget when it comes to how large your code can be,
but you have an extremely tight budget when it comes to how large your data
is. (Of course, in absolute terms you access significantly more data than
code, but you generally mostly worry about data size, not so much code size.)

------
throwaway7645
I wish there was a way to use this at my company without the monstrous price
tag. I also wish kdb+ came with much better dashboard support kinda like
Tableau...I want to retrieve and mold my data quickly with kdb+'s query
language and once it is in the proper form, play with several chart types.

~~~
gaius
The 32-bit edition is free

~~~
icen
Can't be used commercially.

~~~
np_tedious
You can build a PoC/argument for why your organization should use it
commercially

~~~
throwaway7645
Very difficult with large IT departments to get something unusual like this
since you need hardware, a way to transfer data from your main database into
an SSD to really take advantage of kdb+. Doing that would step on the toes of
several departments and would require a large project to happen. Then you have
the problem where many people aren't array language savvy. If kOS had a better
way to build real time dashboards I would pay more attention. I really wish I
could get a personal setup at work to do this (on the cheap), but classic IT
really hates things outside their cotrol.

~~~
gaius
_classic IT really hates things outside their cotrol._

[https://en.wikipedia.org/wiki/Shadow_IT](https://en.wikipedia.org/wiki/Shadow_IT)

The author of that article has a very biased view tho'. An organisation that
finds a lot of shadow IT happening probably ought to replace the head of the
IT department since he or she is clearly not delivering what the users
actually need.

From the link: _Shadow IT can act as a brake on the adoption of new
technology_

LOL!!!

~~~
throwaway7645
IT should exist to help the domain users, but after a point they get too big
and start to dictate what gets used even if nobody wants it. Like choosing the
data analytics technology for the data analytics team when it is what they
don't want/need. So then your data analytics team eventually goes outside
budget to get what they actually wanted and you have two products. Very
dysfunctional and common.

------
empath75
Is there a variant of this family of language that has readable syntax?

~~~
throwaway7645
Yea it's actually the most readable in the family to those who haven't put the
effort into learning the array paradigm. Dyalog APL & J are the two other main
languages in the family that have the largest user bases. Both are pretty
awesome.

~~~
coliveira
+1 to J. It is open source, has lots of libraries, documentation, examples,
and runs on most mainstream desktop platforms.

------
feelin_googley
I still use k2 in addition to k4. Being able to see which things change and
which stay the same between versions helps in the slow, thorough type of
learning process. (I am not a fast learner. I am a thorough learner.) I would
love to get a copy of k5 or greater but I am not a programmer by trade and
doubt I would pass the audition.

Although I stopped using Windows, X11 and Mac a long time ago, I have tried k2
on Windows and it is _extremley fast_ , irrespective of whether the computer
is old and "underpowered". I used it to instantaneously generate plots and
charts from the command line, instead of driving Excel with some scripting
language. This is software from the late 1990s early 2000s.

RosettaCode wiki needs to fix its k entries or turn off the silly Cloudflare
email protection or whatever is causing the problem. The "@" symbol and
subsequent characters are being replaced by "[email protected]".

[https://rosettcode.org/wiki/Category:K](https://rosettcode.org/wiki/Category:K)

k4 can be integrated into daily use in UNIX-like OS for text processing.

Professional example:
[https://github.com/adavies42/qist/blob/master/lib/awq.k](https://github.com/adavies42/qist/blob/master/lib/awq.k)

Amateur example: (remove all duplicate lines)

    
    
       AWK
       #!/bin/sh
       awk '!a[$0]++' < $1 > $1.tmp;
       exec mv -v $1.tmp $1;
    
       k4
       #!/bin/sh
       exec echo "k).Q.fs[l:0::\`:$1];l:?:l;\`:$1 0:l"|exec q >&2;
    

Experiment: keep increasing the size of the file passed as $1 until the AWK
version fails.

------
feelin_googley
[https://github.com/JohnEarnest/ok/blob/gh-
pages/docs/Fromk5T...](https://github.com/JohnEarnest/ok/blob/gh-
pages/docs/Fromk5Tok6.md):

Padding Can Truncate (Done)

In k5, padding was treated as a minimum size for the output string:

    
    
      2$"abcde"
     "ab"
      6$"abcde"
     "abcde "
    

In k6, as in older Ks, padding will produce a string with an exact length,
truncating if necessary:

    
    
      2$"abcde"
     "ab"
      6$"abcde"
     "abcde "
    
    

Is this correct?

    
    
       / k4
       6$"abcde"
      "abcde "
       2$"abcde"
      "ab"  
    
       / k2 
       6$"abcde"
      " abcde"
       2$"abcde"
      "de"

