From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 54582 invoked by alias); 18 Aug 2015 04:43:17 -0000 Mailing-List: contact kawa-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: kawa-owner@sourceware.org Received: (qmail 54569 invoked by uid 89); 18 Aug 2015 04:43:16 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.1 required=5.0 tests=AWL,BAYES_05,KAM_LAZY_DOMAIN_SECURITY,RP_MATCHES_RCVD autolearn=no version=3.3.2 X-HELO: mail.theptrgroup.com Received: from mail.theptrgroup.com (HELO mail.theptrgroup.com) (71.178.251.9) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 18 Aug 2015 04:43:14 +0000 Received: from [10.11.21.6] (unknown [10.11.21.6]) by mail.theptrgroup.com (Postfix) with ESMTPS id 81DE440622 for ; Tue, 18 Aug 2015 00:42:13 -0400 (EDT) Content-Type: text/plain; charset=windows-1252 Mime-Version: 1.0 (Mac OS X Mail 6.6 \(1510\)) Subject: Re: APL-style array indexing in Kawa - a sketch From: Jamison Hope In-Reply-To: <55D2B087.2020506@bothner.com> Date: Tue, 18 Aug 2015 04:43:00 -0000 Content-Transfer-Encoding: quoted-printable Message-Id: <20F7F178-F95E-42A1-AF5E-D5DA09F9553F@theptrgroup.com> References: <55CD4B6A.8010606@bothner.com> <4E24D268-9062-465B-BF2B-966806394A39@theptrgroup.com> <55D2648B.2040203@bothner.com> <112A4F9C-EF11-4386-B0DC-BA93C01A9CA0@theptrgroup.com> <55D2B087.2020506@bothner.com> To: "kawa@sourceware.org list" X-IsSubscribed: yes X-SW-Source: 2015-q3/txt/msg00030.txt.bz2 On Aug 18, 2015, at 12:11 AM, Per Bothner wrote: > On 08/17/2015 08:37 PM, Jamison Hope wrote: >> On Aug 17, 2015, at 6:47 PM, Per Bothner wrote: >>=20 >>> We could define [<:] as a supported syntax (see $bracket-list$ in synta= x.scm). >>> It would be relative easy to define 0 as the default lower bound, but t= hat >>> doesn't quite do what you want. We could define [<;] as a special magi= c value >>> that gets coerced to a specific range depending on context. >>=20 >> Hmm can we pick something that won't confuse paredit mode? >=20 > I'm not familiar with paredit - but why would [<:] confuse it? Oh that was supposed to be a colon. OK nevermind then, [<:] sounds good. [>:] might also be useful, such that (VEC [>:]) reverses the sequence. >> (array-map (lambda (x) (* 2 x)) ARR) =3D> a new array where each element >> is doubled in value. >>=20 >> (array-map + ARR1 ARR2) =3D> a new array representing the elementwise sum >> of the elements of ARR1 and ARR2. >>=20 >> (I'm imagining that the array-map call wouldn't allocate a new data >> buffer, but would just close over the supplied function and array(s), >> lazily applying the function to array elements as needed.) >=20 > "Lazily" is confusing here. I think array-map should eager, in the > same way vector-map is. What it should do: > (1) Figure out the shape of the result. (The same as the argument, if > a single argument; otherwise they may be "broadcast" in the Racket sense.) > (2) Allocate an Object[] buffer for the result whose length is the > element-count of the shape. > (3) Iterate of the shape (which is the same as iterating over the buffer); > extract the corresponding argument array value using the same (or broadca= st) > indexes; call the function. >=20 > http://docs.racket-lang.org/math/array_pointwise.html >=20 > The Racket array functions is another place to look for inspiration. Perhaps "map" is the wrong name to use. My motivation for wanting it to be lazy is to be able to compose these things without causing a bunch of temporary buffers to get allocated needlessly. For instance, suppose * is redefined such that (* ATOM SEQ) performs (array-map (lambda (x) (* ATOM x)) SEQ), and - is redefined such that (- SEQ1 SEQ2) performs (array-map - SEQ1 SEQ2). Then suppose we have an assignment such as (define x (array-copy (* alpha (- u v)))) where alpha is a number and u and v (and x) are sequences. If array-map is lazy, then the iteration only needs to happen a single time, with each element (x i) being effectively evaluated as (* alpha (- (u i) (v i))). If array-map is eager, then the iteration must happen three times, first for the subtraction, then for the multiplication, and then for the copy, allocating data buffers each time. https://en.wikipedia.org/wiki/Expression_templates Like I said, maybe this should be called something other than "map". Although SRFI-42 called its thing stream-map, so there's precedent for lazy mapping. -- Jamison Hope The PTR Group www.theptrgroup.com