Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

.Re: Any way to collect all the values of a hash table more concisely ?

5 views
Skip to first unread message

Robert L.

unread,
Jan 21, 2017, 3:03:02 AM1/21/17
to
Leandro Rios wrote:

> Leandro Rios escribi\xF3:
>
> > So this new version works as intended:
>
> > (defun cluster-by (fn list &aux (clusters (make-hash-table)) (result nil))
> > (mapcar #'(lambda (x) (push x (gethash (funcall fn x) clusters))) list)
> > (maphash #'(lambda (key value) (push value result)) clusters)
> > (sort result #'< :key #'(lambda (x) (length (car x)))))
>
> Um, sorry:
>
> (defun cluster-by (fn list &aux (clusters (make-hash-table)) (result nil))
> (mapcar #'(lambda (x) (push x (gethash (funcall fn x) clusters))) list)
> (maphash #'(lambda (key value) (push value result)) clusters)
> (sort result #'< :key #'(lambda (x) (funcall fn (car x)))))
> ^^^^^^^^^^

Ruby:

["a", "b", "abc", "bc", "a", "abcd", "e", "fg"].group_by(&:size)
===>
{1=>["a", "b", "a", "e"], 3=>["abc"], 2=>["bc", "fg"], 4=>["abcd"]}

In Forth?


--
Jews totally run Hollywood.... But I don't care if Americans think we're
running the news media, Hollywood, Wall Street, or the government. I just care
that we get to keep running them. --- Joel Stein
articles.latimes.com/2008/dec/19/opinion/oe-stein19
archive.org/download/DavidDukeTv/DoJewsControlTheMediaTheLaTimesSaysYes.flv
0 new messages