From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=AWL,HTML_MESSAGE autolearn=disabled version=3.1.3 Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by yquem.inria.fr (Postfix) with ESMTP id 08872BC6B for ; Wed, 2 Jan 2008 18:06:19 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAAM9Se0dCm3xrnWdsb2JhbACCOjUpjHkBAQEBgU6WSQ X-IronPort-AV: E=Sophos;i="4.24,235,1196636400"; d="scan'208,217";a="20849526" Received: from www.janestcapital.com (HELO smtp.janestcapital.com) ([66.155.124.107]) by mail4-smtp-sop.national.inria.fr with ESMTP; 02 Jan 2008 18:06:17 +0100 Received: from [172.25.129.161] [38.96.172.125] by janestcapital.com with ESMTP (SMTPD-9.10) id A48702C0; Wed, 02 Jan 2008 12:06:15 -0500 Message-ID: <477BC486.3050101@janestcapital.com> Date: Wed, 02 Jan 2008 12:06:14 -0500 From: Brian Hurt User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.2) Gecko/20040804 Netscape/7.2 (ax) X-Accept-Language: en-us, en MIME-Version: 1.0 To: =?ISO-8859-1?Q?Jean-Christophe_Filli=E2tre?= , Caml List Subject: Re: [Caml-list] [NEWBIE] is there an in-place map? References: <200801021152.30105.ober.14@osu.edu> <477BC31D.7060401@lri.fr> In-Reply-To: <477BC31D.7060401@lri.fr> Content-Type: multipart/alternative; boundary="------------030206020804060708070405" X-Spam: no; 0.00; filliatre:01 blit:01 arrays:01 allocates:01 iteri:01 iteri:01 equivalently:01 filliatre:01 blit:01 arrays:01 allocates:01 equivalently:01 wrote:01 wrote:01 caml-list:01 This is a multi-part message in MIME format. --------------030206020804060708070405 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit 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 --------------030206020804060708070405 Content-Type: text/html; charset=us-ascii Content-Transfer-Encoding: 7bit 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


--------------030206020804060708070405--