Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

You single-handedly fought your way into this hopeless mess.


comp / comp.lang.lisp / Re: walk through list and add all n'th item

SubjectAuthor
* Re: walk through list and add all n'th itemB. Pym
`- Re: walk through list and add all n'th itemKaz Kylheku

1
Subject: Re: walk through list and add all n'th item
From: B. Pym
Newsgroups: comp.lang.lisp
Organization: A noiseless patient Spider
Date: Fri, 30 Aug 2024 16:42 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: walk through list and add all n'th item
Date: Fri, 30 Aug 2024 16:42:30 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 59
Message-ID: <vasspk$inrq$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Date: Fri, 30 Aug 2024 18:42:30 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="7471b6bf57a1160866342d87b5967bc4";
logging-data="614266"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/o7aYssbvDID9Wl+tvSPUG"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:0eNM2Fx+b8UFt5ydFh0W0fasKqw=
View all headers

> > I'm just wondering if there is a lispier way to scan once through a
> > list and add each n'th item with n+constant.
> > Like kind of apply a list through a vector..
> > In my approach i just used the loop macro:
> > (defparameter vals '(1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1
> > 2 3 4 5 6 7 8))
> ...
> > CL-USER> (sum-values vals)
> > (4 8 12 16 20 24 28 32)
> >
> > Can you do better? (i hope you do and I am prepared to bear the shame)
>
> I see that you ask to "scan once" but still feel awfully
> tempted to reblock the data
>
> CL-USER> (defun batch (list size)
> (if (endp list)
> '()
> (cons (subseq list 0 size)
> (batch (nthcdr size list) size))))
> BATCH
>
> CL-USER> (batch vals 8)
>
> ((1 2 3 4 5 6 7 8) (1 2 3 4 5 6 7 8) (1 2 3 4 5 6 7 8) (1 2 3 4 5 6 7 8))
>
> CL-USER> (defun add (list)
> (reduce (lambda(u v)(mapcar #'+ u v)) list))
> ADD
>
> CL-USER> (add (batch vals 8))
> (4 8 12 16 20 24 28 32)

Mark Wooding wrote:

> Or you could just do it the easy way:
>
> (defun sum-every-softcore (period list)
> (loop with sums = (make-array period :initial-element 0)
> for item in list
> for i = 0 then (mod (1+ i) period)
> do (incf (aref sums i) item)
> finally (return (coerce sums 'list))))

Gauche Scheme

(use srfi-42) ;; do-ec

(define (sum-every-nth n nlist)
(rlet1 result (make-vector n 0)
(do-ec (:list x (index i) nlist)
(inc! (vector-ref result (mod i n)) x))))

(define vals '(1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1
2 3 4 5 6 7 8))

(sum-every-nth 8 vals)

#(4 8 12 16 20 24 28 32)

Subject: Re: walk through list and add all n'th item
From: Kaz Kylheku
Newsgroups: comp.lang.lisp
Organization: A noiseless patient Spider
Date: Fri, 30 Aug 2024 17:02 UTC
References: 1
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 643-408-1753@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.lisp
Subject: Re: walk through list and add all n'th item
Date: Fri, 30 Aug 2024 17:02:59 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 74
Message-ID: <20240830100202.195@kylheku.com>
References: <vasspk$inrq$1@dont-email.me>
Injection-Date: Fri, 30 Aug 2024 19:02:59 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="cbe2877523f20dda3b599fd18e2277a3";
logging-data="618460"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18vRa7F/sHOguHsW7Ib7eQMwHLVPqcYh7k="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:L4bLHuHp3oQX/IEFcKSmhkQxV2s=
View all headers

On 2024-08-30, B. Pym <Nobody447095@here-nor-there.org> wrote:
>> > I'm just wondering if there is a lispier way to scan once through a
>> > list and add each n'th item with n+constant.
>> > Like kind of apply a list through a vector..
>> > In my approach i just used the loop macro:
>> > (defparameter vals '(1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1
>> > 2 3 4 5 6 7 8))
>> ...
>> > CL-USER> (sum-values vals)
>> > (4 8 12 16 20 24 28 32)
>> >
>> > Can you do better? (i hope you do and I am prepared to bear the shame)
>>
>> I see that you ask to "scan once" but still feel awfully
>> tempted to reblock the data
>>
>> CL-USER> (defun batch (list size)
>> (if (endp list)
>> '()
>> (cons (subseq list 0 size)
>> (batch (nthcdr size list) size))))
>> BATCH
>>
>> CL-USER> (batch vals 8)
>>
>> ((1 2 3 4 5 6 7 8) (1 2 3 4 5 6 7 8) (1 2 3 4 5 6 7 8) (1 2 3 4 5 6 7 8))
>>
>> CL-USER> (defun add (list)
>> (reduce (lambda(u v)(mapcar #'+ u v)) list))
>> ADD
>>
>> CL-USER> (add (batch vals 8))
>> (4 8 12 16 20 24 28 32)
>
> Mark Wooding wrote:
>
>> Or you could just do it the easy way:
>>
>> (defun sum-every-softcore (period list)
>> (loop with sums = (make-array period :initial-element 0)
>> for item in list
>> for i = 0 then (mod (1+ i) period)
>> do (incf (aref sums i) item)
>> finally (return (coerce sums 'list))))
>
> Gauche Scheme
>
> (use srfi-42) ;; do-ec
>
> (define (sum-every-nth n nlist)
> (rlet1 result (make-vector n 0)
> (do-ec (:list x (index i) nlist)
> (inc! (vector-ref result (mod i n)) x))))
>
> (define vals '(1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1
> 2 3 4 5 6 7 8))
>
> (sum-every-nth 8 vals)
>
> #(4 8 12 16 20 24 28 32)

This is the TXR Lisp interactive listener of TXR 296.
Quit with :quit or Ctrl-D on an empty line. Ctrl-X ? for cheatsheet.
TXR Lisp environments use genuine Saskatchewan sealskin bindings.
1> (flow '(1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8)
(tuples 8)
transpose
(mapcar sum))
(4 8 12 16 20 24 28 32)

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

1

rocksolid light 0.9.8
clearnet tor