2bam

C# AsParallel can't reentry

Jul 12th, 2017
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.73 KB | None | 0 0
  1.  
  2. namespace ParallelTest {
  3.     class Program {
  4.  
  5.         static IEnumerable<string> AllCombinationsNoRepeat(string prefix, int length) {
  6.             return
  7.                 length == 0
  8.                 ? new[] { prefix }
  9.                 : new[] { prefix }.Concat(
  10.                     Enumerable.Range(0, 9)
  11.                         //.AsParallel() //FIXME: Se debe quedar deadlocked esperando un worker thread libre...
  12.                         .Select(n => prefix+n)                                          //Append char
  13.                         .Where(s => s.GroupBy(ch=>ch).All(g=>g.Take(2).Count() < 2))    //No chars repeated
  14.                         .SelectMany(s => AllCombinationsNoRepeat(s, length-1))          //Combine recursively
  15.                 )
  16.                 ;
  17.         }
  18.  
  19.         static void Main(string[] args) {
  20.             foreach(var x in AllCombinationsNoRepeat("", 9))
  21.                 Console.WriteLine(x);
  22.  
  23.             Console.ReadKey();
  24.  
  25.         }
  26.     }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment