package enumerators

  1. Overview
  2. Docs


This library enables you to work with large sequences of elements. It provides constructors and combinators to build finite sequences that can be iterated through with a low memory footprint.



Published: 07 Nov 2016


Finite lazy enumerators

The enumerators library enables you to work with large sequences of elements.


Let's assume you want to scan ports of a few hosts on your network. Your first task is to enumerate those ports. Naturally, as the list can grow big, you don't want to have them all in memory at the same time. This is were this library comes into play.

In this example, targets have the type string * int where the first element is the IP address and the second element is the network port. Here is a function to print such a target:

let print_target (ip, port) =
  Printf.printf "[%s]:%d\n" ip port

The following instructions will enable you to get a listing of the targets:

  • Define the address enumerator from a list of strings with make.

  • Define the port enumerator with range.

  • Define the enumerator for targets as a cartesian product of the addresses and the ports with product.

  • Iterate over this final enumerator to print each element.

Here's how you can do it:

let () =
  let addresses = Enumerator.make ["2001:db8::1"; "2001:db8::2"] in
  let ports = Enumerator.range 1 1024 in
  let targets = Enumerator.product addresses ports in
  Enumerator.iter print_target targets

You should get the following output:



This library is available under the 2-clause BSD license. See for more information.

Dependencies (4)

  1. topkg build
  2. ocamlfind build
  3. ocamlbuild build
  4. ocaml >= "4.02.0"

Dev Dependencies (1)

  1. ounit with-test

Used by