
Determining an Election in K - luu
http://leahneukirchen.org/blog/archive/2020/03/determining-an-election-in-k.html
======
chrispsn
Another approach to the 'highest averages' method, using a k9 dict:

    
    
        v: 10400 3400 6200
        s: 15
    
        quotients: ,/v%\.5+!s
        labels:    s/!s*#v              / list of 15x 0, 1, 2. arbitrary; could instead be party names
        top:       s#>labels!quotients  / k dicts can have duplicate keys; can sort items by key(^) or val(<>)
        seats:     `freq@!top           / count appearances of dict's keys
    

The ability to transform a dict's values while keeping the labels (keys)
unchanged - sort, arithmetic, etc - is really powerful.

This answer is longer when minified, but it's faster in 2020.03.27:

    
    
        \t:100000 +/s>(#v)^<>,/v%/.5+!s           ⇒ 1494
        \t:100000 `freq@!s#>(s/!s*#v)!,/v%\.5+!s  ⇒ 691

~~~
chrispsn
Correction: I think the above can be a bit faster and shorter if we skip the
dict, and just index into the keys. Compare:

    
    
         \t:100000 `freq@!s#>(s/!s*#v)!,/v%\.5+!s
        941
         \t:100000 `freq(s/!s*#v)s#>,/v%\.5+!s
        901

------
nprescott
Always a pleasant surprise to find niche or mundane uses of array languages. I
really appreciate the explication because it made it possible for me to follow
along in J - there's vanishingly little material to patiently explain bits
like the "grade-up grade-down to return rank" that permeates so much APL-style
code.

I'm similarly pleased to have found a correspondence from K's _'_ (also
present in Klong) to J, where it ends up being _" 0_ in this instance.

    
    
        r=. v&%"0 (0.5+i. s)
        +/s>($r)$/:\:,/r

------
RodgerTheGreat
A nice demonstration of how the K REPL can be used as a calculator and
exploratory tool. The concise syntax particularly shines in interactive cases.

~~~
DaiPlusPlus
> The concise syntax particularly shines in interactive cases

If you can tolerate the learning-curve though.

For comparison, I do appreciate that many (most?) Linux shell utilities and
command-line programs in general have adopted standard conventions for
arguments (-f for short, --full for verbose) which make it easier for people
new to a program to take a cryptic command-line and look-up the full-versions
of the abbreviated commands to see what's going on - PowerShell does the same
thing with its strict aliasing of cmdlets. Unfortunately I can't see that K
(or many other programming languages really) do the same thing, where a a
short, cryptic program can be syntactically expanded to one using verbose
syntax with no changes in program semantics (unless you count VB.NET vs C#
equivalence, heh).

Is there any automated explainer tools for the K language for beginners?

~~~
nprescott
Not long ago there was a post about using English language instead of the J
symbology[0] but the end result isn't meaningfully easier to understand
because of all the prerequisite knowledge of how evaluation works etc., once
you've worked that out most of the "line noise" has already sunk in. I think
that's a big sticking point against creating a second "inflatable" notation -
though there is always Nial[1]

Not entirely an answer to your question, but J provides introspection/help in
a pretty direct way via two distinct means, the first is _;:_ ("words") which
will show you the parse of a J phrase (here, a pretty direct translation of
the algorithm in the article):

    
    
        ;:'+/s>(s,#v)$/:\:,/v&%"0 (0.5+i.s)'
        ┌─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬──┬──┬─┬─┬─┬─┬─┬─┬─┬─┬───┬─┬──┬─┬─┐
        │+│/│s│>│(│s│,│#│v│)│$│/:│\:│,│/│v│&│%│"│0│(│0.5│+│i.│s│)│
        └─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴──┴──┴─┴─┴─┴─┴─┴─┴─┴─┴───┴─┴──┴─┴─┘
    

Which shows the tokens of the phrase, which can be helpful if you are unsure
of where a symbol or diacritic is being used. Inside the J IDE you can get
context sensitive help by pressing Ctrl-F1 on a symbol, if you were interested
in _/ :_ for example, you are directly linked here[2].

I am unaware of a similar tool in K, which should not be taken to mean it
doesn't or can't exist. One of the reasons I'm interested in J has been the
tooling and documentation available.

[0]: [https://wjmn.github.io/posts/j-can-look-like-
apl/](https://wjmn.github.io/posts/j-can-look-like-apl/)

[1]:
[https://tangentstorm.github.io/nial/intro.ndf.html](https://tangentstorm.github.io/nial/intro.ndf.html)

[2]:
[https://code.jsoftware.com/wiki/Vocabulary/slashco](https://code.jsoftware.com/wiki/Vocabulary/slashco)

------
notafraudster
This is concise in a sort of code golf way, but not substantially faster to
write than equivalent R code -- I chose R because math operations are natively
vectorized and the basic data type is a vector, and because the language has a
user-friendly REPL -- would be, and certainly less readable. I read the linked
K documentation and it seems like much of K's syntax was motivated by wanting
to get away from "the programmer way of doing things", but the lack of
readability would seem to get in the way of that.

Still neat as a toy exercise using what seems like a fun little micro-
language.

~~~
yiyus
Do you consider add(a,multiply(b,c)) certainly more readable than a+b*c?

The article shows some nice k idioms that I find much more readable written in
a concise form. For example, I find |^... easier than
reverse(sort_ascending(...)), or <>... easier than grade_down(grade_up(...)).
I guess the solution in other languages would be to define more functions for
all these common operations, but then you will need much more documentation
for all these extra functions and learn them, so I don't see it as an
improvement.

At difference of most golfing languages, the set of k primitives is very
small. Many people who criticize k forget that property, but it's a very
important one. It would be interesting to see the R version and compare the
amount of documentation you need to read to completely understand what each of
the programs is doing (ie. to read the program). That would be, in my opinion,
a better measurement of readability than a subjective opinion (unfortunately,
the k9 documentation is too scarce at this point to make this exercise in a
proper way).

~~~
newswasboring
> Do you consider add(a,multiply(b,c)) certainly more readable than a+b*c?

No, but it took me 12 years of school education to get to that point. Question
is, for a large code base how many symbols are you willing to remember? At
some point it hits diminishing returns, doesn't it?

~~~
yiyus
Well, 12 years is an exaggeration. Once you have learned the concepts (which
you need to learn independently of which programming language you use),
getting used to the symbols is just a matter of a bit of practice. For
example, I learnt to multiply with 3x2, but it did not take me 12 years to get
used to the 3*2 notation used by most programming languages.

The symbols you have to remember are the ASCII symbols (most of them are used
in other programming languages too), with two operations for symbol, plus a
few special cases and a bunch of extra functions. In total, less than 50
primitives. I think that any programmer can easily handle that.

There are many people who do not agree with this, but there is objective data
(see the papers by Iverson about his efforts to teach mathematics using APL)
that say it is easy to learn. In all these threads, most people with
experience in array languages say it is not so hard as it looks, although
there are some exceptions.

You may prefer other kind of languages or other kind of notation. There
certainly is some subjective factor here. But I suggest everyone to give it a
try before forming an opinion.

------
cabalamat
I suspect the Python or Haskell equivalent of

    
    
        _q+(s-+/_q)><>q-_q:s*v%+/v
    

would be longer, but it would also probably be more readable.

~~~
ulucs
The only difference is a matter of alphabets. Is English more readable than
Japanese because the words aren't as condensed?

------
maest
Though I would miss the daily K post, but here it is!

------
qubex
K definitely meets my criteria for being a _Write-Only Language_. I remember a
quote about APL (a precursor to K) being “indistinguishable from line noise”.

[https://en.wikipedia.org/wiki/Write-
only_language](https://en.wikipedia.org/wiki/Write-only_language)

