<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Computing a Cartesian Product with LINQ</title><link>http://blogs.msdn.com/b/ericlippert/archive/2010/06/28/computing-a-cartesian-product-with-linq.aspx</link><description>And here we have yet another post inspired by a question on StackOverflow : how do you compute the Cartesian product of arbitrarily many sequences using LINQ? (*) 
 UPDATE: Ian Griffiths has an interesting series of articles that approaches this question</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>re: Computing a Cartesian Product with LINQ</title><link>http://blogs.msdn.com/b/ericlippert/archive/2010/06/28/computing-a-cartesian-product-with-linq.aspx#10372151</link><pubDate>Tue, 27 Nov 2012 10:56:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10372151</guid><dc:creator>Tyler</dc:creator><description>&lt;p&gt;Is there any chance of this being written in TSQL?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10372151" width="1" height="1"&gt;</description></item><item><title>re: Computing a Cartesian Product with LINQ</title><link>http://blogs.msdn.com/b/ericlippert/archive/2010/06/28/computing-a-cartesian-product-with-linq.aspx#10360460</link><pubDate>Wed, 17 Oct 2012 17:42:41 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10360460</guid><dc:creator>gautam</dc:creator><description>&lt;p&gt;Can you give example input structure and code that calls this method?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10360460" width="1" height="1"&gt;</description></item><item><title>re: Computing a Cartesian Product with LINQ</title><link>http://blogs.msdn.com/b/ericlippert/archive/2010/06/28/computing-a-cartesian-product-with-linq.aspx#10269777</link><pubDate>Mon, 20 Feb 2012 15:48:17 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10269777</guid><dc:creator>Senthil_Benasainnce</dc:creator><description>&lt;p&gt;I am getting outofmemory exception when I am trying to compute cartesian product of 10 array (each has 100 items). I am getting this in the middle of computation. Is it possible to clean up the memory in the middle and let the accumulator use the unused memory?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10269777" width="1" height="1"&gt;</description></item><item><title>re: Computing a Cartesian Product with LINQ</title><link>http://blogs.msdn.com/b/ericlippert/archive/2010/06/28/computing-a-cartesian-product-with-linq.aspx#10244946</link><pubDate>Wed, 07 Dec 2011 07:40:05 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10244946</guid><dc:creator>Mikael</dc:creator><description>&lt;p&gt;This blogpost just saved my day.&lt;/p&gt;
&lt;p&gt;Thank you!&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10244946" width="1" height="1"&gt;</description></item><item><title>re: Computing a Cartesian Product with LINQ</title><link>http://blogs.msdn.com/b/ericlippert/archive/2010/06/28/computing-a-cartesian-product-with-linq.aspx#10204802</link><pubDate>Fri, 02 Sep 2011 01:26:15 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10204802</guid><dc:creator>Brook</dc:creator><description>&lt;p&gt;Totally awesome! &amp;nbsp;Thank you for helping me out bigtime!&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10204802" width="1" height="1"&gt;</description></item><item><title>re: Computing a Cartesian Product with LINQ</title><link>http://blogs.msdn.com/b/ericlippert/archive/2010/06/28/computing-a-cartesian-product-with-linq.aspx#10036572</link><pubDate>Fri, 09 Jul 2010 20:19:46 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10036572</guid><dc:creator>Dave</dc:creator><description>&lt;p&gt;@Jarno: Good catch, I changed it to ns.Skip(1).&lt;/p&gt;
&lt;p&gt;@bman654: You&amp;#39;re right, that fixes it. I understand now, basically .NET only defers the evaluation of the enumerable elements... it does not defer method application unless explicitly told to do so (with the EnumerableEx.Defer method). So when the sequence defintion is recurisvle defined, it will stack overflow because of the method application (that is, the recurive call). It works in Haskell becuase the opposite is true there, everything is defered unless explicitly told not to do so.&lt;/p&gt;
&lt;p&gt;Thanks.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10036572" width="1" height="1"&gt;</description></item><item><title>re: Computing a Cartesian Product with LINQ</title><link>http://blogs.msdn.com/b/ericlippert/archive/2010/06/28/computing-a-cartesian-product-with-linq.aspx#10035681</link><pubDate>Thu, 08 Jul 2010 03:11:57 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10035681</guid><dc:creator>bman654</dc:creator><description>&lt;p&gt;@Dave: &amp;nbsp;Your GetPrimes method is infinitely recursive and overflows the stack while building the LINQ expression.&lt;/p&gt;
&lt;p&gt;Try using EnumerableEx.Defer() from the Reactive framework. &amp;nbsp;Something like this might work for you:&lt;/p&gt;
&lt;p&gt;return ns.Take(1).Concat(EnumerableEx.Defer(() =&amp;gt; GetPrimes(ns.Where(n =&amp;gt; n % p != 0))));&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10035681" width="1" height="1"&gt;</description></item><item><title>re: Computing a Cartesian Product with LINQ</title><link>http://blogs.msdn.com/b/ericlippert/archive/2010/06/28/computing-a-cartesian-product-with-linq.aspx#10035227</link><pubDate>Wed, 07 Jul 2010 05:46:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10035227</guid><dc:creator>Jarno Peschier</dc:creator><description>&lt;p&gt;Article makes perfect sense: excellent functional programming example, I think.&lt;/p&gt;
&lt;p&gt;@Szindbad: If you have unit tests to verify functional behavior and the method is documented nobody should give a rats ass about the implementation and if they can (immediately) understand it, right? Just because you don&amp;#39;t understand it (as the &amp;quot;average&amp;quot; team member) doesn&amp;#39;t mean the writer didn&amp;#39;t understand. Or the team cannot use the code and and benefit?&lt;/p&gt;
&lt;p&gt;@Dave: This works fine as far as I know (I&amp;#39;ve worked succesfully with infinate return values like your From() function). Aren&amp;#39;t you using the whole sequence again in you &amp;quot;return ns.Take(1).Concat(GetPrimes(ns.Where(n =&amp;gt; n % p != 0)));&amp;quot;? Shouldn&amp;#39;t that second ns be ns.Skip() or something? As in ns = x:xs and you&amp;#39;re doing the where clause on ns instead of just on xs?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10035227" width="1" height="1"&gt;</description></item><item><title>re: Computing a Cartesian Product with LINQ</title><link>http://blogs.msdn.com/b/ericlippert/archive/2010/06/28/computing-a-cartesian-product-with-linq.aspx#10034878</link><pubDate>Tue, 06 Jul 2010 12:20:20 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10034878</guid><dc:creator>Sjoerd Visscher</dc:creator><description>&lt;p&gt;Instead of var s = sequence, you can probably also switch the froms:&lt;/p&gt;
&lt;p&gt; &amp;nbsp;foreach(var sequence in sequences) &lt;/p&gt;
&lt;p&gt; &amp;nbsp;{ &lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;// recursive case: use SelectMany to build the new product out of the old one &lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;result = &amp;nbsp;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;from item in sequence&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;from seq in result &lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;select seq.Concat(new[] {item}); &lt;/p&gt;
&lt;p&gt; &amp;nbsp;} &lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10034878" width="1" height="1"&gt;</description></item><item><title>re: Computing a Cartesian Product with LINQ</title><link>http://blogs.msdn.com/b/ericlippert/archive/2010/06/28/computing-a-cartesian-product-with-linq.aspx#10033623</link><pubDate>Thu, 01 Jul 2010 22:36:17 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10033623</guid><dc:creator>Dave</dc:creator><description>&lt;p&gt;gotta love that cartesian product, this reminded me of some code I&amp;#39;d written to find prime numbers using the Sieve of Eratosthenes once. It&amp;#39;s not meant to be uber efficent or anything, just call it a programming challenge. I wrote it like so:&lt;/p&gt;
&lt;p&gt;sealed class Program&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;	static IEnumerable&amp;lt;BigInteger&amp;gt; GetPrimes(IEnumerable&amp;lt;BigInteger&amp;gt; ns)&lt;/p&gt;
&lt;p&gt;	{&lt;/p&gt;
&lt;p&gt;		var p = ns.First();&lt;/p&gt;
&lt;p&gt;		return ns.Take(1).Concat(GetPrimes(ns.Where(n =&amp;gt; n % p != 0)));&lt;/p&gt;
&lt;p&gt;	}&lt;/p&gt;
&lt;p&gt;	static IEnumerable&amp;lt;BigInteger&amp;gt; From(BigInteger n)&lt;/p&gt;
&lt;p&gt;	{&lt;/p&gt;
&lt;p&gt;		while (true) { yield return n++; }&lt;/p&gt;
&lt;p&gt;	}&lt;/p&gt;
&lt;p&gt;	static void Main(string[] args)&lt;/p&gt;
&lt;p&gt;	{&lt;/p&gt;
&lt;p&gt;		var two = new BigInteger(2);&lt;/p&gt;
&lt;p&gt;		var primes = GetPrimes(From(two));&lt;/p&gt;
&lt;p&gt;		foreach (var p in primes.Take(10)) { Console.WriteLine(p); }&lt;/p&gt;
&lt;p&gt;	}&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;But it goes into infinite recursion and blows the stack to pieces (how&amp;#39;s that for StackOverflow). Anyway, it works just fine in Haskell:&lt;/p&gt;
&lt;p&gt;Prelude&amp;gt; let primes = let sieve (x:xs) = x:sieve [y | y &amp;lt;- xs, y `mod` x /= 0] in sieve [2..]&lt;/p&gt;
&lt;p&gt;Prelude&amp;gt; take 10 primes&lt;/p&gt;
&lt;p&gt;[2,3,5,7,11,13,17,19,23,29]&lt;/p&gt;
&lt;p&gt;Haskell has non-strict semantics (lazy) and C# doesn&amp;#39;t but it is supposed to for IEnumerable... best I could conclude is that Haskell is &amp;quot;more lazy&amp;quot; than C#. Anyone have a more concrete explanation?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10033623" width="1" height="1"&gt;</description></item></channel></rss>