Last active
April 24, 2019 15:51
-
-
Save vyzo/b9564169237db0d7224f81329b221a89 to your computer and use it in GitHub Desktop.
wc with raw devices
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)) | |
(buf (open-file-input-buffer path))) | |
(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