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