Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

This looks incredibly clunky and counter-intuitive.

Compare that to yield return, .Select and .Where methods in C#, or Filter and Map in many popular languages - it's not a good look.

Or when writing manually, compare it to

    static IEnumerable<T> Filter<T>(
        IEnumerable<T> source, Func<T, bool> predicate)
    {
        foreach (var item in source)
        {
            if (predicate(item))
                yield return item;
        }
    }
Could also compare to how easy it is to use for extremely common patterns in general purpose code:

    var numbers = Enumerable.Range(0, 10);
    var even = numbers.Where(n => n % 2 is 0);
    var strings = even.Select(n => n.ToString());


Isn’t the only real difference that the yield function is being passed into the iterator instead of being a reserved word? I don’t think it’s clunky, although it took a few minutes for me to get it.


In C#, `yield` is a kind of return, not a function call. The two approaches to iteration are quite different (external vs internal iteration).


No, before C# got generators, some of the machinery had to be manually implemented, with yeld, the compiler generates the necessary implementation for IEnumerable.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: