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 that
List.mapi (fun i x -> (i,x)) ['a';'b';'c']
will return a new list [0,'a'; 1, 'b'; 2, 'c']
. It is basically what your enumerate
function is doing.
Of course, it is not a lazy generator. There is the lazy generator module Seq
in the standard library, but it was added pretty recently so the interface is still developing. Alternatively, you can use the Sequence
module available in Janestreet's Base
(and its extension, Core
), e.g.,
open Base
let enumerate xs =
Sequence.of_list xs |>
Sequence.mapi ~f:Tuple.T2.create
The generated sequence is lazy so the function enumerate xs
is O(1) that doesn't iterate the list until the sequence is used, e.g.,
open Base
let () = enumerate ['a'; 'b'; 'c'] |> Sequence.iter ~f:(fun (i,c) ->
printf "%d %c\n" i c
Of course, the same could be done easily with, the Sequence.iteri
or even with List.iteri
iterators. This brings us to the topic of iterators. In OCaml, as well as in many other functional programming languages, we use iterators, such as iter
, map
, and fold
to 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.