public inbox for kawa@sourceware.org
 help / color / mirror / Atom feed
From: Jamison Hope <jrh@theptrgroup.com>
To: "kawa@sourceware.org list" <kawa@sourceware.org>
Subject: Re: APL-style array indexing in Kawa - a sketch
Date: Tue, 18 Aug 2015 04:43:00 -0000	[thread overview]
Message-ID: <20F7F178-F95E-42A1-AF5E-D5DA09F9553F@theptrgroup.com> (raw)
In-Reply-To: <55D2B087.2020506@bothner.com>

On Aug 18, 2015, at 12:11 AM, Per Bothner <per@bothner.com> wrote:

> On 08/17/2015 08:37 PM, Jamison Hope wrote:
>> On Aug 17, 2015, at 6:47 PM, Per Bothner <per@bothner.com> wrote:
>> 
>>> We could define [<:] as a supported syntax (see $bracket-list$ in syntax.scm).
>>> It would be relative easy to define 0 as the default lower bound, but that
>>> doesn't quite do what you want.  We could define [<;] as a special magic value
>>> that gets coerced to a specific range depending on context.
>> 
>> Hmm can we pick something that won't confuse paredit mode?
> 
> 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) => a new array where each element
>> is doubled in value.
>> 
>> (array-map + ARR1 ARR2) => a new array representing the elementwise sum
>> of the elements of ARR1 and ARR2.
>> 
>> (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.)
> 
> "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 broadcast)
> indexes; call the function.
> 
> http://docs.racket-lang.org/math/array_pointwise.html
> 
> 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



      reply	other threads:[~2015-08-18  4:43 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-08-14  1:59 Per Bothner
2015-08-17 20:24 ` Jamison Hope
2015-08-17 22:48   ` Per Bothner
2015-08-18  3:37     ` Jamison Hope
2015-08-18  4:12       ` Per Bothner
2015-08-18  4:43         ` Jamison Hope [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20F7F178-F95E-42A1-AF5E-D5DA09F9553F@theptrgroup.com \
    --to=jrh@theptrgroup.com \
    --cc=kawa@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).