Created
October 25, 2019 08:59
-
-
Save vyzo/863189dfb97ef8b17ebb0b5f2574ac72 to your computer and use it in GitHub Desktop.
wc with presized (huge) hash table
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(import :std/net/bio | |
:std/sort | |
:std/srfi/1) | |
(export main) | |
(declare (not safe)) | |
(def (read-next-word buf) | |
(let lp ((chars [])) | |
(let (next (bio-read-char buf)) | |
(cond | |
((eof-object? next) | |
(if (null? chars) | |
next | |
(list->string (reverse! chars)))) | |
((or (eq? next #\space) (eq? next #\newline)) | |
(list->string (reverse! chars))) | |
(else | |
(lp (cons next chars))))))) | |
(def (main path) | |
(let ((words (make-hash-table size: 1000000)) | |
(buf (open-file-input-buffer path 8192))) | |
(let lp () | |
(let (word (read-next-word buf)) | |
(unless (eof-object? word) | |
(hash-update! words word fx1+ 0) | |
(lp)))) | |
(for-each (lambda (x) (displayln (car x) " " (cdr x))) | |
(sort! (hash->list words) | |
(lambda (a b) (> (cdr a) (cdr b))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment