Skip to content

Instantly share code, notes, and snippets.

@eigenhombre
Created August 27, 2014 02:33
Show Gist options
  • Select an option

  • Save eigenhombre/820187bae6c11969a0f3 to your computer and use it in GitHub Desktop.

Select an option

Save eigenhombre/820187bae6c11969a0f3 to your computer and use it in GitHub Desktop.
(defun get-file (filename)
(with-open-file (stream filename)
(loop for line = (read-line stream nil)
while line
collect line)))
(defvar *filename*
"/Users/jacobsen/Programming/Lisp/commonlisp/word-pairs.txt")
(defun hash-keys (hash-table)
(loop for key being the hash-keys of hash-table collect key))
(defun take (n s) (subseq s 0 n))
(defun split (string)
;; http://stackoverflow.com/questions/6152055/
;; common-lisp-the-fastest-way-to-read-the-stream
(let ((space-position (position #\, string)))
(list
(subseq string 0 space-position)
(subseq string (+ space-position 1)))))
(defun hash-keys (hash-table)
(loop for key being the hash-keys of hash-table collect key))
(defun group-by (f s)
(let ((h (make-hash-table :test 'equal)))
(loop for el in s do
(let ((cur (gethash (funcall f el) h)))
(setf (gethash (car el) h) (cons el cur))))
h))
(defun hash-pairs (h)
(mapcar #'(lambda (k) (list k (gethash k h))) (hash-keys h)))
;; (let* ((h (group-by #'car '((1 1) (0 2) (1 1) (2 5) (0 6)))))
;; (hash-pairs h))
;;=> ((1 ((1 1) (1 1))) (0 ((0 6) (0 2))) (2 ((2 5))))
;; (let* ((s (take 10 (mapcar #'split (get-file *filename*))))
;; (h (group-by #'car s)))
;; (hash-keys h))
;;=> ("it" "was" "the" "best" "of" "times" "worst")
;; (length (hash-keys (group-by #'car (mapcar #'split (get-file *filename*)))))
;;=> 11707
(time
(loop for i upto 9 do
(setq result (group-by #'car (mapcar #'split (get-file *filename*))))))
;; 1.290 seconds
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment