fork(1) download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. public class Test
  6. {
  7. public delegate void IntConsumer(List<int> ints);
  8.  
  9. public static void doStuff(List<List<int>> arrays, List<IntConsumer> consumers) {
  10. processArrays(arrays, consumers, new List<int>());
  11. }
  12.  
  13. private static void processArrays(List<List<int>> arrays, List<IntConsumer> consumers, List<int> consumerArgsSoFar) {
  14. if(consumers.Count == 0 || arrays.Count == 0) {
  15. return;
  16. }
  17.  
  18. List<int> currentArray = arrays[0];
  19. List<List<int>> remainingArrays = arrays.GetRange(1, arrays.Count-1);
  20. IntConsumer currentConsumer = consumers[0];
  21. List<IntConsumer> remainingConsumers = consumers.GetRange(1, arrays.Count-1);
  22.  
  23. if(currentArray.Count == 0) {
  24. processArrays(remainingArrays, remainingConsumers, consumerArgsSoFar);
  25. } else {
  26. for(int i = 0; i < currentArray.Count; i++) {
  27. List<int> consumerArgs = new List<int>(consumerArgsSoFar);
  28. consumerArgs.Add(currentArray[i]);
  29.  
  30. currentConsumer(consumerArgs);
  31.  
  32. processArrays(remainingArrays, remainingConsumers, consumerArgs);
  33. }
  34. }
  35. }
  36.  
  37. public static void log1(List<int> x) {
  38. Console.WriteLine("log1");
  39. x.ForEach(Console.Write);
  40. Console.WriteLine();
  41. }
  42. public static void log2(List<int> x) {
  43. Console.WriteLine("log2");
  44. x.ForEach(Console.Write);
  45. Console.WriteLine();
  46. }
  47. public static void log3(List<int> x) {
  48. Console.WriteLine("log3");
  49. x.ForEach(Console.Write);
  50. Console.WriteLine();
  51. }
  52.  
  53. public static void Main() {
  54. List<int> dim1 = new List<int>(){10, 20, 30};
  55. List<int> dim2 = new List<int>(){};
  56. List<int> dim3 = new List<int>(){12, 22, 32};
  57.  
  58. List<List<int>> arrays = new List<List<int>>(){dim1, dim2, dim3};
  59. List<IntConsumer> consumers = new List<IntConsumer>(){log1, log2, log3};
  60.  
  61. doStuff(arrays, consumers);
  62. }
  63. }
Success #stdin #stdout 0.04s 23984KB
stdin
Standard input is empty
stdout
log1
10
log3
1012
log3
1022
log3
1032
log1
20
log3
2012
log3
2022
log3
2032
log1
30
log3
3012
log3
3022
log3
3032