[
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: | 2006-07-25 (08:29) |
From: | Christoph Bauer <christoph.bauer@l...> |
Subject: | generic Hashtbl.to_array |
Hi, what is the best way to write Hashtbl.to_array? Hashtbl.to_array : ('a, 'b) Hashtbl.t -> ('a * 'b) array The simples idea has the problem, that you don't have a initial value to make the result array: let to_array t = let a = Array.init make (Hashtbl.length t) ?init? in ignore (Hashtbl.fold (fun k v i -> a.(i) <- (k, v); i + 1) t 0); a The best solution I found is let to_array t = let dummy = Array.init 0 (fun _ -> raise Not_found) in fst (Hashtbl.fold (fun k v (a, i) -> if i = 0 then let a = Array.make (Hashtbl.length t) (k, v) in (a, 0) else (a.(i) <- (k, v); (a, i + 1))) t (dummy, 0)) Is there a better one? Thanks, Christoph Bauer