From: Brian H. <bh...@sp...> - 2003-04-18 15:52:05
|
On Fri, 18 Apr 2003, Nicolas Cannasse wrote: > BTW, about your "count" problem you HAVE TO return the lines count, because > for exemple Array.of_enum will require it to create the array before putting > elements inside. The solution is quite easy to do : when "count" is called > the first time, you're reading all remaining files and then have to modify > your Enum.t which will now iter on the builded list. This is almost what the > "Enum.force" is doing, but I modified its behavior so it can be used this > way. This is suboptimal in cases- like Xarray and list- where they don't really need the count. Xarray is a very good example. Count isn't *needed*, but it is handy. Instead, I'd implement Array.of_enum like: let rec of_enum enum = let c = Enum.count enum in if (c < 0) then (* Convert the enum to a list, then create the array from the list *) of_enum (List.enum_of (List.of_enum enum)) else Array.init c (fun _ -> Enum.next enum) ;; This basically does the same thing your example does- it recreates the enum as a list, and then enumerates that. The only of_enum function I can think of that *needs* a count is Array.of_enum. I can think of a lot of enumerations where I don't know, at the start, how many elements are in the enumeration. Brian |