Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

BOFH excuse #104: backup tape overwritten with copy of system manager's favourite CD


comp / comp.lang.lisp / Re: How to improve my summarizing code

SubjectAuthor
* Re: How to improve my summarizing codeB. Pym
+- Re: How to improve my summarizing codeB. Pym
+- Re: How to improve my summarizing codeB. Pym
`- Re: How to improve my summarizing codeB. Pym

1
Subject: Re: How to improve my summarizing code
From: B. Pym
Newsgroups: comp.lang.lisp
Organization: A noiseless patient Spider
Date: Mon, 24 Jun 2024 02:19 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: How to improve my summarizing code
Date: Mon, 24 Jun 2024 02:19:34 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 65
Message-ID: <v5al3j$nuim$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Date: Mon, 24 Jun 2024 04:19:35 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="0d722ca2524cc8ec2b9b17c124dc6f1b";
logging-data="784982"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19+7kurir+GbyiTFYRyC4mU"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:PsN1H9CJFltwP/CkObCev/7LdIQ=
View all headers

Wade Humeniuk wrote:

> Yeah, but I do not worry about it, much, at the beginning. Here
> is another, it seems easier to think in the morning
>
> (defun summarize (list)
> (let ((summary nil))
> (map nil (lambda (elt)
> (let ((sum (find (first elt) summary :test #'eql :key #'first)))
> (if sum
> (incf (second sum) (second elt))
> (push elt summary))))
> list)
> summary))
>
> and its loop version
>
> (defun summarize (list)
> (loop with summary = nil
> for elt in list
> for sum = (find (first elt) summary :test #'eql :key #'first)
> if sum do (incf (second sum) (second elt))
> else do (push elt summary)
> finally (return summary)))
>
> CL-USER 2 > (summarize '((c 7) (a 1) (a 3) (b 1) (b 10) (b 100)))
> ((B 111) (A 4) (C 7))

Gauche Scheme

(use gauche.sequence) ;; group-sequence

(define (summarize alist)
(map
(lambda(xs) (list (caar xs) (fold + 0 (map cadr xs))))
(group-sequence alist :key car)))

(summarize '((c 7) (a 1) (a 3) (b 1) (b 10) (b 100)))
===>
((c 7) (a 4) (b 111))

Another way:

(use srfi-1) ;; list-index

(define (index x xs) (list-index (cut equal? x <>) xs))

(define (summarize alist)
(let1 keys (delete-duplicates (map car alist) )
(map
cons
keys
(apply map
+
(map
(lambda(kv)
(let1 v (make-vector (length keys) 0)
(vector-set! v (index (car kv) keys) (cadr kv))
(vector->list v)))
alist)))))

(summarize '((c 7) (a 1) (a 3) (b 1) (b 10) (b 100)))
===>
((c . 7) (a . 4) (b . 111))

Subject: Re: How to improve my summarizing code
From: B. Pym
Newsgroups: comp.lang.lisp
Organization: A noiseless patient Spider
Date: Mon, 24 Jun 2024 15:44 UTC
References: 1
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: How to improve my summarizing code
Date: Mon, 24 Jun 2024 15:44:07 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 88
Message-ID: <v5c482$10fmh$1@dont-email.me>
References: <v5al3j$nuim$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Date: Mon, 24 Jun 2024 17:44:07 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="7ceb891a79063de1ea430bfb036aaa7d";
logging-data="1064657"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19kNyABraL/77AOIT2OWg3Y"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:ihkK6QQe7UCfkuIIARkPBmu9CuU=
View all headers

On 6/23/2024, B. Pym wrote:

> Wade Humeniuk wrote:
>
> > Yeah, but I do not worry about it, much, at the beginning. Here
> > is another, it seems easier to think in the morning
> >
> > (defun summarize (list)
> > (let ((summary nil))
> > (map nil (lambda (elt)
> > (let ((sum (find (first elt) summary :test #'eql :key #'first)))
> > (if sum
> > (incf (second sum) (second elt))
> > (push elt summary))))
> > list)
> > summary))
> >
> > and its loop version
> >
> > (defun summarize (list)
> > (loop with summary = nil
> > for elt in list
> > for sum = (find (first elt) summary :test #'eql :key #'first)
> > if sum do (incf (second sum) (second elt))
> > else do (push elt summary)
> > finally (return summary)))
> >
> > CL-USER 2 > (summarize '((c 7) (a 1) (a 3) (b 1) (b 10) (b 100)))
> > ((B 111) (A 4) (C 7))
>
> Gauche Scheme
>
> (use gauche.sequence) ;; group-sequence
>
> (define (summarize alist)
> (map
> (lambda(xs) (list (caar xs) (fold + 0 (map cadr xs))))
> (group-sequence alist :key car)))
>
> (summarize '((c 7) (a 1) (a 3) (b 1) (b 10) (b 100)))
> ===>
> ((c 7) (a 4) (b 111))
>
>
> Another way:
>
> (use srfi-1) ;; list-index
>
> (define (index x xs) (list-index (cut equal? x <>) xs))
>
> (define (summarize alist)
> (let1 keys (delete-duplicates (map car alist) )
> (map
> cons
> keys
> (apply map
> +
> (map
> (lambda(kv)
> (let1 v (make-vector (length keys) 0)
> (vector-set! v (index (car kv) keys) (cadr kv))
> (vector->list v)))
> alist)))))
>
> (summarize '((c 7) (a 1) (a 3) (b 1) (b 10) (b 100)))
> ===>
> ((c . 7) (a . 4) (b . 111))

Another way:

(define (summarize alist)
(delete-duplicates
(fold
(lambda (kv accum)
(let* ((k (car kv))
(v (cdr kv))
(n (assoc-ref accum k 0)))
(cons (cons k (+ n v)) accum)))
'()
alist)
(lambda(a b)(equal? (car a)(car b)))))

(summarize '((c . 7) (a . 1) (a . 3) (b . 1) (b . 10) (b . 100)))
===>
((b . 111) (a . 4) (c . 7))

Subject: Re: How to improve my summarizing code
From: B. Pym
Newsgroups: comp.lang.lisp
Organization: A noiseless patient Spider
Date: Mon, 24 Jun 2024 18:04 UTC
References: 1
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: How to improve my summarizing code
Date: Mon, 24 Jun 2024 18:04:16 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 60
Message-ID: <v5cces$124eb$1@dont-email.me>
References: <v5al3j$nuim$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Date: Mon, 24 Jun 2024 20:04:16 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="6992755104a3d7a4ecae094c19f92520";
logging-data="1118667"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19QAS5YKRFoFc5murEyEPTi"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:JtR5Muy1JCIL0ml/PS9L7XXgRnw=
View all headers

On 6/23/2024, B. Pym wrote:

> Wade Humeniuk wrote:
>
> > Yeah, but I do not worry about it, much, at the beginning. Here
> > is another, it seems easier to think in the morning
> >
> > (defun summarize (list)
> > (let ((summary nil))
> > (map nil (lambda (elt)
> > (let ((sum (find (first elt) summary :test #'eql :key #'first)))
> > (if sum
> > (incf (second sum) (second elt))
> > (push elt summary))))
> > list)
> > summary))
> >
> > and its loop version
> >
> > (defun summarize (list)
> > (loop with summary = nil
> > for elt in list
> > for sum = (find (first elt) summary :test #'eql :key #'first)
> > if sum do (incf (second sum) (second elt))
> > else do (push elt summary)
> > finally (return summary)))
> >
> > CL-USER 2 > (summarize '((c 7) (a 1) (a 3) (b 1) (b 10) (b 100)))
> > ((B 111) (A 4) (C 7))
>
> Gauche Scheme
>
> (use gauche.sequence) ;; group-sequence
>
> (define (summarize alist)
> (map
> (lambda(xs) (list (caar xs) (fold + 0 (map cadr xs))))
> (group-sequence alist :key car)))
>
> (summarize '((c 7) (a 1) (a 3) (b 1) (b 10) (b 100)))
> ===>
> ((c 7) (a 4) (b 111))

Another way:

(use util.match)

(define (summarize assoclist)
(define tree (make-tree-map))
(for-each
(match-lambda
[(key val) (tree-map-update! tree key (cut + <> val) 0)])
assoclist)
(tree-map->alist tree))

(summarize '((c 7) (a 1) (a 3) (b 1) (b 10) (b 100)))
===>
((a . 4) (b . 111) (c . 7))

Subject: Re: How to improve my summarizing code
From: B. Pym
Newsgroups: comp.lang.lisp
Organization: A noiseless patient Spider
Date: Tue, 25 Jun 2024 06:56 UTC
References: 1
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: How to improve my summarizing code
Date: Tue, 25 Jun 2024 06:56:39 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 29
Message-ID: <v5dpn6$1elf9$1@dont-email.me>
References: <v5al3j$nuim$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Date: Tue, 25 Jun 2024 08:56:39 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="f3516bad7bad6e8b4774a100ca8eaa7a";
logging-data="1529321"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/SS0qBc67nj/n7v1exGLtM"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:9i/KokX8iWOx5PMPDDF3ebJ9ats=
View all headers

On 6/23/2024, B. Pym wrote:

> > (defun summarize (list)
> > (loop with summary = nil
> > for elt in list
> > for sum = (find (first elt) summary :test #'eql :key #'first)
> > if sum do (incf (second sum) (second elt))
> > else do (push elt summary)
> > finally (return summary)))
> >
> > CL-USER 2 > (summarize '((c 7) (a 1) (a 3) (b 1) (b 10) (b 100)))
> > ((B 111) (A 4) (C 7))

(define (summarize alist)
(let1 keys (delete-duplicates (map car alist))
(map
(lambda(k)
(list k
(apply +
(filter-map
(lambda(x) (and (equal? k (car x)) (cadr x)))
alist))))
keys)))

(summarize '((c 7) (a 1) (a 3) (b 1) (b 10) (b 100)))
===>
((c 7) (a 4) (b 111))

1

rocksolid light 0.9.8
clearnet tor