From: Brian Hurt <bhurt@janestcapital.com>
To: "Jean-Christophe Filliâtre" <Jean-Christophe.Filliatre@lri.fr>,
"Caml List" <caml-list@yquem.inria.fr>
Subject: Re: [Caml-list] [NEWBIE] is there an in-place map?
Date: Wed, 02 Jan 2008 12:06:14 -0500 [thread overview]
Message-ID: <477BC486.3050101@janestcapital.com> (raw)
In-Reply-To: <477BC31D.7060401@lri.fr>
[-- Attachment #1: Type: text/plain, Size: 1345 bytes --]
Jean-Christophe Filliâtre wrote:
>Kuba Ober a écrit :
>
>
>>I need functionality of map, but done in such a way that the output array
>>is given as the argument, not as a return value. The closest I could get was
>>
>>let inplace_map f a b = Array.blit (map f a) 0 b 0 (Array.length a)
>>
>>Arrays a and b are of the same size.
>>This seems very inelegant.
>>
>>
>
>I guess you mean Array.map. Indeed, this is not optimal because
>Array.map allocates a new array, whose contents is immediately copied
>into b.
>
>
>
There's a reason for that...
>>One could use an adapter function and iteri, but that adds very noticeable
>>overhead, and doesn't seem too elegant either.
>>
>>let inplace_map f a b = Array.iteri (fun i src -> b.(i) <- f src; ()) a
>>
>>
>
>You may find this inelegant too, but it is clearly more efficient than
>your previous version using map. Note that "; ()" can be omitted, since
>the assignment <- already has type unit.
>
>Equivalently, you could use a for loop instead of Array.iteri.
>
>
>
Both of these maps have the type ('a -> 'a) -> 'a array -> 'a array, as
opposed to the normal map, which has the type ('a -> 'b) -> 'a array ->
'b array. Once you allocate an array, you can't change the type it
holds. So I'd probably give the function some other name, maybe
"modify" or similiar.
Brian
[-- Attachment #2: Type: text/html, Size: 1978 bytes --]
next prev parent reply other threads:[~2008-01-02 17:06 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-01-02 16:52 Kuba Ober
2008-01-02 17:00 ` [Caml-list] " Jean-Christophe Filliâtre
2008-01-02 17:06 ` Brian Hurt [this message]
2008-01-02 17:26 ` Edgar Friendly
2008-01-02 17:36 ` Brian Hurt
2008-01-02 19:20 ` Edgar Friendly
2008-01-03 16:01 ` Kuba Ober
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=477BC486.3050101@janestcapital.com \
--to=bhurt@janestcapital.com \
--cc=Jean-Christophe.Filliatre@lri.fr \
--cc=caml-list@yquem.inria.fr \
/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