Version française
Home     About     Download     Resources     Contact us    
Browse thread
Strange observation on polymorphic '<'
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Frédéric_Gava <frederic.gava@w...>
Subject: Re: [Caml-list] Missing a function
> > I have done a comparaison on three data structures of the stdlib of
OCaml (I
> > do parallel implementation of this data structure) and I find that the
> > module Map does not contain any function for counting the number of
elements
> > of the data structure:
> >    Set, cardinal: t -> int
> >    Hashtbl,  lenght : ('a,'b) t -> int
> >    Map,    you have to do   "let length the_map = fold (fun _ _ i ->
i+1)
> > the_map 0 "
> > And Set and Hastbl have also fold...
> > Is there an explanation (or is it just a missing ;-) ) ?
>
> There is a 'fold' function in functor Make.
> val fold : (key -> 'a -> 'b -> 'b) -> 'a t -> 'b -> 'b
> so :
> let cardinal e = fold (fun _ _ x -> succ x) e 0;;

Hi,

> >    Map,    you have to do   "let length the_map = fold (fun _ _ i ->
i+1)
> let cardinal e = fold (fun _ _ x -> succ x) e 0;;

Ok. That is the same things (except the name) that I have written  in  my
mail... but this method is not very efficient because you apply many time
a function without using its arguments. Map are also implemented as balanced
tree (like sets) and therefore, it is easy to add a function of "cardinal".

Cheers,
Frédéric Gava