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=none autolearn=disabled version=3.1.3 Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by yquem.inria.fr (Postfix) with ESMTP id CF38FBC6B for ; Wed, 2 Jan 2008 18:00:16 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAAFZSe0eBrw8Eh2dsb2JhbACQEQEBAQgKKZdg X-IronPort-AV: E=Sophos;i="4.24,235,1196636400"; d="scan'208";a="7389222" Received: from ext.lri.fr ([129.175.15.4]) by mail3-smtp-sop.national.inria.fr with ESMTP; 02 Jan 2008 18:00:16 +0100 Received: from localhost (localhost [127.0.0.1]) by ext.lri.fr (Postfix) with ESMTP id 74385A4401; Wed, 2 Jan 2008 18:00:16 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at lri.fr Received: from ext.lri.fr ([127.0.0.1]) by localhost (ext.lri.fr [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 6ljvwRSr0Q-k; Wed, 2 Jan 2008 18:00:16 +0100 (CET) Received: from [82.249.236.101] (lns-bzn-44-82-249-236-101.adsl.proxad.net [82.249.236.101]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by ext.lri.fr (Postfix) with ESMTP id 12C15A43AD; Wed, 2 Jan 2008 18:00:16 +0100 (CET) Message-ID: <477BC31D.7060401@lri.fr> Date: Wed, 02 Jan 2008 18:00:13 +0100 From: =?ISO-8859-1?Q?Jean-Christophe_Filli=E2tre?= User-Agent: Thunderbird 1.5.0.14pre (X11/20071022) MIME-Version: 1.0 To: Kuba Ober Cc: caml-list@yquem.inria.fr Subject: Re: [Caml-list] [NEWBIE] is there an in-place map? References: <200801021152.30105.ober.14@osu.edu> In-Reply-To: <200801021152.30105.ober.14@osu.edu> X-Enigmail-Version: 0.94.2.0 OpenPGP: url=http://www.lri.fr/~filliatr/mykey.asc Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Spam: no; 0.00; filliatre:01 filliatre:01 lri:01 blit:01 arrays:01 allocates:01 iteri:01 iteri:01 equivalently:01 lri:01 filliatr:01 caml-list:01 omitted:01 argument:02 newbie:02 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. > 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. -- Jean-Christophe Filliâtre http://www.lri.fr/~filliatr/