Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

Q: What do you call a blind, deaf-mute, quadraplegic Virginian? A: Trustworthy.


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

SubjectAuthor
* Re: Any way to collect all the values of a hash table more concisely ?B. Pym
`- Re: Any way to collect all the values of a hash table more concisely ?B. Pym

1
Subject: Re: Any way to collect all the values of a hash table more concisely ?
From: B. Pym
Newsgroups: comp.lang.lisp
Organization: A noiseless patient Spider
Date: Fri, 5 Jul 2024 22:09 UTC
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: No_spamming@noWhere_7073.org (B. Pym)
Newsgroups: comp.lang.lisp
Subject: Re: Any way to collect all the values of a hash table more concisely ?
Date: Fri, 5 Jul 2024 22:09:09 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 37
Message-ID: <v69qu1$3f6ps$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Date: Sat, 06 Jul 2024 00:09:10 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="36dabbcb546c8dd3a3c5c81a41ee2e18";
logging-data="3644220"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18bh6ihYUj9ioTmmqOSDDoJ"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:SPNv4WU90oR3OBkZWGGfn1EwL8s=
View all headers

> > 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)))))
> ^^^^^^^^^^

"lambda" is a macro that expands to (function (lambda ...)).
So instead of
#'(lambda
he ought to use
(lambda

The poster prefers #'lambda because he prefers ugliness.

Common Lisp is used by those who want code to be as
hideous as possible.

Gauche Scheme

(define (cluster-by fn lst)
(let ((clusters (make-hash-table 'equal?)))
(dolist (k lst)
(hash-table-push! clusters (fn k) k))
(hash-table-values clusters)))

(cluster-by string-length '("a" "b" "abc" "bc" "a" "abcd" "e" "fg"))
===>
(("abcd") ("e" "a" "b" "a") ("fg" "bc") ("abc"))

Subject: Re: Any way to collect all the values of a hash table more concisely ?
From: B. Pym
Newsgroups: comp.lang.lisp
Organization: A noiseless patient Spider
Date: Mon, 19 Aug 2024 05:58 UTC
References: 1
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Nobody447095@here-nor-there.org (B. Pym)
Newsgroups: comp.lang.lisp
Subject: Re: Any way to collect all the values of a hash table more concisely ?
Date: Mon, 19 Aug 2024 05:58:38 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 56
Message-ID: <v9umuc$2osfp$1@dont-email.me>
References: <v69qu1$3f6ps$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Date: Mon, 19 Aug 2024 07:58:39 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="e424c484fd84d3ea93231443f92efc4c";
logging-data="2912761"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/H5UOAp1WpcGelCrv74TLQ"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:1K8gUPqapEK/22CfYS+ajRgh17U=
View all headers

B. Pym wrote:

> > > 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)))))
> > ^^^^^^^^^^

Testing:

(cluster-by #'length '("a" "b" "abc" "bc" "a" "abcd" "e" "fg"))
===>
(("e" "a" "b" "a") ("fg" "bc") ("abc") ("abcd"))

The items are grouped by "length" and then sorted
by "length".

newLISP

(define (group-by func lst , (alist '()) result)
(dolist (x lst) (ainc! alist (func x) x cons '()))
(set 'result (sort alist (fn (a b) (< (first a) (first b)))))
(map last result))

(group-by length '("a" "b" "abc" "bc" "a" "abcd" "e" "fg"))
===>
(("e" "a" "b" "a") ("fg" "bc") ("abc") ("abcd"))
Given:

(macro (ainc! Alist Key Value Function Deflt)
(local (E-Message Val Func Def)
(setq Func Function)
(if (true? Func)
(setq Val Value)
(begin (setq Func +) (setq Val (or Value 1))))
(setq Def Deflt)
(if (= nil Def) (setq Def 0))
(unless
(catch
(setf (assoc Key Alist)
(list ($it 0) (Func Val ($it 1))))
'E-Message)
(if (starts-with E-Message "ERR: no reference")
(setf Alist (cons (list Key (Func Val Def)) Alist))
(throw-error E-Message)))))

1

rocksolid light 0.9.8
clearnet tor