Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

BOFH excuse #132: SCSI Chain overterminated


comp / comp.lang.lisp / Re: Slow Loop (alternatives in lisp?)

SubjectAuthor
o Re: Slow Loop (alternatives in lisp?)B. Pym

1
Subject: Re: Slow Loop (alternatives in lisp?)
From: B. Pym
Newsgroups: comp.lang.lisp
Organization: A noiseless patient Spider
Date: Sat, 17 Aug 2024 09:30 UTC
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: Slow Loop (alternatives in lisp?)
Date: Sat, 17 Aug 2024 09:30:28 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 51
Message-ID: <v9pqjh$1s18u$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Date: Sat, 17 Aug 2024 11:30:28 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="ebe088d483852b5a9dd0cb0bd8d9bdb5";
logging-data="1967390"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+Ypv9yWskiwLtr7iJh3/AO"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:naNJ0hipCO71T141u8ZvDtnpkUk=
View all headers

> (defun distribution1 (items values test)
> (let ((table (make-hash-table :test test)))
> (loop for item in items
> for value in values
> do (incf (gethash item table 0) value))
> (let ((items-list nil))
> (maphash (lambda (item sum-value)
> (push (cons item sum-value) items-list))
> table)
> (sort items-list #'> :key #'cdr))))
>
> An example call:
>
> CL-USER 58 > (distribution1 '("a" "b" "c" "b" "a" "f" "e" "g"
> "h" "k" "z" "k" "r" "u" "f")
> '(1 5 8 7 14 8 3 7 9 4 3 21 5 7 9)
> #'equal)
> (("k" . 25) ("f" . 17) ("a" . 15) ("b" . 12) ("h" . 9) ("c" . 8)
> ("g" . 7) ("u" . 7) ("r" . 5) ("e" . 3) ("z" . 3))

newLISP

Let's simply use an association list.

(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)
(setf Alist (cons (list Key (Func Val Def)) Alist)))))

(define (distribution1 items vals)
(let (table '())
(dolist (it items) (ainc! table it (pop vals)))
(sort table (fn (a b) (> (a 1) (b 1))))))

(distribution1
'("a" "b" "c" "b" "a" "f" "e" "g" "h" "k" "z" "k" "r" "u" "f")
'(1 5 8 7 14 8 3 7 9 4 3 21 5 7 9))

===>
(("k" 25) ("f" 17) ("a" 15) ("b" 12) ("h" 9) ("c" 8) ("g" 7)
("u" 7) ("r" 5) ("e" 3) ("z" 3))

1

rocksolid light 0.9.8
clearnet tor