package tablecloth-base

  1. Overview
  2. Docs

Module Tablecloth.ComparatorSource

Comparator provide a way for custom data structures to be used with Maps and Sets

Say we have a module Book which we want to be able to create a Set of

  module Book = struct
    type t = {
      isbn: string;
      title: string;
    }

    let compare book_a book_b =
      String.compare book_a.isbn book_b.isbn
  end

First we need to make our module conform to the S signature.

This can be done by using the Make functor.

  module Book = struct
    type t = {
      isbn: string;
      title: string;
    }

    let compare book_a book_b =
      String.compare book_a.isbn book_b.isbn

    include Comparator.Make(struct
      type nonrec t = t

      let compare = compare
    end)
  end

Now we can create a Set of books:

  Set.from_list (module Book) [
    { isbn="9788460767923"; title="Moby Dick or The Whale" }
  ]
Sourcemodule type T = sig ... end

T represents the input for the Make functor.

Sourcetype ('a, 'identity) t = ('a, 'identity) Base.Comparator.t
Sourcetype ('a, 'identity) comparator = ('a, 'identity) t

This just is an alias for t.

Sourcemodule type S = sig ... end

The output type of Make.

Sourcetype ('a, 'identity) s = (module S with type identity = 'identity and type t = 'a)

A type alias that is useful typing functions which accept first class modules like Map.empty or Set.from_array.Tablecloth

Sourcemodule Make (M : T) : S with type t := M.t

Create a new comparator by providing a module which satisifies T.

OCaml

Innovation. Community. Security.