Browse thread
modifying hash tables
-
Sam Steingold
- Christopher L Conway
- Sam Steingold
- David Allsopp
- Zheng Li
- Jon Harrop
[
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: | Christopher L Conway <cconway@c...> |
| Subject: | Re: [Caml-list] modifying hash tables |
Assuming that your question is "is it OK to modify a Hashtbl while you
iterate over it?" and assuming that the answer is "maybe, but maybe
not" and assuming that I am looking for a way to avoid doing real
work, you might try the following:
(Hashtbl.fold
(fun key data f ->
(fun ht -> Hashtable.replace ht key (data+1); f ht))
ht
(fun _ -> ()))
ht
Chris
On 7/18/07, Sam Steingold <sds@gnu.org> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> I want to modify the _current_ datum in Hashtable.iter, e.g., increment
> a count. Is this code valid?
> Hashtable.iter ht ~f:(fun ~key ~data ->
> Hashtable.replace ht ~key ~data:(data+1))
> (it appears to work, but I was told that "it is not guaranteed to work",
> i.e., the documentation does not promise that).
> If the above code is wrong, what is TRT?
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.6 (GNU/Linux)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
>
> iD8DBQFGnjFxPp1Qsf2qnMcRAhFaAJ0WEo90RwF5e1Ue+0tCdLyW2R5gawCcCJfd
> T8szhrihvlLwCh5L+er0N1E=
> =9bhb
> -----END PGP SIGNATURE-----
>
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>
>