Python-style enumerate for Nemerle
So instead of just whining about Nemerle, here's a something potentially useful. I found I missed Python's built-in enumerate
function. It wasn't too hard to implement in Nemerle:
public class Enumerator['a] : IEnumerable[(int * 'a)] {
private e: IEnumerable['a];
public this(e: IEnumerable['a]) {
this.e = e;
}
public GetEnumerator(): IEnumerator[(int * 'a)] {
mutable i = 0;
foreach (elem in this.e) {
yield (i, elem);
i++;
}
}
}
Usage:
foreach ((index, item) in Enumerator(list)) {
System.Console.WriteLine($"index for $item: $index");
}
The naming might not be optimal, with potential confusion with the standard libraries, but I don't care at the moment. Use and enjoy.
25 August 2007