randUser

log of (2592) query executions

Aug 19th, 2020
1,116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.51 KB | None | 0 0
  1. Can some who is very familiar with LINQ help answer my questions about the following LINQ query?
  2.  
  3. The following code performs a faro shuffle.
  4.  
  5. // Program.cs
  6. public static void Main(string[] args)
  7. {
  8.     var startingDeck = (from s in Suits().LogQuery("Suit Generation")
  9.                         from r in Ranks().LogQuery("Rank Generation")
  10.                         select new { Suit = s, Rank = r }).LogQuery("Starting Deck");
  11.  
  12.     foreach (var c in startingDeck)
  13.     {
  14.         Console.WriteLine(c);
  15.     }
  16.  
  17.     Console.WriteLine();
  18.     var times = 0;
  19.     var shuffle = startingDeck;
  20.  
  21.     do
  22.     {
  23.         // Out shuffle
  24.                 shuffle = shuffle.Take(26)
  25.             .LogQuery("Top Half")
  26.             .InterleaveSequenceWith(shuffle.Skip(26)
  27.             .LogQuery("Bottom Half"))
  28.             .LogQuery("Shuffle");
  29.        
  30.  
  31.         foreach (var c in shuffle)
  32.         {
  33.             Console.WriteLine(c);
  34.         }
  35.  
  36.         times++;
  37.         Console.WriteLine(times);
  38.     } while (!startingDeck.SequenceEquals(shuffle));
  39.  
  40.     Console.WriteLine(times);
  41. }
  42.  
  43. 1. Why is the order of the (first) execution of the StartingDeck query as follows:
  44. Executing Query Suit Generation
  45. Executing Query Starting Deck
  46. Executing Query Rank Generation
  47. Executing Query Rank Generation
  48. Executing Query Rank Generation
  49. Executing Query Rank Generation
  50.  
  51. I expected the order to be Suit Generation, Rank, StartingDeck because that is the order in the query:
  52. (from s in Suits().LogQuery("Suit Generation")
  53.                         from r in Ranks().LogQuery("Rank Generation")
  54.                         select new { Suit = s, Rank = r }).LogQuery("Starting Deck");
  55.  
  56. 2. I understand that in each iteration of the do-while loop the queries are executed again and again by reconstructing the deck of cards and reshuffling it every time. But what do these executions look like? For example, in the first iteration of the do-while loop because shuffle points to the same object that startingDeck points to I assume the execution of the shuffle query looks like this:
  57. shuffle = startingDeck.Skip(26).LogQuery("Bottom Half")
  58.                 .InterleaveSequenceWith(startingDeck.Take(26).LogQuery("Top Half"))
  59.                 .LogQuery("Shuffle");
  60.  
  61. Or in other words:
  62.  
  63. shuffle = (from s in Suits().LogQuery("Suit Generation")
  64.                         from r in Ranks().LogQuery("Rank Generation")
  65.                         select new { Suit = s, Rank = r }).LogQuery("Starting Deck").Skip(26).LogQuery("Bottom Half")
  66.                 .InterleaveSequenceWith((from s in Suits().LogQuery("Suit Generation")
  67.                         from r in Ranks().LogQuery("Rank Generation")
  68.                         select new { Suit = s, Rank = r }).LogQuery("Starting Deck").Take(26).LogQuery("Top Half"))
  69.                 .LogQuery("Shuffle");
  70.  
  71. However, in the 2nd iteration of the do-while loop, the variable shuffle now stores the query from the previous iteration of the do-while loop, so does the execution of the shuffle query in the 2nd iteration of the do-while loop look like this:
  72.  
  73. shuffle = (from s in Suits().LogQuery("Suit Generation")
  74.                         from r in Ranks().LogQuery("Rank Generation")
  75.                         select new { Suit = s, Rank = r }).LogQuery("Starting Deck").Skip(26).LogQuery("Bottom Half")
  76.                 .InterleaveSequenceWith((from s in Suits().LogQuery("Suit Generation")
  77.                         from r in Ranks().LogQuery("Rank Generation")
  78.                         select new { Suit = s, Rank = r }).LogQuery("Starting Deck").Take(26).LogQuery("Top Half"))
  79.                 .LogQuery("Shuffle").Skip(26).LogQuery("Bottom Half")
  80.                 .InterleaveSequenceWith((from s in Suits().LogQuery("Suit Generation")
  81.                         from r in Ranks().LogQuery("Rank Generation")
  82.                         select new { Suit = s, Rank = r }).LogQuery("Starting Deck").Skip(26).LogQuery("Bottom Half")
  83.                 .InterleaveSequenceWith((from s in Suits().LogQuery("Suit Generation")
  84.                         from r in Ranks().LogQuery("Rank Generation")
  85.                         select new { Suit = s, Rank = r }).LogQuery("Starting Deck").Take(26).LogQuery("Top Half"))
  86.                 .LogQuery("Shuffle").Take(26).LogQuery("Top Half"))
  87.                 .LogQuery("Shuffle");
  88.  
  89.  
  90. /*Bonus questions:
  91.  
  92. * In the following query, are the iterator methods executed when the query startingDeck is used in the foreach statement, or are they executed when the query is defined (i.e. when program execution reaches the query expression)?
  93.  
  94.     var startingDeck = (from s in Suits().LogQuery("Suit Generation")
  95.                         from r in Ranks().LogQuery("Rank Generation")
  96.                         select new { Suit = s, Rank = r }).LogQuery("Starting Deck");
  97. * In the following query expression I believe that the query named shuffle is executed when Skip and Take are called. However I want to confirm whether the query named shuffle is executed when Take and Skip are called, or is the query named shuffle executed only when shuffle is used in the foreach statement that follows?
  98.     shuffle = shuffle.Take(26)
  99.             .LogQuery("Top Half")
  100.             .InterleaveSequenceWith(shuffle.Skip(26)
  101.             .LogQuery("Bottom Half"))
  102.             .LogQuery("Shuffle");
  103.  
  104. If the query named shuffle is executed when Take and Skip are called, then does that mean that Take and Skip are executed twice, first when program execution reaches the query expression named shuffle in the do-while loop, and again in the foreach statement?*/
  105.  
  106.  
Advertisement
Add Comment
Please, Sign In to add comment