Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reimplement AdjacentPairs as a composition of RepeatFirst and Edges
- Loading branch information
Showing
7 changed files
with
82 additions
and
30 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Kethane.EnumerableExtensions | ||
{ | ||
public static class EdgesEx | ||
{ | ||
public static IEnumerable<Pair<T>> Edges<T>(this IEnumerable<T> sequence) | ||
{ | ||
if (sequence == null) { throw new ArgumentNullException("sequence"); } | ||
return edgesImpl(sequence); | ||
} | ||
|
||
public static IEnumerable<Pair<T>> EdgesCircular<T>(this IEnumerable<T> sequence) | ||
{ | ||
return sequence.RepeatFirstOrEmpty().Edges(); | ||
} | ||
|
||
private static IEnumerable<Pair<T>> edgesImpl<T>(IEnumerable<T> sequence) | ||
{ | ||
using (var enumerator = sequence.GetEnumerator()) | ||
{ | ||
if (!enumerator.MoveNext()) { yield break; } | ||
T previous = enumerator.Current; | ||
while (enumerator.MoveNext()) | ||
{ | ||
T current = enumerator.Current; | ||
yield return new Pair<T>(previous, current); | ||
previous = current; | ||
} | ||
} | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Kethane.EnumerableExtensions | ||
{ | ||
public static class Repeat | ||
{ | ||
public static IEnumerable<T> RepeatFirst<T>(this IEnumerable<T> sequence) | ||
{ | ||
if (sequence == null) { throw new ArgumentNullException("sequence"); } | ||
return repeatFirstImpl(sequence, true); | ||
} | ||
|
||
public static IEnumerable<T> RepeatFirstOrEmpty<T>(this IEnumerable<T> sequence) | ||
{ | ||
if (sequence == null) { throw new ArgumentNullException("sequence"); } | ||
return repeatFirstImpl(sequence, false); | ||
} | ||
|
||
private static IEnumerable<T> repeatFirstImpl<T>(IEnumerable<T> sequence, bool throwOnEmpty) | ||
{ | ||
using (var enumerator = sequence.GetEnumerator()) | ||
{ | ||
if (!enumerator.MoveNext()) | ||
{ | ||
if (throwOnEmpty) { throw new InvalidOperationException("Sequence is empty"); } | ||
yield break; | ||
} | ||
|
||
T first = enumerator.Current; | ||
yield return first; | ||
|
||
while (enumerator.MoveNext()) | ||
{ | ||
yield return enumerator.Current; | ||
} | ||
|
||
yield return first; | ||
} | ||
} | ||
} | ||
} |
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
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
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
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