Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

PURGE COMPLETE.


devel / comp.lang.lisp / .Re: Basic List processing

SubjectAuthor
o .Re: Basic List processingRobert L.

1
.Re: Basic List processing

<suium5$1vjm$1@gioia.aioe.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=16867&group=comp.lang.lisp#16867

  copy link   Newsgroups: comp.lang.lisp
Path: i2pn2.org!i2pn.org!aioe.org!qEYNA8HgrHeNBpz3ZeV8rQ.user.46.165.242.75.POSTED!not-for-mail
From: No_spamming@noWhere_7073.org (Robert L.)
Newsgroups: comp.lang.lisp
Subject: .Re: Basic List processing
Date: Wed, 16 Feb 2022 13:37:10 -0000 (UTC)
Organization: Aioe.org NNTP Server
Message-ID: <suium5$1vjm$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Info: gioia.aioe.org; logging-data="65142"; posting-host="qEYNA8HgrHeNBpz3ZeV8rQ.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: XanaNews/1.18.1.6
X-Notice: Filtered by postfilter v. 0.9.2
 by: Robert L. - Wed, 16 Feb 2022 13:37 UTC

> > Suppose I have:
> >
> > (defvar *list*
> > '((1 2)
> > (3 4)
> > (5 6)
> > (1 7)))
> >
> >
> > Now, suppose I want the "keys" of the list, defined by the first element
> > of the list. Is there a Lisp function which is callable something like:
> > (keys *list* :key #'first) ; => '(1 3 5)
> >
> > Suppose further that I want to accumulate totals based on keys. The
> > first element in the list is the key, and the second element in the list
> > is the value. Is there a Lisp function which is callable something like:
> > (accum *list*) ; => '( (1 9) (3 4) (5 6) )
>
> What we can do is define a somewhat general function for processing this type
> of associative list.
>
> (defun histogram (assoc-list reduce-func &rest reduce-args)
> (let ((hash (make-hash-table :test #'eql)))
> (loop for (key value) in assoc-list
> do (push value (gethash key hash)))
> (loop for key being the hash-keys of hash
> using (hash-value value-list)
> collect `(,key
> ,(apply #'reduce reduce-func value-list reduce-args)))))
>
> What HISTOGRAM does is collates the values that share the same key into
> lists, and then it processes each list through REDUCE, so that you can
> summarize the values using arbitrary arithmetic, not only addition.
>
> Some tests:
>
> Add:
>
> (histogram '((1 2) (3 4) (5 6) (1 7)) #'+)
>
> -> ((5 6) (3 4) (1 9))
>
> Multiply:
>
> (histogram '((1 2) (3 4) (5 6) (1 7)) #'*)
>
> -> ((5 6) (3 4) (1 14))
>
> Add, supplying initial value for each summation:
>
> (histogram '((1 2) (3 4) (5 6) (1 7)) #'+ :initial-value 100)
>
> -> ((5 106) (3 104) (1 109))

Gauche Scheme:

(define (assoc-consolidate alist :optional (func +) (init #f))
(let*
((keys (delete-duplicates (map car alist)))
(bundles
(map
(lambda (k) (map cdr (filter (lambda(x)(equal? k (car x))) alist)))
keys))
(vals
(map
(lambda (xs)
(reduce func #f (append (if init (list init) '()) xs)))
bundles)))
(map cons keys vals)))

(assoc-consolidate '((a . 2) (b . 3) (c . 4) (a . 20) (b . 30) (a . 200)))
===>
((a . 222) (b . 33) (c . 4))

(assoc-consolidate '((a . 2) (b . 3) (c . 4) (a . 20) (b . 30) (a . 200))
+ 5000)
===>
((a . 5222) (b . 5033) (c . 5004))


devel / comp.lang.lisp / .Re: Basic List processing

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor