Last active
November 22, 2018 07:53
-
-
Save JarkoDubbeldam/57430846438be59f15f68a8d6ddeeea4 to your computer and use it in GitHub Desktop.
Eager vs lazy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Eagerly performs nullchecks. | |
public static IEnumerable<TResult> ZipNew<TFirst, TSecond, TResult>( | |
this IEnumerable<TFirst> first, | |
IEnumerable<TSecond> second, | |
Func<TFirst, TSecond, TResult> resultSelector) { | |
if(first == null) throw new ArgumentNullException(nameof(first)); | |
if(second == null) throw new ArgumentNullException(nameof(second)); | |
if(resultSelector == null) throw new ArgumentNullException(nameof(resultSelector)); | |
return ZipNewInternal(first, second, resultSelector); | |
} | |
// Uses yield return, so entire method is only evaluated when iteration happens. | |
private static IEnumerable<TResult> ZipNewInternal<TFirst, TSecond, TResult>( | |
IEnumerable<TFirst> first, | |
IEnumerable<TSecond> second, | |
Func<TFirst, TSecond, TResult> resultSelector) { | |
using(ZipEnumerator<TFirst, TSecond, TResult> zipEnumerator = new ZipEnumerator<TFirst, TSecond, TResult>(first, second, resultSelector)) { | |
while(zipEnumerator.MoveNext()) { | |
yield return zipEnumerator.Current; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment