Skip to content

Instantly share code, notes, and snippets.

@lispm
Created November 16, 2013 12:11
Show Gist options
  • Save lispm/7499480 to your computer and use it in GitHub Desktop.
Save lispm/7499480 to your computer and use it in GitHub Desktop.
Collect items with the same head.
(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