Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

Q: How do you save a drowning lawyer? A: Throw him a rock.


comp / comp.lang.lisp / Re: map and reduce

SubjectAuthor
o Re: map and reduceB. Pym

1
Subject: Re: map and reduce
From: B. Pym
Newsgroups: comp.lang.lisp
Organization: A noiseless patient Spider
Date: Fri, 19 Jul 2024 15:48 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: map and reduce
Date: Fri, 19 Jul 2024 15:48:02 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 54
Message-ID: <v7e1rf$321ib$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Date: Fri, 19 Jul 2024 17:48:03 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="adff5a388ff6d0ae53c744bdb00d17f0";
logging-data="3212875"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18U3nPu+TY4zvPkpJzbedSf"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:WrXsORZW1DpUpMEDYFLx6eKbGbo=
View all headers

> > Consider n vectors (or lists) v1, ..., vn of equal length, an n-
> > variate function f, and a bivariate function g.
> >
> > I want to calculate (reduce g (map 'vector f v1 v2 ... vn)), eg
> >
> > (reduce #'+ (map 'vector #'* '(1 2 3) '(4 5 6))) ; => 32
> >
> > but without the intermediate vector (my vectors are long). I can of
> > course write a function to do this, but I am wondering if there is a
> > clever way to do it with CL library functions.
>
> I don't think there is a way to do this sort of thing directly. It's
> not difficult (or even particularly ugly) to do using LOOP, though. For
> the case G = #'+, it's particularly nice:
>
> (loop for x across first-vector
> for y across second-vector
> sum (funcall f x y))

That cannot handle any number of vectors.

>
> but for general G, you need
>
> (loop for x across first-vector
> for y across second-vector
> for temp = (funcall f x y)
> for result = temp then (funcall result temp)

Wrong. Try (funcall G result temp)

> finally (return result))

Gauche Scheme

(use srfi-43) ;; vector ops.

(define (vec-map-reduce red-func seed map-func . vectors)
(apply vector-fold
(lambda (i accum . elements)
(red-func accum (reduce map-func #f elements)))
seed
vectors))

(vec-map-reduce + 0 *
#(9 2 3 4)
#(5 6 7 8)
#(20 22 23 24))

===>
2415

1

rocksolid light 0.9.8
clearnet tor