Permutations: Difference between revisions

1) Needs to be in special class for extensions. 2) T needs to be scoped to class.
(Added Wren)
(1) Needs to be in special class for extensions. 2) T needs to be scoped to class.)
Line 2,024:
Recursive Linq
{{works with|C sharp|C#|7}}
<lang csharp>public static IEnumerable<IEnumerable<T>>class Permutations<T>(this IEnumerable<T> values)Extension
{
public static IEnumerable<IEnumerable<T>> Permutations<T>(this IEnumerable<T> values) where T : class
if (values.Count() == 1)
{
return new [] {values};
return values.SelectMany(v => Permutationsif (values.WhereCount(x=> x != v)),(v, p) =>= p.Prepend(v1));
return new [] { values };
return values.SelectMany(v => Permutations(values.Where(x => x != v)), (v, p) => p.Prepend(v));
}
}</lang>
Usage
Anonymous user