News for hackers. Stuff that matters.
Re:More shell, less egg (2011) by CrazyStat on Sunday June 04, 2023 @05:45PM (#36189882)
This has been posted a number of times on HN, I'll just point to the must recent one that spawned a fairly large number of comments (which in turn has links to various other previous discussions courtesy of dang):

https://news.ycombinator.com/item?id=30017311

Re:More shell, less egg (2011) by metadat on Sunday June 04, 2023 @06:08PM (#36190064)
Thanks! FWIW, this is the only prior submission with more than 2-3 comments.
Re:More shell, less egg (2011) by CrazyStat on Sunday June 04, 2023 @06:22PM (#36190145)
Re:More shell, less egg (2011) by metadat on Sunday June 04, 2023 @06:25PM (#36190164)
Oh shit, nice. I went through every page of results on https://news.ycombinator.com/from?site=leancrew.com but somehow missed these. Thank you!
Re:More shell, less egg (2011) by PeterWhittaker on Sunday June 04, 2023 @05:52PM (#36189946)
And that’s why I love pipelines. McIlroy’s solution and explanation are succinct and clear, and the solution itself easily and readily expandable and repurposable. Bravo.
Re:More shell, less egg (2011) by tyingq on Sunday June 04, 2023 @07:37PM (#36190584)
It is elegant, and easy to understand and modify. But it's less efficient and significantly slower than even a naïve python or perl script since it sorts a lot more than it needs to.
Re:More shell, less egg (2011) by taeric on Sunday June 04, 2023 @08:02PM (#36190746)
Me thinks we see different naive python on the regular. Because I wouldn't be shocked to see many flat out failures.

That said https://www.amazon.com/Exercises-Programming-Style-Cristina-... is a good sized book that is nothing but takes on this program from different styles. It is amusing how far you can take that.

Which... is kind of the point of the Knuth solution here. It was a pedagogical exercise. As much to show how literate programs can be presented as it was to "solve" the stated problem.

Edit: It occurs to me you could be referring to the pipeline version. Which, yeah, I don't think that really changes my post. :D. The final paragraph would be changed to the point of the Milroy's code is to get the answer, not to have a library function that is expected to be used again and again on input.

Re:More shell, less egg (2011) by tyingq on Sunday June 04, 2023 @08:06PM (#36190773)
Yes, I was talking about the pipeline version. Last time this came up, I tried a simple perl script[1] and it was quite a lot faster than the pipeline. Even without using a heap to further limit the sorting.

[1] https://news.ycombinator.com/item?id=30038415

Re:More shell, less egg (2011) by im3w1l on Sunday June 04, 2023 @09:54PM (#36191387)
I think it will be faster than python or perl, because the actual work is done by optimized c-code.

However if you want to extend the program from a toy to something that can handle words like "won't" "c-code" "a.i." or "naïve" then it will be quite difficult to do so.

Re:More shell, less egg (2011) by tyingq on Sunday June 04, 2023 @10:00PM (#36191415)
I tried a perl script versus the shell pipeline, see my other comments in the thread. It's significantly faster, because using sort and uniq -c is a pretty high effort way to count words, especially for big lists. Your words seem to work fine with it, it's just splitting on whitespace.
Re:More shell, less egg (2011) by tuatoru on Sunday June 04, 2023 @10:54PM (#36191718)
That'd be a problem if it were to run a million times a day or on exabytes of data.

But it's a "nonce": to be used once, on a small desktop machine (for low-integer values of once). That's not really a good objection.

Re:More shell, less egg (2011) by tyingq on Sunday June 04, 2023 @11:42PM (#36191981)
It's not an objection, it was an observation.
Re:More shell, less egg (2011) by jjgreen on Monday June 05, 2023 @09:20AM (#36195806)
I was appalled when I first heard a colleague use "nonce" in this manner, I'd only previously heard it used as slang for sex-offender/paedophile, https://www.collinsdictionary.com/dictionary/english/nonce
Re:More shell, less egg (2011) by tyingq on Monday June 05, 2023 @09:33AM (#36195970)
That use of "nonce" supposedly comes from a once used acronym associated with some segregated prisoners..."not on normal courtyard exercise".
Re:More shell, less egg (2011) by jjgreen on Monday June 05, 2023 @10:00AM (#36196268)
I'd heard the same, but half-expected a backformation
Re:More shell, less egg (2011) by tyingq on Monday June 05, 2023 @10:04AM (#36196318)
Ah, yeah. Googling around a bit suggests some disagreement on whether that's true or not. It appeared in a documentary, then was disputed by others afterwards.
Re:More shell, less egg (2011) by adityaathalye on Monday June 05, 2023 @01:42AM (#36192778)
The ACM has kindly published the original source along with the rest of their archives.

cf. "Programming Pearls: A literate program" https://dl.acm.org/doi/10.1145/5948.315654

Aside: I received a masterclass from Gradmaster McIlroy, in context of this selfsame shell pipeline: https://www.evalapply.org/posts/shell-aint-a-bad-place-to-fp...

I was floating in the air all week :D

(edit: typo)

Re:More shell, less egg (2011) by heavypixels on Monday June 05, 2023 @04:02AM (#36193711)
I think most of us are used to seeing literate programming in old articles, often by Knuth himself. One excellent, modern and practical example of literate programming can be found in Physically Based Rendering, by Pharr, Jakob and Humphreys. It's not just the industry-standard overview of rendering theory and contemporary techniques in a big ol' hardcover book, it's also the source for an advanced ray tracer incorporating these techniques. And written in modern C++, not ancient Pascal.

The 3rd edition can be read online here: https://www.pbr-book.org/