<?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>Immutability in C# Part 10: A double-ended queue</title><link>http://blogs.msdn.com/ericlippert/archive/2008/01/22/immutability-in-c-part-10-a-double-ended-queue.aspx</link><description>Based on the comments, the implementation of a single-ended queue as two stacks was somewhat mind-blowing for a number of readers. People, you ain't seen nothing yet. Before we get into the actual bits and bytes of the solution, think for a bit about</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: Immutability in C# Part 10: A double-ended queue</title><link>http://blogs.msdn.com/ericlippert/archive/2008/01/22/immutability-in-c-part-10-a-double-ended-queue.aspx#7199804</link><pubDate>Tue, 22 Jan 2008 20:20:05 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7199804</guid><dc:creator>Aaron G</dc:creator><description>&lt;p&gt;It does leave a little to be desired. &amp;nbsp;Now instead of an O(n&amp;#178;) algorithm, you have a recursive O(n&amp;#178;) algorithm, so we can hammer both the CPU and the stack with it. &amp;nbsp;Nice. &amp;nbsp;;)&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part 10: A double-ended queue</title><link>http://blogs.msdn.com/ericlippert/archive/2008/01/22/immutability-in-c-part-10-a-double-ended-queue.aspx#7199828</link><pubDate>Tue, 22 Jan 2008 20:22:28 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7199828</guid><dc:creator>Olmo del Corral</dc:creator><description>&lt;p&gt;The problem here is the recursive chain that is produced when you call EnqueueXXX and DequeXXX over a non trivial deque. &lt;/p&gt;
&lt;p&gt;I'm working in a solution that has, for every generarl Deque, &amp;nbsp;a cachedDequeLeft and cachedDequeRight, but I'm not sure if it&amp;#180;s going to work. &lt;/p&gt;
&lt;p&gt;Cross your fingers :) &lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part 10: A double-ended queue</title><link>http://blogs.msdn.com/ericlippert/archive/2008/01/22/immutability-in-c-part-10-a-double-ended-queue.aspx#7200034</link><pubDate>Tue, 22 Jan 2008 20:53:46 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7200034</guid><dc:creator>Eric Lippert</dc:creator><description>&lt;p&gt;Aaron: Yep! It's truly awful. Each enqueue is O(n) in stack consumption, time and number of new nodes allocated, so enqueuing n items in a row is O(n&amp;#178;) in time.&lt;/p&gt;
&lt;p&gt;Olmo: Sounds interesting!&lt;/p&gt;
</description></item><item><title>Immutability in C#</title><link>http://blogs.msdn.com/ericlippert/archive/2008/01/22/immutability-in-c-part-10-a-double-ended-queue.aspx#7200361</link><pubDate>Tue, 22 Jan 2008 21:31:01 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7200361</guid><dc:creator>&lt;a href="http://weblogs.asp.net/bleroy"&gt;Tales from the Evil Empire&lt;/a&gt;</dc:creator><description>&lt;p&gt;For some reason, there's been a lot of buzz lately around immutability in C#. If you're interested in&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part 10: A double-ended queue</title><link>http://blogs.msdn.com/ericlippert/archive/2008/01/22/immutability-in-c-part-10-a-double-ended-queue.aspx#7201056</link><pubDate>Tue, 22 Jan 2008 22:31:29 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7201056</guid><dc:creator>Olmo del Corral</dc:creator><description>&lt;p&gt;I've been working on it for a while and there is no end. &lt;/p&gt;
&lt;p&gt;In my implementation Deque is FAST so eneque have to be defined in terms of Deque. &lt;/p&gt;
&lt;p&gt;The end of the history is that, while you can reuse a lot of this trees (should we call them firs? hehe), for a deque with n elements you need about n^2 that represent every single instance of all the possible subintervals. &amp;nbsp;So having a structure that needs so much memory is stupid. Also, inserting an element stills O(n)...&lt;/p&gt;
&lt;p&gt;So a way to nowhere ... :S&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part 10: A double-ended queue</title><link>http://blogs.msdn.com/ericlippert/archive/2008/01/22/immutability-in-c-part-10-a-double-ended-queue.aspx#7201290</link><pubDate>Tue, 22 Jan 2008 22:59:19 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7201290</guid><dc:creator>Aaron G</dc:creator><description>&lt;p&gt;I'm thinking that you could just throw together an immutable version of a doubly-linked list. &amp;nbsp;You did say that the solution was tricky, and this one is downright banal, so it probably isn't what you're looking for, but it *would* work and give O(1) enqueue and dequeue performance.&lt;/p&gt;
&lt;p&gt;All you'd need is an Element&amp;lt;T&amp;gt; with (read only) Left, Right and Value properties, and you could give the Deque&amp;lt;T&amp;gt; &amp;quot;beginning&amp;quot; and &amp;quot;end&amp;quot; fields of type Element&amp;lt;T&amp;gt;. &amp;nbsp;Throw in a constructor that takes two Element&amp;lt;T&amp;gt; parameters and you've got yourself an immutable Deque.&lt;/p&gt;
&lt;p&gt;I guess the problem is that it would have to allocate a new Deque&amp;lt;T&amp;gt; for each and every Dequeue operation. &amp;nbsp;The memory performance isn't stellar. &amp;nbsp;Then again, that's exactly what we did for the Queue&amp;lt;T&amp;gt;, so does it make a difference?&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part 10: A double-ended queue</title><link>http://blogs.msdn.com/ericlippert/archive/2008/01/22/immutability-in-c-part-10-a-double-ended-queue.aspx#7201372</link><pubDate>Tue, 22 Jan 2008 23:10:53 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7201372</guid><dc:creator>Aaron G</dc:creator><description>&lt;p&gt;Actually... never mind, I can see now why that wouldn't work - the Element&amp;lt;T&amp;gt; would have to be mutable. &amp;nbsp;The Deque could look immutable on the outside, but I imagine the point of this exercise is to build the whole thing using completely immutable data structures. &amp;nbsp;So, scratch that, back to square one. &amp;nbsp;:-)&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part 10: A double-ended queue</title><link>http://blogs.msdn.com/ericlippert/archive/2008/01/22/immutability-in-c-part-10-a-double-ended-queue.aspx#7201623</link><pubDate>Tue, 22 Jan 2008 23:39:21 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7201623</guid><dc:creator>Eric Lippert</dc:creator><description>&lt;p&gt;Yep, you got it. Doubly-linked lists are always mutable.&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part 10: A double-ended queue</title><link>http://blogs.msdn.com/ericlippert/archive/2008/01/22/immutability-in-c-part-10-a-double-ended-queue.aspx#7201727</link><pubDate>Tue, 22 Jan 2008 23:58:49 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7201727</guid><dc:creator>Luke H</dc:creator><description>&lt;p&gt;Going back to attempt #1, if when one stack is empty, you reverse and transfer just *half* the elements from the other stack, you get amortised O(1) performance, and you're done ;) &amp;nbsp;This is mentioned in Chris Okasaki's book &amp;quot;Purely functional data structures&amp;quot; which describes a lot of interesting immutable data structures.&lt;/p&gt;
&lt;p&gt;Looking forward to seeing your alternative as well!&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part 10: A double-ended queue</title><link>http://blogs.msdn.com/ericlippert/archive/2008/01/22/immutability-in-c-part-10-a-double-ended-queue.aspx#7202294</link><pubDate>Wed, 23 Jan 2008 01:17:56 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7202294</guid><dc:creator>Aaron G</dc:creator><description>&lt;p&gt;I hope that the half-reverse idea isn't the solution; that was the first thing that popped into my head, but it's only reducing that worst-case O(n&amp;#178;) performance to O(n log n), and it also raises the performance cost of enqueuing everything from one end and dequeuing everything from the other end from O(n) to O(n log n). &amp;nbsp;It's better than what we've got so far, but it ain't great.&lt;/p&gt;
&lt;p&gt;You could get similar performance using an AVL or red-black tree as the internal data structure; the &amp;quot;beginning&amp;quot; is the outer-left leaf and the &amp;quot;end&amp;quot; is the outer-right. &amp;nbsp;But it's still a high price to pay. &amp;nbsp;Knowing Eric, I'm sure he's got some extremely clever and totally nonintuitive hack that gives O(1) performance almost all the time.&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part 10: A double-ended queue</title><link>http://blogs.msdn.com/ericlippert/archive/2008/01/22/immutability-in-c-part-10-a-double-ended-queue.aspx#7202429</link><pubDate>Wed, 23 Jan 2008 01:41:06 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7202429</guid><dc:creator>Eric Lippert</dc:creator><description>&lt;p&gt;Aaron: no, Luke is right. If you are clever about when you rebalance the deque, you can get amortized O(1) performance. You end up having to keep around extra information about how big each queue is, but that's not hard.&lt;/p&gt;
&lt;p&gt;However, that is in fact not the solution I had in mind. &amp;nbsp;Rather than fixing Attempt #1, I'm going to fix Attempt #2 by being more clever about what exactly goes in the left, middle and right sections.&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part 10: A double-ended queue</title><link>http://blogs.msdn.com/ericlippert/archive/2008/01/22/immutability-in-c-part-10-a-double-ended-queue.aspx#7206371</link><pubDate>Wed, 23 Jan 2008 11:06:48 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7206371</guid><dc:creator>Chris M</dc:creator><description>&lt;p&gt;I don't think that's exactly what you meant, and I'm sure I'm missing something, but would&lt;/p&gt;
&lt;p&gt;return new Deque&amp;lt;T&amp;gt;(left, middle.DequeueRight(), middle.PeekRight());&lt;/p&gt;
&lt;p&gt;even work as planned? Wouldn't we end up with the wrong right element after we do this? Did I just say 'wrong right element'? How is it you manage to do this to me almost every time?&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part 10: A double-ended queue</title><link>http://blogs.msdn.com/ericlippert/archive/2008/01/22/immutability-in-c-part-10-a-double-ended-queue.aspx#7210724</link><pubDate>Wed, 23 Jan 2008 20:32:17 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7210724</guid><dc:creator>DRBlaise</dc:creator><description>&lt;p&gt;I was able to make an efficient Deque using 2 &amp;quot;Trimmable Stacks&amp;quot; and keeping Deque Count information, but I am not sure about approach #2. &amp;nbsp;I am thinking it might be possible by making left and right into Stacks and keeping middle a Deque. &amp;nbsp;EnqueueLeft would be:&lt;/p&gt;
&lt;p&gt; &amp;nbsp;return new Deque&amp;lt;T&amp;gt;(left.Push(value), this, right);&lt;/p&gt;
&lt;p&gt;I am just not sure if this works for all other operations.&lt;/p&gt;
&lt;p&gt;Great series by the way. &amp;nbsp;Keep it going!&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part 10: A double-ended queue</title><link>http://blogs.msdn.com/ericlippert/archive/2008/01/22/immutability-in-c-part-10-a-double-ended-queue.aspx#7210927</link><pubDate>Wed, 23 Jan 2008 20:52:47 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7210927</guid><dc:creator>Eric Lippert</dc:creator><description>&lt;p&gt;Chris: Why would we end up with the wrong right element?&lt;/p&gt;
&lt;p&gt;Remember, middle is IMMUTABLE. &amp;nbsp;Dequeuing it does not change the value of middle, it returns a different deque. &amp;nbsp;We can dequeue that thing all we want and its rightmost element is the same as it ever was.&lt;/p&gt;
&lt;p&gt;Dr. Blaise: Your intuition is good. &amp;nbsp;I'm going to stick with something-on-the-left, something-in-the-middle and something-on-the-right, and those somethings will be more complex than Attempt #2. It's not going to be exactly as you've sketched out though.&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part 10: A double-ended queue</title><link>http://blogs.msdn.com/ericlippert/archive/2008/01/22/immutability-in-c-part-10-a-double-ended-queue.aspx#7212431</link><pubDate>Thu, 24 Jan 2008 00:13:23 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7212431</guid><dc:creator>Aaron G</dc:creator><description>&lt;p&gt;I'm not sure if I'm on the right track, but the thing I'm noticing is that since a deque implicitly has 3 pieces, it's easier to deal with 2 elements at a time than it is to deal with just 1. &amp;nbsp;What if you made the left and right sides a kind of &amp;quot;buffer&amp;quot; (stacks with a count, I guess), and did the recursive enqueuing only when you had 2 elements to move?&lt;/p&gt;
&lt;p&gt;For example... if you're enqueuing from the left, and the left buffer has one or two elements, just tack it on. &amp;nbsp;If it has three, then pop all 3 elements, enqueue the last two onto the inner deque, and put the 1st back on the stack, then enqueue the new element normally. &amp;nbsp;There is some recursion, but 50% of the time the depth is zero, 25% of the time it's one level deep, 12.5% of the time it only goes two levels, etc. &amp;nbsp;Once you hit the end, which would be a SingleDeque, then enqueuing two elements at a time is trivial; just move the left to the right, the first element into a new SingleDeque in the middle, and the last element onto the right.&lt;/p&gt;
&lt;p&gt;I think this is also &amp;quot;amortized&amp;quot; O(1) performance, right? &amp;nbsp;To dequeue off of either side you just reverse this process, same performance. &amp;nbsp;It can end up being lopsided, but I don't think it matters; if you run out of elements on the &amp;quot;requested&amp;quot; side, just start taking them off the other side - since you've got a maximum of 3 elements to pop at any level before you find the &amp;quot;tail&amp;quot;, it's still pretty fast.&lt;/p&gt;
&lt;p&gt;Is this making any sense or have I officially gone off the deep end?&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part 10: A double-ended queue</title><link>http://blogs.msdn.com/ericlippert/archive/2008/01/22/immutability-in-c-part-10-a-double-ended-queue.aspx#7213125</link><pubDate>Thu, 24 Jan 2008 02:01:26 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7213125</guid><dc:creator>Eric Lippert</dc:creator><description>&lt;p&gt;Give that man a cigar!&lt;/p&gt;
&lt;p&gt;The part of the trick that you've not explicitly stated is that the inner deque is a deque of _stacks of elements_, not of _elements_. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;I'll be presenting an implementation of this technique in the next few days. &amp;nbsp;&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part 10: A double-ended queue</title><link>http://blogs.msdn.com/ericlippert/archive/2008/01/22/immutability-in-c-part-10-a-double-ended-queue.aspx#7225261</link><pubDate>Thu, 24 Jan 2008 21:29:47 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7225261</guid><dc:creator>DRBlaise</dc:creator><description>&lt;p&gt;WOW! Truly mind blowing stuff.&lt;/p&gt;
&lt;p&gt;Am I wrong or will the final version be:&lt;/p&gt;
&lt;p&gt;IDeque&amp;lt;T&amp;gt; left;&lt;/p&gt;
&lt;p&gt;IDeque&amp;lt;IDeque&amp;lt;T&amp;gt;&amp;gt; middle;&lt;/p&gt;
&lt;p&gt;IDeque&amp;lt;T&amp;gt; right;&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part 10: A double-ended queue</title><link>http://blogs.msdn.com/ericlippert/archive/2008/01/22/immutability-in-c-part-10-a-double-ended-queue.aspx#7225573</link><pubDate>Thu, 24 Jan 2008 22:01:32 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7225573</guid><dc:creator>Eric Lippert</dc:creator><description>&lt;p&gt;Very close. We're actually going to make a &amp;quot;lite&amp;quot; deque that can store between one and four elements, and then we'll have&lt;/p&gt;
&lt;p&gt;Dequelette&amp;lt;T&amp;gt; left&lt;/p&gt;
&lt;p&gt;IDeque&amp;lt;Dequelette&amp;lt;T&amp;gt;&amp;gt; middle&lt;/p&gt;
&lt;p&gt;Dequelette&amp;lt;T&amp;gt; right&lt;/p&gt;
&lt;p&gt;(Since the Dequelette does not meet the IDeque contract, I don't want to say that it does.)&lt;/p&gt;
&lt;p&gt;That gives us fast access to the stuff at the end points, and logarithmic access to the stuff in the middle -- unlike, say, a binary tree, which gives us fast access to the stuff &amp;quot;in the local middle&amp;quot; and logarithmic access to the stuff at the end points. It's like pulling a binary tree inside out, a bit.&lt;/p&gt;
&lt;p&gt;But patience! We'll get to it next week.&lt;/p&gt;
</description></item><item><title>Community Convergence XL</title><link>http://blogs.msdn.com/ericlippert/archive/2008/01/22/immutability-in-c-part-10-a-double-ended-queue.aspx#7314482</link><pubDate>Wed, 30 Jan 2008 02:15:14 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7314482</guid><dc:creator>Charlie Calvert's Community Blog</dc:creator><description>&lt;p&gt;Welcome to the fortieth issue of Community Convergence. This week we have two new releases of note: We&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part 10: A double-ended queue</title><link>http://blogs.msdn.com/ericlippert/archive/2008/01/22/immutability-in-c-part-10-a-double-ended-queue.aspx#7326570</link><pubDate>Wed, 30 Jan 2008 16:38:25 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7326570</guid><dc:creator>Weeble</dc:creator><description>&lt;p&gt;I've convinced myself that this works in C#, but I'm curious as to how the compiler remains happy and sane. The number of concrete types involved seems to be O(log N) in the number of elements, which, while very slow-growing, does not have an upper bound. Are the concrete types really only created as needed at run-time? Would you be unable to implement this same structure using C++ templates? Or have I fundamentally misunderstood something?&lt;/p&gt;
&lt;p&gt;I'm eager to see the next installment!&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part 10: A double-ended queue</title><link>http://blogs.msdn.com/ericlippert/archive/2008/01/22/immutability-in-c-part-10-a-double-ended-queue.aspx#7477812</link><pubDate>Wed, 06 Feb 2008 02:04:26 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7477812</guid><dc:creator>Joren Vermijs</dc:creator><description>&lt;p&gt;Doesn't the compiler simply use the same implementation for Foo&amp;lt;T&amp;gt; as it does for Foo&amp;lt;Foo&amp;lt;T&amp;gt;&amp;gt;, if T is a reference type? After all, there is no need for the _implementation_ to be strongly typed if only typesafe usage of it is allowed to compile.&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part 10: A double-ended queue</title><link>http://blogs.msdn.com/ericlippert/archive/2008/01/22/immutability-in-c-part-10-a-double-ended-queue.aspx#7561300</link><pubDate>Sat, 09 Feb 2008 16:12:12 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7561300</guid><dc:creator>Sebastien Lorion</dc:creator><description>&lt;p&gt;Shouldn't the interface be:&lt;/p&gt;
&lt;p&gt;public interface IDeque&amp;lt;T&amp;gt; &lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;T PeekLeft();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;T PeekRight();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;IDeque&amp;lt;T&amp;gt; EnqueueLeft(T value);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;IDeque&amp;lt;T&amp;gt; EnqueueRight(T value);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;IDeque&amp;lt;T&amp;gt; DequeueLeft(out T value);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;IDeque&amp;lt;T&amp;gt; DequeueRight(out T value);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;bool IsEmpty { get; }&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;ie, add argument &amp;quot;out T value&amp;quot; to Dequeue operations. Same goes for previously posted data structures ...&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part 10: A double-ended queue</title><link>http://blogs.msdn.com/ericlippert/archive/2008/01/22/immutability-in-c-part-10-a-double-ended-queue.aspx#7563169</link><pubDate>Sat, 09 Feb 2008 18:06:49 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7563169</guid><dc:creator>Eric Lippert</dc:creator><description>&lt;p&gt;No. That would be conflating two logically separate operations into one method. One method should do one thing, and do it well. &amp;nbsp;Examining the data structure and producing a new data structure are two entirely different operations, so they should be represented by two methods.&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part 10: A double-ended queue</title><link>http://blogs.msdn.com/ericlippert/archive/2008/01/22/immutability-in-c-part-10-a-double-ended-queue.aspx#7595727</link><pubDate>Mon, 11 Feb 2008 03:08:07 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7595727</guid><dc:creator>Sebastien Lorion</dc:creator><description>&lt;p&gt;I understand your point, thought it seems to conflict with current .NET mutable collections API (not saying one is wrong vs others, just different way of seeing things I guess).&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part 10: A double-ended queue</title><link>http://blogs.msdn.com/ericlippert/archive/2008/01/22/immutability-in-c-part-10-a-double-ended-queue.aspx#7615874</link><pubDate>Mon, 11 Feb 2008 20:34:12 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7615874</guid><dc:creator>Eric Lippert</dc:creator><description>&lt;p&gt;Mutable collections must conflate unrelated operations because mutable collections are impossible to reason about.&lt;/p&gt;
&lt;p&gt;Suppose for example you want to pop a stack AND know what value you popped. In an immutable stack you can ask &amp;quot;are you empty?&amp;quot; and then peek and then pop. &amp;nbsp;In a mutable stack asking &amp;quot;are you empty?&amp;quot; tells you nothing about whether peeking is safe. Someone could have popped everything off the stack on a different thread. &lt;/p&gt;
&lt;p&gt;In a mutable stack, if you peek and then pop you have absolutely no idea if the value you peeked is the value that was just popped off. Someone might have done a push after your peek.&lt;/p&gt;
&lt;p&gt;Mutable structures are &amp;quot;threadsafe&amp;quot; only insofar as they guarantee to behave as though their operations are atomic, not that combinations of those operations are atomic. Therefore, any operations which must be logically atomic in a mutable structure must be conflated together. &amp;nbsp;That's why mutable collections have methods that do everything all at once, rather than cleanly separating different operations into different methods.&lt;/p&gt;
&lt;p&gt;Immutable structures give you a much stronger thread safety -- they give you the ability to reason logically from past calls to future calls. In an immutable stack, the value you peek is always the value you pop.&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part 10: A double-ended queue</title><link>http://blogs.msdn.com/ericlippert/archive/2008/01/22/immutability-in-c-part-10-a-double-ended-queue.aspx#7654446</link><pubDate>Wed, 13 Feb 2008 00:37:06 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7654446</guid><dc:creator>snprbob86</dc:creator><description>&lt;p&gt;I've been loving this immutability series! Any hope for a System.Collections.Immutable someday? :-)&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part 10: A double-ended queue</title><link>http://blogs.msdn.com/ericlippert/archive/2008/01/22/immutability-in-c-part-10-a-double-ended-queue.aspx#7719543</link><pubDate>Fri, 15 Feb 2008 21:11:26 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7719543</guid><dc:creator>Sebastien Lorion</dc:creator><description>&lt;p&gt;Thanks for your thoughtful answer. Following on what you say, then there is no advantage of putting 2 logical operations in one method in a mutable stack who is not threadsafe. A thread could push a value before Pop() get a chance to peek. In that case, an external lock is necessary and then, why not have 2 methods to make this requirement explicit ? Am I right in thinking that way ?&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part 10: A double-ended queue</title><link>http://blogs.msdn.com/ericlippert/archive/2008/01/22/immutability-in-c-part-10-a-double-ended-queue.aspx#7719776</link><pubDate>Fri, 15 Feb 2008 21:31:43 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7719776</guid><dc:creator>Eric Lippert</dc:creator><description>&lt;p&gt;That is a reasonable way of thinking about it, yes.&lt;/p&gt;
</description></item></channel></rss>