Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find the size of `map` in ocaml? I am using `Map.Make` functor to create module?

Tags:

ocaml

I could not find any size or len function in the official documentation. What is a simple way to find the number of elements in a map created using:

module M = Map.Make(String)

I am looking for something like M.size M.empty : 0.

like image 266
abhishek Avatar asked Aug 17 '17 19:08

abhishek


People also ask

What is a functor in OCaml?

What are functors and why do we need them? A functor is a module that is parametrized by another module, just like a function is a value which is parametrized by other values, the arguments. It allows one to parametrize a type by a value, which is not possible directly in OCaml without functors.

What is a sequence in OCaml?

Sequences. A sequence of type 'a Seq. t can be thought of as a delayed list, that is, a list whose elements are computed only when they are demanded by a consumer. This allows sequences to be produced and transformed lazily (one element at a time) rather than eagerly (all elements at once).


1 Answers

The function that you're looking for is called cardinal (as in the cardinality of a set).

Example:

module M = Map.Make(String)

let m = M.singleton "x" "y"
let () = Printf.printf "%d\n" (M.cardinal m)

This will print 1, as there is exactly one binding.

like image 151
Reimer Behrends Avatar answered Dec 08 '22 04:12

Reimer Behrends