Skip to content

Instantly share code, notes, and snippets.

@vyzo
Created October 25, 2019 08:59
Show Gist options
  • Save vyzo/863189dfb97ef8b17ebb0b5f2574ac72 to your computer and use it in GitHub Desktop.
Save vyzo/863189dfb97ef8b17ebb0b5f2574ac72 to your computer and use it in GitHub Desktop.
wc with presized (huge) hash table
(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