public static IEnumerable<IEnumerable<T>> Permutations<T>(this IEnumerable<T> xs)
{
if (!xs.Any())
return EnumerableEx.Return(Enumerable.Empty<T>());
else
return from zs in Permutations(xs.Skip(1))
from i in Enumerable.Range(0, zs.Count() + 1)
select zs.Take(i).Concat(EnumerableEx.Return(xs.First())).Concat(zs.Skip(i));
}
2 comments:
At the minimum that first method should be:
using(var e = xs.GetEnumerator())
{
return !e.MoveNext();
}
in order to properly dispose the IEnumerator.
You can also rewrite it to be:
return !xs.Any();
Thank you.
I agree with that. Stupid mistake.
Post a Comment