5 ms·
Maybe, but pascal didn't actually come with a batteries included piping concept. You'd need to work one up. Well, you could abuse the GNU pipes library and get
by vessenes 11y ago
Maybe, but pascal didn't actually come with a batteries included piping concept. You'd need to work one up. Well, you could abuse the GNU pipes library and get your piping that way maybe by running it through the OS, but that would definitely not be how Knuth rolls.
As a side note, I think the shell script version would have been multicore friendly, at least up to six cores. I am sure Knuth's was single threaded.
Finally, to your point on horrible hacks, maybe Knuth's version dealt with a whole bunch of edge cases in each, like for instance dealing with dos/unix/mac endlines. But, probably not -- it was a toy program written as evangelism and out of courtesy. sort and uniq et al embed many programmer lifetimes of experience and bug fixing at this point. Getting all of that nearly for free is really, really great.
- pascal_cuoq 11y ago> As a side note, I think the shell script version would have been multicore friendly, at least up to six cores. That's not how the sort command, in lines 3 and 5, works.
- regularfry 11y agoIt is how pipelines work, though. 6 commands, 6 processes all started at once. They're each allowed a core to themselves, if the kernel deems it so.
- davidshepherd7 11y agoBut `sort` has to read all the input before writing any output (I think...). So you get some parallelism but not as much as you might hope.
- lisper 11y agoThat's true, but it doesn't really count if only one of the threads is eligible to run at any one time and all the others are blocked.
- lisper 11y ago> pascal didn't actually come with a batteries included piping concept No, it has function calls instead. > I think the shell script version would have been multicore friendly Not unless 'sort' is implemented very cleverly. > maybe Knuth's version We'll never know unless someone manages to dredge up the source code. But the specifics on Knuth's version are not really the point. Someone else might have been able to do better. And someone using a less brain damaged language that Pascal might have been able to do even better. In fact, here it is in four lines of Common Lisp (using Ergolib - https://github.com/rongarret/ergolib https://github.com/rongarret/ergolib): (defun histogram (path) (bb l (split (file-contents path) t :test (fn (a b) (whitespacep b))) l (sort 'string< (mapcar 'string-upcase (remove "" l))) (for item in (remove-duplicates l) collect (list item (count item l))))) Writing a pure CL version is left as an exercise. My guess is it would be 10-20 LOC.