Re:K Isn't Lisp
by tmtvl on Tuesday February 13, 2024 @12:56PM
(#39360557)
Doing the naive thing in CL without thinking about things like 'efficiency' or 'maintainability':
I will admit it's not as short as:
[ Parent
| on HN ]
My naive looked more like: (with apologies on likely messing up spacing in hn.)
(let ((in '((1 "one") (1 "two") (1 "three") (1 "one") (1 "four") (1 "two"))))
(loop with hash = (make-hash-table :test #'equal)
for v in in
do (incf (gethash (cadr v) hash 0))
finally (return (loop for key being the hash-keys of hash
collect (list (gethash key hash) key)))))Now you have me thinking about how short I can make that. My revision:
(defun doit (elts)
(mapcar (lambda (elt) `(,(count elt elts :test 'equal :key 'second) ,elt))
(delete-duplicates (mapcar 'second elts) :test 'equal)))))
or maybe (defun doit (elts)
(loop for elt in (delete-duplicates (mapcar 'second elts) :test 'equal)
collect `(,(count elt elts :test 'equal :key 'second) ,elt)))