From: Jonathan R. <jon...@gm...> - 2005-11-30 23:12:48
|
How about this for Array.filter? # let filter pred array =3D let j =3D ref 0 in for i =3D 0 to Array.length array - 1 do if pred array.(i) then ( array.(!j) <- array.(i); incr j; ) done; Array.sub array 0 !j;; val filter : ('a -> bool) -> 'a array -> 'a array =3D <fun> Although, it's a destructive update, which is probably not desired... in which case, add "let array =3D Array.copy array in" before the loop. I haven't done a benchmark, but I think could be more efficient. Jonathan |