Created
November 16, 2013 12:11
-
-
Save lispm/7499480 to your computer and use it in GitHub Desktop.
Collect items with the same head.
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
| (defun hashtable-to-list (table &aux result) | |
| (maphash (lambda (key value) | |
| (push (cons key value) result)) | |
| table) | |
| result) | |
| (defun collect-into-table (list &key key-fn value-fn &aux (table (make-hash-table))) | |
| (mapc (lambda (item &aux (key (funcall key-fn item))) | |
| (setf (gethash key table) | |
| (nconc (gethash key table) | |
| (funcall value-fn item)))) | |
| list) | |
| table) | |
| (defun example (list) | |
| (mapcar #'rest | |
| (sort (hashtable-to-list | |
| (collect-into-table list | |
| :key-fn #'first | |
| :value-fn #'rest)) | |
| #'< | |
| :key #'first))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment