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
.
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.
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With