<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-848045343259901678</id><updated>2012-01-25T13:12:11.598-08:00</updated><category term='WinRT'/><category term='Unit testing'/><category term='TDD'/><category term='Genealogy'/><category term='http://www.blogger.com/img/blank.gif'/><category term='Windows 8'/><category term='Metro'/><category term='Async CTP'/><category term='3D'/><category term='GPGPU'/><category term='Simulation'/><category term='Flocking'/><category term='RX'/><category term='Reactive Extensions for .NET'/><category term='Oscilloscope'/><category term='F#'/><category term='Boids'/><category term='XAML'/><category term='.NET'/><category term='OpenGL'/><title type='text'>Taumuon Jabuka</title><subtitle type='html'>Gary Evans coding blog</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://taumuon-jabuka.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/848045343259901678/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://taumuon-jabuka.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Taumuon</name><uri>http://www.blogger.com/profile/12874508418428339669</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='27' src='http://bp0.blogger.com/_9XuDfr9pYU0/SGAMUjxkNvI/AAAAAAAAACs/YhujEd8cyMk/S220/colloseum.jpg'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>22</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-848045343259901678.post-5142700386525377726</id><published>2012-01-25T13:12:00.001-08:00</published><updated>2012-01-25T13:12:11.663-08:00</updated><title type='text'>Charting Performance</title><content type='html'>&lt;p&gt;I wasn’t going to blog about performance until a later post, but there was a performance issue I wanted to tackle before discussing some &lt;i&gt;other things.&lt;/i&gt;&lt;/p&gt;  &lt;p&gt;The frame rate sometimes seemed to be as expected, but sometimes slow, and the application was quite unresponsive when handling the Stop button command.&lt;/p&gt;  &lt;p&gt;To help nail this down I created a Buffer Frame Rate performance counter, so that I monitor it along with another couple of metrics (I chose to see the count of gen1 and gen2 garbage collections, along with the % time in GC, and % processor time).&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh5.ggpht.com/-prMYYji_cg8/TyBwAnV-meI/AAAAAAAAAL8/AiHmdGyjPMc/s1600-h/image%25255B4%25255D.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh6.ggpht.com/-lHoHjOoqrkY/TyBwBSLUehI/AAAAAAAAAME/yeFgP4gyQic/image_thumb%25255B2%25255D.png?imgmax=800" width="371" height="473" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;The framerate average isn’t bad, but the frame rate is not consistent; it regularly drops below 10 frames per second. This post will discuss optimising the performance of the application to keep the frame rate steady and ensure that the app is responsive.&lt;/p&gt;  &lt;p&gt;Performance increases should generally focus on reducing unnecessary work in the whole application, and not micro-optimisations. In this case, it’s realising that the top chart is drawing at 40 frames per second (a buffer of 1000 data points into a 40KHz source), whereas the bottom chart is updating at 400 frames per second (sampling the data to 100 points, and updating the chart on each of those sample points).&lt;/p&gt;  &lt;p&gt;However, as there are other types of charts I want to produce it’s worth investigating where the time is spent.&lt;/p&gt;  &lt;p&gt;First off, I tried turning off anti-aliasing, and moving over to the d3future project (&lt;a href="http://d3future.codeplex.com"&gt;http://d3future.codeplex.com&lt;/a&gt; ), to see if that made any performance difference, but the performance was pretty similar, with the CPU pegged at 100%. A further run generated this interesting trace:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/-0xat8MNOeGE/TyBwCE8ruQI/AAAAAAAAAMM/GxIi_m8LlVo/s1600-h/image%25255B8%25255D.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh4.ggpht.com/--zysqrqhf90/TyBwC4NoNqI/AAAAAAAAAMU/G1TT8ML99eU/image_thumb%25255B4%25255D.png?imgmax=800" width="434" height="489" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;The frame count starts initially high, but after a while performance drops off dramatically, which is followed by some long spikes in the % Time in GC. The drop-off maybe seems to correspond with one of the gen 2 collections. This situation wasn’t so reproducible that I could catch it under a profiler.&lt;/p&gt;  &lt;p&gt;My next step was to profile to see where the application was spending its time, just using the Visual Studio profiler. The application’s hot path was in the D3 LineChart’s UpdateCore method, where it was updating the chart’s Bounds using data from the EnumerableDataSource –it was iterating over the whole collection to find the x and y min and max values (to fit the chart to the data area).&lt;/p&gt;  &lt;p&gt;It seems unnecessary to iterate over the whole data source to get the minimum and maximum x values – for a linear chart (i.e. not a scatter chart) it would be expected that the first point is at the minimum x value and the last point the maximum.&lt;/p&gt;  &lt;p&gt;I created a chart derived from LineChart where I could instead pass in a List of points for the datasource – this means that I could get the last point without enumerating the whole collection, allowing the min and max x values to be quickly found. For the y values, I happen to want the chart to be a fixed axis and not scale to fit the data (instead scaled by a user-configurable gain factor), so there was no need at all to iterate over the collection.&lt;/p&gt;  &lt;p&gt;The chart looked like this after those changes:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh3.ggpht.com/-GoQ_3w--bhU/TyBwDXsN4MI/AAAAAAAAAMY/uthIfjDXG1M/s1600-h/image%25255B12%25255D.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh4.ggpht.com/-cXH0dq80bhc/TyBwD2rgxAI/AAAAAAAAAMg/joBeEkG8Fy0/image_thumb%25255B6%25255D.png?imgmax=800" width="318" height="484" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;The frame rate is pretty consistent now. The gen 1 allocations are still occurring at a rate of 200 per minute, but the application is now responsive to the stop click, stopping immediately.&lt;/p&gt;  &lt;p&gt;Profiling with memory allocations turned on showed that the types with most instances was System.Byte[] with 32% of the instances allocated in the profiling period.&lt;/p&gt;  &lt;p&gt;ConvertByteArrayToFloatArray() previously looked like this:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/-lHajzBLahWk/TyBwEXnKBdI/AAAAAAAAAMo/17EgLOzl3PM/s1600-h/image%25255B17%25255D.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh4.ggpht.com/-c_bbN-uFmB8/TyBwFIS7TeI/AAAAAAAAAMw/8pJGN-dshZg/image_thumb%25255B9%25255D.png?imgmax=800" width="522" height="138" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;And after changing this to not use LINQ:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh3.ggpht.com/-ldV6xDbM39Y/TyBwFU1NwYI/AAAAAAAAAM0/jItPq1L31fg/s1600-h/image%25255B22%25255D.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh6.ggpht.com/-kQVKD5Ie5Ys/TyBwGLS9v8I/AAAAAAAAANA/M-oZoJb29F4/image_thumb%25255B12%25255D.png?imgmax=800" width="562" height="162" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Gives the following:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/-Uvv-bTInSuI/TyBwGpgdPQI/AAAAAAAAANM/J1jQzWT1uRc/s1600-h/image%25255B27%25255D.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh4.ggpht.com/-HWs9lbY3M4Y/TyBwHlhJtOI/AAAAAAAAANU/wmdfgpmThco/image_thumb%25255B15%25255D.png?imgmax=800" width="406" height="574" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Performance is pretty similar to previous, but the gen 1 collections are occurring at a slightly lower rate. Now, profiling tells me that the types with most instances allocated is pretty much split between System.Action, System.Object, and System.Reactive.Disposables.SingleAssignmentDisposable.&lt;/p&gt;  &lt;p&gt;Profiling at this point identifies the majority of the work now being in LineGraph.UpdateCore() where its transforming all points from their data coordinates into screen coordinates.&lt;/p&gt;  &lt;p&gt;The transformation of coordinates for many points is embarrassingly parallel (think GPGPU). Potentially, the GPU could be put to use by simply drawing the points untransformed, adding a transformation onto the drawing context.&lt;/p&gt;  &lt;p&gt;Further performance increases could be done for the sliding window chart: A sliding window chart redisplays the same points again and again, as they move left along the x axis. It essentially transforms the same point many times into the new viewport. Doing a transform to move an existing point to the left could be more efficient than transforming again from data to screen coordinates.&lt;/p&gt;  &lt;p&gt;This blog so far has looked at some micro-optimisations focussing on identifying areas where work can be reduced. As I said in the beginning, if I wasn’t so interested in optimising for future requirements, I’d simply ensure that I was doing less drawing straight away.&lt;/p&gt;  &lt;p&gt;Anyway, the sliding observable can be change to only take every 10th windowed set of values, by introducing another Sample() on the result of the WindowWithCount():&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh3.ggpht.com/-ZuAamKDINmE/TyBwIGXy2wI/AAAAAAAAANY/fJh9NQNpK5M/s1600-h/image%25255B31%25255D.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh3.ggpht.com/-oe4Um_VsBq0/TyBwIwgz2bI/AAAAAAAAANk/G-PFeRlLVmM/image_thumb%25255B17%25255D.png?imgmax=800" width="591" height="310" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;There isn’t actually that much difference in real-world performance.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/-FzbXeOPSMBM/TyBwJqGVQKI/AAAAAAAAANs/1EgSTWVXEUA/s1600-h/image%25255B36%25255D.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh4.ggpht.com/-XZbIWTyDW9Q/TyBwKZ6St2I/AAAAAAAAAN0/_1PWYujkYiU/image_thumb%25255B20%25255D.png?imgmax=800" width="415" height="640" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;The rate of gen 1 collections is lower still than previous, but the profiler shows that app is still spending the majority of its time in drawing the charts. Surprisingly the CPU usage doesn’t seem to have dropped much, although in the profiler the time can now be seen to be split between the drawing of the two charts, instead of dominated by the drawing of the sliding window chart.&lt;/p&gt;  &lt;p&gt;The micro-optimisations were still worth investigating, as I have charts in mind that will draw multiple traces simultaneously.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/848045343259901678-5142700386525377726?l=taumuon-jabuka.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://taumuon-jabuka.blogspot.com/feeds/5142700386525377726/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=848045343259901678&amp;postID=5142700386525377726' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/848045343259901678/posts/default/5142700386525377726'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/848045343259901678/posts/default/5142700386525377726'/><link rel='alternate' type='text/html' href='http://taumuon-jabuka.blogspot.com/2012/01/charting-performance.html' title='Charting Performance'/><author><name>Taumuon</name><uri>http://www.blogger.com/profile/12874508418428339669</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='27' src='http://bp0.blogger.com/_9XuDfr9pYU0/SGAMUjxkNvI/AAAAAAAAACs/YhujEd8cyMk/S220/colloseum.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh6.ggpht.com/-lHoHjOoqrkY/TyBwBSLUehI/AAAAAAAAAME/yeFgP4gyQic/s72-c/image_thumb%25255B2%25255D.png?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-848045343259901678.post-6038530747887568572</id><published>2012-01-18T12:10:00.001-08:00</published><updated>2012-01-21T07:31:23.535-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='RX'/><category scheme='http://www.blogger.com/atom/ns#' term='.NET'/><category scheme='http://www.blogger.com/atom/ns#' term='Async CTP'/><category scheme='http://www.blogger.com/atom/ns#' term='Reactive Extensions for .NET'/><category scheme='http://www.blogger.com/atom/ns#' term='Oscilloscope'/><title type='text'>Oscilloscope using RX and C# Async CTP</title><content type='html'>&lt;p&gt;In my last blog post I described the implementation of a simple ‘oscilloscope app’ in F#, as I wanted to see how the code would be structured using only F# idioms (&lt;a href="http://taumuon-jabuka.blogspot.com/2012/01/visualising-sound.html"&gt;http://taumuon-jabuka.blogspot.com/2012/01/visualising-sound.html&lt;/a&gt; )&lt;/p&gt;  &lt;p&gt;My natural instinct would have been to implement it using the Reactive Extensions for .NET (RX Framework), but I first wanted to investigate a pure F# solution. This post describes an alternate RX implementation.&lt;/p&gt;  &lt;p&gt;Similar to my last post, I’ll describe the code inside-out.&lt;/p&gt;  &lt;h3&gt;Creating the Observable&lt;/h3&gt;  &lt;p&gt;&lt;a href="http://lh5.ggpht.com/-eAycP7Dcv6A/Txcm6ixX81I/AAAAAAAAAK8/5V9V_w5mY7A/s1600-h/clip_image002%25255B7%25255D.jpg"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="clip_image002" border="0" alt="clip_image002" src="http://lh5.ggpht.com/-S5XNUHZL7XA/Txcm7pK32wI/AAAAAAAAALA/xSNg3KCK0Nc/clip_image002_thumb%25255B4%25255D.jpg?imgmax=800" width="599" height="471" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;This code returns an IObservable with a float array payload, representing each read from the CaptureBuffer. The implementation could have internally started a dedicated Thread from which to push out values, but instead I’m using the new C# 5 async functionality (using the Async CTP Update 3), so that my code looks pretty similar to the previous F# example.&lt;/p&gt;  &lt;p&gt;The observable takes a CancellationToken, whose IsCancellationRequested is set to true once all subscriptions to the observable have been disposed. The CompositeDisposable returned out from the lambda is also disposed of at that point.&lt;/p&gt;  &lt;p&gt;The code loops while its CancellationToken has not been cancelled, asynchronously awaiting for one of the WaitHandles to be set, and then it reads from the buffer. The value is pushed out to subscribers in the OnNext.&lt;/p&gt;  &lt;p&gt;The ConvertByteArrayToFloatArray() is trivial:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh5.ggpht.com/-6t3Ii3C8lLA/Txcm8O-tzvI/AAAAAAAAALE/zu3VDmMOao0/s1600-h/clip_image003%25255B5%25255D.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="clip_image003" border="0" alt="clip_image003" src="http://lh3.ggpht.com/-V_Q4vqmPU8M/Txcm87HsbbI/AAAAAAAAALI/SFXteKVI_KU/clip_image003_thumb%25255B2%25255D.png?imgmax=800" width="493" height="130" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;h3&gt;Subscriptions to the observable&lt;/h3&gt;  &lt;p&gt;&lt;a href="http://lh5.ggpht.com/-EB6Xl75B-lw/Txcm9Ri8ZfI/AAAAAAAAALM/a_5WOAby514/s1600-h/clip_image004%25255B6%25255D.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="clip_image004" border="0" alt="clip_image004" src="http://lh6.ggpht.com/-SE34OuUdWF8/Txcm-O0eiPI/AAAAAAAAALQ/EDaVXlSSUUA/clip_image004_thumb%25255B3%25255D.png?imgmax=800" width="510" height="101" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;First off, the float array returned from the observable is decomposed into the individual values, using a &lt;i&gt;SelectMany&lt;/i&gt;, as sampling, buffering and windowing operations all operate on a stream of floats. Then, the observable is published to an IConnectableObservable. The microphone access returns a Cold observable, meaning that each subscriber to it would end up creating their own microphone access. This would work (the CaptureBuffer doesn’t prevent this), but the connectable observable means that instead all clients share the same observable, and ensures that they all see the same values (so that the traces on the two charts are in sync).&lt;/p&gt;  &lt;p&gt;The RefCount() means that when all subscriptions to the &lt;i&gt;observable&lt;/i&gt; IConnectableObservable variable have been disposed of then the subscription to the underlying observable will also be disposed.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh6.ggpht.com/-jCL2vDpkNXo/Txcm-0RL2mI/AAAAAAAAALU/BvFq2YyuFwU/s1600-h/clip_image005%25255B5%25255D.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="clip_image005" border="0" alt="clip_image005" src="http://lh6.ggpht.com/-AZOf5bPqN9s/Txcm_nn49gI/AAAAAAAAALY/CaJ1_-1PNNE/clip_image005_thumb%25255B2%25255D.png?imgmax=800" width="541" height="205" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;The top ‘oscilloscope’ trace is a simple Observable.Buffer() over the data stream. There is no need to &lt;i&gt;ObserveOn&lt;/i&gt; the dispatcher thread as the subscription occurs on the UI thread. Using RX, it would be easy to schedule various parts of the work onto different threads, but I’ll discuss this in a later article (I want to keep everything on the UI thread for now to compare performance with the single threaded F# implementation).&lt;/p&gt;  &lt;p&gt;All subscriptions are added to a CompositeDisposable member in the ViewModel – the Stop button’s command implementation disposes of this, which causes all subscriptions to be disposed of, and so the microphone access loop to be terminated via its CancellationToken.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/-bZhPhDgghn0/TxcnApTW_NI/AAAAAAAAALc/cCsFEngfE5Y/s1600-h/clip_image006%25255B5%25255D.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="clip_image006" border="0" alt="clip_image006" src="http://lh6.ggpht.com/-udt4kq7bENw/TxcnCEYSFuI/AAAAAAAAALg/krsL9g6jGzk/clip_image006_thumb%25255B2%25255D.png?imgmax=800" width="567" height="250" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;The windowing operation simply samples every 100 data points, and from those sampled data points takes a sliding window of 1000 values to display. The windowCount variable is closed over to allow the y axis to be continually updated.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh3.ggpht.com/-CkBBC9GQSoQ/TxcnC7AAbxI/AAAAAAAAALk/zw-k8yB7LJY/s1600-h/clip_image007%25255B6%25255D.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="clip_image007" border="0" alt="clip_image007" src="http://lh3.ggpht.com/-cd5RhsRXya4/TxcnD53Mc9I/AAAAAAAAALo/2lbvYr78kZQ/clip_image007_thumb%25255B3%25255D.png?imgmax=800" width="471" height="164" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;The Sample operator is simple, but not particularly efficient – it takes a buffer (i.e a non-overlapping window) of &lt;i&gt;count &lt;/i&gt;values, and then takes the last value in the buffer.&lt;/p&gt;  &lt;p&gt;The WindowWithCount operator is the same one I discussed at &lt;a href="http://taumuon-jabuka.blogspot.com/2011/07/rx-framework-performance-awareness.html"&gt;http://taumuon-jabuka.blogspot.com/2011/07/rx-framework-performance-awareness.html&lt;/a&gt; (with implementation grabbed from &lt;a href="http://social.msdn.microsoft.com/Forums/en-US/rx/thread/37428f58-f241-45b3-a878-c1627deb9ac4#bcdc7b79-bbde-4145-88e4-583685285682"&gt;http://social.msdn.microsoft.com/Forums/en-US/rx/thread/37428f58-f241-45b3-a878-c1627deb9ac4#bcdc7b79-bbde-4145-88e4-583685285682&lt;/a&gt; )&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/-4uheJ0pwzP4/TxcnEdmDvfI/AAAAAAAAALs/TA24oDx6St8/s1600-h/clip_image008%25255B5%25255D.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="clip_image008" border="0" alt="clip_image008" src="http://lh3.ggpht.com/-zyoDyBo4ln0/TxcnFp4F20I/AAAAAAAAALw/NDbhvztr-lM/clip_image008_thumb%25255B2%25255D.png?imgmax=800" width="577" height="243" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;And as I also talked about in that post, the RX guidelines recommend implementing an operator in terms of existing operator. There’s only one problem in this case, it’s quite slow, (I’ll get quantitative figures on this in a future blog post discussing performance of all approaches).&lt;/p&gt;  &lt;p&gt;The following is faster (again, I know more specific figures are needed):&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/-rwo_mA39Gfo/TxcnHCYs87I/AAAAAAAAAL0/4FZvEso1Has/s1600-h/clip_image009%25255B6%25255D.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="clip_image009" border="0" alt="clip_image009" src="http://lh3.ggpht.com/-JKKLsu0tqy8/TxcnIStGmCI/AAAAAAAAAL4/ovg1lfMqM2Y/clip_image009_thumb%25255B3%25255D.png?imgmax=800" width="514" height="340" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;h3&gt;Comparing implementations&lt;/h3&gt;  &lt;p&gt;For me, the RX solution is cleaner than the F# solution, and easier to follow. I did implement my F# solution in quite an imperative way though, and should have perhaps used AsyncSeq or mailbox processors, but as reading from the microphone is a push-based activity, none of those solutions would be as clean as RX (of course I haven’t covered using RX in F#). The F# version is much faster, and I’ll take a big more of a dig into performance in an upcoming blog post.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/848045343259901678-6038530747887568572?l=taumuon-jabuka.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://taumuon-jabuka.blogspot.com/feeds/6038530747887568572/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=848045343259901678&amp;postID=6038530747887568572' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/848045343259901678/posts/default/6038530747887568572'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/848045343259901678/posts/default/6038530747887568572'/><link rel='alternate' type='text/html' href='http://taumuon-jabuka.blogspot.com/2012/01/oscilloscope-using-rx-and-c-async-ctp.html' title='Oscilloscope using RX and C# Async CTP'/><author><name>Taumuon</name><uri>http://www.blogger.com/profile/12874508418428339669</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='27' src='http://bp0.blogger.com/_9XuDfr9pYU0/SGAMUjxkNvI/AAAAAAAAACs/YhujEd8cyMk/S220/colloseum.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh5.ggpht.com/-S5XNUHZL7XA/Txcm7pK32wI/AAAAAAAAALA/xSNg3KCK0Nc/s72-c/clip_image002_thumb%25255B4%25255D.jpg?imgmax=800' height='72' width='72'/><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-848045343259901678.post-6212803882509703180</id><published>2012-01-10T14:38:00.001-08:00</published><updated>2012-01-21T06:35:01.591-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='.NET'/><category scheme='http://www.blogger.com/atom/ns#' term='F#'/><category scheme='http://www.blogger.com/atom/ns#' term='Oscilloscope'/><title type='text'>Visualising Sound</title><content type='html'>&lt;p&gt;This is the first in a series of blog posts about visualising sound, in a similar way to an oscilloscope. The geek in me thought it’d be a fun thing to do, as well investigate different technological approaches to implementing the app (interesting as it’s a real-time performant app). &lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh3.ggpht.com/-XAYRLeQsY3o/Twy94FZe95I/AAAAAAAAAHo/9zfAHCbWY9A/s1600-h/clip_image001%25255B4%25255D.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="clip_image001" alt="clip_image001" src="http://lh6.ggpht.com/-qX4HS0ris9s/Twy94lcjPCI/AAAAAAAAAHw/cXFKtMeV1TI/clip_image001_thumb%25255B1%25255D.png?imgmax=800" border="0" height="345" width="515" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;An oscilloscope has a single screen, which refreshes on a given time period, displaying a number of traces. The user can control that time period, as well as the gain (the y axis scale).&lt;/p&gt;  &lt;p&gt;The top graph is essentially the same view as an oscilloscope containing a single trace, and a fixed time period (further blog posts may investigate varying time periods).&lt;/p&gt;  &lt;p&gt;The bottom graph is a sliding window with a longer time period – this is the advantage of implementing an oscilloscope in code, we can create charts that aren’t really feasible in a classic CRT oscilloscope.&lt;/p&gt;  &lt;p&gt;This series of blog posts will investigate implementing this screen using F# idioms, as well as using the Reactive Extension for .NET (RX Framework), and TPL Dataflow.&lt;/p&gt;  &lt;p&gt;There are further things that could be implemented in future blog posts which may be interesting to see how the varying approaches look like:&lt;/p&gt;  &lt;p&gt;· Trigger, or Trigger and hold: Only refresh the oscilloscope view once a certain trigger level has been reached. This is interesting as may want to include a certain number of immediately prior to the point where the trigger was set.&lt;/p&gt;  &lt;p&gt;· Log many traces.&lt;/p&gt;  &lt;p&gt;· Spectrum analyser/FFT.&lt;/p&gt;  &lt;p&gt;· Digital filtering.&lt;/p&gt;  &lt;p&gt;· Comparing traces, or more complicated math channels.&lt;/p&gt;  &lt;p&gt;· Heatmap.&lt;/p&gt;  &lt;p&gt;· Adjustable time offset (delay) – useful on the oscilloscope view to centre a waveform on the screen, or for when comparing two or more channels output.&lt;/p&gt;  &lt;h3&gt;F# Implementation&lt;/h3&gt;  &lt;p&gt;This blog post covers an F# implementation of the microphone, using asynchronous workflows. The graphing is being done by a nightly build download of &lt;a href="http://dynamicdatadisplay.codeplex.com/"&gt;Dynamic Data Display (D3)&lt;/a&gt;. I’m really impressed with the performance, but it is quite difficult to customise.&lt;/p&gt;  &lt;p&gt;The low-latency microphone code is from this article on gamedev (&lt;a title="http://www.gamedev.net/topic/586706-slimdx-directsound-capture-microphone-stream-with-low-latency-example/" href="http://www.gamedev.net/topic/586706-slimdx-directsound-capture-microphone-stream-with-low-latency-example/"&gt;http://www.gamedev.net/topic/586706-slimdx-directsound-capture-microphone-stream-with-low-latency-example/&lt;/a&gt;).&lt;/p&gt;  &lt;p&gt;The implementation of this is pretty simple; I’ll start from the detail out.&lt;/p&gt;  &lt;p&gt;The inner loop of the program is an asynchronous workflow that reads from the buffer and returns a sequence:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh3.ggpht.com/--2pBXN42PBA/Twy95opEFRI/AAAAAAAAAH0/6Beo_KDDlD0/s1600-h/clip_image003%25255B5%25255D.jpg"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="clip_image003" alt="clip_image003" src="http://lh6.ggpht.com/-XfomP2_0mM4/Twy96Fq1dFI/AAAAAAAAAIA/KGyj9rBsCKo/clip_image003_thumb%25255B2%25255D.jpg?imgmax=800" border="0" height="346" width="607" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Note the slightly-strange looking code:&lt;/p&gt;  &lt;p&gt;let task = System.Threading.Tasks.Task&amp;lt;int&amp;gt;.Factory.StartNew(fun () -&amp;gt; WaitHandle.WaitAny(waitHandles))&lt;/p&gt;  &lt;p&gt;F# has an Async.AwaitWaitHandle() method, which unfortunately only waits on a single handle. We want to wait on both handles so that we get notified when the buffer is full every 4096 instead of every 8192 bytes. With 4 bytes per sample, and a sample rate off 44KHz, this is equivalent to getting notified at an approximate rate of 40 times per second instead of 20 times per second.&lt;/p&gt;  &lt;p&gt;I could have implemented Async.AwaitAnyWaitHandle() taking an array of WaitHandles, but looking at the code in the F# PowerPack, the code was quite complex. So, the code instead creates a new future to do the waiting and let us know which WaitHandle was set (this does mean that we’ve got the minor overhead of scheduling a new task to run on the task pool).&lt;/p&gt;  &lt;p&gt;The Async.StartImmediate method ensures that the ProcessStream call is marshalled back onto the UI thread. It may be worth in the future looking at doing more of the data processing on a dedicated thread, leaving the UI thread free for drawing and user input.&lt;/p&gt;  &lt;p&gt;The convertByteArrayToSequence is simple, it just iterates over the buffer in 4 byte chunks, and converts the values to floats, which it yields in the sequence:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh5.ggpht.com/-fBC8G3ZE-Hg/Twy964_5WbI/AAAAAAAAAII/codL6Szmdpo/s1600-h/clip_image004%25255B4%25255D.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="clip_image004" alt="clip_image004" src="http://lh4.ggpht.com/-6wHf1z92QH4/Twy97rnlaDI/AAAAAAAAAIQ/WwIdIBKo_mQ/clip_image004_thumb%25255B1%25255D.png?imgmax=800" border="0" height="62" width="524" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;The ProcessStream method looks like this:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh5.ggpht.com/--_3QdWf1-40/Twy98QMLbCI/AAAAAAAAAIU/1kvlxGVTxtk/s1600-h/clip_image006%25255B4%25255D.jpg"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="clip_image006" alt="clip_image006" src="http://lh6.ggpht.com/-rcm-S5LsLfg/Twy980iCnYI/AAAAAAAAAIc/NKcfl4QeVj0/clip_image006_thumb%25255B1%25255D.jpg?imgmax=800" border="0" height="376" width="599" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;For completeness, this is the Seq.sample module extension:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh3.ggpht.com/-Cm36iVMMo60/Twy99f017rI/AAAAAAAAAIk/04wgV-paowQ/s1600-h/clip_image007%25255B4%25255D.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="clip_image007" alt="clip_image007" src="http://lh5.ggpht.com/-8z65PJkV3qY/Twy9-EBsTWI/AAAAAAAAAIs/hUfT9judcXo/clip_image007_thumb%25255B1%25255D.png?imgmax=800" border="0" height="103" width="521" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;The nastiest bit of the code in ProcessStream is probably towards the end, where the windowedUnderlyingData is manipulated to ensure that the window only contains 1000 samples. It would be nice to do this in a non-imperative way, using Seq.windowed, but the problem is, is that the sequence we’re operating on is only the result of one buffer read operation, whereas windows etc. should operate over the whole data stream, and the sequences can’t be combined into a single sequence using &lt;i&gt;yield!&lt;/i&gt; as they’re all generated asynchronously. Similarly, the buffer takes non-overlapping windows over the input sequence, and without taking a buffer over the whole stream, it may miss samples off the end of the sequence portions. Tomas Petricek has written a library, AsyncSeq, &lt;a href="http://tomasp.net/blog/async-sequences.aspx"&gt;http://tomasp.net/blog/async-sequences.aspx&lt;/a&gt; which I may investigate in a later post. &lt;/p&gt;  &lt;p&gt;The alternative to this would be to implement mailbox processors, having different agents for the buffering, windowing and sampling operations. I did start investigating this, but didn’t feel happy with them being pull-based (they don’t return data unless asked). I could have set them up to act more like a dataflow network, but it does seem to go against their intended use of managing state between different threads. I may revisit this in a future blog post.&lt;/p&gt;  &lt;p&gt;I feel that even though F#’s does have nice features which helped to quickly implement the app, RX would probably be a better fit. I guess I won’t know until I implement it in RX and compare the differences.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/848045343259901678-6212803882509703180?l=taumuon-jabuka.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://taumuon-jabuka.blogspot.com/feeds/6212803882509703180/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=848045343259901678&amp;postID=6212803882509703180' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/848045343259901678/posts/default/6212803882509703180'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/848045343259901678/posts/default/6212803882509703180'/><link rel='alternate' type='text/html' href='http://taumuon-jabuka.blogspot.com/2012/01/visualising-sound.html' title='Visualising Sound'/><author><name>Taumuon</name><uri>http://www.blogger.com/profile/12874508418428339669</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='27' src='http://bp0.blogger.com/_9XuDfr9pYU0/SGAMUjxkNvI/AAAAAAAAACs/YhujEd8cyMk/S220/colloseum.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh6.ggpht.com/-qX4HS0ris9s/Twy94lcjPCI/AAAAAAAAAHw/cXFKtMeV1TI/s72-c/clip_image001_thumb%25255B1%25255D.png?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-848045343259901678.post-6835055663552726810</id><published>2011-10-30T11:49:00.000-07:00</published><updated>2012-01-21T06:35:01.591-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Metro'/><category scheme='http://www.blogger.com/atom/ns#' term='WinRT'/><category scheme='http://www.blogger.com/atom/ns#' term='.NET'/><category scheme='http://www.blogger.com/atom/ns#' term='http://www.blogger.com/img/blank.gif'/><category scheme='http://www.blogger.com/atom/ns#' term='XAML'/><category scheme='http://www.blogger.com/atom/ns#' term='Windows 8'/><category scheme='http://www.blogger.com/atom/ns#' term='Genealogy'/><title type='text'>Family tree timelines</title><content type='html'>&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/-x5c9PsYo0yg/Tq2ce9wOy4I/AAAAAAAAAHI/_HC_NRbDN_k/s1600/Timeline.png"&gt;&lt;img style="cursor: pointer; width: 663px; height: 352px;" src="http://1.bp.blogspot.com/-x5c9PsYo0yg/Tq2ce9wOy4I/AAAAAAAAAHI/_HC_NRbDN_k/s400/Timeline.png" alt="" id="BLOGGER_PHOTO_ID_5669359561716190082" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;As I talked about in my previous post, I created a simple family tree visualisation program to let me know which of my ancestors I have the most interest for.&lt;br /&gt;&lt;br /&gt;I also prototyped up a timeline view - in the above image. It's probably pretty obvious that it's a prototype, as there are no labels to identify any of the individuals.&lt;br /&gt;&lt;br /&gt;The idea for this came about as genesreunited has a non-validated freeform text field for all entries of dates. There's no way to validate whether this information is valid for GEDCOM export - the GEDCOM spec lets various dates, such as approximate dates, bounded dates, dates within a specific quarter etc. to be specified but obviously the date has to be specified in the correct format.&lt;br /&gt;&lt;br /&gt;I wanted to check that all dates were both in the correct format, and actually valid (i.e. check for nonsensical dates such as parents born after their children etc).&lt;br /&gt;&lt;br /&gt;The idea behind the colouring is for green to show a GEDCOM format valid date, yellow to specify a missing date (with the date inferred), and red to indicate an invalid date.&lt;br /&gt;&lt;br /&gt;The opacity is to indicate the 'confidence' in a date - with a specified date range not being at full opacity. Also, the dates can be inferred using a set of rules (e.g. parents should be at least 12 years older than their children, a child is probably born within a year of his or her baptism, the first child is born within a year of his or her parents marriage etc.). These rules could obviously get quite complicated.&lt;br /&gt;&lt;br /&gt;The layout matches pretty much the layout of the ancestor chart, with ancestors being adjacent to each other. I was fussing over this a little bit - I thought it'd be nice for the descendents on the chart to be nearest to each other on the x-axis), but there's no way for this to happen through the tree.&lt;br /&gt;&lt;br /&gt;I was feeling pretty happy with this, and thought that it's probably worth putting into the app (with some tweakes such as making the y-axis an adorner layer that adjusts with scale etc), but then I found that there's some software which has a pretty-much identical view to this (I did google around for this before implementing, I obviously didn't google hard enough).&lt;br /&gt;&lt;br /&gt;&lt;a href="http://progenygenealogy.com/products/timeline-charts/genelines-sample-charts/pedigree.aspx"&gt;Progeny Genealogy&lt;/a&gt; has an identical layout (OK, turned on its side). It even has the same idea of using opacity to indicate what data is estimated (not varying opacity, but even still). I guess that there's only so many ways to solve a problem, but it's still gutting when you think you've had an original idea!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/848045343259901678-6835055663552726810?l=taumuon-jabuka.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://taumuon-jabuka.blogspot.com/feeds/6835055663552726810/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=848045343259901678&amp;postID=6835055663552726810' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/848045343259901678/posts/default/6835055663552726810'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/848045343259901678/posts/default/6835055663552726810'/><link rel='alternate' type='text/html' href='http://taumuon-jabuka.blogspot.com/2011/10/family-tree-timelines.html' title='Family tree timelines'/><author><name>Taumuon</name><uri>http://www.blogger.com/profile/12874508418428339669</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='27' src='http://bp0.blogger.com/_9XuDfr9pYU0/SGAMUjxkNvI/AAAAAAAAACs/YhujEd8cyMk/S220/colloseum.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/-x5c9PsYo0yg/Tq2ce9wOy4I/AAAAAAAAAHI/_HC_NRbDN_k/s72-c/Timeline.png' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-848045343259901678.post-9117096643859055524</id><published>2011-10-29T12:35:00.000-07:00</published><updated>2012-01-21T06:35:01.592-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Metro'/><category scheme='http://www.blogger.com/atom/ns#' term='WinRT'/><category scheme='http://www.blogger.com/atom/ns#' term='.NET'/><category scheme='http://www.blogger.com/atom/ns#' term='XAML'/><category scheme='http://www.blogger.com/atom/ns#' term='Windows 8'/><category scheme='http://www.blogger.com/atom/ns#' term='Genealogy'/><title type='text'>Windows 8 Metro Development Experience</title><content type='html'>I’ve been running the Windows 8 developer preview for a few weeks, and thought  I’d blog about my experiences in converting a Silverlight app I’ve written into  a Metro one. I’ll first describe the app, then my experiences developing it.&lt;br /&gt;&lt;h3&gt;The App&lt;/h3&gt; &lt;p&gt;The application is a family tree viewing application. It came about as I’ve  found a few branches my family tree back to the 1600s (with help hooking up with  other people on GenesReunited). It isn’t very friendly if you want to see the  whole tree as it shuffles all of the individuals around to minimise the screen  real estate, which is good for printing, but not so good to make sense of the  tree. I was interested in seeing which of my ancestors I had the most history  for, and which I wanted to research next, so I created an app to show ancestors  without siblings, and to favour clarity over screen real estate.&lt;/p&gt; &lt;p&gt;(When I started writing this app, there didn’t seem to be any family tree  apps which showed the whole family tree. I’ve since found that &lt;a href="http://gramps-project.org/"&gt;Gramps&lt;/a&gt; uses GraphViz to do exactly that).&lt;/p&gt; &lt;p&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/-ubpw2PLa8TA/TqxVtIVCi8I/AAAAAAAAAGk/Tg1C9k5Hu5k/s1600/FullTree.png"&gt;&lt;img style="cursor:pointer; cursor:hand;width: 400px; height: 225px;" src="http://1.bp.blogspot.com/-ubpw2PLa8TA/TqxVtIVCi8I/AAAAAAAAAGk/Tg1C9k5Hu5k/s400/FullTree.png" alt="" id="BLOGGER_PHOTO_ID_5669000264770816962" border="0" /&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;The screenshot above shows that Metro apps really don’t much unnecessary chrome.  Any infrequently used commands are hidden in the app bar at the bottom of the  screen. The next screenshot shows the app at full zoom with the application bar  hidden.&lt;/p&gt;&lt;h3&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/-_TmjbJ6y7p0/TqxV87in7pI/AAAAAAAAAGw/SNI2nsvSOSE/s1600/Zoomed.png"&gt;&lt;img style="cursor:pointer; cursor:hand;width: 400px; height: 225px;" src="http://1.bp.blogspot.com/-_TmjbJ6y7p0/TqxV87in7pI/AAAAAAAAAGw/SNI2nsvSOSE/s400/Zoomed.png" alt="" id="BLOGGER_PHOTO_ID_5669000536216039058" border="0" /&gt;&lt;/a&gt;&lt;/h3&gt; &lt;h3&gt;Development&lt;/h3&gt; &lt;p&gt;Now I’ve laid out the background, I can talk about the development  experience, and some of that will include talking about Windows 8. First off,  Metro’s UI is a bit toy-ish, but I can see that it will feel slick on a  touchscreen (I’ve got a Windows Phone 7 device, and am struck by the  similarities). The green is pretty garish thoughout, but I’ve attempted to  follow this look in my app.&lt;/p&gt; &lt;h4&gt;UI and controls&lt;/h4&gt; &lt;p&gt;It doesn’t feel as if Microsoft has paid enough attention to using Metro apps  with the mouse and keyboard yet. A case in point is in the zooming in this  application – I’ve provided a slider in the search bar, but it’s unclear whether  there will be a system-wide gesture mapping to pinch-to-zoom in the release.  This was necessary for testing, as even though Visual Studio has a simulator  which can be used to simulate gestures, the zoom gesture uses the mousewheel,  which my laptop does not have.&lt;/p&gt;  &lt;p&gt;The actual conversion was surprisingly simple (they promised that this would  be the case on the Build videos), but I’m glad that I didn’t hit any blockers.  My ViewModels needed minor changes (which I’ll talk about below), and most of my  Xaml just got moved across.&lt;/p&gt;  &lt;p&gt;The ScrollViewer now allows zooming and panning, so I was able to use this  instead of my own ZoomPanControl. Strangely, the zooming is performed by a  method instead of a dependency property (making it easier to animate zooms etc).  Also, even though it’s drawing visuals, the zoom seems to take a bitmap at full  zoom and simply scale that down (this is speculation on my part, but to my eyes  the scaled content looks a lot worse than a WPF version which uses scale  transforms). Here’s a WPF version of my app at similar zoom:&lt;/p&gt; &lt;h3&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/-dpJFWiB8L_w/TqxWYY30zsI/AAAAAAAAAG8/qauNGG97LWQ/s1600/wpf.png"&gt;&lt;img style="cursor:pointer; cursor:hand;width: 400px; height: 162px;" src="http://4.bp.blogspot.com/-dpJFWiB8L_w/TqxWYY30zsI/AAAAAAAAAG8/qauNGG97LWQ/s400/wpf.png" alt="" id="BLOGGER_PHOTO_ID_5669001007946059458" border="0" /&gt;&lt;/a&gt;&lt;/h3&gt;  &lt;p&gt;The text is almost readable, and the connecting lines aren’t suffering from  the ugly aliasing.&lt;/p&gt; &lt;p&gt;There are some further niggles, it seems that the ScrollViewer has a minimum  zoom factor of 0.1. Additionally, the slider control, even though it was set  with a minimum of 0.05 and maximum of 1, would only display values of 0 and 1 on  the popup.&lt;/p&gt;  &lt;h4&gt;Coding&lt;/h4&gt; &lt;p&gt;Now I’ve described some of the structural changes, I can describe some of the  coding changes. Most of the changes were pretty trivial in the ViewModel – apart  from the use of &lt;strong&gt;async&lt;/strong&gt;. The Silverlight of the app was using the  Reactive Framework. Other than reading a couple of articles, I hadn’t gotten  into looking into C# 5 async yet, but it was pretty trivial to switch over.  Testing it is another matter – I ended up downloading the Async CTP to see their  unit testing sample, and found that it was the most complex sample throughout.  The Reactive Framework allows you to simply control &lt;em&gt;Virtual time &lt;/em&gt;using  the scheduler, and I haven’t seen anything so simple or elegant for C# 5  async (though testing Observable.FromAsyncPattern methods are similarly tricky to test, as they don't use the scheduler requested; relying on the underlying IO Completion ports for scheduling the work).&lt;br /&gt;&lt;/p&gt; &lt;p&gt;[Edit] I originally blogged that I was concerned that there wouldn't be a version of the Reactive Framework for .NET 4.5/WinRT, following some forum rumours. However, the guys have a &lt;a href="http://channel9.msdn.com/Shows/Going+Deep/Bart-De-Smet-Rx-Updat-NET-45-Async-WinRT"&gt;.NET 4.5 RX&lt;/a&gt; build ready.&lt;br /&gt;&lt;/p&gt; &lt;p&gt;Other changes in the code mapped naturally across – the FileDialog now  returns an IInputStream, but this has a method AsString() to map across to a  .NET stream. The List class has had a few of its methods (such as the Sort  overload taking a delegate) removed, annoyingly.&lt;/p&gt; &lt;p&gt;Also, now that asynchronous calls are so pervasive, I’m surprised that the  Silverlight Toolkit BusyIndicator didn’t make it in.&lt;/p&gt; &lt;h4&gt;Visual Studio&lt;/h4&gt; &lt;p&gt;First, the unit testing tool is definitely pre-beta. I didn’t actually finish  investigating how to unit test my C# 5 async method conversions, as I couldn’t  stomach using the tool any longer. I’m also not overly-enamoured with the new  Find dialog. Otherwise, it seems to be pretty stable.&lt;/p&gt;  &lt;h3&gt;Metro-ising&lt;/h3&gt; &lt;p&gt;So far I’ve spoken about how easy it was to convert over a Silverlight  application to Metro, but Metro does allow many compelling features to be very  easily added to the application. Charms could be provided to allow searching of  family tree information, and to allow the images to be easily shared.&lt;/p&gt; &lt;p&gt;Once I get hold of a touchscreen device, I’d love to add some snap points to  the chart.&lt;/p&gt; &lt;h3&gt;Other thoughts&lt;/h3&gt; &lt;p&gt;Overall, I’m pleased with the development experience of targeting Windows 8  Metro. I haven’t spoken about the actual platform in this blog, but after the  months of silence and confusion about Windows 8, it’s all good news. .NET  developers are still first class citizens.&lt;/p&gt; &lt;p&gt;I’m happy that WinRT is back to native code, and as I have experience in  C++/CLI I’m very happy that C++/CX seems pretty much identical. From a .NET  coder point of view, it’s a little concerning that .NET apps will have a slight  performance disadvantage from the COM Interop of the WinRT projected types, but  I suppose that’s simply the whole ‘use .NET for productivity, C++ for  performance’ argument. And having worked on a few apps that had all of the  performant (and usually legacy) parts of the system in C++, with a .NET UI, and  the subsequent marshalling layer, it’s quite heartening to think that now we can  stay in C++ and write a fast and fluid UI without changing languages.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/848045343259901678-9117096643859055524?l=taumuon-jabuka.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://taumuon-jabuka.blogspot.com/feeds/9117096643859055524/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=848045343259901678&amp;postID=9117096643859055524' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/848045343259901678/posts/default/9117096643859055524'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/848045343259901678/posts/default/9117096643859055524'/><link rel='alternate' type='text/html' href='http://taumuon-jabuka.blogspot.com/2011/10/windows-8-metro-development-experience.html' title='Windows 8 Metro Development Experience'/><author><name>Taumuon</name><uri>http://www.blogger.com/profile/12874508418428339669</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='27' src='http://bp0.blogger.com/_9XuDfr9pYU0/SGAMUjxkNvI/AAAAAAAAACs/YhujEd8cyMk/S220/colloseum.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/-ubpw2PLa8TA/TqxVtIVCi8I/AAAAAAAAAGk/Tg1C9k5Hu5k/s72-c/FullTree.png' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-848045343259901678.post-1178897998054595150</id><published>2011-07-11T14:03:00.001-07:00</published><updated>2012-01-21T06:35:01.593-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='RX'/><category scheme='http://www.blogger.com/atom/ns#' term='.NET'/><category scheme='http://www.blogger.com/atom/ns#' term='Reactive Extensions for .NET'/><title type='text'>RX Framework Performance Awareness</title><content type='html'>&lt;p&gt;In a post a while ago, &lt;a href="http://taumuon-jabuka.blogspot.com/2010/11/rx-framework-for-manipulating-real-life.html"&gt;here&lt;/a&gt;, I implemented a DifferentiateWithTime operator, which was implemented in terms of other RX operators, and at the end I closed out by saying “It does say in section 6.1 of the &lt;a href="http://blogs.msdn.com/b/rxteam/archive/2010/10/28/rx-design-guidelines.aspx"&gt;RX Design Guidelines&lt;/a&gt; that new operators should be composed of existing operators, unless performance is a concern – this is something I may get around to investigating in a future blog post.” and thought I’d finally get around to looking at this.&lt;/p&gt;  &lt;p&gt;As a recap, the differentiate operator was written using a sliding window operator (updated to use RX 1.1 experimental):&lt;/p&gt;  &lt;pre class="csharpcode"&gt;&lt;pre class="csharpcode"&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; IObservable&amp;lt;&lt;span class="kwrd"&gt;double&lt;/span&gt;&amp;gt; DifferentiateWithTime&lt;br /&gt;            (&lt;span class="kwrd"&gt;this&lt;/span&gt; IObservable&amp;lt;&lt;span class="kwrd"&gt;double&lt;/span&gt;&amp;gt; obs)&lt;br /&gt;        {&lt;br /&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; (from j &lt;span class="kwrd"&gt;in&lt;/span&gt; obs.SlidingWindow(2) &lt;br /&gt;                    select j[1] - j[0]);&lt;br /&gt;        }&lt;br /&gt;&lt;/pre&gt;&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;br /&gt;&lt;/style&gt;&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;br /&gt;&lt;/style&gt;&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;br /&gt;&lt;/style&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;        &lt;span class="rem"&gt;// from http://social.msdn.microsoft.com/Forums/en-US/rx/thread/37428f58-f241-45b3-a878-c1627deb9ac4#bcdc7b79-bbde-4145-88e4-583685285682&lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; IObservable&amp;lt;IList&amp;lt;TSource&amp;gt;&amp;gt; &lt;br /&gt;            SlidingWindow&amp;lt;TSource&amp;gt;(&lt;span class="kwrd"&gt;this&lt;/span&gt; IObservable&amp;lt;TSource&amp;gt; source,&lt;br /&gt;            &lt;span class="kwrd"&gt;int&lt;/span&gt; count)&lt;br /&gt;        {&lt;br /&gt;            Contract.Requires(source != &lt;span class="kwrd"&gt;null&lt;/span&gt;);&lt;br /&gt;            Contract.Requires(count &amp;gt;= 0);&lt;br /&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; source.Publish(published =&amp;gt;&lt;br /&gt;                from x &lt;span class="kwrd"&gt;in&lt;/span&gt; published&lt;br /&gt;                from buffer &lt;span class="kwrd"&gt;in&lt;/span&gt; published.StartWith(x).Buffer(count).Take(1)&lt;br /&gt;                &lt;span class="kwrd"&gt;where&lt;/span&gt; buffer.Count == count&lt;br /&gt;                select buffer&lt;br /&gt;            );&lt;br /&gt;        }&lt;/pre&gt;&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;br /&gt;&lt;/style&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;I created a simple test fixture to exercise this.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;            &lt;span class="kwrd"&gt;const&lt;/span&gt; &lt;span class="kwrd"&gt;double&lt;/span&gt; accelerationGravity = 9.81;&lt;br /&gt;            var count = 10000.0;&lt;br /&gt;            var positions = Observable.Generate(0.0,&lt;br /&gt;                i =&amp;gt; i &amp;lt; count,&lt;br /&gt;                i =&amp;gt; i + 1.0,&lt;br /&gt;                i =&amp;gt; accelerationGravity * i * i / 2.0);&lt;br /&gt;&lt;br /&gt;            var stopwatch = &lt;span class="kwrd"&gt;new&lt;/span&gt; Stopwatch();&lt;br /&gt;            stopwatch.Start();&lt;br /&gt;&lt;br /&gt;            var velocity = positions.DifferentiateWithTime();&lt;br /&gt;&lt;br /&gt;            var sumVelocity = 0.0;&lt;br /&gt;            &lt;span class="kwrd"&gt;using&lt;/span&gt; (velocity.Subscribe(i =&amp;gt;&lt;br /&gt;                            { sumVelocity += i; })) { };&lt;br /&gt;&lt;br /&gt;            var acceleration = velocity.DifferentiateWithTime();&lt;br /&gt;&lt;br /&gt;            var sumAcceleration = 0.0;&lt;br /&gt;            &lt;span class="kwrd"&gt;using&lt;/span&gt; (acceleration.Subscribe(i =&amp;gt;&lt;br /&gt;                                { sumAcceleration += i; })) { };&lt;br /&gt;&lt;br /&gt;            stopwatch.Stop();&lt;br /&gt;&lt;br /&gt;            Console.WriteLine(&lt;span class="str"&gt;&amp;quot;sumVel:{0} sumAcc:{1} time:{2}&amp;quot;&lt;/span&gt;, &lt;br /&gt;                sumVelocity,&lt;br /&gt;                sumAcceleration,&lt;br /&gt;                stopwatch.ElapsedMilliseconds);&lt;/pre&gt;&lt;br /&gt;&lt;style type="text/css"&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;style type="text/css"&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Monitoring in Perfmon, I saw that during the 10000 iterations there were 170 Generation 0 garbage collections, which does seem quite overkill. The total time was 6580 milliseconds.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;I then implemented the DifferentiateWithTime operator directly:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; IObservable&amp;lt;&lt;span class="kwrd"&gt;double&lt;/span&gt;&amp;gt; DifferentiateWithTime&lt;br /&gt;            (&lt;span class="kwrd"&gt;this&lt;/span&gt; IObservable&amp;lt;&lt;span class="kwrd"&gt;double&lt;/span&gt;&amp;gt; obs)&lt;br /&gt;        {&lt;br /&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; Observable.Create&amp;lt;&lt;span class="kwrd"&gt;double&lt;/span&gt;&amp;gt;(o =&amp;gt;&lt;br /&gt;            {&lt;br /&gt;                &lt;span class="kwrd"&gt;double&lt;/span&gt; previousValue = 0.0;&lt;br /&gt;                var initialized = &lt;span class="kwrd"&gt;false&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;                &lt;span class="kwrd"&gt;return&lt;/span&gt; obs.Subscribe(i =&amp;gt;&lt;br /&gt;                {&lt;br /&gt;                    &lt;span class="kwrd"&gt;if&lt;/span&gt; (initialized)&lt;br /&gt;                    {&lt;br /&gt;                        o.OnNext(i - previousValue);&lt;br /&gt;                    }&lt;br /&gt;                    &lt;span class="kwrd"&gt;else&lt;/span&gt;&lt;br /&gt;                    {&lt;br /&gt;                        initialized = &lt;span class="kwrd"&gt;true&lt;/span&gt;;&lt;br /&gt;                    }&lt;br /&gt;                    previousValue = i;&lt;br /&gt;                });&lt;br /&gt;            });&lt;br /&gt;        }&lt;/pre&gt;&lt;br /&gt;&lt;style type="text/css"&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;style type="text/css"&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;This time there were only 4 Generation 0 garbage collections during the test run, and even more excitingly, the execution time was 379 milliseconds. A 17 time speedup is quite impressive, and shows that it’s worth being careful!&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;It’s likely that other operations based on sliding windows (rolling averages, VWAP etc) may have similar issues, and may benefit from similar observations. Instead of forcing the user to manually make these changes, it may be possible to do this in a more automated way (I’ve got some ideas I’ll play with when I get time.)&lt;/p&gt;&lt;br /&gt;&lt;style type="text/css"&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/848045343259901678-1178897998054595150?l=taumuon-jabuka.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://taumuon-jabuka.blogspot.com/feeds/1178897998054595150/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=848045343259901678&amp;postID=1178897998054595150' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/848045343259901678/posts/default/1178897998054595150'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/848045343259901678/posts/default/1178897998054595150'/><link rel='alternate' type='text/html' href='http://taumuon-jabuka.blogspot.com/2011/07/rx-framework-performance-awareness.html' title='RX Framework Performance Awareness'/><author><name>Taumuon</name><uri>http://www.blogger.com/profile/12874508418428339669</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='27' src='http://bp0.blogger.com/_9XuDfr9pYU0/SGAMUjxkNvI/AAAAAAAAACs/YhujEd8cyMk/S220/colloseum.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-848045343259901678.post-3161597370538701899</id><published>2011-07-06T12:40:00.001-07:00</published><updated>2012-01-21T06:35:01.593-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='.NET'/><category scheme='http://www.blogger.com/atom/ns#' term='GPGPU'/><title type='text'>Quick Play with the AMD Stream SDK</title><content type='html'>&lt;p&gt;I mentioned in my last blog post that I was disappointed with the performance of Microsoft Accelerator, and wanted to play around with Brahma. I was going to do this sooner, but have been side-tracked with playing around with XNA on Windows Phone 7.&lt;/p&gt;  &lt;p&gt;I downloaded the latest OpenCL version of &lt;a href="http://blog.ananthonline.net/?p=42"&gt;Brahma&lt;/a&gt;, but had trouble with the nested loops and aggregation operations (force summations), so didn't get as far as I'd hoped. It’s a shame, as the concept of LINQ to GPU is a great one.&lt;/p&gt;  &lt;p&gt;I then took a look at running the OpenCL NBody simulation from the Stream SDK. I couldn't get the simulation to run using the GPU despite trying various Catalyst versions, it failed with a runtime error message &amp;quot;This OpenCL build requires verison 1.4.879, version 1.4.696 installed&amp;quot;, but in spite of this, I was impressed with the performance of using the Stream SDK, even running on the CPU.&lt;/p&gt;  &lt;p&gt;Whereas my managed CPU-version of the nbody simulation achieved 5 fps (frames per second), (or 8 fps with the drawing disabled – as discussed earlier the WPF drawing code is slow), drawing 2000 bodies, the OpenCL SDK ran at 25 fps drawing 2048 bodies, i.e. a factor of 5 speedup. I didn't bother to parallelise my code but the theoretical maximum speedup on my dual core machine would obviously be a factor of 2, so that's a factor of 2.5 speedup using the Stream SDK on the same hardware.&lt;/p&gt;  &lt;p&gt;I switched the Stream SDK NBody example to use the nBodyCPUReference() method to see whether it's slow because of the difference between managed and native code, and it runs at 5 fps compiled native on the CPU, i.e. in the same ballpark as the managed version. As it's not running on the GPU, the Stream version must be faster than the vanilla C++ version because it's making use of the processor's vector hardware, but I can't be bothered to manually code the SSE intrinsics to see if that's the case (but it might be cool to play around with &lt;a href="http://tirania.org/blog/archive/2008/Nov-03.html"&gt;Mono.SIMD&lt;/a&gt; if I get time).&lt;/p&gt;  &lt;p&gt;Oh, I suppose I should talk about how the code looks - the guts of the algorithm doesn't look much different between the vanilla C++ and the OpenCL version, but there is a lot of hideous boilerplate/setup code different between the two. This is why it'd be great to get a workable managed library to hide all this (alternately, it'll be interesting to see whether &lt;a href="https://blogs.msdn.com/b/vcblog/archive/2011/06/15/introducing-amp.aspx"&gt;C++ AMP&lt;/a&gt; abstracts away the OpenCL/DirectCompute complexity).&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/848045343259901678-3161597370538701899?l=taumuon-jabuka.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://taumuon-jabuka.blogspot.com/feeds/3161597370538701899/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=848045343259901678&amp;postID=3161597370538701899' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/848045343259901678/posts/default/3161597370538701899'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/848045343259901678/posts/default/3161597370538701899'/><link rel='alternate' type='text/html' href='http://taumuon-jabuka.blogspot.com/2011/07/quick-play-with-amd-stream-sdk.html' title='Quick Play with the AMD Stream SDK'/><author><name>Taumuon</name><uri>http://www.blogger.com/profile/12874508418428339669</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='27' src='http://bp0.blogger.com/_9XuDfr9pYU0/SGAMUjxkNvI/AAAAAAAAACs/YhujEd8cyMk/S220/colloseum.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-848045343259901678.post-1568619673509195085</id><published>2011-04-23T08:04:00.000-07:00</published><updated>2012-01-21T06:35:01.594-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='.NET'/><category scheme='http://www.blogger.com/atom/ns#' term='GPGPU'/><title type='text'>GPGPU–playing with Microsoft Accelerator</title><content type='html'>It’s probably being screamingly obvious to some readers that the boids simulations I’ve been playing with are embarassingly parallel, so I thought I’d have a quick play.&lt;br /&gt;&lt;br /&gt;I’ve been reading around about OpenCL and CUDA, but as there’s a Microsoft library with a .NET API for easily programming the GPU, I thought I’d have a play with &lt;a href="http://research.microsoft.com/en-us/projects/accelerator/"&gt;Accelerator&lt;/a&gt; (another interesting .NET GPGPU library is &lt;a href="http://brahma.ananthonline.net/"&gt;Brahma&lt;/a&gt; – I might get around to playing with that one day). Accelerator is higher-level, no need to worry about the low-levels of the GPU memory management.&lt;br /&gt;&lt;br /&gt;I decided to play with a simpler example than the boids, to focus on the technology instead of the problem domain. I chose to look at the all-pairs NBody simulation (see more info &lt;a href="http://http.developer.nvidia.com/GPUGems3/gpugems3_ch31.html"&gt;here&lt;/a&gt;).&lt;br /&gt;&lt;br /&gt;I quickly coded up a simple example using 1000 bodies. The CPU was able to draw at approx 15 frames per second (I didn’t bother parallelising the simulation on the CPU, as I was hoping for an order of magnitude increase in speed on the GPU). WPF is incredibly slow in drawing, and I found (unexpectedly) that using DrawingVisuals to be even slower. For that reason, I’m only drawing 100 bodies, but all of them are included in the simulation. I was intending to reduce the bottleneck by using Direct2D, and then getting Accelerator to write out to texture memory to save transferring data over the bus.&lt;br /&gt;&lt;br /&gt;I didn’t get the results I expected when using Accelerator – I first began by converting the main simulation routine (integration of positions) onto the GPU, and left the all-body force calculation on the CPU. I was surprised to find the simulation slower – I was hitting frame rates of 10 fps.&lt;br /&gt;&lt;br /&gt;I guessed that maybe it was maybe transferring too much data between the CPU and GPU, so I then moved onto the force calculation. I was very surprised to find that this made the simulation orders of magnitude slower (i.e. hitting frame rates &amp;lt; 0.01 fps). I profiled this to find that the majority of the time was spent in CompileShader. This isn’t so surprising – I was building up the same calculation for each body, for each frame.&lt;br /&gt;&lt;br /&gt;Following the advice in the Accelerator Programmers Guide, I then moved onto using Parameter Objects. This means that it’s able to use the same computation graph with different input data. This did help, but only by an order of magnitude. It’s still not approaching anywhere near real-time frame rates.&lt;br /&gt;&lt;br /&gt;I can’t remember where I read it, but I read that it’s recommended using input data sizes of the order 1e6 elements to overcome the overhead of transferring data to and from the GPU. This does make sense, but I was expecting to be at least getting interactive frame rates (as the OpenCL simulations are obtaining). It may be that Accelerator is faster than the CPU with a large number of elements, but it may be that e.g. instead of rendering a frame in an hour, it takes five minutes. It doesn’t seem to be suitable for interactive simulations.&lt;br /&gt;&lt;br /&gt;This could be a simple case of user-error. I’ve got the code available on &lt;a href="http://www.taumuon.co.uk/jabuka/FSharpNBody.zip"&gt;taumuon&lt;/a&gt;. If I’m missing something obvious, or you can get faster frame rates than the CPU, please post in the comments&lt;br /&gt;&lt;br /&gt;(As I’m discussing performance I guess I should disclose the software and hardware specs. Running Windows x64, on a HP DV3 laptop – 4GB ram, dual core Pentium P6100, ATI Radeon 5470).&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/848045343259901678-1568619673509195085?l=taumuon-jabuka.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://taumuon-jabuka.blogspot.com/feeds/1568619673509195085/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=848045343259901678&amp;postID=1568619673509195085' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/848045343259901678/posts/default/1568619673509195085'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/848045343259901678/posts/default/1568619673509195085'/><link rel='alternate' type='text/html' href='http://taumuon-jabuka.blogspot.com/2011/04/gpgpuplaying-with-microsoft-accelerator.html' title='GPGPU–playing with Microsoft Accelerator'/><author><name>Taumuon</name><uri>http://www.blogger.com/profile/12874508418428339669</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='27' src='http://bp0.blogger.com/_9XuDfr9pYU0/SGAMUjxkNvI/AAAAAAAAACs/YhujEd8cyMk/S220/colloseum.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-848045343259901678.post-8610197978261099741</id><published>2011-03-29T14:54:00.001-07:00</published><updated>2012-01-21T06:35:01.594-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='.NET'/><category scheme='http://www.blogger.com/atom/ns#' term='F#'/><category scheme='http://www.blogger.com/atom/ns#' term='Flocking'/><category scheme='http://www.blogger.com/atom/ns#' term='Boids'/><title type='text'>F# Flocking (boids)</title><content type='html'>&lt;p&gt;A couple of years ago I was blogging about some simple boid behaviour I had implemented in C# (&lt;a href="http://taumuon-jabuka.blogspot.com/2008/11/faster-boids.html"&gt;here&lt;/a&gt;). I came across this paper which had some interesting ideas I wanted to try out: &amp;quot;Self-organised complex aerial displays of thousands of starlings: a model&amp;quot; by H. Hildenbrandt, C. Carere, C-K. Hemelrijk. &lt;/p&gt;  &lt;p&gt;As I'm getting more into F#, I thought this would be a simple reasonable sized application to create to get a feel for how F# works on a slightly larger project (though definitely not large-scale). &lt;/p&gt;  &lt;p&gt;Before taking a look at the code, I'll quickly describe which parts of the paper I implemented. &lt;/p&gt;  &lt;p&gt;The video generated is available on &lt;a href="http://www.youtube.com/watch?v=eil5K7Ir3i8"&gt;http://www.youtube.com/watch?v=eil5K7Ir3i8&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;I haven't implemented all of the ideas in the paper, so my simple application doesn't exhibit the same realistic flocking behaviour, but it does have more realistic behaviour than my earlier efforts. The interesting behaviours were the desire to not stray too far from the roost, the attempt to maintain a steady cruise speed, and to use the nearest seven topological neighbours for cohesion and alignment. I implemented the simulation in 2D for simplicity. &lt;/p&gt;  &lt;p&gt;I hardcoded a perception radius instead of implementing the continual adjustment of the perception radius for seven topological neighbours. I also have totally omitted the flight dynamic simulation (no gravity, no banked turns). The paper discusses that the banked turns gives a further air of realism to the simulation.&amp;#160;&amp;#160; &lt;/p&gt;  &lt;h3&gt;The coding&lt;/h3&gt;  &lt;p&gt;This was a real pleasure to implement in F# - a lot less time was thinking about application design, classes and their interactions, the logic in the paper was easy to transfer directly to the keyboard. F# is very compact, so the core logic can be seen on just a couple of screens. &lt;/p&gt;  &lt;p&gt;I didn't feel the lack of intellisense too much when developing the core algorithm, I did miss the ability to refactor functions and navigate to usages. I especially missed intellisense when developing the WPF visualisation part of the app, when interacting with .NET objects; I did miss the ability for Visual Studio to automatically add the required namespaces. I must have being spoilt by Visual Studio and Resharper for too long! &lt;/p&gt;  &lt;p&gt;The actual WPF application wasn’t such a good fit for F# – there’s no generated code behind file for the XAML, and I feel that using F# would be painful in a WPF or Silverlight application (but just for the view, it should be OK for the ViewModels down). &lt;/p&gt;  &lt;p&gt;I implemented a F# Vector type, which can be specified with units of measure (or none). I used units of measure throughout – this was really powerful, and did let me quickly find a few bugs in the implementation.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;(NOTE: I still haven’t found a code websnippet tool I’m happy with – you need to click in each of the regions below and scroll down and right to see the whole code). &lt;/strong&gt;Alternately, the zipped up solution can be downloaded from &lt;a href="http://www.taumuon.co.uk/jabuka/FSharpFlock.zip"&gt;http://www.taumuon.co.uk/jabuka/FSharpFlock.zip&lt;/a&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Vector3D.fs:&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:95741918-1038-4e1a-96a4-319c8e9735ea" class="wlWriterEditableSmartContent"&gt;&lt;pre class="brush: csharp; gutter: false; first-line: 1; tab-size: 4;  toolbar: false;  width: 894px; height: 355px;" style=" width: 894px; height: 355px;overflow: auto;"&gt;module Vector3D&lt;br /&gt;&lt;br /&gt;type Vector3D&amp;lt;[&amp;lt;Measure&amp;gt;] 'u&amp;gt;(x : float&amp;lt;'u&amp;gt;, y : float&amp;lt;'u&amp;gt;, z : float&amp;lt;'u&amp;gt;) = &lt;br /&gt;    static member Zero() = Vector3D&amp;lt;_&amp;gt;(0.0&amp;lt;_&amp;gt;, 0.0&amp;lt;_&amp;gt;, 0.0&amp;lt;_&amp;gt;)&lt;br /&gt;&lt;br /&gt;    member v.X = x&lt;br /&gt;    member v.Y = y&lt;br /&gt;    member v.Z = z&lt;br /&gt;&lt;br /&gt;    static member (+) (lhs:Vector3D&amp;lt;_&amp;gt;, rhs:Vector3D&amp;lt;_&amp;gt;) =&lt;br /&gt;      Vector3D(lhs.X + rhs.X, lhs.Y + rhs.Y, lhs.Z + rhs.Z)&lt;br /&gt;&lt;br /&gt;    static member (-) (lhs:Vector3D&amp;lt;_&amp;gt;, rhs:Vector3D&amp;lt;_&amp;gt;) =&lt;br /&gt;      Vector3D(lhs.X - rhs.X, lhs.Y - rhs.Y, lhs.Z - rhs.Z)&lt;br /&gt;&lt;br /&gt;    static member (*) (v:Vector3D&amp;lt;_&amp;gt;, a:float&amp;lt;_&amp;gt;) =&lt;br /&gt;      Vector3D(v.X * a, v.Y * a, v.Z * a)&lt;br /&gt;&lt;br /&gt;    static member (*) (a:float&amp;lt;_&amp;gt;, v:Vector3D&amp;lt;_&amp;gt;) =&lt;br /&gt;      Vector3D(a * v.X, a * v.Y, a * v.Z)&lt;br /&gt;      &lt;br /&gt;    static member (/) (v:Vector3D&amp;lt;_&amp;gt;, a) =&lt;br /&gt;      Vector3D(v.X / a, v.Y / a, v.Z / a)&lt;br /&gt;&lt;br /&gt;    member v.DotProduct(rhs:Vector3D&amp;lt;_&amp;gt;) = (v.X * rhs.X) + (v.Y * rhs.Y) + (v.Z * rhs.Z)&lt;br /&gt;&lt;br /&gt;    member v.magnitude = sqrt(v.DotProduct(v)) * 1.0&amp;lt;_&amp;gt;&lt;br /&gt;&lt;br /&gt;    member lhs.CrossProduct(rhs:Vector3D&amp;lt;_&amp;gt;) =&lt;br /&gt;      Vector3D((lhs.Y * rhs.Z - lhs.Z * rhs.Y) * 1.0&amp;lt;_&amp;gt;,&lt;br /&gt;          (-lhs.X * rhs.Z + lhs.Z * rhs.X) * 1.0&amp;lt;_&amp;gt;,&lt;br /&gt;          (lhs.X * rhs.Y - lhs.Y * rhs.X) * 1.0&amp;lt;_&amp;gt;)&lt;br /&gt;&lt;br /&gt;    member v.normalise = &lt;br /&gt;      let magnitude = float v.magnitude&lt;br /&gt;      Vector3D&amp;lt;_&amp;gt;((v.X / magnitude), (v.Y / magnitude), (v.Z / magnitude))&lt;br /&gt;&lt;br /&gt;let sumVectors(vectors : Vector3D&amp;lt;_&amp;gt;[]) = &lt;br /&gt;  let initial = Vector3D&amp;lt;_&amp;gt;(0.0&amp;lt;_&amp;gt;, 0.0&amp;lt;_&amp;gt;, 0.0&amp;lt;_&amp;gt;)&lt;br /&gt;  Array.fold (+) initial vectors&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;&lt;br /&gt;&lt;style type="text/css"&gt;&lt;br /&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;BoidUtils.fs:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:e884a38e-2a03-4eab-b764-39586bb23736" class="wlWriterEditableSmartContent"&gt;&lt;pre class="brush: csharp; gutter: false; first-line: 1; tab-size: 4;  toolbar: false;  width: 894px; height: 355px;" style=" width: 894px; height: 355px;overflow: auto;"&gt;module BoidUtils&lt;br /&gt;&lt;br /&gt;open Microsoft.FSharp.Math&lt;br /&gt;open Vector3D&lt;br /&gt;open SI&lt;br /&gt;&lt;br /&gt;let radiusRoost = 150.0&amp;lt;m&amp;gt;&lt;br /&gt;let hardRadius = 2.0&amp;lt;m&amp;gt; // 0.2&amp;lt;m&amp;gt;&lt;br /&gt;let mass = 0.08&amp;lt;kg&amp;gt;&lt;br /&gt;let timeStep = 0.005&amp;lt;s&amp;gt;&lt;br /&gt;let relaxationTime = 0.05&amp;lt;s&amp;gt;&lt;br /&gt;let cruiseSpeed = 20.0&amp;lt;m/s&amp;gt;&lt;br /&gt;let horizontalRoostWeighting = 0.01&amp;lt;N/m&amp;gt;&lt;br /&gt;let weightingAlignment = 0.5&amp;lt;kg * s^-2&amp;gt;&lt;br /&gt;let weightingCohesion = 1.0&amp;lt;kg s^-2&amp;gt;&lt;br /&gt;let weightingSeparation = 2.0&lt;br /&gt;let perceptionRadius = 50.0&amp;lt;m&amp;gt;&lt;br /&gt;&lt;br /&gt;type BodyAxes =&lt;br /&gt;  { Forward:Vector3D&amp;lt;1&amp;gt;;&lt;br /&gt;    Side:Vector3D&amp;lt;1&amp;gt;;&lt;br /&gt;    Up:Vector3D&amp;lt;1&amp;gt; }&lt;br /&gt;&lt;br /&gt;type Boid = &lt;br /&gt;  { Position:Vector3D&amp;lt;m&amp;gt;;&lt;br /&gt;    Speed:float&amp;lt;m / s&amp;gt;;&lt;br /&gt;    Orientation:BodyAxes; }&lt;br /&gt;&lt;br /&gt;// All parameterless functions are evaluated once, just on module opening, so pass random in.&lt;br /&gt;let InitialiseRandomPosition(rand:System.Random) = &lt;br /&gt;  Vector3D&amp;lt;m&amp;gt;((300.0 * (rand.NextDouble()-0.5)) * 1.0&amp;lt;m&amp;gt;,&lt;br /&gt;             ((300.0 * (rand.NextDouble()-0.5)) * 1.0&amp;lt;m&amp;gt;), &lt;br /&gt;             0.0&amp;lt;m&amp;gt;)&lt;br /&gt;&lt;br /&gt;let InitialiseRandomVelocity(rand:System.Random) = &lt;br /&gt;  Vector3D&amp;lt;m/s&amp;gt;((100.0 * (-0.5 + rand.NextDouble()) * 1.0&amp;lt;m/s&amp;gt;),&lt;br /&gt;                (100.0 * (-0.5 + rand.NextDouble())) * 1.0&amp;lt;m/s&amp;gt;,&lt;br /&gt;                0.0&amp;lt;m/s&amp;gt;)&lt;br /&gt;&lt;br /&gt;let InitialiseRandomOrientation(rand:System.Random) = &lt;br /&gt;  {Forward=Vector3D(0.0, 1.0, 0.0);&lt;br /&gt;   Side=Vector3D(1.0, 0.0, 0.0);&lt;br /&gt;   Up=Vector3D(0.0, 0.0, 1.0)}&lt;br /&gt;&lt;br /&gt;let setOrientation(oldOrientation:BodyAxes, velocity:Vector3D&amp;lt;m/s&amp;gt;) =&lt;br /&gt;  let normalisedVelocity = velocity.normalise&lt;br /&gt;  let y = normalisedVelocity.CrossProduct(Vector3D&amp;lt;m / s&amp;gt;(0.0&amp;lt;m/s&amp;gt;, 0.0&amp;lt;m/s&amp;gt;, 1.0&amp;lt;m/s&amp;gt;))&lt;br /&gt;  {oldOrientation with Forward=normalisedVelocity * 1.0&amp;lt;m^-1 s&amp;gt;; Side=y*1.0&amp;lt;m^-2 s^2&amp;gt;}&lt;br /&gt;&lt;br /&gt;let calculateCruiseSpeedForce (boid:Boid) =&lt;br /&gt;  (mass / relaxationTime) * (cruiseSpeed - boid.Speed) * boid.Orientation.Forward&lt;br /&gt;&lt;br /&gt;let calculateRoostForce (boid:Boid) =&lt;br /&gt;  let horizontalPosition = Vector3D(boid.Position.X, boid.Position.Y, 0.0&amp;lt;_&amp;gt;)&lt;br /&gt;  let distanceFromOrigin = horizontalPosition.magnitude&lt;br /&gt;  match (distanceFromOrigin) with&lt;br /&gt;  | _ when distanceFromOrigin &amp;lt; radiusRoost -&amp;gt; Vector3D&amp;lt;N&amp;gt;(0.0&amp;lt;N&amp;gt;, 0.0&amp;lt;N&amp;gt;, 0.0&amp;lt;N&amp;gt;)&lt;br /&gt;  | _ -&amp;gt; let normalRoostingArea = horizontalPosition.normalise&lt;br /&gt;         let d = boid.Orientation.Forward.DotProduct normalRoostingArea&lt;br /&gt;         let distanceFromRoost = distanceFromOrigin - radiusRoost&lt;br /&gt;         let orientationRoostDotProduct = boid.Orientation.Side.DotProduct normalRoostingArea&lt;br /&gt;         let weightingFactor = match (orientationRoostDotProduct) with&lt;br /&gt;                               | _ when orientationRoostDotProduct &amp;gt; 0.0&amp;lt;m&amp;gt; -&amp;gt; -1.0&lt;br /&gt;                               | _ -&amp;gt; 1.0&lt;br /&gt;         weightingFactor * (radiusRoost * horizontalRoostWeighting * (0.5 + (0.5&amp;lt;m^-1&amp;gt; * d)) * (boid.Orientation.Side))&lt;br /&gt;&lt;br /&gt;let findDistanceBetweenBoids(boid:Boid, other:Boid) = &lt;br /&gt;  (boid.Position - other.Position).magnitude&lt;br /&gt;&lt;br /&gt;let findNearestNeighbours(boid:Boid, boids:Boid[]) =&lt;br /&gt;  let sortedByDistance = boids |&amp;gt; Array.sortBy(fun other -&amp;gt; findDistanceBetweenBoids(boid, other))&lt;br /&gt;  Array.sub sortedByDistance 0 7&lt;br /&gt;&lt;br /&gt;let findAverageForwardDirectionDifference(boid:Boid, boids:Boid[]) = &lt;br /&gt;  let differences = boids |&amp;gt; Array.map (fun i -&amp;gt; 1.0&amp;lt;m&amp;gt; * (i.Orientation.Forward - boid.Orientation.Forward))&lt;br /&gt;  let sumDifferences = sumVectors(differences)&lt;br /&gt;  (1.0 / (float sumDifferences.magnitude)) * sumDifferences&lt;br /&gt;&lt;br /&gt;let calculateAlignmentForce(boid:Boid, nearest:Boid[]) =&lt;br /&gt;  let averageDifference = findAverageForwardDirectionDifference(boid, nearest)&lt;br /&gt;  weightingAlignment * averageDifference&lt;br /&gt;&lt;br /&gt;let findAveragePosition(boid:Boid, boids:Boid[]) =&lt;br /&gt;  let positions = boids |&amp;gt; Array.map (fun i -&amp;gt; i.Position)&lt;br /&gt;  let sumPositions = sumVectors(positions)&lt;br /&gt;  (1.0 / float boids.Length) * sumPositions&lt;br /&gt;&lt;br /&gt;let findNeighboursInRadius(boid:Boid, boids:Boid[], radius:float&amp;lt;m&amp;gt;) =&lt;br /&gt;  boids |&amp;gt; Array.filter(fun other -&amp;gt; other &amp;lt;&amp;gt; boid &amp;amp;&amp;amp; findDistanceBetweenBoids(boid, other) &amp;lt;= radius)&lt;br /&gt;&lt;br /&gt;let calculateCentrality(boid:Boid, boids:Boid[]) = &lt;br /&gt;  let separations = boids |&amp;gt; Array.map(fun i -&amp;gt; (i.Position - boid.Position).normalise)&lt;br /&gt;  let sumSeparations = sumVectors(separations)&lt;br /&gt;  let count = boids.Length&lt;br /&gt;  match (count) with&lt;br /&gt;  | 0 -&amp;gt; 1.0&lt;br /&gt;  | _ -&amp;gt; (1.0 / float count) * (sumSeparations.magnitude / 1.0&amp;lt;m&amp;gt;)&lt;br /&gt;&lt;br /&gt;let calculateCohesionForce(boid:Boid, nearest:Boid[], boidsInPerceptionRadius:Boid[]) =&lt;br /&gt;  let boidsOutsideHardRadius = nearest |&amp;gt; Array.filter(fun i -&amp;gt; abs ((boid.Position - i.Position).magnitude) &amp;gt; hardRadius)&lt;br /&gt;  let centrality = calculateCentrality(boid, boidsInPerceptionRadius)&lt;br /&gt;  let averagePosition = findAveragePosition(boid, nearest)&lt;br /&gt;  centrality * weightingCohesion * (averagePosition - boid.Position)&lt;br /&gt;&lt;br /&gt;let calculateSeparationForce(boid:Boid, boidsInPerceptionRadius:Boid[]) = &lt;br /&gt;  let nearest = boidsInPerceptionRadius&lt;br /&gt;  let separations = nearest |&amp;gt; Array.map(fun i -&amp;gt; i.Position - boid.Position)&lt;br /&gt;  let sigma = 1.8&lt;br /&gt;  let forcesToNeighbours = separations |&amp;gt; Array.map(fun i -&amp;gt; &lt;br /&gt;    let magnitude = i.magnitude&lt;br /&gt;    let multiplier = &lt;br /&gt;        match (magnitude) with&lt;br /&gt;        | _ when magnitude &amp;lt; hardRadius -&amp;gt; 1.0&lt;br /&gt;        | _ -&amp;gt; System.Math.Exp(-((magnitude - hardRadius)*(magnitude - hardRadius)/1.0&amp;lt;m^2&amp;gt;) / (sigma * sigma))&lt;br /&gt;    multiplier * magnitude * (i.normalise) * 1.0&amp;lt;kg * m^-1 * s^-2&amp;gt;)&lt;br /&gt;  let sumForces = sumVectors(forcesToNeighbours)&lt;br /&gt;  match (nearest.Length) with&lt;br /&gt;    | _ when (nearest.Length) = 0 -&amp;gt; Vector3D&amp;lt;N&amp;gt;.Zero()&lt;br /&gt;    | _ -&amp;gt; (-weightingSeparation / float nearest.Length) * sumForces&lt;br /&gt;&lt;br /&gt;let calculateSocialForce(boid:Boid, boids:Boid[]) =&lt;br /&gt;  let nearest = findNearestNeighbours(boid, boids)&lt;br /&gt;  let boidsInPerceptionRadius = findNeighboursInRadius(boid, boids, perceptionRadius)&lt;br /&gt;  calculateAlignmentForce(boid, nearest)&lt;br /&gt;  + calculateCohesionForce(boid, nearest, boidsInPerceptionRadius)&lt;br /&gt;  + calculateSeparationForce(boid, boidsInPerceptionRadius)&lt;br /&gt;&lt;br /&gt;let calculateForce (boid:Boid, boids:Boid[]) =&lt;br /&gt;  (boid |&amp;gt; calculateRoostForce)&lt;br /&gt;  + (boid |&amp;gt; calculateCruiseSpeedForce) &lt;br /&gt;  + (calculateSocialForce(boid, boids))&lt;br /&gt;&lt;br /&gt;let iterateBoid (boid:Boid, boids:Boid[]) = &lt;br /&gt;  let originalPosition = boid.Position&lt;br /&gt;  let originalVelocity = boid.Speed * boid.Orientation.Forward&lt;br /&gt;  let force = calculateForce(boid, boids)&lt;br /&gt;  let acceleration = force/mass&lt;br /&gt;  let velocity = originalVelocity + (acceleration * timeStep)&lt;br /&gt;  let position = originalPosition + (velocity * timeStep)&lt;br /&gt;  let newOrientation = setOrientation(boid.Orientation, velocity)&lt;br /&gt;  {Position=position;Speed=velocity.magnitude;Orientation=newOrientation}&lt;br /&gt;    &lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&amp;#160;&lt;/pre&gt;&lt;br /&gt;&lt;style type="text/css"&gt;&lt;br /&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;Program.fs: &lt;br /&gt;&lt;br /&gt;&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:4ba03d06-995d-4b99-b750-33c58fca6b39" class="wlWriterEditableSmartContent"&gt;&lt;pre class="brush: csharp; gutter: false; first-line: 1; tab-size: 4;  toolbar: false;  width: 894px; height: 355px;" style=" width: 894px; height: 355px;overflow: auto;"&gt;module Boids&lt;br /&gt;&lt;br /&gt;open SI&lt;br /&gt;open Vector3D&lt;br /&gt;open BoidUtils&lt;br /&gt;&lt;br /&gt;open System&lt;br /&gt;open System.IO&lt;br /&gt;open System.Windows&lt;br /&gt;open System.Windows.Threading&lt;br /&gt;open System.Windows.Controls&lt;br /&gt;open System.Windows.Shapes&lt;br /&gt;open System.Windows.Media&lt;br /&gt;open System.Windows.Media.Imaging&lt;br /&gt;&lt;br /&gt;let window = Application.LoadComponent(new System.Uri(&amp;quot;/FSharpFlock;component/MainWindow.xaml&amp;quot;,&lt;br /&gt;                                           System.UriKind.Relative)) :?&amp;gt; Window&lt;br /&gt;&lt;br /&gt;let rand = new System.Random()&lt;br /&gt;let viewWidth = 480.0&lt;br /&gt;let viewHeight = 360.0&lt;br /&gt;&lt;br /&gt;let mutable frameCount = 0&lt;br /&gt;let mutable boids =&lt;br /&gt;  [|for i in 0 .. 300 -&amp;gt;&lt;br /&gt;    let position = InitialiseRandomPosition(rand)&lt;br /&gt;    let velocity = InitialiseRandomVelocity(rand)&lt;br /&gt;    let tempOrientation = {Forward=Vector3D(0.0, 1.0, 0.0);&lt;br /&gt;                           Side=Vector3D(1.0, 0.0, 0.0);&lt;br /&gt;                           Up=Vector3D(0.0, 0.0, 1.0)}&lt;br /&gt;    let orientation = setOrientation(tempOrientation, velocity)&lt;br /&gt;    {Position = position; Speed=velocity.magnitude; Orientation=orientation}&lt;br /&gt;    |];;&lt;br /&gt;&lt;br /&gt;let updateBoids(boids) =&lt;br /&gt;  boids |&amp;gt; Array.map (fun boid -&amp;gt; iterateBoid(boid, boids))&lt;br /&gt;&lt;br /&gt;let (?) (fe:FrameworkElement) name : 'T =&lt;br /&gt;  fe.FindName(name) :?&amp;gt; 'T&lt;br /&gt;&lt;br /&gt;let GetRotationForBoid(boid:Boid) =&lt;br /&gt;  let forward = boid.Orientation.Forward&lt;br /&gt;  let angleRadians = Math.Atan2(forward.X, forward.Y)&lt;br /&gt;  let angleDegrees = angleRadians * 180.0 / Math.PI&lt;br /&gt;  let rotateTransform = new Media.RotateTransform(angleDegrees, 0.0, 0.0)&lt;br /&gt;  rotateTransform&lt;br /&gt;&lt;br /&gt;let color = Media.Color.FromArgb(64uy, 255uy, 0uy, 0uy)&lt;br /&gt;&lt;br /&gt;let saveFrame(canvas : Canvas) = &lt;br /&gt;  let size = new Size(viewWidth, viewHeight)&lt;br /&gt;  canvas.Measure(size)&lt;br /&gt;  canvas.Arrange(new Rect(size))&lt;br /&gt;  let renderTargetBitmap = new RenderTargetBitmap(int viewWidth, int viewHeight, 96.0, 96.0, PixelFormats.Pbgra32)&lt;br /&gt;  let sourceBrush = new VisualBrush(canvas)&lt;br /&gt;  renderTargetBitmap.Render(canvas)&lt;br /&gt;&lt;br /&gt;  let bitmapFrame = BitmapFrame.Create(renderTargetBitmap)&lt;br /&gt;  let jpegEncoder = new JpegBitmapEncoder()&lt;br /&gt;  jpegEncoder.Frames.Add(bitmapFrame)&lt;br /&gt;  let filename = System.String.Format(&amp;quot;C:\Anim\{0:0000}.jpg&amp;quot;, frameCount)&lt;br /&gt;  use stream = new FileStream(filename, FileMode.CreateNew)&lt;br /&gt;  jpegEncoder.Save(stream)&lt;br /&gt;  &lt;br /&gt;let createBoidGraphics(boid:Boid) =&lt;br /&gt;  let obj : Polygon = new Polygon()&lt;br /&gt;  obj.Points &amp;lt;- new Media.PointCollection( [|new Point(-10.0, -5.0);&lt;br /&gt;                                             new Point(0.0, 0.0);&lt;br /&gt;                                             new Point(-10.0, 5.0);&lt;br /&gt;                                             new Point(-5.0, 0.0)|] )&lt;br /&gt;  obj.RenderTransform &amp;lt;- GetRotationForBoid(boid)&lt;br /&gt;  obj.Fill &amp;lt;- new Media.SolidColorBrush(color)&lt;br /&gt;  obj.Stroke &amp;lt;- Media.Brushes.Black&lt;br /&gt;  obj.StrokeThickness &amp;lt;- 1.0&lt;br /&gt;  obj&lt;br /&gt;&lt;br /&gt;let drawBoids() =&lt;br /&gt;  let win : Window = window&lt;br /&gt;  let canvas : Canvas = win?canvas&lt;br /&gt;  canvas.Children.Clear()&lt;br /&gt;  for i = 0 to boids.Length - 1 do&lt;br /&gt;    let graphicalBoid = createBoidGraphics(boids.[i])&lt;br /&gt;    let unitlessPosition = boids.[i].Position * 1.0&amp;lt;m^-1&amp;gt;&lt;br /&gt;    System.Windows.Controls.Canvas.SetTop(graphicalBoid, (viewHeight / 2.0) + (unitlessPosition.X))&lt;br /&gt;    System.Windows.Controls.Canvas.SetLeft(graphicalBoid, (viewWidth / 2.0) + (unitlessPosition.Y))&lt;br /&gt;    canvas.Children.Add(graphicalBoid) |&amp;gt; ignore&lt;br /&gt;  // saveFrame(canvas)&lt;br /&gt;  frameCount &amp;lt;- frameCount + 1&lt;br /&gt;&lt;br /&gt;let timer = new DispatcherTimer();&lt;br /&gt;timer.Tick.Add(fun evArgs -&amp;gt; &lt;br /&gt;  [0..5]&lt;br /&gt;  |&amp;gt; Seq.iter(fun x -&amp;gt; boids &amp;lt;- boids |&amp;gt; updateBoids)&lt;br /&gt;  drawBoids()&lt;br /&gt;  )&lt;br /&gt;timer.Interval &amp;lt;- TimeSpan.FromMilliseconds(1.0);&lt;br /&gt;&lt;br /&gt;let setup(win: Window) =&lt;br /&gt;  win.Loaded.Add(fun _ -&amp;gt; timer.Start())&lt;br /&gt;&lt;br /&gt;[&amp;lt;STAThread&amp;gt;]&lt;br /&gt;[&amp;lt;EntryPoint&amp;gt;]&lt;br /&gt;let main args =&lt;br /&gt;  setup window&lt;br /&gt;  (new Application()).Run(window) |&amp;gt; ignore&lt;br /&gt;  0&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;MainWindow.xaml:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&amp;lt;Window xmlns=&lt;span class="str"&gt;&amp;quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&amp;quot;&lt;/span&gt;&lt;br /&gt;        xmlns:x=&lt;span class="str"&gt;&amp;quot;http://schemas.microsoft.com/winfx/2006/xaml&amp;quot;&lt;/span&gt;&lt;br /&gt;        Title=&lt;span class="str"&gt;&amp;quot;F# Flock&amp;quot;&lt;/span&gt; Height=&lt;span class="str"&gt;&amp;quot;360&amp;quot;&lt;/span&gt; Width=&lt;span class="str"&gt;&amp;quot;480&amp;quot;&lt;/span&gt;&amp;gt;&lt;br /&gt;    &amp;lt;Canvas Name=&lt;span class="str"&gt;&amp;quot;canvas&amp;quot;&lt;/span&gt; HorizontalAlignment=&lt;span class="str"&gt;&amp;quot;Stretch&amp;quot;&lt;/span&gt; VerticalAlignment=&lt;span class="str"&gt;&amp;quot;Stretch&amp;quot;&lt;/span&gt; &lt;br /&gt;    Background=&lt;span class="str"&gt;&amp;quot;LightBlue&amp;quot;&lt;/span&gt;/&amp;gt;&lt;br /&gt;&amp;lt;/Window&amp;gt;&lt;/pre&gt;&lt;br /&gt;&lt;style type="text/css"&gt;&lt;br /&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;The rest of the code I hope is simple to read and understand. Coding this up I had some of the most fun programming for a while. With F#, I found I was able to concentrate on the core of the algorithm and very quickly see results. &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;For this type of project, very algorithmic, F# was a perfect fit. I'd like to get a feel for how to implement F# on a large scale project, and to see how it feels to expose the functionality via an object oriented layer. &lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/848045343259901678-8610197978261099741?l=taumuon-jabuka.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://taumuon-jabuka.blogspot.com/feeds/8610197978261099741/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=848045343259901678&amp;postID=8610197978261099741' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/848045343259901678/posts/default/8610197978261099741'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/848045343259901678/posts/default/8610197978261099741'/><link rel='alternate' type='text/html' href='http://taumuon-jabuka.blogspot.com/2011/03/f-flocking-boids.html' title='F# Flocking (boids)'/><author><name>Taumuon</name><uri>http://www.blogger.com/profile/12874508418428339669</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='27' src='http://bp0.blogger.com/_9XuDfr9pYU0/SGAMUjxkNvI/AAAAAAAAACs/YhujEd8cyMk/S220/colloseum.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-848045343259901678.post-4474369563322757845</id><published>2010-11-20T10:04:00.001-08:00</published><updated>2012-01-21T06:35:01.595-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='RX'/><category scheme='http://www.blogger.com/atom/ns#' term='.NET'/><category scheme='http://www.blogger.com/atom/ns#' term='Reactive Extensions for .NET'/><category scheme='http://www.blogger.com/atom/ns#' term='F#'/><title type='text'>F# Units of Measure with the Reactive Framework</title><content type='html'>&lt;p&gt;My last blog post dealt with manipulating incoming streams of data, such of position data, composing those streams, and manipulating them, and saw how well RX handles these operations.&lt;/p&gt;  &lt;p&gt;As I was doing this, I felt that this would benefit from the extra safety that you can get from F#’s Units of Measure feature.&lt;/p&gt;  &lt;p&gt;We can implement an operation to find the separation between two positions (the same as in my previous C# post):&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:307592de-b380-41c8-a312-f06b01cbdd5b" class="wlWriterEditableSmartContent"&gt;&lt;pre style=" width: 810px; height: 597px;background-color:White;overflow: auto;"&gt;&lt;div&gt;&lt;!--&lt;br /&gt;&lt;br /&gt;Code highlighting produced by Actipro CodeHighlighter (freeware)&lt;br /&gt;http://www.CodeHighlighter.com/&lt;br /&gt;&lt;br /&gt;--&gt;&lt;span style="color: #000000;"&gt;open System&lt;br /&gt;open System.Linq&lt;br /&gt;open System.Threading&lt;br /&gt;open System.Collections.Generic&lt;br /&gt;open Microsoft.FSharp.Math&lt;br /&gt;open Microsoft.FSharp.Linq&lt;br /&gt;open Microsoft.FSharp.Linq.Query&lt;br /&gt;&lt;br /&gt;module ObservableEx &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt;&lt;br /&gt;  type System.IObservable&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000;"&gt;'&lt;/span&gt;&lt;span style="color: #800000;"&gt;u&amp;gt; with&lt;/span&gt;&lt;span style="color: #800000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;    member &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;this&lt;/span&gt;&lt;span style="color: #000000;"&gt;.WindowWithCount(count:&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;int&lt;/span&gt;&lt;span style="color: #000000;"&gt;)&lt;br /&gt;      &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;this&lt;/span&gt;&lt;span style="color: #000000;"&gt;.Publish(Func&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;_,_&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;(fun (p:IObservable&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000;"&gt;'&lt;/span&gt;&lt;span style="color: #800000;"&gt;u&amp;gt;) -&amp;gt; &lt;/span&gt;&lt;span style="color: #800000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;        p.SelectMany(&lt;br /&gt;          Func&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;_,_&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;(fun x &lt;/span&gt;&lt;span style="color: #000000;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; p.StartWith(x).BufferWithCount(count).Take(&lt;/span&gt;&lt;span style="color: #800080;"&gt;1&lt;/span&gt;&lt;span style="color: #000000;"&gt;)),&lt;br /&gt;          Func&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;_,_,_&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;(fun x buffer &lt;/span&gt;&lt;span style="color: #000000;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; buffer)).Where(&lt;br /&gt;            Func&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;IList&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000;"&gt;'&lt;/span&gt;&lt;span style="color: #800000;"&gt;u&amp;gt;,_&amp;gt;(fun x -&amp;gt; x.Count = count)).Select(&lt;/span&gt;&lt;span style="color: #800000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;              Func&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;_,_&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; (fun x &lt;/span&gt;&lt;span style="color: #000000;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; x))));&lt;br /&gt;&lt;br /&gt;open ObservableEx&lt;br /&gt;&lt;br /&gt;let accelerationGravity &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;9.81&lt;/span&gt;&lt;span style="color: #000000;"&gt;;&lt;br /&gt;let positions &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; Observable.Generate(&lt;/span&gt;&lt;span style="color: #800080;"&gt;0.0&lt;/span&gt;&lt;span style="color: #000000;"&gt;,&lt;br /&gt;                  Func&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;_,_&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;(fun i &lt;/span&gt;&lt;span style="color: #000000;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; i &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;10.0&lt;/span&gt;&lt;span style="color: #000000;"&gt;),&lt;br /&gt;                  Func&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;_,_&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;(fun i &lt;/span&gt;&lt;span style="color: #000000;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; i &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;1.0&lt;/span&gt;&lt;span style="color: #000000;"&gt;),&lt;br /&gt;                  Func&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;_,_&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;(fun i &lt;/span&gt;&lt;span style="color: #000000;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; accelerationGravity &lt;/span&gt;&lt;span style="color: #000000;"&gt;*&lt;/span&gt;&lt;span style="color: #000000;"&gt; i &lt;/span&gt;&lt;span style="color: #000000;"&gt;*&lt;/span&gt;&lt;span style="color: #000000;"&gt; i &lt;/span&gt;&lt;span style="color: #000000;"&gt;/&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;2.0&lt;/span&gt;&lt;span style="color: #000000;"&gt;)); &lt;br /&gt;let positions2 &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; Observable.Generate(&lt;/span&gt;&lt;span style="color: #800080;"&gt;0.0&lt;/span&gt;&lt;span style="color: #000000;"&gt;,&lt;br /&gt;                  Func&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;_,_&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;(fun i &lt;/span&gt;&lt;span style="color: #000000;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; i &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;10.0&lt;/span&gt;&lt;span style="color: #000000;"&gt;),&lt;br /&gt;                  Func&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;_,_&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;(fun i &lt;/span&gt;&lt;span style="color: #000000;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; i &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;1.0&lt;/span&gt;&lt;span style="color: #000000;"&gt;),&lt;br /&gt;                  Func&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;_,_&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;(fun i &lt;/span&gt;&lt;span style="color: #000000;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; i)); &lt;br /&gt;let separation &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; Observable.Zip(positions,&lt;br /&gt;                   positions2, Func&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;_,_,_&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;(fun i j &lt;/span&gt;&lt;span style="color: #000000;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; j &lt;/span&gt;&lt;span style="color: #000000;"&gt;-&lt;/span&gt;&lt;span style="color: #000000;"&gt; i)); &lt;br /&gt;let res &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; separation.Subscribe(fun i &lt;/span&gt;&lt;span style="color: #000000;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; i &lt;/span&gt;&lt;span style="color: #000000;"&gt;|&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; printfn &lt;/span&gt;&lt;span style="color: #800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #800000;"&gt;%f&lt;/span&gt;&lt;span style="color: #800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;);&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;But let’s see what happens if we accidentally do something physically meaningless such as adding a velocity to a position:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:e301d036-8466-46c9-b59c-3f602d56315a" class="wlWriterEditableSmartContent"&gt;&lt;pre style=" width: 894px; height: 290px;background-color:White;overflow: auto;"&gt;&lt;div&gt;&lt;!--&lt;br /&gt;&lt;br /&gt;Code highlighting produced by Actipro CodeHighlighter (freeware)&lt;br /&gt;http://www.CodeHighlighter.com/&lt;br /&gt;&lt;br /&gt;--&gt;&lt;span style="color: #000000;"&gt;let accelerationGravity &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;9.81&lt;/span&gt;&lt;span style="color: #000000;"&gt;; &lt;br /&gt;let positions &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; Observable.Generate(&lt;br /&gt;                  &lt;/span&gt;&lt;span style="color: #800080;"&gt;0.0&lt;/span&gt;&lt;span style="color: #000000;"&gt;,&lt;br /&gt;                  Func&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;_,_&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;(fun i &lt;/span&gt;&lt;span style="color: #000000;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; i &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;10.0&lt;/span&gt;&lt;span style="color: #000000;"&gt;),&lt;br /&gt;                  Func&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;_,_&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;(fun i &lt;/span&gt;&lt;span style="color: #000000;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; i &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;1.0&lt;/span&gt;&lt;span style="color: #000000;"&gt;),&lt;br /&gt;                  Func&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;_,_&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;(fun i &lt;/span&gt;&lt;span style="color: #000000;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; accelerationGravity &lt;/span&gt;&lt;span style="color: #000000;"&gt;*&lt;/span&gt;&lt;span style="color: #000000;"&gt; i &lt;/span&gt;&lt;span style="color: #000000;"&gt;*&lt;/span&gt;&lt;span style="color: #000000;"&gt; i &lt;/span&gt;&lt;span style="color: #000000;"&gt;/&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;2.0&lt;/span&gt;&lt;span style="color: #000000;"&gt;)); &lt;br /&gt;let positions2 &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; Observable.Generate(&lt;/span&gt;&lt;span style="color: #800080;"&gt;0.0&lt;/span&gt;&lt;span style="color: #000000;"&gt;,&lt;br /&gt;                   Func&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;_,_&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;(fun i &lt;/span&gt;&lt;span style="color: #000000;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; i &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;10.0&lt;/span&gt;&lt;span style="color: #000000;"&gt;),&lt;br /&gt;                   Func&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;_,_&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;(fun i &lt;/span&gt;&lt;span style="color: #000000;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; i &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;1.0&lt;/span&gt;&lt;span style="color: #000000;"&gt;),&lt;br /&gt;                   Func&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;_,_&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;(fun i &lt;/span&gt;&lt;span style="color: #000000;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; i)); &lt;br /&gt;let DifferentiateWithTime (input: IObservable&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000;"&gt;'&lt;/span&gt;&lt;span style="color: #800000;"&gt;a&amp;gt;) =&lt;/span&gt;&lt;span style="color: #800000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;  input.WindowWithCount(&lt;/span&gt;&lt;span style="color: #800080;"&gt;2&lt;/span&gt;&lt;span style="color: #000000;"&gt;).Select(fun (j:IList&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000;"&gt;'&lt;/span&gt;&lt;span style="color: #800000;"&gt;a&amp;gt;) -&amp;gt; (j.[1]-j.[0])); &lt;/span&gt;&lt;span style="color: #800000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;let velocities &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; DifferentiateWithTime(positions); &lt;br /&gt;let separation &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; Observable.Zip(positions, velocities, Func&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;_,_,_&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;(fun i j &lt;/span&gt;&lt;span style="color: #000000;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; j &lt;/span&gt;&lt;span style="color: #000000;"&gt;-&lt;/span&gt;&lt;span style="color: #000000;"&gt; i)); &lt;br /&gt;let res &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; separation.Subscribe(fun i &lt;/span&gt;&lt;span style="color: #000000;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; i &lt;/span&gt;&lt;span style="color: #000000;"&gt;|&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; printfn &lt;/span&gt;&lt;span style="color: #800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #800000;"&gt;%f&lt;/span&gt;&lt;span style="color: #800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;);&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;&lt;br /&gt;The program compiles and runs normally (as we’d expect, the compiler doesn’t know better than the fact that it’s dealing with some float values).&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Now, let’s annotate our code with units of measure (I’m using the F# PowerPack). We can calculate the difference between two positions:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:b5acce71-0716-47c8-aa03-9ef29f17b6f7" class="wlWriterEditableSmartContent"&gt;&lt;pre class="brush: csharp; gutter: false; first-line: 1; tab-size: 4;  toolbar: false;  width: 894px; height: 355px;" style=" width: 894px; height: 355px;overflow: auto;"&gt;let accelerationGravity = 9.81&amp;lt;SI.m SI.s^-2&amp;gt; &lt;br /&gt;let positions = Observable.Generate(0.0&amp;lt;SI.s&amp;gt;, Func&amp;lt;_,_&amp;gt;(fun i -&amp;gt; i &amp;lt; 10.0&amp;lt;SI.s&amp;gt;), &lt;br /&gt;                  Func&amp;lt;_,_&amp;gt;(fun i -&amp;gt; i + 1.0&amp;lt;SI.s&amp;gt;),&lt;br /&gt;                  Func&amp;lt;_,_&amp;gt;(fun i -&amp;gt; accelerationGravity * i * i / 2.0)); &lt;br /&gt;let positions2 = Observable.Generate(0.0&amp;lt;SI.s&amp;gt;,&lt;br /&gt;                  Func&amp;lt;_,_&amp;gt;(fun i -&amp;gt; i &amp;lt; 10.0&amp;lt;SI.s&amp;gt;),&lt;br /&gt;                  Func&amp;lt;_,_&amp;gt;(fun i -&amp;gt; i + 1.0&amp;lt;SI.s&amp;gt;),&lt;br /&gt;                  Func&amp;lt;_,_&amp;gt;(fun i -&amp;gt; 5.0 * accelerationGravity * i * i / 2.0)); &lt;br /&gt;let DifferentiateWithTime (input: IObservable&amp;lt;float&amp;lt;_&amp;gt;&amp;gt;) = &lt;br /&gt;  input.WindowWithCount(2).Select(&lt;br /&gt;    fun (j:IList&amp;lt;float&amp;lt;_&amp;gt;&amp;gt;) -&amp;gt; (((j.[1]-j.[0])/1.0&amp;lt;SI.s&amp;gt;))); &lt;br /&gt;let velocities = DifferentiateWithTime(positions); &lt;br /&gt;let accelerations = DifferentiateWithTime(velocities); &lt;br /&gt;let separation = Observable.Zip(positions, positions2, Func&amp;lt;_,_,_&amp;gt;(fun i j -&amp;gt; j - i)); &lt;br /&gt;let res = separation.Subscribe(fun i -&amp;gt; float i |&amp;gt; printfn &amp;quot;%f&amp;quot;); &lt;br /&gt;// Next line will not compile &lt;br /&gt;//let wrongseparation = Observable.Zip(&lt;br /&gt;//                        accelerations, velocities, Func&amp;lt;_,_,_&amp;gt;(fun i j -&amp;gt; j - i));&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;p align="left"&gt;But if we try instead to calculate the difference between the position and the velocity, the code will no longer compile. This is very cool.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;We can also do the same by annotating our IObservables with units:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:a6025a92-5a77-41cc-86e3-769d4d0b952b" class="wlWriterEditableSmartContent"&gt;&lt;pre class="brush: csharp; gutter: false; first-line: 1; tab-size: 4;  toolbar: false;  width: 894px; height: 290px;" style=" width: 894px; height: 290px;overflow: auto;"&gt;let accelerationGravity = 9.81&amp;lt;SI.m SI.s^-2&amp;gt; &lt;br /&gt;let DifferentiateWithTime (input: IObservable&amp;lt;float&amp;lt;_&amp;gt;&amp;gt;) = &lt;br /&gt;  input.WindowWithCount(2).Select(&lt;br /&gt;    fun (j:IList&amp;lt;float&amp;lt;_&amp;gt;&amp;gt;) -&amp;gt; (((j.[1]-j.[0])/1.0&amp;lt;SI.s&amp;gt;)));&lt;br /&gt;let positions = Observable.Generate(0.0&amp;lt;SI.s&amp;gt;,&lt;br /&gt;                  Func&amp;lt;_,_&amp;gt;(fun i -&amp;gt; i &amp;lt; 10.0&amp;lt;SI.s&amp;gt;), &lt;br /&gt;                  Func&amp;lt;_,_&amp;gt;(fun i -&amp;gt; i + 1.0&amp;lt;SI.s&amp;gt;),&lt;br /&gt;                  Func&amp;lt;_,_&amp;gt;(fun i -&amp;gt; accelerationGravity * i * i / 2.0)); &lt;br /&gt;let velocities = DifferentiateWithTime(positions); &lt;br /&gt;let accelerations = DifferentiateWithTime(velocities); &lt;br /&gt;let res = positions.Subscribe(fun i -&amp;gt; float i |&amp;gt; printfn &amp;quot;%f&amp;quot;); &lt;br /&gt;printfn &amp;quot;-- velocities -- &amp;quot;;&lt;br /&gt;let res2 = velocities.Subscribe(fun i -&amp;gt; float i |&amp;gt; printfn &amp;quot;%f&amp;quot;);&lt;br /&gt;printf &amp;quot;-- accelerations -- &amp;quot;; &lt;br /&gt;let res3 = accelerations.Subscribe(fun i -&amp;gt; float i |&amp;gt; printfn &amp;quot;%f&amp;quot;); &lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;p align="left"&gt;I love this feature, and can see that it would be incredibly useful with RX, as RX statements can include all sorts of streams of data into a complex operation.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;(I was originally mistaken in the original version of this posting, and thought that I couldn’t create the DifferentiateWithTime method to be generic to the units of measure, but was saved by a posting on stackoverflow, &lt;a href="http://stackoverflow.com/questions/4234261/f-units-of-measure-reuse-method-with-different-units-passed-in"&gt;here&lt;/a&gt;).&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/848045343259901678-4474369563322757845?l=taumuon-jabuka.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://taumuon-jabuka.blogspot.com/feeds/4474369563322757845/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=848045343259901678&amp;postID=4474369563322757845' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/848045343259901678/posts/default/4474369563322757845'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/848045343259901678/posts/default/4474369563322757845'/><link rel='alternate' type='text/html' href='http://taumuon-jabuka.blogspot.com/2010/11/f-units-of-measure-with-reactive.html' title='F# Units of Measure with the Reactive Framework'/><author><name>Taumuon</name><uri>http://www.blogger.com/profile/12874508418428339669</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='27' src='http://bp0.blogger.com/_9XuDfr9pYU0/SGAMUjxkNvI/AAAAAAAAACs/YhujEd8cyMk/S220/colloseum.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-848045343259901678.post-8111110116684129199</id><published>2010-11-20T09:03:00.001-08:00</published><updated>2012-01-21T06:35:01.595-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='RX'/><category scheme='http://www.blogger.com/atom/ns#' term='.NET'/><category scheme='http://www.blogger.com/atom/ns#' term='Reactive Extensions for .NET'/><title type='text'>RX Framework for manipulating real-life data</title><content type='html'>&lt;p&gt; &lt;/p&gt;  &lt;p&gt;As I said in my previous blog post, in one of my previous roles involved performing real-time analysis of incoming sensor and telemetry data, combining streams of data of different data rates, allowing the user to write their own calculation operations on the data, and displaying it to the user. I wondered what this would have been like to implement using RX.&lt;/p&gt;  &lt;p&gt;As an aside, it may be that the new &lt;a href="http://www.microsoft.com/downloads/en/details.aspx?FamilyID=d5b3e1f8-c672-48e8-baf8-94f05b431f5c&amp;amp;displaylang=en"&gt;TPL Dataflow&lt;/a&gt; library would be more suitable for this – and I am a bit confused about the overlap between some many different technologies, such as RX and the forthcoming C# 5.0 async, or whether TPL Dataflow replaces the Actor model in &lt;a href="http://msdn.microsoft.com/en-us/devlabs/dd795202.aspx"&gt;Microsoft Axum&lt;/a&gt; or F#).&lt;/p&gt;  &lt;p&gt;Anyway, back with the plot…&lt;/p&gt;  &lt;p&gt;Imagine that you have to handle incoming streams of sensor data. This doesn’t have to be hardware sensor data, it could be a phone’s GPS or accelerometer data, or incoming streams of trade data. In the old style of programming there would be a callback or event handler that would be triggered when the data arrived, and to perform aggregate calculations over multiple data streams would involve convoluted code with locks – with RX there’s none of that, just a simple linq query to combine the data:&lt;/p&gt;  &lt;p&gt;Imagine that you had two streams of position data (here the observables are generated, but they could be observables from events, the usage is identical, which is beautiful!)&lt;/p&gt; &lt;span style="font-family:Consolas;font-size:78%;color:#000000;"&gt;   &lt;p align="left"&gt;&lt;/p&gt;   &lt;span style="font-family:Consolas;font-size:78%;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#000000;"&gt; positions = &lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#2b91af;"&gt;Observable&lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#000000;"&gt;.Generate(0.0, i =&amp;gt; i &amp;lt; 10.0, i =&amp;gt; i + 1.0, i =&amp;gt; i);&lt;/span&gt;    &lt;p align="left"&gt;&lt;/p&gt;   &lt;span style="font-family:Consolas;font-size:78%;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#000000;"&gt; positions2 = &lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#2b91af;"&gt;Observable&lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#000000;"&gt;.Generate(10.0, i =&amp;gt; i &amp;gt;= 0.0, i =&amp;gt; i - 1.0, i =&amp;gt; i);&lt;/span&gt;    &lt;p align="left"&gt;&lt;/p&gt; &lt;/span&gt;  &lt;p&gt;An operator to calculate the separation is as simple as:&lt;/p&gt;  &lt;p&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#000000;"&gt;     &lt;/span&gt;&lt;/p&gt;&lt;p align="left"&gt;&lt;/p&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#000000;"&gt;     &lt;span style="font-family:Consolas;font-size:78%;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#000000;"&gt; separation = positions.Zip(positions2, (i, j) =&amp;gt; &lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#000000;"&gt; { i, j }).Select(pos =&amp;gt; &lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#2b91af;"&gt;Math&lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#000000;"&gt;.Abs(pos.j - pos.i));&lt;/span&gt;&lt;/span&gt;  &lt;br /&gt;&lt;br /&gt;That’s really nice and succinct, and the logic is all in one place (instead of being spread out amongst different callbacks).&lt;p&gt;&lt;/p&gt;  &lt;p&gt;As well as calculating the difference between two positions, you might want to find the rate of change of some input data:&lt;/p&gt; &lt;span style="font-family:Consolas;font-size:78%;color:#000000;"&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#000000;"&gt;     &lt;p align="left"&gt;&lt;/p&gt;     &lt;span style="font-family:Consolas;font-size:78%;color:#0000ff;"&gt;static&lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#000000;"&gt; &lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#0000ff;"&gt;void&lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#000000;"&gt; Main(&lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#0000ff;"&gt;string&lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#000000;"&gt;[] args)&lt;/span&gt;      &lt;p align="left"&gt;{&lt;/p&gt;      &lt;p align="left"&gt;&lt;/p&gt;     &lt;span style="font-family:Consolas;font-size:78%;color:#0000ff;"&gt;const&lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#000000;"&gt; &lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#0000ff;"&gt;double&lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#000000;"&gt; accelerationGravity = 9.81;&lt;/span&gt;      &lt;p align="left"&gt;&lt;/p&gt;      &lt;p align="left"&gt;&lt;/p&gt;     &lt;span style="font-family:Consolas;font-size:78%;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#000000;"&gt; positions = &lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#2b91af;"&gt;Observable&lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#000000;"&gt;.Generate(0.0, i =&amp;gt; i &amp;lt; 10.0, i =&amp;gt; i + 1.0, i =&amp;gt; accelerationGravity * i * i / 2.0);&lt;/span&gt;      &lt;p align="left"&gt;&lt;/p&gt;      &lt;p align="left"&gt;&lt;/p&gt;     &lt;span style="font-family:Consolas;font-size:78%;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#000000;"&gt; velocity = positions.DifferentiateWithTime();&lt;/span&gt;      &lt;p align="left"&gt;&lt;/p&gt;      &lt;p align="left"&gt;&lt;/p&gt;     &lt;span style="font-family:Consolas;font-size:78%;color:#2b91af;"&gt;Console&lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#000000;"&gt;.WriteLine(&lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#a31515;"&gt;"-- velocities -- "&lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#000000;"&gt;);&lt;/span&gt;      &lt;p align="left"&gt;velocity.Subscribe(i =&amp;gt; { &lt;/p&gt;     &lt;span style="font-family:Consolas;font-size:78%;color:#2b91af;"&gt;Console&lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#000000;"&gt;.WriteLine(i); });&lt;/span&gt;      &lt;p align="left"&gt;&lt;/p&gt;      &lt;p align="left"&gt;&lt;/p&gt;     &lt;span style="font-family:Consolas;font-size:78%;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#000000;"&gt; acceleration = velocity.DifferentiateWithTime();&lt;/span&gt;      &lt;p align="left"&gt;&lt;/p&gt;      &lt;p align="left"&gt;&lt;/p&gt;     &lt;span style="font-family:Consolas;font-size:78%;color:#2b91af;"&gt;Console&lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#000000;"&gt;.WriteLine(&lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#a31515;"&gt;"-- accelerations -- "&lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#000000;"&gt;);&lt;/span&gt;      &lt;p align="left"&gt;acceleration.Subscribe(i =&amp;gt; { &lt;/p&gt;     &lt;span style="font-family:Consolas;font-size:78%;color:#2b91af;"&gt;Console&lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#000000;"&gt;.WriteLine(i); });&lt;/span&gt;      &lt;p align="left"&gt;}&lt;/p&gt;      &lt;p align="left"&gt;}&lt;/p&gt;   &lt;/span&gt;&lt;/span&gt;  &lt;p&gt;Again, this is nice. The acceleration is correctly written out for every value as 9.81 (well, within rounding errors).&lt;/p&gt;  &lt;p&gt;DifferentiateWithTime is implemented as:&lt;/p&gt; &lt;span style="font-family:Consolas;font-size:78%;color:#000000;"&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#000000;"&gt;     &lt;p align="left"&gt;&lt;/p&gt;     &lt;span style="font-family:Consolas;font-size:78%;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#000000;"&gt; &lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#0000ff;"&gt;static&lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#000000;"&gt; &lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#0000ff;"&gt;partial&lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#000000;"&gt; &lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#0000ff;"&gt;class&lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#000000;"&gt; &lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#2b91af;"&gt;ObservableEx2&lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#000000;"&gt;&lt;/span&gt;      &lt;p align="left"&gt;{&lt;/p&gt;      &lt;p align="left"&gt;&lt;/p&gt;     &lt;span style="font-family:Consolas;font-size:78%;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#000000;"&gt; &lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#0000ff;"&gt;static&lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#000000;"&gt; &lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#2b91af;"&gt;IObservable&lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#0000ff;"&gt;double&lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#000000;"&gt;&amp;gt; DifferentiateWithTime(&lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#0000ff;"&gt;this&lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#000000;"&gt; &lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#2b91af;"&gt;IObservable&lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#0000ff;"&gt;double&lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#000000;"&gt;&amp;gt; obs)&lt;/span&gt;      &lt;p align="left"&gt;{&lt;/p&gt;      &lt;p align="left"&gt;&lt;/p&gt;     &lt;span style="font-family:Consolas;font-size:78%;color:#0000ff;"&gt;return&lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#000000;"&gt; (&lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#0000ff;"&gt;from&lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#000000;"&gt; j &lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#0000ff;"&gt;in&lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#000000;"&gt; obs.WindowWithCount(2) &lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#0000ff;"&gt;select&lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#000000;"&gt; j[1] - j[0]);&lt;/span&gt;      &lt;p align="left"&gt;}&lt;/p&gt;      &lt;p align="left"&gt;&lt;/p&gt;   &lt;/span&gt;&lt;/span&gt;  &lt;p align="left"&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#000000;"&gt;&lt;/span&gt;    &lt;/p&gt;&lt;p align="left"&gt;..&lt;/p&gt; &lt;p&gt;&lt;/p&gt;  &lt;p&gt;In this case we’re taking the current and previous value and taking the difference between them – this of course is assuming that we are receiving values once per second. We could include the time of each sample in a Tuple, and use the Where operator to ignore values which occur at the same time period (to avoid divide-by-zero errors).&lt;/p&gt;  &lt;p&gt;WindowWithCount I found on &lt;a href="http://social.msdn.microsoft.com/Forums/en-US/rx/thread/37428f58-f241-45b3-a878-c1627deb9ac4#bcdc7b79-bbde-4145-88e4-583685285682"&gt;StackOverflow&lt;/a&gt;. This takes a rolling window of values around an observables value, which can be used for operations such as a moving average.&lt;/p&gt; &lt;span style="font-family:Consolas;font-size:78%;color:#000000;"&gt;   &lt;p align="left"&gt;&lt;/p&gt;   &lt;span style="font-family:Consolas;font-size:78%;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#000000;"&gt; &lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#0000ff;"&gt;static&lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#000000;"&gt; &lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#2b91af;"&gt;IObservable&lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#2b91af;"&gt;IList&lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#000000;"&gt;&amp;lt;TSource&amp;gt;&amp;gt; WindowWithCount&amp;lt;TSource&amp;gt;(&lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#0000ff;"&gt;this&lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#000000;"&gt; &lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#2b91af;"&gt;IObservable&lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#000000;"&gt;&amp;lt;TSource&amp;gt; source, &lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#0000ff;"&gt;int&lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#000000;"&gt; count)&lt;/span&gt;    &lt;p align="left"&gt;{&lt;/p&gt;    &lt;p align="left"&gt;&lt;/p&gt;   &lt;span style="font-family:Consolas;font-size:78%;color:#2b91af;"&gt;Contract&lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#000000;"&gt;.Requires(source != &lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#0000ff;"&gt;null&lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#000000;"&gt;);&lt;/span&gt;    &lt;p align="left"&gt;&lt;/p&gt;   &lt;span style="font-family:Consolas;font-size:78%;color:#2b91af;"&gt;Contract&lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#000000;"&gt;.Requires(count &amp;gt;= 0);&lt;/span&gt;    &lt;p align="left"&gt;&lt;/p&gt;    &lt;p align="left"&gt;&lt;/p&gt;   &lt;span style="font-family:Consolas;font-size:78%;color:#0000ff;"&gt;return&lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#000000;"&gt; source.Publish(published =&amp;gt;&lt;/span&gt;    &lt;p align="left"&gt;&lt;/p&gt;   &lt;span style="font-family:Consolas;font-size:78%;color:#0000ff;"&gt;from&lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#000000;"&gt; x &lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#0000ff;"&gt;in&lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#000000;"&gt; published&lt;/span&gt;    &lt;p align="left"&gt;&lt;/p&gt;   &lt;span style="font-family:Consolas;font-size:78%;color:#0000ff;"&gt;from&lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#000000;"&gt; buffer &lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#0000ff;"&gt;in&lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#000000;"&gt; published.StartWith(x).BufferWithCount(count).Take(1)&lt;/span&gt;    &lt;p align="left"&gt;&lt;/p&gt;   &lt;span style="font-family:Consolas;font-size:78%;color:#0000ff;"&gt;where&lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#000000;"&gt; buffer.Count == count&lt;/span&gt;    &lt;p align="left"&gt;&lt;/p&gt;   &lt;span style="font-family:Consolas;font-size:78%;color:#0000ff;"&gt;select&lt;/span&gt;&lt;span style="font-family:Consolas;font-size:78%;color:#000000;"&gt; buffer&lt;/span&gt;    &lt;p align="left"&gt;);&lt;/p&gt;    &lt;p align="left"&gt;}&lt;/p&gt; &lt;/span&gt;  &lt;p&gt;This is cool, but I am concerned that we’re returning a new list for each sample, even though we’re only interested in two values, this could be hammering the garbage collector for high rates of data. It does say in section 6.1 of the &lt;a href="http://blogs.msdn.com/b/rxteam/archive/2010/10/28/rx-design-guidelines.aspx"&gt;RX Design Guidelines&lt;/a&gt; that new operators should be composed of existing operators, unless performance is a concern – this is something I may get around to investigating in a future blog post.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/848045343259901678-8111110116684129199?l=taumuon-jabuka.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://taumuon-jabuka.blogspot.com/feeds/8111110116684129199/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=848045343259901678&amp;postID=8111110116684129199' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/848045343259901678/posts/default/8111110116684129199'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/848045343259901678/posts/default/8111110116684129199'/><link rel='alternate' type='text/html' href='http://taumuon-jabuka.blogspot.com/2010/11/rx-framework-for-manipulating-real-life.html' title='RX Framework for manipulating real-life data'/><author><name>Taumuon</name><uri>http://www.blogger.com/profile/12874508418428339669</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='27' src='http://bp0.blogger.com/_9XuDfr9pYU0/SGAMUjxkNvI/AAAAAAAAACs/YhujEd8cyMk/S220/colloseum.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-848045343259901678.post-6847006182717166016</id><published>2010-11-20T08:10:00.001-08:00</published><updated>2012-01-21T06:35:01.596-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='RX'/><category scheme='http://www.blogger.com/atom/ns#' term='.NET'/><category scheme='http://www.blogger.com/atom/ns#' term='Reactive Extensions for .NET'/><title type='text'>Reactive Framework thoughts</title><content type='html'>&lt;p&gt;I have been very slack with blogging recently, as I was too busy with work and life. I've recently moved jobs, and even though I'm just as busy as I was in my previous job, I have a long train commute so I thought that I may as well do something constructive with my time. I figured that blogging is as good as anything else.&lt;/p&gt;  &lt;p&gt;In my new role, I'm working on a project that makes heavy use of the the RX Framework, and I’m getting up to speed as fast as I can. It's actually really fun – the way to approach a problem is different to the old method I’m used-to of having async callbacks everywhere, it’s much more functional, and at the start I felt a little bit like my brain had been turned inside out.&lt;/p&gt;  &lt;p&gt;The project is using RX for handling the streaming of trading data, and for orchestrating asynchronous operations. The code base is a lot more succinct, understandable and elegant, than it would be without RX (though I’m finding it takes more time to think through how to write the RX operations). There are no .NET events throughout the codebase, all events are exposed as IObservables, allowing for composition with all sorts of async operations.&lt;/p&gt;  &lt;p&gt;I've come across the following blog post, where the author was performing calculations on trade data, and that sprung to mind how in a previous job I was manipulating streams of data from hardware devices, and I thought I might go ahead and have a bit of a play with this.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://eprystupa.wordpress.com/2009/12/20/running-vwap-calculation-using-reactive-extensions-for-net/"&gt;http://eprystupa.wordpress.com/2009/12/20/running-vwap-calculation-using-reactive-extensions-for-net/&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;I’ll play with this in my next blog post(s).&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/848045343259901678-6847006182717166016?l=taumuon-jabuka.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://taumuon-jabuka.blogspot.com/feeds/6847006182717166016/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=848045343259901678&amp;postID=6847006182717166016' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/848045343259901678/posts/default/6847006182717166016'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/848045343259901678/posts/default/6847006182717166016'/><link rel='alternate' type='text/html' href='http://taumuon-jabuka.blogspot.com/2010/11/reactive-framework-thoughts.html' title='Reactive Framework thoughts'/><author><name>Taumuon</name><uri>http://www.blogger.com/profile/12874508418428339669</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='27' src='http://bp0.blogger.com/_9XuDfr9pYU0/SGAMUjxkNvI/AAAAAAAAACs/YhujEd8cyMk/S220/colloseum.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-848045343259901678.post-2856405131864845152</id><published>2009-01-11T14:46:00.000-08:00</published><updated>2012-01-21T06:35:01.596-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='.NET'/><category scheme='http://www.blogger.com/atom/ns#' term='Unit testing'/><category scheme='http://www.blogger.com/atom/ns#' term='TDD'/><title type='text'>Theory driven development using Microsoft Pex Explorer</title><content type='html'>&lt;p&gt;EDIT: NOTE that some of the contents of this post have been recently been edited thanks to great feedback from Peli de Halleux (http://blog.dotnetwiki.org/). He pointed me at a great post on Test Driven Development with Parameterized Unit Tests that he'd wrote here: &lt;a href="http://blog.dotnetwiki.org/TestDrivenDevelopmentWithParameterizedUnitTests.aspx"&gt;http://blog.dotnetwiki.org/TestDrivenDevelopmentWithParameterizedUnitTests.aspx&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;I've been playing with Microsoft Pex to try out some theory-driven development. I've found it a great tool – actually much better than I was hoping, it's found more bugs, and test cases, than using just unit testing or TDD (Test-Driven Development). It's test-driven development on steroids!&lt;/p&gt;  &lt;p&gt;If you've been reading this blog, you'll remember I blogged a while ago about TDD using theories, and what theories are – with the following posts:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://taumuon-jabuka.blogspot.com/2008/03/comparing-theories-to-more-traditional.html"&gt;http://taumuon-jabuka.blogspot.com/2008/03/comparing-theories-to-more-traditional.html&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://taumuon-jabuka.blogspot.com/2008/03/theories.html"&gt;http://taumuon-jabuka.blogspot.com/2008/03/theories.html&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;I mentioned that theories could have data-points generated by some sore of explorer, and Jeff Brown (&lt;a title="http://blog.bits-in-motion.com/" href="http://blog.bits-in-motion.com/"&gt;http://blog.bits-in-motion.com/&lt;/a&gt;) one of the comments replied that maybe I could use Microsof Pex to do so, and I've found it to be perfect! A lot more powerful than I hoped.&lt;/p&gt;  &lt;p&gt;This blog post will try to detail my experiences, but by far the best way to get to grips with this is to download Pex and try writing some theories!&lt;/p&gt;  &lt;p&gt;In the example below, I'll create a queue with the following operations:&lt;/p&gt;  &lt;p&gt;Enqueue()&lt;/p&gt;  &lt;p&gt;Dequeue()&lt;/p&gt;  &lt;p&gt;Capacity&lt;/p&gt;  &lt;p&gt;Count&lt;/p&gt;  &lt;p&gt;It will have an initial capacity, and it will double in size once its capacity is exceeded (similar to the interface on .NET lists, for instance).&lt;/p&gt;  &lt;p&gt;First, I'll skip over a few tests written via TDD (a red-bar green-bar refactor cycle), before I get onto writing some theories.&lt;/p&gt;  &lt;p&gt;First, in MSTest, we write a test.&lt;/p&gt;  &lt;pre class="csharpcode"&gt;        [TestMethod]&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Test_NewlyConstructedQueue_CountIsZero()&lt;br /&gt;        {&lt;br /&gt;            Queue&amp;lt;&lt;span class="kwrd"&gt;int&lt;/span&gt;&amp;gt; queue = &lt;span class="kwrd"&gt;new&lt;/span&gt; Queue&amp;lt;&lt;span class="kwrd"&gt;int&lt;/span&gt;&amp;gt;();&lt;br /&gt;            Assert.AreEqual&amp;lt;&lt;span class="kwrd"&gt;int&lt;/span&gt;&amp;gt;(0, queue.Count);&lt;br /&gt;        }&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;style type="text/css"&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Then we get the tests to compile, by implementing our queue with a Count property, such that we get a red bar, by getting the count property to return -1.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;We then fix the test to get a green bar, but letting the Count return zero: &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Queue&amp;lt;T&amp;gt;&lt;br /&gt;    {&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; Count&lt;br /&gt;        {&lt;br /&gt;            get { &lt;span class="kwrd"&gt;return&lt;/span&gt; 0; }&lt;br /&gt;        }&lt;br /&gt;    }&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;We then introduce a test that default constructor creates stack with default stack of capacity 8.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;        [TestMethod]&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Test_ConstructDefaultConstructor_InitialCapacityIsEight()&lt;br /&gt;        {&lt;br /&gt;            Queue&amp;lt;&lt;span class="kwrd"&gt;int&lt;/span&gt;&amp;gt; queue = &lt;span class="kwrd"&gt;new&lt;/span&gt; Queue&amp;lt;&lt;span class="kwrd"&gt;int&lt;/span&gt;&amp;gt;();&lt;br /&gt;            Assert.AreEqual&amp;lt;&lt;span class="kwrd"&gt;int&lt;/span&gt;&amp;gt;(8, queue.Capacity);&lt;br /&gt;        }&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;The implementation is pretty simple:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;const&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; initialCapacity = 8;&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; Capacity&lt;br /&gt;        {&lt;br /&gt;            get { &lt;span class="kwrd"&gt;return&lt;/span&gt; initialCapacity; }&lt;br /&gt;        }&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Now, want to add a Dequeue method, and test that calling it on an &lt;b&gt;empty stack&lt;/b&gt; throws an InvalidOperationException.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;        [TestMethod]&lt;br /&gt;        [ExpectedException(&lt;span class="kwrd"&gt;typeof&lt;/span&gt;(InvalidOperationException))]&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Test_NewlyConstructedQueue_DequeueThrowsException()&lt;br /&gt;        {&lt;br /&gt;            Queue&amp;lt;&lt;span class="kwrd"&gt;int&lt;/span&gt;&amp;gt; queue = &lt;span class="kwrd"&gt;new&lt;/span&gt; Queue&amp;lt;&lt;span class="kwrd"&gt;int&lt;/span&gt;&amp;gt;();&lt;br /&gt;            &lt;span class="kwrd"&gt;int&lt;/span&gt; i = queue.Dequeue();&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; T Dequeue()&lt;br /&gt;        {&lt;br /&gt;            &lt;span class="kwrd"&gt;throw&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; InvalidOperationException(&lt;span class="str"&gt;&amp;quot;Should not call Dequeue on an empty queue&amp;quot;&lt;/span&gt;);&lt;br /&gt;        }&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;We're going to come back to look at this test shortly.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;We add a test for Enqueue, to check its count is 1. &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;        [TestMethod]&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Test_NewlyConstructedQueue_Enqueue_CountIsOne()&lt;br /&gt;        {&lt;br /&gt;            Queue&amp;lt;&lt;span class="kwrd"&gt;int&lt;/span&gt;&amp;gt; queue = &lt;span class="kwrd"&gt;new&lt;/span&gt; Queue&amp;lt;&lt;span class="kwrd"&gt;int&lt;/span&gt;&amp;gt;();&lt;br /&gt;            queue.Enqueue(1);&lt;br /&gt;            Assert.AreEqual(1, queue.Count);&lt;br /&gt;        }&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;style type="text/css"&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Add an Enqueue method that does nothing to get this to compile and redbar. We actually have to add some implementation now, to get this to pass. &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Queue&amp;lt;T&amp;gt;&lt;br /&gt;    {&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;const&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; initialCapacity = 8;&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; T[] items = &lt;span class="kwrd"&gt;null&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; Count&lt;br /&gt;        {&lt;br /&gt;            get { &lt;span class="kwrd"&gt;return&lt;/span&gt; items == &lt;span class="kwrd"&gt;null&lt;/span&gt; ? 0 : 1; }&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; Capacity&lt;br /&gt;        {&lt;br /&gt;            get { &lt;span class="kwrd"&gt;return&lt;/span&gt; initialCapacity; }&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; T Dequeue()&lt;br /&gt;        {&lt;br /&gt;            &lt;span class="kwrd"&gt;throw&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; InvalidOperationException(&lt;span class="str"&gt;&amp;quot;Should not call Dequeue on an empty queue&amp;quot;&lt;/span&gt;);&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Enqueue(T item)&lt;br /&gt;        {&lt;br /&gt;            items = &lt;span class="kwrd"&gt;new&lt;/span&gt; T[initialCapacity];&lt;br /&gt;            items[0] = item;&lt;br /&gt;        }&lt;br /&gt;    }&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;style type="text/css"&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;There are lots of things wrong with this implementation – there are some obvious bugs, we're recreating the array on each Enqueue, not to mention the array being a fixed size, with no capability to grow., and the weird Count implementation. This obviously needs implementing and refactoring, but it passes all our tests, and that's TDD – we don't go adding code until we drive it via adding more tests. So, lets add another couple of tests to drive the implementation: &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;        [TestMethod]&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Test_NewlyConstructedQueue_Enqueue_DequeueReturnsItem()&lt;br /&gt;        {&lt;br /&gt;            Queue&amp;lt;&lt;span class="kwrd"&gt;int&lt;/span&gt;&amp;gt; queue = &lt;span class="kwrd"&gt;new&lt;/span&gt; Queue&amp;lt;&lt;span class="kwrd"&gt;int&lt;/span&gt;&amp;gt;();&lt;br /&gt;            queue.Enqueue(1);&lt;br /&gt;            &lt;span class="kwrd"&gt;int&lt;/span&gt; item = queue.Dequeue();&lt;br /&gt;            Assert.IsTrue(1 == item);&lt;br /&gt;        }&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;style type="text/css"&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;And can get the tests to pass with the following: &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; T Dequeue()&lt;br /&gt;        {&lt;br /&gt;            &lt;span class="kwrd"&gt;if&lt;/span&gt; (items == &lt;span class="kwrd"&gt;null&lt;/span&gt;)&lt;br /&gt;            {&lt;br /&gt;                &lt;span class="kwrd"&gt;throw&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; InvalidOperationException(&lt;span class="str"&gt;&amp;quot;Should not call Dequeue on an empty queue&amp;quot;&lt;/span&gt;);&lt;br /&gt;            }&lt;br /&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; items[0];&lt;br /&gt;        }&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;style type="text/css"&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;NOTE: We're really taking baby steps, but to ensure that we're not writing any untested implementation.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Now let's add a test for adding and removing more than one item: &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;        [TestMethod]&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Test_NewlyConstructedQueue_EnqueueTwoItems_DequeueReturnsFirstItem()&lt;br /&gt;        {&lt;br /&gt;            Queue&amp;lt;&lt;span class="kwrd"&gt;int&lt;/span&gt;&amp;gt; queue = &lt;span class="kwrd"&gt;new&lt;/span&gt; Queue&amp;lt;&lt;span class="kwrd"&gt;int&lt;/span&gt;&amp;gt;();&lt;br /&gt;            queue.Enqueue(1);&lt;br /&gt;            queue.Enqueue(2);&lt;br /&gt;            &lt;span class="kwrd"&gt;int&lt;/span&gt; item = queue.Dequeue();&lt;br /&gt;            Assert.IsTrue(1 == item);&lt;br /&gt;        }&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;style type="text/css"&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;To get this to pass we can more property implement our queue. &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Queue&amp;lt;T&amp;gt;&lt;br /&gt;    {&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; tailIndex = 0; &lt;span class="rem"&gt;// insertion&lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; headIndex = 0; &lt;span class="rem"&gt;// removal&lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;const&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; initialCapacity = 8;&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; T[] items = &lt;span class="kwrd"&gt;new&lt;/span&gt; T[initialCapacity];&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; Count&lt;br /&gt;        {&lt;br /&gt;            get { &lt;span class="kwrd"&gt;return&lt;/span&gt; tailIndex - headIndex; }&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; Capacity&lt;br /&gt;        {&lt;br /&gt;            get { &lt;span class="kwrd"&gt;return&lt;/span&gt; initialCapacity; }&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; T Dequeue()&lt;br /&gt;        {&lt;br /&gt;            &lt;span class="kwrd"&gt;if&lt;/span&gt; (Count == 0)&lt;br /&gt;            {&lt;br /&gt;                &lt;span class="kwrd"&gt;throw&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; InvalidOperationException(&lt;span class="str"&gt;&amp;quot;Should not call Dequeue on an empty queue&amp;quot;&lt;/span&gt;);&lt;br /&gt;            }&lt;br /&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; items[headIndex--];&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Enqueue(T item)&lt;br /&gt;        {&lt;br /&gt;            items[tailIndex++] = item;&lt;br /&gt;        }&lt;br /&gt;    }&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;style type="text/css"&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;To get this to work we had to add more and more example tests – we were saying quite general statements, but expressing this in more and more specific example cases, adding functionality as we went. This process is called triangulation (as I mentioned in an earlier blog post).&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;There are still a number of limitations here: if we exceed 8 items in the queue, we'll get an IndexOutOfRange exception.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Can quite easily write a test for adding more values for the auto-growth.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Also, a more insiduous bug is that our values can &lt;i&gt;migrate&lt;/i&gt; along the array – we're not moving items back towards the back (or front of the queue), so could run out of space even with less than 8 values – a much harder bug to find. This is a subtle bug, and to find it using a unit test would be quite tricky, if you didn't see it up front. USING PEX TO AUTOMATICALLY GENERATE THE THEORY VALIDATION IS GREAT FOR THESE TYPES OF BUGS!&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Let's see if we could have written our test in such a way that this problem may have been detected.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;        [PexMethod]&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Theory_CanAddRemoveAnyNumberOfItemsToNewlyConstructedQueue_IsEmptyAfterwards(&lt;span class="kwrd"&gt;int&lt;/span&gt; num)&lt;br /&gt;        {&lt;br /&gt;            Queue&amp;lt;&lt;span class="kwrd"&gt;int&lt;/span&gt;&amp;gt; queue = &lt;span class="kwrd"&gt;new&lt;/span&gt; Queue&amp;lt;&lt;span class="kwrd"&gt;int&lt;/span&gt;&amp;gt;();&lt;br /&gt;            &lt;span class="kwrd"&gt;for&lt;/span&gt; (&lt;span class="kwrd"&gt;int&lt;/span&gt; i = 0; i &amp;lt; num; i++)&lt;br /&gt;            {&lt;br /&gt;                queue.Enqueue(i);&lt;br /&gt;            }&lt;br /&gt;            &lt;span class="kwrd"&gt;for&lt;/span&gt; (&lt;span class="kwrd"&gt;int&lt;/span&gt; i = 0; i &amp;lt; num; i++)&lt;br /&gt;            {&lt;br /&gt;                queue.Dequeue();&lt;br /&gt;            }&lt;br /&gt;            Assert.AreEqual&amp;lt;&lt;span class="kwrd"&gt;int&lt;/span&gt;&amp;gt;(0, queue.Count);&lt;br /&gt;        }&lt;/pre&gt;&lt;br /&gt;&lt;style type="text/css"&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Use the menu option “Run Pex Explorations”.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;4 explorations (unit tests) are generated for this – passing in values of num 0, 1073741824, and 2 and 1.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;0 passes. &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;1073741824 throws an IndexOutOfRangeException&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;2 throws an IndexOutOfRangeException&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;1 throws an AssertFailedException&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;This straight away shows that the code is more bugged than I thought. This would probably have been spotted on the introduction of further tests (for instance, test adding and removing three items from the list), but already highlights the power of theories.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;i&gt;Note: This could have been written as a unit test, but would have had to iterate over all possible integers, instead of just the 4 values that Pex provided.&lt;/i&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Passing in a value of 1 – we had a similar test to this in enqueing and dequeing a single item, but we tested that the item dequeued was the same as that enqueued, we didn't test the Count property.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;This bug is due to the fact that the headIndex should have been incremented, instead of decremented on dequeuing.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Fixing that and running our tests (including the previously generated ones – without reexploring) results in 9 of the 10 tests passing. The failing case is we're passing in a large in value.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;This is obviously due to the fact that we've got a fixed size array, and it doesn't auto-grow.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Let's add an assumption to get our test to pass, before we go on to add the auto-grow functionality.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;PexAssume.IsTrue(num &amp;lt;= queue.Capacity, &amp;quot;num is less than capacity&amp;quot;);&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;In theories, assumptions allows us to state the input data for which the theory is valid.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;We need to re-run the explorations so we're not calling our test with data points that fail the assumption.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Before we go to get our queue to expand its capacity, let's change our theory to see if it can detect the problem of running out of space after adding and removing items even before it goes out of capacity.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;I originally wrote my theory in the format below, but see the second format and following comments, following feedback from Peli de Halleux. &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;        [PexMethod]&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Theory_CanAddRemoveAnyNumberOfItemsAnyEmptyQueue_IsEmptyAfterwards(&lt;br /&gt;            [PexAssumeNotNull]Queue&amp;lt;&lt;span class="kwrd"&gt;int&lt;/span&gt;&amp;gt; queue, &lt;span class="kwrd"&gt;int&lt;/span&gt; num)&lt;br /&gt;        {&lt;br /&gt;            PexAssume.IsTrue(num &amp;lt;= queue.Capacity, &lt;span class="str"&gt;&amp;quot;num is less than capacity&amp;quot;&lt;/span&gt;);&lt;br /&gt;            PexAssume.IsTrue(queue.Count == 0);&lt;br /&gt;            &lt;span class="kwrd"&gt;for&lt;/span&gt; (&lt;span class="kwrd"&gt;int&lt;/span&gt; i = 0; i &amp;lt;= num; i++)&lt;br /&gt;            {&lt;br /&gt;                queue.Enqueue(i);&lt;br /&gt;            }&lt;br /&gt;            &lt;span class="kwrd"&gt;for&lt;/span&gt; (&lt;span class="kwrd"&gt;int&lt;/span&gt; i = 0; i &amp;lt;= num; i++)&lt;br /&gt;            {&lt;br /&gt;                queue.Dequeue();&lt;br /&gt;            }&lt;br /&gt;            Assert.AreEqual&amp;lt;&lt;span class="kwrd"&gt;int&lt;/span&gt;&amp;gt;(0, queue.Count);&lt;br /&gt;        }&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;style type="text/css"&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;This was rewritten to: &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;        [PexMethod]&lt;br /&gt;        [PexGenericArguments(&lt;span class="kwrd"&gt;typeof&lt;/span&gt;(&lt;span class="kwrd"&gt;int&lt;/span&gt;))]&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Theory_CanAddRemoveAnyNumberOfItemsAnyEmptyQueue_IsEmptyAfterwards&amp;lt;T&amp;gt;(&lt;br /&gt;            [PexAssumeUnderTest]Queue&amp;lt;T&amp;gt; queue, [PexAssumeNotNull]T[] values)&lt;br /&gt;        {&lt;br /&gt;            PexAssume.IsTrue(values.Length &amp;lt;= queue.Capacity, &lt;span class="str"&gt;&amp;quot;num is less than capacity&amp;quot;&lt;/span&gt;);&lt;br /&gt;            PexAssume.IsTrue(queue.Count == 0);&lt;br /&gt;            &lt;span class="kwrd"&gt;for&lt;/span&gt; (&lt;span class="kwrd"&gt;int&lt;/span&gt; i = 0; i &amp;lt; values.Length; i++)&lt;br /&gt;            {&lt;br /&gt;                queue.Enqueue(values[i]);&lt;br /&gt;            }&lt;br /&gt;            &lt;span class="kwrd"&gt;for&lt;/span&gt; (&lt;span class="kwrd"&gt;int&lt;/span&gt; i = 0; i &amp;lt; values.Length; i++)&lt;br /&gt;            {&lt;br /&gt;                queue.Dequeue();&lt;br /&gt;            }&lt;br /&gt;            Assert.AreEqual&amp;lt;&lt;span class="kwrd"&gt;int&lt;/span&gt;&amp;gt;(0, queue.Count);&lt;br /&gt;        }&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;style type="text/css"&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;The PexAssumeNotNullAttribute states that the theory is not valid for null values of that parameter, but the PexAssumeUnderTest, as well as stating that the parameter cannot be null, gives Pex more hints for tuning its search strategies.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;This is a much more general theory – we're now saying given any empty queue, it will be empty after enqueueing and dequeueing.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;We can instruct Pex how to construct a queue, that has had items added and removed: &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;        [PexFactoryMethod(&lt;span class="kwrd"&gt;typeof&lt;/span&gt;(Queue&amp;lt;&lt;span class="kwrd"&gt;int&lt;/span&gt;&amp;gt;))]&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; Queue&amp;lt;&lt;span class="kwrd"&gt;int&lt;/span&gt;&amp;gt; Create(&lt;span class="kwrd"&gt;int&lt;/span&gt; numberInitialAdds,&lt;br /&gt;            &lt;span class="kwrd"&gt;int&lt;/span&gt; numberInitialRemoves)&lt;br /&gt;        {&lt;br /&gt;            Queue&amp;lt;&lt;span class="kwrd"&gt;int&lt;/span&gt;&amp;gt; queue = &lt;span class="kwrd"&gt;new&lt;/span&gt; Queue&amp;lt;&lt;span class="kwrd"&gt;int&lt;/span&gt;&amp;gt;();&lt;br /&gt;            &lt;span class="kwrd"&gt;if&lt;/span&gt; (numberInitialAdds &amp;lt;= 0 || numberInitialRemoves &amp;lt; 0)&lt;br /&gt;            {&lt;br /&gt;                &lt;span class="kwrd"&gt;return&lt;/span&gt; queue;&lt;br /&gt;            }&lt;br /&gt;            &lt;span class="kwrd"&gt;for&lt;/span&gt; (&lt;span class="kwrd"&gt;int&lt;/span&gt; i = 0; i &amp;lt; numberInitialAdds; ++i)&lt;br /&gt;            {&lt;br /&gt;                queue.Enqueue(i);&lt;br /&gt;            }&lt;br /&gt;            &lt;span class="kwrd"&gt;int&lt;/span&gt; numberToRemove = numberInitialAdds &amp;gt; numberInitialRemoves ?&lt;br /&gt;            numberInitialRemoves : numberInitialAdds;&lt;br /&gt;            &lt;span class="kwrd"&gt;for&lt;/span&gt; (&lt;span class="kwrd"&gt;int&lt;/span&gt; i = 0; i &amp;lt; numberToRemove; ++i)&lt;br /&gt;            {&lt;br /&gt;                queue.Dequeue();&lt;br /&gt;            }&lt;br /&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; queue;&lt;br /&gt;        }&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;style type="text/css"&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Now, running the theory gives an IndexOutOfRangeException after adding and removing ight items to the queue in the factory – and then subsequently adding and removing one items. This should work – as we're not exceeding the capacity.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;This is powerful – if we were unaware of the existence of this bug it's difficult to imagine how we would have come across it in TDD.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Let's fix the failing theory before we remove our assumptions. &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; T Dequeue()&lt;br /&gt;        {&lt;br /&gt;            &lt;span class="kwrd"&gt;if&lt;/span&gt; (Count == 0)&lt;br /&gt;            {&lt;br /&gt;                &lt;span class="kwrd"&gt;throw&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; InvalidOperationException(&lt;span class="str"&gt;&amp;quot;Should not call Dequeue on an empty queue&amp;quot;&lt;/span&gt;);&lt;br /&gt;            }&lt;br /&gt;            T item = items[headIndex++];&lt;br /&gt;            &lt;span class="kwrd"&gt;if&lt;/span&gt; (headIndex == tailIndex)&lt;br /&gt;            {&lt;br /&gt;                headIndex = tailIndex = 0;&lt;br /&gt;            }&lt;br /&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; item;&lt;br /&gt;        }&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;style type="text/css"&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Now, the tests run. Let's go on to address the auto-grow issue.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Now, remove the assumption that values.Length &amp;lt;= queue.Capacity and re-explore this theory.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Get an index out of range exception when attempting an array of 15 items to the queue.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;We can fix this by implementing auto-grow in the stack: &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Enqueue(T item)&lt;br /&gt;        {&lt;br /&gt;            &lt;span class="kwrd"&gt;if&lt;/span&gt; (tailIndex == items.Length - 1)&lt;br /&gt;            {&lt;br /&gt;                items = &lt;span class="kwrd"&gt;new&lt;/span&gt; T[items.Length * 2];&lt;br /&gt;            }&lt;br /&gt;            items[tailIndex++] = item;&lt;br /&gt;        }&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;style type="text/css"&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;We can now re-run our tests and they all pass.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Now, deleting all Generated Unit Tests in the class, and re-running Pex Explorations, gives Path bounds exceeded messages – the time is being spent in the factory adding and removing items to the queue, and exploring the paths of auto-growing the stack.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Instead of getting our queue into the required states by manipulating it via its public interface, we can more-directly construct it into any state.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Remove the factory, and add an overloaded constructor.&amp;#160; &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; Queue(T[] items, &lt;span class="kwrd"&gt;int&lt;/span&gt; headIndex, &lt;span class="kwrd"&gt;int&lt;/span&gt; tailIndex)&lt;br /&gt;        {&lt;br /&gt;            &lt;span class="kwrd"&gt;if&lt;/span&gt; ((&lt;span class="kwrd"&gt;uint&lt;/span&gt;)tailIndex &amp;gt;= (&lt;span class="kwrd"&gt;uint&lt;/span&gt;)(items.Length))&lt;br /&gt;                &lt;span class="kwrd"&gt;throw&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; ArgumentException(&lt;span class="str"&gt;&amp;quot;(uint)tailIndex &amp;gt;= (uint)(items.Length)&amp;quot;&lt;/span&gt;);&lt;br /&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;this&lt;/span&gt;.items = items;&lt;br /&gt;            &lt;span class="kwrd"&gt;this&lt;/span&gt;.headIndex = headIndex;&lt;br /&gt;            &lt;span class="kwrd"&gt;this&lt;/span&gt;.tailIndex = tailIndex;&lt;br /&gt;        }&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;style type="text/css"&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;And instruct Pex to use this constructor with an assembly attribute: &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;[assembly: PexExplorableFromConstructor(&lt;span class="kwrd"&gt;typeof&lt;/span&gt;(Queue&amp;lt;&lt;span class="kwrd"&gt;int&lt;/span&gt;&amp;gt;),&lt;br /&gt;    &lt;span class="kwrd"&gt;typeof&lt;/span&gt;(&lt;span class="kwrd"&gt;int&lt;/span&gt;[]), &lt;span class="kwrd"&gt;typeof&lt;/span&gt;(&lt;span class="kwrd"&gt;int&lt;/span&gt;), &lt;span class="kwrd"&gt;typeof&lt;/span&gt;(&lt;span class="kwrd"&gt;int&lt;/span&gt;))]&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;style type="text/css"&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Now, Pex is able to explore our theory, and finds no failing cases.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;NOTE: In my original version of the theory, my theory took an int, the number of values to add to the queue, instead of the array of values, and it that case Pex would pass in a number of integers to add that would cause OutOfMemoryExceptions to occur. Instead, passing in the array of values means that Pex passes in more meaningful data.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;The implementation of the queue is progressing; we're autogrowing the stack, but we're not copying the values across. We're also not updating the capacity. We need to check that we can enqueue or dequeue items,and we'd obviously write more theories to do this.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;This does show that the thought process behind writing theories is similar to TDD – the tests/theories are only as good as you write, and it's still easy to miss obvious implentation (you still need to think about what you're writing), but I feel that writing theories, with an explorer, allows many more bugs to be caught during the process. Hopefully this has given a good taste of theory driven development. &lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/848045343259901678-2856405131864845152?l=taumuon-jabuka.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://taumuon-jabuka.blogspot.com/feeds/2856405131864845152/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=848045343259901678&amp;postID=2856405131864845152' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/848045343259901678/posts/default/2856405131864845152'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/848045343259901678/posts/default/2856405131864845152'/><link rel='alternate' type='text/html' href='http://taumuon-jabuka.blogspot.com/2009/01/theory-driven-development-using_11.html' title='Theory driven development using Microsoft Pex Explorer'/><author><name>Taumuon</name><uri>http://www.blogger.com/profile/12874508418428339669</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='27' src='http://bp0.blogger.com/_9XuDfr9pYU0/SGAMUjxkNvI/AAAAAAAAACs/YhujEd8cyMk/S220/colloseum.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-848045343259901678.post-2735630029113119381</id><published>2008-11-16T10:05:00.000-08:00</published><updated>2012-01-21T06:35:01.597-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='3D'/><category scheme='http://www.blogger.com/atom/ns#' term='.NET'/><category scheme='http://www.blogger.com/atom/ns#' term='Flocking'/><category scheme='http://www.blogger.com/atom/ns#' term='Boids'/><category scheme='http://www.blogger.com/atom/ns#' term='OpenGL'/><category scheme='http://www.blogger.com/atom/ns#' term='Simulation'/><title type='text'>Faster boids</title><content type='html'>&lt;object width="425" height="350"&gt; &lt;param name="movie" value="http://www.youtube.com/v/3gPU9-vF4A0"&gt;  &lt;embed src="http://www.youtube.com/v/3gPU9-vF4A0" type="application/x-shockwave-flash" width="425" height="350"&gt;&lt;/embed&gt;  &lt;/object&gt;&lt;br /&gt;&lt;br /&gt;I used the built in performance profiler in VSTS to look at speeding the boids up (and there seems to be a bug in the profiler - when memory profiling was turned on, it seemed unable to resolve symbols, so I switched to the CLRProfiler to look at the memory being used).&lt;br /&gt;&lt;br /&gt;Most of the time in the application was spent in calculating the boids' steering behaviours.&lt;br /&gt;The optimisations here were to try to reduce the time spent in calculating the forces - one optimisation was that instead of each behaviour (cohesion, separation, alignment) each scanning through the entire set of boids, the results of one of the behaviours could be used by both others.&lt;br /&gt;&lt;br /&gt;Secondly, the cohesion force was not updated on each frame - the value is cached for 10 iterations. This should not be a fixed value - there should be some heuristic to determine how it should be varied once work is in to measure the "quality" of the flock.&lt;br /&gt;&lt;br /&gt;This boosted the frame rate from approx 3fps to 10fps (dropping to 5 fps after some time).&lt;br /&gt;&lt;br /&gt;Next came some micro-optimisations.&lt;br /&gt;&lt;br /&gt;Changing the Vector to be a struct instead of a class (to avoid over-stressing the garbage collector) resulted in 14fps dropping to 8 fps after running.&lt;br /&gt;&lt;br /&gt;Other micro-optimisations that I tried, but didn't result in any real performance difference included:&lt;br /&gt;&lt;br /&gt;Making the trail generator to use a fixed size array (instead of adding/removing from a list).&lt;br /&gt;&lt;br /&gt;Removing IEulerRigidBody, calling onto objects directly instead of through an interface.&lt;br /&gt;&lt;br /&gt;Changing all doubles to be floats (didn't actually keep this change in the code).&lt;br /&gt;&lt;br /&gt;For Scene8, the drawing code was the real bottleneck, changing the gluSphere to be drawn using VertexBufferObjects resulted in double the frame rate (and it isn't particularly optimised - could use glDrawElements).&lt;br /&gt;&lt;br /&gt;For Scene7, it's limited by the O(n2) collision detection (this is the next thing to tackle).&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/848045343259901678-2735630029113119381?l=taumuon-jabuka.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://taumuon-jabuka.blogspot.com/feeds/2735630029113119381/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=848045343259901678&amp;postID=2735630029113119381' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/848045343259901678/posts/default/2735630029113119381'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/848045343259901678/posts/default/2735630029113119381'/><link rel='alternate' type='text/html' href='http://taumuon-jabuka.blogspot.com/2008/11/faster-boids.html' title='Faster boids'/><author><name>Taumuon</name><uri>http://www.blogger.com/profile/12874508418428339669</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='27' src='http://bp0.blogger.com/_9XuDfr9pYU0/SGAMUjxkNvI/AAAAAAAAACs/YhujEd8cyMk/S220/colloseum.jpg'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-848045343259901678.post-2014696658987921606</id><published>2008-08-17T09:45:00.000-07:00</published><updated>2012-01-21T06:35:01.598-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='3D'/><category scheme='http://www.blogger.com/atom/ns#' term='.NET'/><category scheme='http://www.blogger.com/atom/ns#' term='Flocking'/><category scheme='http://www.blogger.com/atom/ns#' term='Boids'/><category scheme='http://www.blogger.com/atom/ns#' term='OpenGL'/><category scheme='http://www.blogger.com/atom/ns#' term='Simulation'/><title type='text'>Boids</title><content type='html'>&lt;object width="425" height="344"&gt;&lt;param name="movie" value="http://www.youtube.com/v/9fpVptqhBco&amp;hl=en&amp;fs=1"&gt;&lt;/param&gt;&lt;param name="allowFullScreen" value="true"&gt;&lt;/param&gt;&lt;embed src="http://www.youtube.com/v/9fpVptqhBco&amp;hl=en&amp;fs=1" type="application/x-shockwave-flash" allowfullscreen="true" width="425" height="344"&gt;&lt;/embed&gt;&lt;/object&gt;&lt;br /&gt;&lt;br /&gt;An improvement of the flocking behaviour in &lt;a href="http://taumuon.co.uk/jabuka"&gt;Jabuka&lt;/a&gt; is available.&lt;br /&gt;&lt;br /&gt;There are now very simplistic birds drawn, to give an idea of the boid's orientation.&lt;br /&gt;&lt;br /&gt;The separation behaviour has been fixed. The behaviour was garbage before, the force away from another boid was proportional to the distance away from it. Now the force increases the nearer another boid is.&lt;br /&gt;&lt;br /&gt;The boid's orientation is taken as the smallest angle between being aligned with the World's x axis, and the boid's velocity. The roll doesn't behave realistically (see scene 13), but the boid's behaviour isn't realistic. A boid would really change its velocity by adjusting its orientation (change the wing shape to apply a torque to allow it to rotate...)&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.cs.auckland.ac.nz/%7Eburkhard/Reports/2003_SS_TaeRimHan.pdf"&gt;This paper&lt;/a&gt; describes how the steering behaviours should change the intention of the boid, instead of providing actual forces onto the boid.&lt;br /&gt;&lt;br /&gt;A simple particle system has been introduced to allow the path of a few individual boids to be more easily seen.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Performance.&lt;br /&gt;Running under the Visual Studio profiler, most of the time was seen to be spent finding the flockmates in proximity of the boid.&lt;br /&gt;&lt;br /&gt;The same calculation was being done twice, for both the cohesion and alignment behaviours. Adding a ProximityFinderCache increased the frame rate by 50%, with no change in the calculations. Adjusting the code so that the boids in proximity are only calculated on every third iteration added another 20% to the framerate (giving 15fps with only two TrailGenerators attached).&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/848045343259901678-2014696658987921606?l=taumuon-jabuka.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://taumuon-jabuka.blogspot.com/feeds/2014696658987921606/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=848045343259901678&amp;postID=2014696658987921606' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/848045343259901678/posts/default/2014696658987921606'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/848045343259901678/posts/default/2014696658987921606'/><link rel='alternate' type='text/html' href='http://taumuon-jabuka.blogspot.com/2008/08/boids.html' title='Boids'/><author><name>Taumuon</name><uri>http://www.blogger.com/profile/12874508418428339669</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='27' src='http://bp0.blogger.com/_9XuDfr9pYU0/SGAMUjxkNvI/AAAAAAAAACs/YhujEd8cyMk/S220/colloseum.jpg'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-848045343259901678.post-9071821214022834265</id><published>2008-04-21T13:25:00.000-07:00</published><updated>2012-01-21T06:35:01.598-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='.NET'/><category scheme='http://www.blogger.com/atom/ns#' term='Unit testing'/><category scheme='http://www.blogger.com/atom/ns#' term='TDD'/><title type='text'>Maslina and Rakija updates for NUnit 2.4.7</title><content type='html'>The above NUnit extensions have been updated, &lt;a href="http://www.taumuon.co.uk/rakija"&gt;Rakija &lt;/a&gt;(the data-driven tests extension) has simply been rebuilt against NUnit 2.4.7.&lt;br /&gt;&lt;br /&gt;Maslina (the theory extension) has had some changes.&lt;br /&gt;Firstly, inline Assumptions have been added (i.e. you can Assume.IsTrue()) in the code.&lt;br /&gt;&lt;br /&gt;The most important change, however, is that a failure of the pre-called assumptions now results in the body of the theory method not being called, i.e. theories are no longer validated (and similarly, Assume.IsTrue() exits the theory method immediately).&lt;br /&gt;&lt;br /&gt;There were some very interesting discussions on the NUnit developer list recently which persuaded me of the error of my ways, most specifically, it can be too 'dangerous' to continue execution of a theory if the user has signalled that the data is invalid.&lt;br /&gt;&lt;br /&gt;You should validate that your assumptions are valid when writing theories, but this should be done in either another theory, or plain vanilla unit test.&lt;br /&gt;&lt;br /&gt;Comments welcome as always :)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/848045343259901678-9071821214022834265?l=taumuon-jabuka.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://taumuon-jabuka.blogspot.com/feeds/9071821214022834265/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=848045343259901678&amp;postID=9071821214022834265' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/848045343259901678/posts/default/9071821214022834265'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/848045343259901678/posts/default/9071821214022834265'/><link rel='alternate' type='text/html' href='http://taumuon-jabuka.blogspot.com/2008/04/maslina-and-rakija-updates-for-nunit.html' title='Maslina and Rakija updates for NUnit 2.4.7'/><author><name>Taumuon</name><uri>http://www.blogger.com/profile/12874508418428339669</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='27' src='http://bp0.blogger.com/_9XuDfr9pYU0/SGAMUjxkNvI/AAAAAAAAACs/YhujEd8cyMk/S220/colloseum.jpg'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-848045343259901678.post-7485973157118343150</id><published>2008-03-19T15:32:00.000-07:00</published><updated>2012-01-21T06:35:01.599-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='.NET'/><category scheme='http://www.blogger.com/atom/ns#' term='Unit testing'/><category scheme='http://www.blogger.com/atom/ns#' term='TDD'/><title type='text'>Comparing Theories to more traditional testing</title><content type='html'>My old work colleague &lt;a href="http://www.agilemicroisv.com/"&gt;Tim&lt;/a&gt; has recently blogged about using NSpec to &lt;a href="http://www.agilemicroisv.com/2008/03/executable-spec.html"&gt;specify a stack&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;NSpec has the same sort of functionality as a unit testing framework such as NUnit. The terminology has been changed to get over the roadblock that some people have in adopting tests.&lt;br /&gt;&lt;br /&gt;Theories actually give something over and above normal unit testing, and that's what I'm going to look at in this blog post. I'm going to look at Tim's example and show how using theories actually differ from Tim's more traditional example.&lt;br /&gt;&lt;br /&gt;The stack interface for which the implementation was arrived at via speccing is as follows:&lt;br /&gt;&lt;br /&gt;&lt;pre class="name" code="c-sharp"&gt;&lt;br /&gt;public class Stack&amp;lt;t&gt;&lt;br /&gt;{&lt;br /&gt;public Stack();&lt;br /&gt;public void Clear();&lt;br /&gt;public bool Contains(T item);&lt;br /&gt;public T Peek();&lt;br /&gt;public T Pop();&lt;br /&gt;public void Push(T item);&lt;br /&gt;&lt;br /&gt;// Properties&lt;br /&gt;public int Count { get; }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The following tests were arrived at:&lt;br /&gt;&lt;br /&gt;&lt;pre class="name" code="c-sharp"&gt;&lt;br /&gt;namespace Stack.Specs&lt;br /&gt;{&lt;br /&gt;[Context]&lt;br /&gt;public class WhenTheStackIsEmpty&lt;br /&gt;{&lt;br /&gt;    Stack&lt;int&gt; _stack = new Stack&amp;lt;int&gt;();&lt;br /&gt;&lt;br /&gt;    [Specification]&lt;br /&gt;    public void CountShouldBeZero()&lt;br /&gt;    {&lt;br /&gt;        Specify.That(_stack.Count).ShouldEqual(0);&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    [Specification]&lt;br /&gt;    public void PeekShouldThrowException()&lt;br /&gt;    {&lt;br /&gt;        MethodThatThrows mtt = delegate()&lt;br /&gt;           {&lt;br /&gt;               _stack.Peek();&lt;br /&gt;           };&lt;br /&gt;&lt;br /&gt;        Specify.ThrownBy(mtt).ShouldBeOfType(typeof(InvalidOperationException));&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;That's ample for us to discuss the difference between theories and more normal testing.&lt;br /&gt;&lt;br /&gt;For the PeekShouldThrowException test/specification, we can see from the naming of the context that the developer intends to show that for an empty stack, the Peek operation throws an exception. However, what the developer has actually shown is that calling Peek on a newly-created stack throws an exception.&lt;br /&gt;&lt;br /&gt;Developers tend to think in fairly general terms, and express this generality by using more specific cases. However, some of this generality can get lost. Theories aim to keep more of that generality.&lt;br /&gt;&lt;br /&gt;We can demonstrate this in a theory (don't take much note of the syntax, just the concepts)&lt;br /&gt;&lt;br /&gt;&lt;pre class="name2" code="c-sharp"&gt;&lt;br /&gt; [Theory]&lt;br /&gt; public void PeekOnEmptyStackShouldThrow(Stack&amp;lt;int&gt; stack)&lt;br /&gt; {&lt;br /&gt;     try&lt;br /&gt;     {&lt;br /&gt;         stack.Peek();&lt;br /&gt;         Assert.Fail(ExpectedExceptionNotThrown);&lt;br /&gt;     }&lt;br /&gt;     catch (InvalidOperationException) { }&lt;br /&gt; }&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;This states that calling Peek() on ANY stack should fail, we need to show that this is only true for an empty stack. We could do this by simply checking for this:&lt;br /&gt;&lt;br /&gt;&lt;pre class="somename" code="c-sharp"&gt;&lt;br /&gt; [Theory]&lt;br /&gt; public void PeekOnEmptyStackShouldThrow(Stack&amp;lt;int&gt; stack)&lt;br /&gt; {&lt;br /&gt;     try&lt;br /&gt;     {&lt;br /&gt;         if (stack.Count == 0)&lt;br /&gt;         {&lt;br /&gt;             stack.Peek();&lt;br /&gt;             Assert.Fail(ExpectedExceptionNotThrown);&lt;br /&gt;         }&lt;br /&gt;     }&lt;br /&gt;     catch (InvalidOperationException) { }&lt;br /&gt; }&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;But as we'll see in a bit, using assumptions gives us some extra feedback (again, don't focus on the syntax).&lt;br /&gt;&lt;br /&gt;&lt;pre class="name3" code="c-sharp"&gt;&lt;br /&gt; [Theory]&lt;br /&gt; [Assumption("AssumeStackIsEmpty")]&lt;br /&gt; public void PeekOnEmptyStackShouldThrow(Stack&amp;lt;int&gt; stack)&lt;br /&gt; {&lt;br /&gt;     try&lt;br /&gt;     {&lt;br /&gt;&lt;br /&gt;         stack.Peek();&lt;br /&gt;         Assert.Fail(ExpectedExceptionNotThrown);&lt;br /&gt;     }&lt;br /&gt;     catch (InvalidOperationException) { }&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; public bool AssumeStackIsEmpty(Stack&amp;lt;int&gt; stack)&lt;br /&gt; {&lt;br /&gt;     return stack.Count == 0;&lt;br /&gt; }&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;This is a much more general statement than the original specification/test, we're saying that the stack should fail if we try to Peek on it for ANY empty stack.&lt;br /&gt;&lt;br /&gt;We don't care whether this is a newly-created stack, or it is a stack which has been manipulated via its public interface. Also, Liskov Substitution Principle states that we should be able to use any classes derived from Stack, and the theories should hold true.&lt;br /&gt;&lt;br /&gt;We validate this theory with example data, in much the same way as when we're doing test-driven development. The extra power comes from the generality in the way that the theory is written - we can imagine a tool that performs static code analysis on the Stack class to confirm that it obeys this.&lt;br /&gt;&lt;br /&gt;However, the literature mentions that the most likely way to validate a theory is via an exploration phase, via a plug-in tool that will try various combinations of input data to look for anything that fails the theory.&lt;br /&gt;&lt;br /&gt;It is prohibilively expensive to explore every possible combination of inputs, imagine all the possible values of a double, or in our example, there are an infinite number of operations that could happen to a stack that gets passed in.&lt;br /&gt;&lt;br /&gt;This fits in nicely with the name theory with parallels with science - it's not feasible to prove it, but we look for data to disprove it.&lt;br /&gt;&lt;br /&gt;The example data is important for the red-green-refactor cycle. The exploration phase sits outside that - it finds which input data doesn't fit the theory, allowing the theory to be modifed. There are exploration tools in Java, and I haven't looked too much into it, but it may be possible to use Microsoft's &lt;a href="http://research.microsoft.com/Pex/"&gt;Pex&lt;/a&gt; as an exploration tool?&lt;br /&gt;&lt;br /&gt;Before I forget, this is a possible way to specify the example data for our stack:&lt;br /&gt;&lt;br /&gt;&lt;pre class="name4" code="c-sharp"&gt;&lt;br /&gt;  [Theory]&lt;br /&gt;  [Assumption("AssumeStackIsEmpty")]&lt;br /&gt;  [InlineData("EmptyStack", new Stack())]&lt;br /&gt;  [PropertyData("EmptiedStack")]&lt;br /&gt;  public void PeekOnEmptyStackShouldThrow(Stack&amp;lt;int&gt; stack)&lt;br /&gt;  {&lt;br /&gt;      try&lt;br /&gt;      {&lt;br /&gt;          stack.Peek();&lt;br /&gt;          Assert.Fail(ExpectedExceptionNotThrown);&lt;br /&gt;      }&lt;br /&gt;      catch (InvalidOperationException) { }&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  public List&amp;lt;exampledata&gt; EmptiedStack&lt;br /&gt;  {&lt;br /&gt;      get&lt;br /&gt;      {&lt;br /&gt;          List&amp;lt;exampledata&gt; data = new List&amp;lt;ExampleData&gt;();&lt;br /&gt;          Stack stack = new Stack();&lt;br /&gt;          stack.Push(2);&lt;br /&gt;          stack.Push(3);&lt;br /&gt;          stack.Pop();&lt;br /&gt;          stack.Pop();&lt;br /&gt;          data.Add(stack);&lt;br /&gt;          return data;&lt;br /&gt;      }&lt;br /&gt;  }&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;In my prototype extension, the assumptions are important and are validated, as they tell us something vital about the code. I think that all the information about the behaviour of the system is vital, and should be documented and validated, but there are varied opinions on the list. That's why I'm blogging - give me your feedback :)&lt;br /&gt;&lt;br /&gt;If the user changed the behaviour of Peek() such that it was valid on an empty stack (it may return a Null Object for certain generic types), then our assumption would not detect this if it was simply filtering the data - the assumption would say "Peek() fails, but only on empty stacks", whereas Peek() would not fail on empty stacks. See my previous post for the behaviours I have implemented.&lt;br /&gt;&lt;br /&gt;Notice in Tim's implementation how his stack is hardcoded to have at most 10 items. When TDDing we may make slightly less obviously limited implementations to get our tests to pass, but forget to add the extra test cases to show this limitation (the process of progressively adding more and more general test cases is called triangulation).&lt;br /&gt;&lt;br /&gt;When writing theories, the same process happens, but writing the theories as a more general statement means that a code reviewer/automated tool can see that the developer intended that we intended that we can push a new item onto ANY stack, not just a stack that contained 9 or less items.&lt;br /&gt;&lt;br /&gt;Any thoughts? Have I got the wrong end of the stick? If anyone found this post useful, I might full flesh out the equivalent of Tim's example.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/848045343259901678-7485973157118343150?l=taumuon-jabuka.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://taumuon-jabuka.blogspot.com/feeds/7485973157118343150/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=848045343259901678&amp;postID=7485973157118343150' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/848045343259901678/posts/default/7485973157118343150'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/848045343259901678/posts/default/7485973157118343150'/><link rel='alternate' type='text/html' href='http://taumuon-jabuka.blogspot.com/2008/03/comparing-theories-to-more-traditional.html' title='Comparing Theories to more traditional testing'/><author><name>Taumuon</name><uri>http://www.blogger.com/profile/12874508418428339669</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='27' src='http://bp0.blogger.com/_9XuDfr9pYU0/SGAMUjxkNvI/AAAAAAAAACs/YhujEd8cyMk/S220/colloseum.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-848045343259901678.post-609004072790246393</id><published>2008-03-19T13:57:00.000-07:00</published><updated>2012-01-21T06:35:01.600-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='.NET'/><category scheme='http://www.blogger.com/atom/ns#' term='Unit testing'/><category scheme='http://www.blogger.com/atom/ns#' term='TDD'/><title type='text'>Sample Theory Implementation as NUnit Extension.</title><content type='html'>There's been lots of comments bouncing around on the NUnit mailing list about what exactly constitutes a Theory, and what the desired features are, so I've created an NUnit extension with a sample Theory implementation - you can get it, Maslina version 1.0.0.0, from &lt;a href="http://www.taumuon.co.uk/rakija"&gt;www.taumuon.co.uk/rakija&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;xUnit.Net implements theories but does not have any in-built Assumption mechanism (you can effectively filter out bad data, which is the same as a filtering assumption). JUnit 4.4, I think, only filters out data - it doesn't tell us anything about the state of an assumption.&lt;br /&gt;&lt;br /&gt;Anyway, from reading the literature on theories (see my previous blog posting), I quite like the idea of having assumptions tell us something about the code, that those assumptions are validated.&lt;br /&gt;&lt;br /&gt;The syntax of my addin is quite poor, and there's not really enough validation of user input, but I'm aiming to try to do some theory-driven development (theorizing?) using it, to see what feels good and what grates.&lt;br /&gt;&lt;br /&gt;Any feedback gratefully received (especially - is it valid to say that this is an implementation of a Theory, are validation of assumptions useful or unnecessary fluff?)&lt;br /&gt;&lt;br /&gt;Here is the syntax of my extension.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt; [TestFixture]&lt;br /&gt; public class TheorySampleFixture&lt;br /&gt; {&lt;br /&gt;     [Theory]&lt;br /&gt;     [PropertyData("MyTestMethodData")]&lt;br /&gt;     [InlineData("Parity", new object[] { 1.0, 1.0, 1.0 })]&lt;br /&gt;     [InlineData("Parity 2", new object[] { 2.0, 2.0, 1.0 })]&lt;br /&gt;     [InlineData("Double Euros", new object[] { 2.0, 1.0, 2.0 })]&lt;br /&gt;     // This does not match the assumption, and will cause this&lt;br /&gt;     //specific theory Assert to fail, in which case we will get a pass overall.&lt;br /&gt;     // If the unit under test were changed to somehow handle zero exchange rate,&lt;br /&gt;     // the body of the theory method would pass, but the&lt;br /&gt;     // assumption would still not be met and overall we will register a failure.&lt;br /&gt;     [InlineData("ExchangeRate Assumption Check", new object[] { 2.0, 1.0, 0.0 })]&lt;br /&gt;     // This case will fail, there is an assumption that the dollar value is not three,&lt;br /&gt;     // but passing in a value of 3 doesn't cause a failure in the code, demonstrating&lt;br /&gt;     // that the assumption serves no purpose&lt;br /&gt;     [InlineData("This should fail, assumption met but no failure in method", new object[] { 3.0, 1.0, 3.0 })]&lt;br /&gt;     [Assumption("ConvertToEurosAndBackExchangeRateIsNotZero")]&lt;br /&gt;     [Assumption("DollarsNotThree")]&lt;br /&gt;     public void MyTheoryCanConvertToFromEuros(double amountDollars, double amountEuros, double exchangeRateDollarsPerEuro)&lt;br /&gt;     {&lt;br /&gt;         // Should check are equivalent within a tolerance&lt;br /&gt;         // Calls static method on Convert method&lt;br /&gt;         Assert.AreEqual(amountDollars, Converter.ConvertEurosToDollars(Converter.ConvertDollarsToEuros(amountDollars,&lt;br /&gt;         exchangeRateDollarsPerEuro), exchangeRateDollarsPerEuro));&lt;br /&gt;     }&lt;br /&gt;&lt;br /&gt;     // Assumption is that the exchange rate is not zero&lt;br /&gt;     public bool ConvertToEurosAndBackExchangeRateIsNotZero(double amountDollars, double amountEuros, double exchangeRateDollarsPerEuro)&lt;br /&gt;     {&lt;br /&gt;         // Should have a tolerance on this&lt;br /&gt;         return exchangeRateDollarsPerEuro != 0.0;&lt;br /&gt;     }&lt;br /&gt;&lt;br /&gt;     // Assume that dollar value not equal to three&lt;br /&gt;     // This is just to demonstrate that an invalid assumption results in a failure.&lt;br /&gt;     public bool DollarsNotThree(double amountDollars, double amountEuros, double exchangeRateDollarsPerEuro)&lt;br /&gt;     {&lt;br /&gt;         return amountDollars != 3.0;&lt;br /&gt;     }&lt;br /&gt;&lt;br /&gt;     /// Returns the data for MyTestMethod&lt;br /&gt;     ///&lt;br /&gt;     public IList&lt;theoryexampledatadetail&gt; MyTestMethodData&lt;br /&gt;     {&lt;br /&gt;         get&lt;br /&gt;         {&lt;br /&gt;             List&lt;theoryexampledatadetail&gt; details = new List&lt;theoryexampledatadetail&gt;();&lt;br /&gt;             details.Add(new TheoryExampleDataDetail("Some other case should pass", new object[] { 2.0, 20.0, 5.0}));&lt;br /&gt;             return details;&lt;br /&gt;         }&lt;br /&gt;     }&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; public static class Converter&lt;br /&gt; {&lt;br /&gt;     public static double ConvertEurosToDollars(double amountDollars,&lt;br /&gt;         double dollarsPerEuro)&lt;br /&gt;     {&lt;br /&gt;         return amountDollars * dollarsPerEuro;&lt;br /&gt;     }&lt;br /&gt;&lt;br /&gt;     public static double ConvertDollarsToEuros(double amountEuros,&lt;br /&gt;         double dollarsPerEuro)&lt;br /&gt;     {&lt;br /&gt;         return amountEuros / dollarsPerEuro;&lt;br /&gt;     }&lt;br /&gt; }&lt;br /&gt;&lt;/theoryexampledatadetail&gt;&lt;/theoryexampledatadetail&gt;&lt;/theoryexampledatadetail&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;A nicer syntax/api would be to have the assumptions inline:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;public void CanConvertToEurosAndBack(double amountDollars, double amountEuros, double exchangeRateDollarsPerEuro)&lt;br /&gt;{&lt;br /&gt;Assume.That(exchangeRateDollarsPerEuro != 0.0);&lt;br /&gt;Assume.That(amountDollars != 0.0);&lt;br /&gt;&lt;br /&gt;// Checks are equivalent within a tolerance&lt;br /&gt;// Calls static method on Convert method&lt;br /&gt;Assert.AreEqual&lt;double&gt;(amountDollars, Converter.ConvertEurosToDollars(Converter.ConvertDollarsToEuros(amountDollars,&lt;br /&gt;exchangeRateDollarsPerEuro),exchangeRateDollarsPerEuro));&lt;br /&gt;}&lt;br /&gt;&lt;/double&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Here's the rules of my Theory Implementation&lt;br /&gt;&lt;br /&gt;If there is no example data, the theory passes (we may want to change this in the future).&lt;br /&gt;If there are no assumptions for a theory, then each set of example data is executed against the theory each producing its own pass or fail.&lt;br /&gt;&lt;br /&gt;If assumptions exist, the each set of data is first validated against the assumption - if it meets the assumption, then the test proceeds and any test failure is flagged as an error.&lt;br /&gt;If the example data does not meet the assumption, then if the test passes it indicates that the assumption is invalid, and that case is marked as a failure, with a specific message "AssumptionFailed". Any assertion failures or exceptions in the actual theory code are treated as passes. (in the future, would we want to mark the specific exception expected in the test methdo if an assumption is not met?).&lt;br /&gt;&lt;br /&gt;NOTE: we may want to mark as a failure any theory for which ALL example data fails the assumptions, as a check that the&lt;br /&gt;actual body of the theory is actually being executed. I've not done this for now as it would be trickier with the current&lt;br /&gt;NUnit implementation.&lt;br /&gt;&lt;br /&gt;Similarly, I was thinking of failing if any of the assumptions weren't actually executed, but again, this is tricky in the current NUnit implementation (and may not give us much).&lt;br /&gt;&lt;br /&gt;Automated exploration would not follow the last two suggested rules. The automation API would need to generate its data and execute it as if it were inline data. It may be helpful for the automated tool to be able to retrieve the user-supplied example data, so it doesn't report a failure for any known case, but this is probably not necessary.&lt;br /&gt;&lt;br /&gt;Feedback on these rules would be most welcome. If you want to change the behaviour of the assumptions (i.e. have assumptions only filter and nothing more), then the behaviour can be changed in TheoryMethod.RunTestMethod()&lt;br /&gt;&lt;br /&gt;Here's the output of the above theory:&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_9XuDfr9pYU0/R-GCwWzBIOI/AAAAAAAAABo/pTunN-TdUXQ/s1600-h/Maslina.jpg"&gt;&lt;img style="cursor: pointer;" src="http://2.bp.blogspot.com/_9XuDfr9pYU0/R-GCwWzBIOI/AAAAAAAAABo/pTunN-TdUXQ/s320/Maslina.jpg" alt="" id="BLOGGER_PHOTO_ID_5179564813717676258" border="0" /&gt;&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/848045343259901678-609004072790246393?l=taumuon-jabuka.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://taumuon-jabuka.blogspot.com/feeds/609004072790246393/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=848045343259901678&amp;postID=609004072790246393' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/848045343259901678/posts/default/609004072790246393'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/848045343259901678/posts/default/609004072790246393'/><link rel='alternate' type='text/html' href='http://taumuon-jabuka.blogspot.com/2008/03/sample-theory-implementation-as-nunit.html' title='Sample Theory Implementation as NUnit Extension.'/><author><name>Taumuon</name><uri>http://www.blogger.com/profile/12874508418428339669</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='27' src='http://bp0.blogger.com/_9XuDfr9pYU0/SGAMUjxkNvI/AAAAAAAAACs/YhujEd8cyMk/S220/colloseum.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/_9XuDfr9pYU0/R-GCwWzBIOI/AAAAAAAAABo/pTunN-TdUXQ/s72-c/Maslina.jpg' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-848045343259901678.post-7972037494026904276</id><published>2008-03-13T14:17:00.000-07:00</published><updated>2012-01-21T06:35:01.600-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='.NET'/><category scheme='http://www.blogger.com/atom/ns#' term='Unit testing'/><category scheme='http://www.blogger.com/atom/ns#' term='TDD'/><title type='text'>Theories</title><content type='html'>I've just released a slightly updated version of my NUnit extension for data-driven unit testing.&lt;br /&gt;&lt;br /&gt;There's been a lot of discussion on the NUnit developer list recently regarding Theories - something new in JUnit and xUnit.Net, and it's taken a while to discover why they're so powerful (they're superficially very similar to data-driven unit tests, and a lot of the differences are semantics).&lt;br /&gt;&lt;br /&gt;First, there's some good background on theories written by David Saff:&lt;br /&gt;http://shareandenjoy.saff.net/tdd-specifications.pdf&lt;br /&gt;http://shareandenjoy.saff.net/2007/04/popper-and-junitfactory.html&lt;br /&gt;http://dspace.mit.edu/bitstream/1721.1/40090/1/MIT-CSAIL-TR-2008-002.pdf&lt;br /&gt;&lt;br /&gt;Theories on first glance look like a data-driven unit test, but I think that the most important difference is, is that:&lt;br /&gt;&lt;br /&gt;Theories are, in theory (excuse the pun), supposed to pass for ANY POSSIBLE parameters, whereas data-driven tests only express the behaviour examples that the developer has provided (they are nothing new in unit testing - just a way for a developer to more clearly group parameters together, or get the parameterized data from an external data source without recompiling tests).&lt;br /&gt;&lt;br /&gt;Theories are a generalized statement of how the program should run, whereas in TDDing, a very explicit statement of intent is made, which can be made to pass by coding that specific case in the implementation, and then the program is made to work by triangulization - expressing the generalization by giving more inputs. However, the theory literature points out that as we haven't passed in too many data points we can't be sure whether we've actually expressed what we meant.&lt;br /&gt;&lt;br /&gt;Theories, by forcing us to write our tests such that they take any inputs, are much more powerful a statement, and allow for the possible inputs to be explored with external tools.&lt;br /&gt;&lt;br /&gt;As an aside, one question I posted to the NUnit developer list regarding theories: "One thing that comes to mind, is that theories are written such that all possible inputs should pass. Apart from using a tool such as agitator, is there a way to test that the tests are written in a general way (I mean, if you had a theory that took parameters, but it totally ignored those parameters and worked as a vanilla unit test - i.e. created its own input), then it's not really a valid theory - is&lt;br /&gt;there a way to detect these cases? Probably not, but I was just idly wondering." Answers on a postcard to... well, I'd prefer a reply comment ;-)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/848045343259901678-7972037494026904276?l=taumuon-jabuka.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://taumuon-jabuka.blogspot.com/feeds/7972037494026904276/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=848045343259901678&amp;postID=7972037494026904276' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/848045343259901678/posts/default/7972037494026904276'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/848045343259901678/posts/default/7972037494026904276'/><link rel='alternate' type='text/html' href='http://taumuon-jabuka.blogspot.com/2008/03/theories.html' title='Theories'/><author><name>Taumuon</name><uri>http://www.blogger.com/profile/12874508418428339669</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='27' src='http://bp0.blogger.com/_9XuDfr9pYU0/SGAMUjxkNvI/AAAAAAAAACs/YhujEd8cyMk/S220/colloseum.jpg'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-848045343259901678.post-3483083950188450345</id><published>2008-02-02T15:06:00.002-08:00</published><updated>2012-01-21T06:35:01.601-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='.NET'/><category scheme='http://www.blogger.com/atom/ns#' term='Unit testing'/><category scheme='http://www.blogger.com/atom/ns#' term='TDD'/><title type='text'></title><content type='html'>This is a blog post to explain exactly what exactly parameterized data-driven unit testing is and why it’s there, and then to explain a bit about how this effected the implementation of the Rakija NUnit add-in.&lt;br /&gt;&lt;br /&gt;First, what is data-driven unit testing? It’s running tests with data which is driven from external data (as may be from an xml file or database). NUnit makes this quite difficult, as the tests are attribute driven, they are fixed, and fixed at the time that the tests are created.&lt;br /&gt;&lt;br /&gt;Data-driven fixtures.&lt;br /&gt;A common desire is to run fixtures with different set of parameters. An example of this is to test key input filtering under different UI cultures.&lt;br /&gt;&lt;br /&gt;This is a very contrived example, and the tests are in no way realistic, but it demonstrates what data-driven tests are about. My example is a simple class that takes various string inputs and performs operations on them. Here it is:&lt;br /&gt;&lt;br /&gt;   /// &lt;summary&gt;&lt;br /&gt;   /// Parses and operates on various string values.&lt;br /&gt;   /// &lt;/summary&gt;&lt;br /&gt;   public static class ValueParser&lt;br /&gt;   {&lt;br /&gt;       /// &lt;summary&gt;&lt;br /&gt;       /// Returns the addition of two strings representing&lt;br /&gt;       /// double values.&lt;br /&gt;       /// &lt;/summary&gt;&lt;br /&gt;       /// &lt;returns&gt;The combined value.&lt;/returns&gt;&lt;br /&gt;       public static double GetAdditiveValue&lt;br /&gt;           (string value1, string value2)&lt;br /&gt;       {&lt;br /&gt;           return double.Parse(value1) + double.Parse(value2);&lt;br /&gt;       }&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;And of course, the contributing unit test:&lt;br /&gt;&lt;br /&gt;   [TestFixture]&lt;br /&gt;   public class TestValueParser&lt;br /&gt;   {&lt;br /&gt;       [Test]&lt;br /&gt;       public void TestAdditiveValue()&lt;br /&gt;       {&lt;br /&gt;           Assert.AreEqual(4.5, ValueParser.GetAdditiveValue(&lt;br /&gt;               "2.3", "2.2"));&lt;br /&gt;       }&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;Of course in real life, we’d have tests to pass in garbage input, checking for any exceptions etc. Our class would also be much larger, containing many methods, along with many (maybe hundreds) of corresponding unit tests in our test fixture. We’ve happily deployed the first release of our software, and have many happy customers, when our customer representative or product manager informs us that we need our software to run on European users computers. We don’t get panicked, we know that our input data is coming from XML, and is formatted in invariant culture, but we need to make sure to change our code such that double.Parse takes in CultureInfo.InvariantCulture. We’d ideally like to ensure that our tests run in both cultures. We could duplicate our fixture with a different setup test, but we all know that duplicate code is bad, right? So we use the following pattern.&lt;br /&gt;&lt;br /&gt;   // NOTE: TestFixture class has been removed.&lt;br /&gt;   public class TestValueParser&lt;br /&gt;   {&lt;br /&gt;       protected string cultureString = "invalid";&lt;br /&gt;       private CultureInfo initialCulture;&lt;br /&gt;&lt;br /&gt;       [SetUp]&lt;br /&gt;       public void Setup()&lt;br /&gt;       {&lt;br /&gt;           this.initialCulture&lt;br /&gt;               = Thread.CurrentThread.CurrentCulture;&lt;br /&gt;           Thread.CurrentThread.CurrentCulture =&lt;br /&gt;               CultureInfo.GetCultureInfoByIetfLanguageTag(&lt;br /&gt;               cultureString);&lt;br /&gt;       }&lt;br /&gt;&lt;br /&gt;       [TearDown]&lt;br /&gt;       public void TearDown()&lt;br /&gt;       {&lt;br /&gt;           Thread.CurrentThread.CurrentCulture =&lt;br /&gt;               this.initialCulture;&lt;br /&gt;       }&lt;br /&gt;&lt;br /&gt;       [Test]&lt;br /&gt;       public void TestAdditiveValue()&lt;br /&gt;       {&lt;br /&gt;           Assert.AreEqual(4.5, ValueParser.GetAdditiveValue(&lt;br /&gt;               "2.3", "2.2"));&lt;br /&gt;       }&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   [TestFixture]&lt;br /&gt;   public class TestValueParserUSCulture : TestValueParser&lt;br /&gt;   {&lt;br /&gt;       public TestValueParserUSCulture()&lt;br /&gt;       {&lt;br /&gt;           cultureString = "en-US";&lt;br /&gt;       }&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   [TestFixture]&lt;br /&gt;   public class TestValueParserFRCulture : TestValueParser&lt;br /&gt;   {&lt;br /&gt;       public TestValueParserFRCulture()&lt;br /&gt;       {&lt;br /&gt;           cultureString = "fr-FR";&lt;br /&gt;       }&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;We leave all of our tests intact, we have to make some minor modifications to our original test fixture (remove the test fixture attribute, and do some setup work), but we’re able to test our fix very quickly (obviously we follow red bar/green bar/refactor, and write the tests before changing our code), and as we have a high test code the new release of the software works flawlessly, we finish early and spend the remaining time playing Crysis.&lt;br /&gt;&lt;br /&gt;This pattern is very common in NUnit, but I haven’t found it named anywhere, I call it the Test Fixture Inheritance Pattern. NOTE: This pattern is not supported in Visual Studio Team System 2005 testing, but it’s fixed in the 2008 version.&lt;br /&gt;&lt;br /&gt;(As an aside, I’ve read about the upcoming xUnit framework, and it says that tests aren’t grouped into fixtures, I’m not sure how it would cope with this pattern in that case? It’s possible that you could parameterized each test, and use something like a RowTest attribute to pass in the culture string, but by testing a new culture you’d have to add a new attribute to each and every test.)&lt;br /&gt;&lt;br /&gt;The first thing to notice about the Test Fixture Inheritance Pattern is that it isn’t very OO. It wouldn’t be too much work though to create an NUnit extension that would allow parameterizing the test fixture constructor, in a similar way to how the RowTest attribute works, i.e.&lt;br /&gt;&lt;br /&gt;   [TestFixture]&lt;br /&gt;   [Parameter("en-US")]&lt;br /&gt;   [Parameter("fr-FR")]&lt;br /&gt;   public class TestValueParser&lt;br /&gt;   {&lt;br /&gt;       protected string cultureString = "invalid";&lt;br /&gt;       private CultureInfo initialCulture;&lt;br /&gt;&lt;br /&gt;       public TestValueParser(string cultureString)&lt;br /&gt;       {&lt;br /&gt;           this.cultureString = cultureString;&lt;br /&gt;       }&lt;br /&gt;&lt;br /&gt;       [SetUp]&lt;br /&gt;       public void Setup()&lt;br /&gt;       {&lt;br /&gt;           this.initialCulture&lt;br /&gt;               = Thread.CurrentThread.CurrentCulture;&lt;br /&gt;           Thread.CurrentThread.CurrentCulture =&lt;br /&gt;               CultureInfo.GetCultureInfoByIetfLanguageTag(&lt;br /&gt;               cultureString);&lt;br /&gt;       }&lt;br /&gt;&lt;br /&gt;       [TearDown]&lt;br /&gt;       public void TearDown()&lt;br /&gt;       {&lt;br /&gt;           Thread.CurrentThread.CurrentCulture =&lt;br /&gt;               this.initialCulture;&lt;br /&gt;       }&lt;br /&gt;&lt;br /&gt;       [Test]&lt;br /&gt;       public void TestAdditiveValue()&lt;br /&gt;       {&lt;br /&gt;           Assert.AreEqual(4.5, ValueParser.GetAdditiveValue(&lt;br /&gt;               "2.3", "2.2"));&lt;br /&gt;       }&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;That would be nothing more than syntactic sugar though (it does make the tests look much neater). This could be more powerful though – it could be expanded to read data from a database or an XML file (again, similar to VSTS’s DataSource attribute).&lt;br /&gt;&lt;br /&gt;Rakija instead provides an interface IDynamicFixtureSpecifier, which is used on a class to specify which fixtures to create. Each fixture then must implement another interface, and the fixture need to implement IDynamicFixture. Our example now looks like this:&lt;br /&gt;&lt;br /&gt;   // NOTE: TestFixture attribute has been removed.&lt;br /&gt;   public class TestValueParser : IDynamicFixture&lt;br /&gt;   {&lt;br /&gt;       protected string cultureString = "invalid";&lt;br /&gt;       private CultureInfo initialCulture;&lt;br /&gt;&lt;br /&gt;       public TestValueParser(string cultureString)&lt;br /&gt;       {&lt;br /&gt;           this.cultureString = cultureString;&lt;br /&gt;       }&lt;br /&gt;&lt;br /&gt;       [SetUp]&lt;br /&gt;       public void Setup()&lt;br /&gt;       {&lt;br /&gt;           this.initialCulture&lt;br /&gt;               = Thread.CurrentThread.CurrentCulture;&lt;br /&gt;           Thread.CurrentThread.CurrentCulture =&lt;br /&gt;               CultureInfo.GetCultureInfoByIetfLanguageTag(&lt;br /&gt;               cultureString);&lt;br /&gt;       }&lt;br /&gt;&lt;br /&gt;       [TearDown]&lt;br /&gt;       public void TearDown()&lt;br /&gt;       {&lt;br /&gt;           Thread.CurrentThread.CurrentCulture =&lt;br /&gt;               this.initialCulture;&lt;br /&gt;       }&lt;br /&gt;&lt;br /&gt;       [Test]&lt;br /&gt;       public void TestAdditiveValue()&lt;br /&gt;       {&lt;br /&gt;           Assert.AreEqual(4.5, ValueParser.GetAdditiveValue(&lt;br /&gt;               "2.3", "2.2"));&lt;br /&gt;       }&lt;br /&gt;&lt;br /&gt;       #region IDynamicFixture Members&lt;br /&gt;&lt;br /&gt;       public string Name&lt;br /&gt;       {&lt;br /&gt;           get { return "TestValueParser " + cultureString; }&lt;br /&gt;       }&lt;br /&gt;&lt;br /&gt;       #endregion&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;The fact that this is a test fixture is detected from the add-in by the fact that it implements IDynamicFixture, I could have instead created a new attribute for the class, and either reflected for the Name property (or created an attribute to return the name), but that’s by-the-by.&lt;br /&gt;&lt;br /&gt;To actually specify the instances of our fixture, we use code like the following:&lt;br /&gt;&lt;br /&gt;   public class SpecifyFixtures : IDynamicFixtureSpecifier&lt;br /&gt;   {&lt;br /&gt;       #region IDynamicFixtureSpecifier Members&lt;br /&gt;&lt;br /&gt;       public Type FixtureType&lt;br /&gt;       {&lt;br /&gt;           get&lt;br /&gt;           {&lt;br /&gt;               return typeof(TestValueParser);&lt;br /&gt;           }&lt;br /&gt;       }&lt;br /&gt;&lt;br /&gt;       public IList&lt;idynamicfixture&gt; GetUserFixtures()&lt;br /&gt;       {&lt;br /&gt;           List&lt;idynamicfixture&gt; fixtures =&lt;br /&gt;               new List&lt;idynamicfixture&gt;(2);&lt;br /&gt;           fixtures.Add(new TestValueParser("fr-FR"));&lt;br /&gt;           fixtures.Add(new TestValueParser("en-US"));&lt;br /&gt;           return fixtures;&lt;br /&gt;       }&lt;br /&gt;&lt;br /&gt;       #endregion&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;Your first thought may be to wonder why I’ve created such a convoluted way of creating fixtures, when a page or so ago I showed how neatly this could be done by creating a new attribute. Well, this is much more powerful, we’re not limited to querying a database or reading an XML file – we could load up our assembly to test, and reflect over certain types in it, creating a new fixture for each type. The sky’s the limit.&lt;br /&gt;&lt;br /&gt;Parameterized test methods.&lt;br /&gt;I’ll quickly go over the motivation over parameterized test methods, before discussing why I’ve been forced to implement these myself due to the current NUnit (as of 2.4.6) extension interface, rather than deferring these to Kelly Anderson’s extension.&lt;br /&gt;&lt;br /&gt;Another common requirement when unit testing is to run a given test with different input data. Before the existence of any RowTest attribute, a common thing to do may have been.&lt;br /&gt;&lt;br /&gt;       [Test]&lt;br /&gt;       public void SomeTest()&lt;br /&gt;       {&lt;br /&gt;           // .. read some data in an xml file&lt;br /&gt;           // .. for each piece of data, perform some common testing&lt;br /&gt;           // .. { assert on the data; }&lt;br /&gt;       }&lt;br /&gt;&lt;br /&gt;This obviously works, but the problem of it is lack of visibility/feedback. For a file that may contain hundreds of pieces of data, we have a single red or green bar. We’d like to have visibility of each individual case. VSTS has a DataSource attribute to cope with this cases like this (reading from a database or XML File).&lt;br /&gt;&lt;br /&gt;Now that I’ve, very briefly, covered the motivations behind parameterized test cases, I’m going to go into the implementation details of Rakija.&lt;br /&gt;&lt;br /&gt;Kelly Anderson has created a more powerful interface, similar to Rakija’s, where a method can be decorated with an IterativeTest attribute to specify the test data for the test. (Kelly’s interface is much more user-friendly than Rakija’s).&lt;br /&gt;&lt;br /&gt;Unfortunately, I’ve had to duplicate the functionality of this Kelly’s work in Rakija, due to the internals of NUnit’s extensibility mechanism. I’ll explain why below, and propose a change to the mechanism to allow the extensions to co-exist.&lt;br /&gt;&lt;br /&gt;Similar to Rakija, Kelly’s mechanism creates a TestCaseBuilder to recognise the parameterized test cases. This works without any problem, but will fail when used with any of the above parameterized test fixtures described above.&lt;br /&gt;&lt;br /&gt;Kelly’s extension works by finding the method decorated with his IterativeTestAttribute, creating a new instance of the type that contains that attribute, and calling the method decorated by that attribute to find the parameters to create the test with. He gets away with creating a new instance of the type by the fact that all instances of the type will contain the same data.&lt;br /&gt;&lt;br /&gt;The problem comes when you have parameterized test cases – each test case has been created with different parameters, and depending on those constructor parameters we may wish to pass different parameters to our tests (our test fixtures may have been created with a given node of an xml file, and we may wish to pass various sub-nodes of that node to an XML file).&lt;br /&gt;&lt;br /&gt;Rakija is forced to deal with this situation by taking control of the parameterized test case situation when parameterized fixtures are also involved.&lt;br /&gt;&lt;br /&gt;The proposed fix, to allow dynamic fixtures to exist with any other extension (e.g. Kelly Anderson’s IterativeTest extension, or Andreas Schlapsi’s RowTest extension) is to make a change to the extensibility mechanism.&lt;br /&gt;&lt;br /&gt;Currently, NUnitTestFixtureBuilder (or our derived class), has to call&lt;br /&gt;&lt;br /&gt;this.testCaseBuilders.BuildFrom(method);&lt;br /&gt;&lt;br /&gt;where method is a MethodInfo&lt;br /&gt;And this forces each test case builder either call a static method, or construct a type to create a method on. The return type from this method is a test, which gets added onto the fixture/suite’s list of tests to run.&lt;br /&gt;&lt;br /&gt;In the parameterized fixture case, the interface on AbstractTestCaseBuilder  would better be:&lt;br /&gt;protected override TestCase MakeTestCase(object instance, MethodInfo method);&lt;br /&gt;&lt;br /&gt;or something similar, where instance would be the instance that contains the method. That would allow the method to be invoked on the instance that it lived on, allowing parameterized fixtures to co-exist with the other mentioned extensions.&lt;br /&gt;&lt;br /&gt;&lt;/idynamicfixture&gt;&lt;/idynamicfixture&gt;&lt;/idynamicfixture&gt;&lt;p class="MsoNormal"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/848045343259901678-3483083950188450345?l=taumuon-jabuka.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://taumuon-jabuka.blogspot.com/feeds/3483083950188450345/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=848045343259901678&amp;postID=3483083950188450345' title='7 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/848045343259901678/posts/default/3483083950188450345'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/848045343259901678/posts/default/3483083950188450345'/><link rel='alternate' type='text/html' href='http://taumuon-jabuka.blogspot.com/2008/02/this-is-blog-post-to-explain-exactly.html' title=''/><author><name>Taumuon</name><uri>http://www.blogger.com/profile/12874508418428339669</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='27' src='http://bp0.blogger.com/_9XuDfr9pYU0/SGAMUjxkNvI/AAAAAAAAACs/YhujEd8cyMk/S220/colloseum.jpg'/></author><thr:total>7</thr:total></entry><entry><id>tag:blogger.com,1999:blog-848045343259901678.post-6727362498003934081</id><published>2008-01-29T13:42:00.000-08:00</published><updated>2012-01-21T06:35:01.601-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='.NET'/><category scheme='http://www.blogger.com/atom/ns#' term='Simulation'/><title type='text'>Excuses</title><content type='html'>For not having another Jabuka post yet... Well, I've been really busy at my previous employers getting upto speed on various new technologies, then as you may have read on my other &lt;a href="http://taumuon.blogspot.com/"&gt;blog&lt;/a&gt;, I've been busy preparing for a new job (I might blog a bit about that at some point), and work always takes priority. I did have to create an interesting small WPF 3D application as part of my application process (I'll tart this up and get it online when I get chance).&lt;br /&gt;&lt;br /&gt;I haven't given up on Jabuka, I spent a lot of time over Christmas researching different constraint methods (LCP solvers, PGS solvers and Sequential Impulses), and will playing around with this as soon as I get a chance to breathe (oh, and I finally packaged up my NUnit data-driven testing extension, &lt;a href="http://www.taumuon.co.uk/rakija/"&gt;here&lt;/a&gt;).&lt;br /&gt;&lt;br /&gt;Otherwise, my new years resolution is to play with &lt;a href="http://research.microsoft.com/fsharp/fsharp.aspx"&gt;F#&lt;/a&gt; - I attended a &lt;a href="http://www.nxtgenug.net/"&gt;Next Gen Usergroup&lt;/a&gt; talk last year at Microsoft's Cambridge site by &lt;a href="http://blogs.msdn.com/dsyme/"&gt;Don Syme&lt;/a&gt;. Since then I've had time to do some reading around, but not to actually create anything in anger. Obviously, some functional ideas have influenced C# (lambda expressions etc.) but I want to see how working in a functional mindset might chance some of my ideas of designing code.&lt;br /&gt;&lt;br /&gt;There's been lots of blogging recently on immutability in C# (see &lt;a href="http://blogs.msdn.com/ericlippert/archive/2007/11/13/immutability-in-c-part-one-kinds-of-immutability.aspx"&gt;here&lt;/a&gt; and &lt;a href="http://blogs.msdn.com/lucabol/archive/2007/12/03/creating-an-immutable-value-object-in-c-part-i-using-a-class.aspx"&gt;here&lt;/a&gt;) recently - in functional languages everything is immutable, and obviously not having immutable state gets around guarding state with multiple threads, it's just the overall program design I can't picture yet (I'm used to thinking of looking for common interfaces, design patterns etc.) There's also new&lt;a href="http://msdn2.microsoft.com/en-us/concurrency/default.aspx"&gt; parallel/Concurrent&lt;/a&gt; resources available from Microsoft which do fit nicely into all this (see &lt;a href="http://through-the-interface.typepad.com/through_the_interface/2008/01/harnessing-f-as.html"&gt;F#'s asynchronous workflows&lt;/a&gt;).&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/848045343259901678-6727362498003934081?l=taumuon-jabuka.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://taumuon-jabuka.blogspot.com/feeds/6727362498003934081/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=848045343259901678&amp;postID=6727362498003934081' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/848045343259901678/posts/default/6727362498003934081'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/848045343259901678/posts/default/6727362498003934081'/><link rel='alternate' type='text/html' href='http://taumuon-jabuka.blogspot.com/2008/01/excuses.html' title='Excuses'/><author><name>Taumuon</name><uri>http://www.blogger.com/profile/12874508418428339669</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='27' src='http://bp0.blogger.com/_9XuDfr9pYU0/SGAMUjxkNvI/AAAAAAAAACs/YhujEd8cyMk/S220/colloseum.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-848045343259901678.post-1993050432885721003</id><published>2007-10-29T14:56:00.000-07:00</published><updated>2012-01-21T06:35:01.602-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='3D'/><category scheme='http://www.blogger.com/atom/ns#' term='.NET'/><category scheme='http://www.blogger.com/atom/ns#' term='OpenGL'/><category scheme='http://www.blogger.com/atom/ns#' term='Simulation'/><title type='text'>A change of approach/jabuka 0.6</title><content type='html'>As mentioned on the &lt;a href="http://www.taumuon.co.uk/jabuka/"&gt;Jabuka website&lt;/a&gt;, I'm moving away from a tutorial-based approach, mainly because of lack of time. It's much quicker to implement new code and then blog retrospectively, highlighting new features and interesting code/design decisions. In the tutorial based approach, making a change/bug fix in an earlier tutorial forces all later tutorials to be updated.&lt;br /&gt;&lt;br /&gt;That being said, it's worth discussing changes are in the latest version, &lt;a href="http://www.taumuon.co.uk/jabuka/Version0_6/JabukaVersion0.6.zip"&gt;0.6, of Jabuka&lt;/a&gt;.&lt;br /&gt;This code introduces a couple of simple steering behaviours, and flocking &lt;a href="http://www.red3d.com/cwr/boids/"&gt;boids&lt;/a&gt;. Given the roadmap, why did I suddenly go off on a total tangent and implement boids? Well, apart from flocking behaviour being especially cool (look on youtube for the Carling Belong commercial, or starling flocks), I wanted to stress the &lt;span style="font-style: italic;"&gt;engine&lt;/span&gt; (I use that term very loosely given the current functionality of Jabuka) as I'm itching to demonstrate multithreading and performance enhancements. This version of Jabuka has shown that it is still too early to look at performance, however, as the application still has some fundamental flaws to be addressed.&lt;br /&gt;&lt;br /&gt;The flocking application implements a flock (based on the algorithms on Chris Reynolds website, and roughly based on the algorithms in the &lt;a href="http://www.amazon.com/Killer-Game-Programming-Andrew-Davison/dp/0596007302"&gt;Killer Game Programming in Java&lt;/a&gt; book). The changes to Jabuka were that the force for a rigid body is obtained in the Update() method from introducing an IForceProvider interface, implemented by various behaviours.&lt;br /&gt;If you look at the implementation of the steering behaviours, you can see that there is a large number of classes implementing this interface, each performing a simple operation such as aggregation/limitation. This does lead to a proliferation of classes, but I'm happy to live with it for now as it makes unit testing easier, and may lead to more easily changing behaviour dynamically at runtime.&lt;br /&gt;&lt;br /&gt;The Update() method calling on the IForceProvider is not perfect in this case - the objects are iterated over, and each object's Update() method calls on its IForceProvider. For the steering behaviours, the behaviour compares the body's location to its neighbours to calculate its force and update its position. The problem is that the resulting locations are dependent on the order in which the bodies are updated. This has problems in measuring the flocking quality (more on this later), and it may be desired to update the steering forces independent of Update(), so we don't do this in every time step (as it's an expensive operation - O(2)?). Also, having the state of an object dependent on every other object on every timestep will get really messy when we introduce multithreading. Having each body's Update() method independent of any other means that it's much easier to parallelise/vectorise/split into multiple threads - it's likely we'd want to avoid making every IEulerRigidBody thread safe, and would want to implement the thread safety at a higher level (though I won't jump the gun, we'll get there soon enough).&lt;br /&gt;&lt;br /&gt;The sort of changes above hint that it may be better to have a Physics Manager class, that owns the Collision object, the rigid bodies, and also has knowledge of the IForceProviders and manages the overall sequence of operation.&lt;br /&gt;&lt;br /&gt;The larger problem with having the currently flocking behaviour, including collision detection, is that I haven't yet implemented resting contacts (well, any contact forces) apart from collisions, and as objects are forced towards each other, the simulation can halt. I've temporarily fixed this by introducing a scene where the boids don't have any collision detection. I've also introduced a scene which demonstrates the halting, with only two spheres.&lt;br /&gt;&lt;br /&gt;As to steering behaviours and flocking itself... I've introduced a scene for the seek behaviour, demonstrating that having the steering force directed towards the target is insufficient, the velocity tangential to the targed needs to be taken into account. The idea is to find the optimal trajectory towards the target. Reynolds copes with this by providing a force such that the body's velocity is adjusted to be the maximum velocity towards the target. This is better, but does mean that the body needs a concept of its maximum velocity (this works OK on Earth, as air friction gives a terminal velocity, but in space this is arbitrary). Also, the maximum velocity may vary - the maximum velocity into the wind is less than having a following wind, and may depend on a body's shape (or its orientation with respect to its velocity).&lt;br /&gt;&lt;br /&gt;Looking at the &lt;a href="http://www.red3d.com/cwr/steer/Arrival.html"&gt;arrive&lt;/a&gt; behaviour, where the speed is arbitrarily slowed down, the first thought that comes to mind is that a PID control towards the target would be desired. However, the problem is that the velocity tangential/perpendicular and distance from the target all need to be taken into account - a more complicated control scheme would need to be used, and that may be too computationally expensive.&lt;br /&gt;&lt;br /&gt;The current boid implementation currently uses the same proximity distance for all individual behaviours (separation, alignment, cohesion). It may be that for separation, a smaller proximity should be used. Also, currently, I'm using a simple sphere without taking into account the blind spot behind the boid.&lt;br /&gt;&lt;br /&gt;There are lots of tweaks to be made (and a Genetic Algorithm could be used to change these dynamically), but many of them are pointless without a method of measuring the quality of the flock. There is an interesting paper on that &lt;a href="http://www3.nccu.edu.tw/%7Eli/Publication/pdf/icara2006.pdf"&gt;here&lt;/a&gt;. Some background on the flock theory / the underlying physics of flocking, can be found &lt;a href="http://www.scienceblog.com/community/older/1998/A/199800332.html"&gt;here&lt;/a&gt;, and &lt;a href="http://angel.elte.hu/%7Evicsek/"&gt;here&lt;/a&gt;.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/848045343259901678-1993050432885721003?l=taumuon-jabuka.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://taumuon-jabuka.blogspot.com/feeds/1993050432885721003/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=848045343259901678&amp;postID=1993050432885721003' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/848045343259901678/posts/default/1993050432885721003'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/848045343259901678/posts/default/1993050432885721003'/><link rel='alternate' type='text/html' href='http://taumuon-jabuka.blogspot.com/2007/10/change-of-approachjabuka-06.html' title='A change of approach/jabuka 0.6'/><author><name>Taumuon</name><uri>http://www.blogger.com/profile/12874508418428339669</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='27' src='http://bp0.blogger.com/_9XuDfr9pYU0/SGAMUjxkNvI/AAAAAAAAACs/YhujEd8cyMk/S220/colloseum.jpg'/></author><thr:total>0</thr:total></entry></feed>
