Live data from Hacker News

Stages of denial in encountering K

nsl.com

301–310 of 432 posts

Re: Stages of denial in encountering K

#302
post #205
post #132

Earlier quoted context omitted.

I don't think Perl coders need any help with that.

For non-perl guys: It is a cryptic example, because it uses ‘system’ variables in a non-obvious way, not because of syntax. I can read it syntactically just fine, but have to look up for names to understand what it does. A fully uncodegolfed variant would require the same effort here.

Golfing is not seen so negatively for Perl. "Embrace the language" they say.

Re: Stages of denial in encountering K

#303
post #232
post #136

Earlier quoted context omitted.

I doubt that K can reduce line count by a factor 1000 though. The examples in the article is mostly about shorter identifiers like "!" instead of "range" and a compact notation. That is perhaps a factor 5 not a factor 1000. The example with a for-loop for summing a range is a blatant strawman. In which modern language would that be idiomatic? Furthermore the examples only show a particular use case: Processing lists…

> I doubt that K can reduce line count by a factor 1000 though. It adds up! Consider something like this: (defun count (list) (let ((hash (make-hash-table))) (dolist (el list) (incf (gethash (cadr el) hash 0) (car el))) (let (result) (maphash (lambda (key val) (push (list val key) result)) hash) result))) That's just #:'=:

OTOH, a more idiomatic version would be (and I would consider using the loop facility to be idiomatic):

  (defun count (pair-list)
    (let ((hash (make-hash-table)))
      (loop for (val key) in pair-list
        do (incf (gethash key hash 0) val))
      (loop for key being the hash-key of hash
        collect (list (gethash key hash) key))))
This drops us from 10 to 7 lines, which is clearly shorter. But, it's also relatively simple to change it from summing a list of ( ) to a list of ( ), something that I genuinely can't comment on for the K version. I suspect it would be as simple as first doing a permutation on the input, and (if needed) un-permute the result.

Re: Stages of denial in encountering K

#304

Earlier quoted context omitted.

> I believe you can learn to read it and read it as well as me as quickly as with a few months of study. Then it's not perfectly readable. That, or your definition of readable is useless because then any programming language is readable given enough study time. > being able to do it easier is the point. I still haven't seen any example where it is actually easier. Shorter? Yes. Easier? Very debatable.

Truly grokking Python as a first language also took me a few months of studying it and writing it, so the author is probably trying to say it's the same as any technology. The only caveat is once you learn something like Algol, you can kind of pick up similar ones fairly quick, while something like APL or K requires you to start over again.

Grokking a language in a few months is different from being able to just read a language in a few months. It's not just the paradigm change: Haskell, Julia, Mathematica or MATLAB for example are far, far more readable than K.

Re: Stages of denial in encountering K

#305

Only because earlier today I so definitely thought about the whole "code is a liability (debt)" reality we all live in... that is to say, I'm spit-balling here: but for once K makes me go "oh shit!" and not because of the craziness of its syntax... rather, if that syntax is so precise/terse, and even if to the outsider it looks fucking insane, does it have a wicked lower TCO because you literally have insanely less l…

Finance uses it because it's great for sorting out medium-big time oriented data. The IQ test aspect of it is probably considered a bonus in some shops.

There are non-terse APLs out there; the most obvious one floating around is Nial[1]. Culturally though, once you've figure out how to read and write in the compressed ascii runes, it's kind of hard to go back to the other way. J is actually denser because of the hooks, forks[2], rank and full tacit aspect of things. Personally whenever I've composed an algorithm in J I feel like I really understand it in a way that pretty much nothing else gives me with all the extra typing.

[1] https://github.com/danlm/qnial7

[2] https://crypto.stanford.edu/~blynn/c/apl.html

Re: Stages of denial in encountering K

#306
post #225

I love the brevity of regular expressions and use them on a daily basis. It is the same argument that keeps me returning to K: the syntax is terse and compact, the semantics are simple and composable, and your eyes get used to it. Beyond a point however, I cannot read my own regex's after a month's absence. Which is why I use perl's /x modifier extensively to split up regex components onto multiple lines and to docum…

lots of comments there! / xml from char

I thought that was a game of life

Re: Stages of denial in encountering K

#307
post #287

Earlier quoted context omitted.

Contrived Debian shootout benchmarks: http://www.kparc.com/z/comp.k https://web.archive.org/web/http://shootout.alioth.debian.or... Note that the relevant k program here is contained entirely on the last line of code and denoted by a comment telling you what it does: http://www.kparc.com/z/fun.k http://c2.com/cgi-bin/wiki?WardNumberInManyProgrammingLangua... Most Joy people are also k people, and Joy appears to be th…

You should probably label your columns, but that seems to be wc output. The Scheme and Haskell versions are actually shorter than you imply since they include the pairings in the count (when the pairings in the code are examples). Additionally, the k version is one line longer than you suggest. The definition of a is on its own line is used in the solution. Still only 2 lines, but if you want to persuade you should p…

Fair crit! Running off of few hours of sleep last night (spent way too long defending k in this thread; need to set sensible noprocrast settings or get some flavor of self-control); accuracy is impaired. Another error I see in retrospect is that I included comments in a few of them but not all of them.

Re: Stages of denial in encountering K

#308
post #232

Earlier quoted context omitted.

> I doubt that K can reduce line count by a factor 1000 though. It adds up! Consider something like this: (defun count (list) (let ((hash (make-hash-table))) (dolist (el list) (incf (gethash (cadr el) hash 0) (car el))) (let (result) (maphash (lambda (key val) (push (list val key) result)) hash) result))) That's just #:'=:

Python 3.7+: Counter(elems).values() But we can play this game both ways. What's the K for (Counter(a) & Counter(b)).most_common(3)?

It's a little funny comparing a language with another language+plus-its-entire-ecosystem, but k fares remarkably well I think.

Counter could be #:'=: (count each group)

    c:#:'=:
Values would just be dot.

Counter[x]&Counter[y] is a bit tricky to write, because while the documentation says set intersection, what they really mean is the set intersection of the keys with the lower of the values.

This is entirely clear in k; First, understand I have a common "inter" function that I use frequently (it's actually called "inter" in q):

    n:{x@&x in y}
and from there, I can implement this weird function with this:

    i:{(?,/n[!x;!y])#x&y}
I've never needed this particular function, but I can read out the characters: distinct raze intersection of keys-of-x and keys-of-y, taking [from] x and y. It sounds very similar to definition of the function I gave above (if you realise the relationship between "and" and "lower of the values"), and this was painless to write.

most_common could be: {k!x k:y#>x} but if I'm going to want the sort (in q) I might write y#desc x which is shorter. In this I can save a character by reversing the arguments:

    m:{k!y k:x#>y}
so our finished program (once we've defined all our "helper functions" in a module) is:

    m[3] i . c'(x;y)
So we're looking at 13 lexemes, versus 19 - a minor victory unless you count the cost of:

    from collections import Counter
and start counting by bytes! But there's more value here:

All of these functions operate on data, so whether the data lives on the disk or not makes no difference to k.

That's really powerful.

In Python I need more code to get it off the disk if that's where it is.

I also may have to deal with the possibility the data might "live" in another object (like numpy) so either I trust in the (in)efficiency of the numpy-to-list conversion, or I might have to do something else (in which case really understanding how Counter[a]&Counter[b] works will be important!).

I might also struggle to make this work on a big data set in python, but k will do just fine even if you give it millions of values to eat.

These things are valuable. Now if you're interested in playing games, let's try something hard:

    "j"$`:x?`f
It creates (if necessary) a new enumeration in a file called "x" which persists the unique index of f, so:

    q)"j"$`:x?`f
    0
    q)"j"$`:x?`g
    1
    q)"j"$`:x?`h
    2
    q)"j"$`:x?`f
    0
How would you do this in Python?

Re: Stages of denial in encountering K

#309
post #213

Earlier quoted context omitted.

> https://a.kx.com/a/k/examples/xml.k > Where's the pedagogy? Where are the comments? Most of that document _is_ comments. There's a comment on almost every line, very similar to your perl example. Comments begin with a "/" character which doesn't have a function to the left (e.g. whitespace). First we have some constants (L,W,B,S,R) which refer to the left-bracket, whitespace (which includes blank), blank space, and…

If the comments were good (more like you wrote) then the ratio of comments to text would be even higher. And as with writing assembly, the risk of comments getting out of sync with the code is higher, too.

Thank you. The narration is what happens in my brain when I read it. I don't need it in the source files. Keeping the file short is the best way to keep it consistent (what you refer to "getting out of sync")

Re: Stages of denial in encountering K

#310
post #284

Earlier quoted context omitted.

> Why do they get a pass while k doesn't? When we are discussing languages, they don't get a pass. Python has a great math library with Numpy, but it doesn't mean that the language itself is optimal for math-related problems. And specifically in that example, using an external database when defending the speed of the language... meh. It's as if I said that Python is very fast and showed a Numpy calculation. > This ma…

And specifically in that example, using an external database when defending the speed of the language kdb isn't written in an external language. Has he? Couldn't find anything (I'd like to see it, honestly, seems a fun experiment). Not entirely him, of course. There are a few remaining files on kparc.com: kparc.com/z/ kparc.com/$/ so on. Not free software, and they seem reluctant to show it off. 'geocar has done a li…

> kdb isn't written in an external language.

But it's an external database with its code and its optimizations. In this case it seemed that it was just doing a binary search, but it was using an already sorted dataset that the OP of that comment wasn't using.

> kparc.com/$/

Well, I can't get anything out of this. See http://kparc.com/$/file.k. The language is cryptic and the variable names are even more cryptic. It's really hard to see what does this code even do. I can't honestly believe anyone that says that's a good way to create code.

Post reply on HN