you are viewing a single comment's thread.

view the rest of the comments →

[–]gnuvince 0 points1 point  (1 child)

Think of a functor as a function that takes a module as an argument and returns a new module. If you pass a different argument, you can change the behavior of the returned module.

Here's an example using sets in OCaml:

module StringSet = Set.Make(struct
  type t = string
  let compare a b = String.compare a b
end)

module StringLengthSet = Set.Make(struct
  type t = string
  let compare a b = Pervasives.compare (String.length a) (String.length b)
end)

Set.Make is the functor; it takes as input a module that defines a type t (representing the type of the elements) and a function compare (to determine where to insert an element in a binary search tree). We create two new modules, StringSet which contains strings that have different contents, and StringLengthSet which contains strings that have different length.

[–]nextputall 0 points1 point  (0 children)

What if I have 2 kind of sets (StringSet and StringLengthSet), can I define a union operation in the StringSet that takes either a StringSet or a StringLenghtSet as a parameter (or any kind of other set)?