Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
;;; 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
(require 'split-sequence)
(defun split-string (seq)
"Like Python's line.split()."
#'(lambda (char) (cond ((eql char #\Space) t)
((eql char #\Tab) t)
((eql char #\Newline) t)
(t nil)))
: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)))
(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