Permutations: Difference between revisions

Added C# version
(+ third D entry)
(Added C# version)
Line 792:
9999,1234,4321,1234
9999,4321,1234,1234</pre>
 
=={{header|C sharp|C#}}==
A recursive Iterator. Runs under C#2 (VS2005), i.e. no `var`, no lambdas,...
<lang csharp>public class Permutations<T>
{
public static System.Collections.Generic.IEnumerable<T[]> AllFor(T[] array)
{
if (array == null || array.Length == 0)
{
yield return new T[0];
}
else
{
for (int pick = 0; pick < array.Length; ++pick)
{
T item = array[pick];
int i = -1;
T[] rest = System.Array.FindAll<T>(
array, delegate(T p) { return ++i != pick; }
);
foreach (T[] restPermuted in AllFor(rest))
{
i = -1;
yield return System.Array.ConvertAll<T, T>(
array,
delegate(T p) {
return ++i == 0 ? item : restPermuted[i - 1];
}
);
}
}
}
}
}</lang>
Usage:
<lang csharp>namespace Permutations_On_RosettaCode
{
class Program
{
static void Main(string[] args)
{
string[] list = "a b c d".Split();
foreach (string[] permutation in Permutations<string>.AllFor(list))
{
System.Console.WriteLine(string.Join(" ", permutation));
}
}
}
}</lang>
 
 
 
=={{header|Clojure}}==
Anonymous user