Module type JoinPool.Shared.Enumerable

module type Enumerable = sig .. end
Functional enumerations


Signature of iterators: modules that offer a functional iterator over collections (type t) of elements (type elt) in a functional way
type t 
Collection
type elt 
Elements in the collection
type enum 
Explicit state.
val start : t -> enum
Start iterating over a collection, start c returns the initial state
val step : enum ->
(elt * enum) option
Iterate once, step st returns None when iteration is over, or Some (e,st') otherwise, where e is the next element and st' is the next explicit state.

An example: iterator over integer intervals:

module Interval = struct
  type t = { low : int; high : int; } (* Interval (low..high) *)
  type elt = int
  type enum = { next : int; max : int; }

  let start x = { next=x.low; max=x.high; }

  let step x =
    if x.next > x.max then None
    else Some (x.next, { x with next=x.next+1; })
end


Another example: iterator over a list:

module  ListMake(E:sig type elt end) = struct
  type t = E.elt list
  type elt = E.elt
  type enum = t

  let start xs = xs

  let step = function
  | [] -> None
  | x::xs -> Some (x,xs)

end