Created
September 23, 2009 22:42
-
-
Save kragen/192354 to your computer and use it in GitHub Desktop.
This file contains 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
;;; Code basically by Rainer Joswig, licensed under a cc-by-sa 2.5 | |
;;; license; Kragen Sitaker cleaned it up a bit to get it to run. | |
;;; In response to my question on | |
;;; http://stackoverflow.com/questions/1467898/what-language-could-i-use-for | |
(require 'split-sequence) | |
(defun split-string (seq) | |
"Like Python's line.split()." | |
(split-sequence:split-sequence-if | |
#'(lambda (char) (cond ((eql char #\Space) t) | |
((eql char #\Tab) t) | |
((eql char #\Newline) t) | |
(t nil))) | |
seq | |
:remove-empty-subseqs t)) | |
(defun dbscan (top-5-table stream) | |
"get triples from each line and put them in the hash table" | |
(loop for line = (read-line stream nil nil) ; no error on eof | |
while line | |
do (destructuring-bind (aa bb cc) | |
(split-string line) ; use your split-string | |
(setf bb (read-from-string bb)) ; bb -> number | |
(let ((current (gethash aa top-5-table))) ; check for an entry | |
(if current | |
(nconc current (list (list bb cc))) ; append | |
(setf current (list (list bb cc)) ; otherwise create one | |
(gethash aa top-5-table) current)); enter it | |
(when (> (length current) 5) ; more then five? | |
(setf (gethash aa top-5-table) ; only the five largest | |
(subseq (sort current #'> :key #'first) 0 5))))))) | |
(defun dbprint (table output) | |
"print the hashtable contents to standard output" | |
(maphash (lambda (aa value) | |
(loop for (bb cc) in value | |
do (format output "~a ~a ~a~%" aa bb cc))) | |
table)) | |
(defun dbsum (input output) | |
"scan and sum from a stream" | |
(let ((top-5-table (make-hash-table :test #'equal))) | |
(dbscan top-5-table input) | |
(dbprint top-5-table output))) | |
(defun fsum (infile outfile) | |
"scan and sum a file" | |
(with-open-file (input infile :direction :input) | |
(with-open-file (output outfile :direction :output) | |
(dbsum input output)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment