In Python enumerate works as follows:
a_list = ['a', 'b', 'c']
for i, x in enumerate(a_list):
print(i, x)
The output will be:
0 a
1 b
2 c
Thus, enumerate actually returns a generator (pretty much a lazy sequence) of pairs of the form (i, x) where i ranges over 0, 1, 2, ... and x are the elements of the list in order.
So far I have come up with this definition for lists, which does not produce a "generator" but also a list of pairs:
let enumerate (a_list: 'a list): (int * 'a) list =
let rec _enumerar (a_list: 'a list) (accum: (int * 'a) list) (i: int): (int * 'a) list =
match a_list with
| [] -> accum
| x::xs -> _enumerar xs ((i, x)::accum) (i+1)
in
_enumerar a_list [] 0 |> List.rev
Example usage:
# enumerate ['a'; 'b'; 'c'];;
- : (int * char) list = [(0, 'a'); (1, 'b'); (2, 'c')]
Any ideas whether this function perhaps with a different name is implemented anywhere in the standard library or in Base?
What about a lazy version using Sequence or Stream?
If you want to add indices to a list, you can easily do this with
List.mapi (fun i x -> (i,x)), so thatwill return a new list
[0,'a'; 1, 'b'; 2, 'c']. It is basically what yourenumeratefunction is doing.Of course, it is not a lazy generator. There is the lazy generator module
Seqin the standard library, but it was added pretty recently so the interface is still developing. Alternatively, you can use theSequencemodule available in Janestreet'sBase(and its extension,Core), e.g.,The generated sequence is lazy so the function
enumerate xsis O(1) that doesn't iterate the list until the sequence is used, e.g.,Of course, the same could be done easily with, the
Sequence.iterior even withList.iteriiterators. This brings us to the topic of iterators. In OCaml, as well as in many other functional programming languages, we use iterators, such asiter,map, andfoldto express iteration over containers. Most of the tasks could be easily expressed using one of these (and derived from them) iterators so it is rarely necessary to use the pythonic approach that will generate a lot of intermediate data structures.