Out of curiousity, I wanted to implement a generic iterative fold/Aggregate function in these languages to see how they compared. They're both quite straight forward and low level:
C#
public T Aggr<T>(IEnumerable<T> eble, Func<T, T, T> fn) {
var etor = eble.GetEnumerator();
etor.MoveNext();
var res = etor.Current;
while (etor.MoveNext())
res = fn(res, etor.Current);
return res;
}
Scala
def aggr[T](iterable: Iterable[T], fn: (T, T) => T): T = {
val it = iterable.iterator
var res = it.next
while (res.hasNext)
res = fn(res, res.next)
res
}
So it has to lock in the T type when it gets to the first parameter; it can't wait and infer T=Any once it's found all the parameters?