3 ms·
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
by neonsunset 2y ago
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());
- doctor_eval 2y agoIsn’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.
- pjmlp 2y agoNo, before C# got generators, some of the machinery had to be manually implemented, with yeld, the compiler generates the necessary implementation for IEnumerable.
- pansa2 2y agoIn C#, `yield` is a kind of return, not a function call. The two approaches to iteration are quite different (external vs internal iteration).