Created
October 27, 2011 12:57
-
-
Save mikeminutillo/1319475 to your computer and use it in GitHub Desktop.
Permutations - writing LISP in C#
This file contains hidden or 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
public static class Extensions | |
{ | |
public static IEnumerable<IEnumerable<T>> Permute<T>(this IEnumerable<T> items) | |
{ | |
var isEmpty = true; | |
foreach (var item in items) | |
{ | |
isEmpty = false; | |
foreach (var p in items.Except(item.Yield()).Permute()) | |
yield return item.Yield().Concat(p); | |
} | |
if (isEmpty) | |
yield return Enumerable.Empty<T>(); | |
} | |
public static IEnumerable<T> Yield<T>(this T item) | |
{ | |
yield return item; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment