* [Caml-list] how to use functors?
@ 2005-11-19 21:37 Jonathan Roewen
2005-11-19 22:11 ` Jon Harrop
2005-11-22 15:22 ` Christophe Raffalli
0 siblings, 2 replies; 3+ messages in thread
From: Jonathan Roewen @ 2005-11-19 21:37 UTC (permalink / raw)
To: caml-list
Hi,
Prolly the wrong list, but I can't access caml.inria.fr's documentation ATM =/
How do I use a functor? For example: build a weak hashtable from Weak
module? Seems useful for building a cache... if only I knew how to use
it =P
Jonathan
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [Caml-list] how to use functors?
2005-11-19 21:37 [Caml-list] how to use functors? Jonathan Roewen
@ 2005-11-19 22:11 ` Jon Harrop
2005-11-22 15:22 ` Christophe Raffalli
1 sibling, 0 replies; 3+ messages in thread
From: Jon Harrop @ 2005-11-19 22:11 UTC (permalink / raw)
To: caml-list
On Saturday 19 November 2005 21:37, Jonathan Roewen wrote:
> Hi,
>
> Prolly the wrong list, but I can't access caml.inria.fr's documentation ATM
> =/
This is probably one for the beginners list. There is a brief explanation of
functors on my "Benefits of OCaml" pages:
http://www.ffconsultancy.com/free/ocaml/
under "Modules and Functors".
Feed the functor a module with a sig that matches that required by the functor
and it will spit out another module based upon the one you gave.
For example, the built-in "String" module contains the "t" type and "compare"
function definitions required by the "Set.Make" functor. So applying "String"
to "Set.Make" gives you a module implementing sets of strings:
# module SetOfStrings = Set.Make(String);;
module SetOfStrings :
sig
type elt = String.t
type t = Set.Make(String).t
val empty : t
val is_empty : t -> bool
val mem : elt -> t -> bool
val add : elt -> t -> t
val singleton : elt -> t
val remove : elt -> t -> t
val union : t -> t -> t
val inter : t -> t -> t
val diff : t -> t -> t
val compare : t -> t -> int
val equal : t -> t -> bool
val subset : t -> t -> bool
val iter : (elt -> unit) -> t -> unit
val fold : (elt -> 'a -> 'a) -> t -> 'a -> 'a
val for_all : (elt -> bool) -> t -> bool
val exists : (elt -> bool) -> t -> bool
val filter : (elt -> bool) -> t -> t
val partition : (elt -> bool) -> t -> t * t
val cardinal : t -> int
val elements : t -> elt list
val min_elt : t -> elt
val max_elt : t -> elt
val choose : t -> elt
val split : elt -> t -> t * bool * t
end
> How do I use a functor? For example: build a weak hashtable from Weak
> module? Seems useful for building a cache... if only I knew how to use
> it =P
You may want something more controllable.
--
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
Objective CAML for Scientists
http://www.ffconsultancy.com/products/ocaml_for_scientists
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [Caml-list] how to use functors?
2005-11-19 21:37 [Caml-list] how to use functors? Jonathan Roewen
2005-11-19 22:11 ` Jon Harrop
@ 2005-11-22 15:22 ` Christophe Raffalli
1 sibling, 0 replies; 3+ messages in thread
From: Christophe Raffalli @ 2005-11-22 15:22 UTC (permalink / raw)
To: Jonathan Roewen; +Cc: caml-list
Jonathan Roewen a écrit :
> Hi,
>
> Prolly the wrong list, but I can't access caml.inria.fr's documentation ATM =/
>
> How do I use a functor? For example: build a weak hashtable from Weak
> module? Seems useful for building a cache... if only I knew how to use
> it =P
>
> Jonathan
>
You want weak key, weak vailue or both ?
I mean you want the binding in the hashtbl to disappear when the key is
no more GC accessible, the value of both ?
Let us assume you want key-weak ...
This is not completely trivial to do what you want from the Weak module
and this is not a problem of functor ...
A way to do that is to do build a weak array for the key, with a
hashtable algorithm for the index (the position of the key in the array
depends upon the hash function)
Then you put the associated value in a separate non weak array, at the
same index than the key.
Finaly, you also put a finaliser on the key to erase the associated
value when the key is collected by the GC ...
I hope this help ... and I think weak hashtbl should be provided by the
language ...
--
Christophe Raffalli
Université de Savoie
Batiment Le Chablais, bureau 21
73376 Le Bourget-du-Lac Cedex
tél: (33) 4 79 75 81 03
fax: (33) 4 79 75 87 42
mail: Christophe.Raffalli@univ-savoie.fr
www: http://www.lama.univ-savoie.fr/~RAFFALLI
---------------------------------------------
IMPORTANT: this mail is signed using PGP/MIME
At least Enigmail/Mozilla, mutt or evolution
can check this signature. The public key is
stored on www.keyserver.net
---------------------------------------------
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2005-11-22 15:22 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-11-19 21:37 [Caml-list] how to use functors? Jonathan Roewen
2005-11-19 22:11 ` Jon Harrop
2005-11-22 15:22 ` Christophe Raffalli
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox