Created
December 12, 2013 17:34
-
-
Save JamieDixon/7932003 to your computer and use it in GitHub Desktop.
Basic AddOne function that adds one to each item in an IEnumerable<int> without using yield of foreach.
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
// Write an AddOne method that doesn't use yield or foreach | |
static IEnumerable<int> AddOne(IEnumerable<int> items) | |
{ | |
var returnEnumer = Enumerable.Empty<int>(); | |
var enumerator = items.GetEnumerator(); | |
while(enumerator.MoveNext()) | |
{ | |
returnEnumer = returnEnumer.Concat(Enumerable.Repeat(enumerator.Current + 1, 1)); | |
} | |
return returnEnumer; | |
} | |
void Main() | |
{ | |
var enumer = Enumerable.Repeat(1,1); | |
enumer = AddOne(enumer); | |
enumer.Dump(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment