[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | Jean-Christophe_Filliātre <Jean-Christophe.Filliatre@l...> |
| Subject: | Re: [Caml-list] [NEWBIE] is there an in-place map? |
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/