From: Brian H. <bri...@ql...> - 2003-05-29 17:53:51
|
On Thu, 29 May 2003, William D. Neumann wrote: > On Thu, 29 May 2003, Brian Hurt wrote: > > Yeah, exposing phony nulls was one of the philisophical issues I was > wrestling with as I added my expansion hack. At the time I decided that > the ability to do this outweighed the potential to do something silly (and > I promised myself to always look out for null values (the null I'm using > in this case would never be a valid value, so I can ignore them when I run > across them). One of the reasons I didn't want to use options as the array elements- for the cases where they weren't needed, and there really was a safe null. > > And you're right, expanding the array the way I described it won't work > like I wanted, you have to do something along the lines of: > > let null = "";; > let j = DynArray.make 2 null;; > DynArray.add j "blah";; > DynArray.add j "flah";; > > (* Now I want to add something to slot 9 *) > > ignore (Dynarray.append j (DynArray.init 8 8 null (fun x -> null)));; > DynArray.set j 9 "flee";; Given: let enum_dup c val = (* returns an enumeration which is just val repeated c times *) let cref = ref c in let next () = if (!cref) > 0 then decr cref; val else raise No_more_elements and count () = !cref in Enum.make ~next:next ~count:count You don't actually need to create the intermediate array. You can just do: DynArray.set_enum j (DynArray.length j) (enum_dup 8 ""); DynArray.set j 9 "flee";; Mental note to self: need to add DynArray.append_enum. > Sigh...why does doing things the right way always interfere with doing > what I think I want... Because what you think you wanted to do is not what you really wanted to do. Perhaps instead of a Dynamic Array, you wanted a hash table mapping int->'a? Brian |