<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Idiomatic F# – Functional vs. Imperative</title><link>http://blogs.msdn.com/b/chrsmith/archive/2009/04/23/idiomatic-f-functional-vs-imperative.aspx</link><description>Our story begins with this guy, Stuart Bowers, sipping functional programming cool aid out of a kickass mug from Cafe Press . Stuart is a developer on Amalga and full-time F# developer at Microsoft. The world is at a loss because this man does not blog</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>re: Idiomatic F# – Functional vs. Imperative</title><link>http://blogs.msdn.com/b/chrsmith/archive/2009/04/23/idiomatic-f-functional-vs-imperative.aspx#9567047</link><pubDate>Fri, 24 Apr 2009 20:13:45 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9567047</guid><dc:creator>ChrSmith</dc:creator><description>&lt;p&gt;Some great responses! To answer the two big points, yes the example is contrived and yes it does have a data race. The imperative example was one from my book to set the stage for introducing the lock function so it was intentional.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9567047" width="1" height="1"&gt;</description></item><item><title>re: Idiomatic F# – Functional vs. Imperative</title><link>http://blogs.msdn.com/b/chrsmith/archive/2009/04/23/idiomatic-f-functional-vs-imperative.aspx#9566904</link><pubDate>Fri, 24 Apr 2009 18:50:12 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9566904</guid><dc:creator>James Hugard</dc:creator><description>&lt;p&gt;(Man, wish I could edit my posts)&lt;/p&gt;
&lt;p&gt;On a sufficiently large number of cores, I suppose one would also like to make sure the code does not reduce to the trivial case of summing 0 or 1 elements on each thread:&lt;/p&gt;
&lt;p&gt;open System.Threading&lt;/p&gt;
&lt;p&gt;let inline sumAsync (xs : 'a seq) =&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;let len = Seq.length xs&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;let n = min len System.Environment.ProcessorCount&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;let sliceSize = len / n&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;if sliceSize &amp;lt; 2&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;then&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;xs |&amp;gt; Seq.sum&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;else&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;let sumSliceAsync sliceN size =&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;let start = (sliceN-1) * size&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;let size &amp;nbsp;= if sliceN &amp;lt; n then size else len - start&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;async { return xs |&amp;gt; Seq.skip start |&amp;gt; Seq.take size |&amp;gt; Seq.sum }&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{1..n}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;|&amp;gt; Seq.map (fun sliceN -&amp;gt; sumSliceAsync sliceN sliceSize)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;|&amp;gt; Async.Parallel&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;|&amp;gt; Async.Run&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;|&amp;gt; Array.sum&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9566904" width="1" height="1"&gt;</description></item><item><title>re: Idiomatic F# – Functional vs. Imperative</title><link>http://blogs.msdn.com/b/chrsmith/archive/2009/04/23/idiomatic-f-functional-vs-imperative.aspx#9566883</link><pubDate>Fri, 24 Apr 2009 18:32:51 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9566883</guid><dc:creator>Jason Kikel</dc:creator><description>&lt;p&gt;@James&lt;/p&gt;
&lt;p&gt;I believe the unsafe sharing of data between threads is intentional and was being used to demonstrate problems of imperatively sharing data between thread pools. &amp;nbsp;Chris was just posing the question of imperative vs functional as a tangent to that example.&lt;/p&gt;
&lt;p&gt;Further, your code solves the problem nicely but can be improved by leveraging the parallelfx library (PLINQ in particular).&lt;/p&gt;
&lt;p&gt;#light&lt;/p&gt;
&lt;p&gt;open System&lt;/p&gt;
&lt;p&gt;open System.Linq&lt;/p&gt;
&lt;p&gt;let psum (coll:IParallelEnumerable&amp;lt;'a&amp;gt;) = &lt;/p&gt;
&lt;p&gt; &amp;nbsp; ParallelEnumerable.Sum(coll)&lt;/p&gt;
&lt;p&gt;let inline sumAsync (xs : 'a seq) =&lt;/p&gt;
&lt;p&gt; &amp;nbsp; xs.AsParallel()&lt;/p&gt;
&lt;p&gt; &amp;nbsp; |&amp;gt; psum &amp;nbsp;&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9566883" width="1" height="1"&gt;</description></item><item><title>re: Idiomatic F# – Functional vs. Imperative</title><link>http://blogs.msdn.com/b/chrsmith/archive/2009/04/23/idiomatic-f-functional-vs-imperative.aspx#9566856</link><pubDate>Fri, 24 Apr 2009 18:17:45 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9566856</guid><dc:creator>James Hugard</dc:creator><description>&lt;p&gt;Oops. &amp;nbsp;Forgot about lengths that don't evenly divide by the number of cores! &amp;nbsp;Also, forgot about handling lengths smaller than the number of cores! (note, the inline is needed to allow operation on any type 'a)&lt;/p&gt;
&lt;p&gt;Try this on for size:&lt;/p&gt;
&lt;p&gt;let inline sumAsync (xs : 'a seq) =&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;let len = Seq.length xs&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;let n = min len System.Environment.ProcessorCount&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;let sliceSize = len / n&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;let sumSliceAsync sliceN size =&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;let start = (sliceN-1) * size&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;let size &amp;nbsp;= if sliceN &amp;lt; n then size else len - start&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;async { return xs |&amp;gt; Seq.skip start |&amp;gt; Seq.take size |&amp;gt; Seq.sum }&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{1..n}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;|&amp;gt; Seq.map (fun sliceN -&amp;gt; sumSliceAsync sliceN sliceSize)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;|&amp;gt; Async.Parallel&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;|&amp;gt; Async.Run&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;|&amp;gt; Seq.sum&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9566856" width="1" height="1"&gt;</description></item><item><title>re: Idiomatic F# – Functional vs. Imperative</title><link>http://blogs.msdn.com/b/chrsmith/archive/2009/04/23/idiomatic-f-functional-vs-imperative.aspx#9566801</link><pubDate>Fri, 24 Apr 2009 17:44:57 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9566801</guid><dc:creator>James Hugard</dc:creator><description>&lt;p&gt;@Chris &amp;amp; Stuart - Won't the value of 'total' often be wrong?&lt;/p&gt;
&lt;p&gt;I fear that I'm missing something fundemental here. &amp;nbsp;Both examples are multithreaded, both examples update a shared mutable value, but neither protects accesss to that variable in any way. &amp;nbsp;If both threads retrieve the current value of 'total' (the same value), then add something to it, then store the result back, won't one thread step on the update of the other?&lt;/p&gt;
&lt;p&gt;@jcmincke - looks like your example does not have that problem &amp;amp; is more succint to boot! &amp;nbsp;However, it makes copies of the arrays which could be a performance issue memory wise.&lt;/p&gt;
&lt;p&gt;Respectfully, all examples will run slower on a single-core by forcing two threads. &amp;nbsp;They are also limited to arrays of ints.&lt;/p&gt;
&lt;p&gt;The following adapts to the number of cores and can work on any kind of sequence. &amp;nbsp;It pays the cost of finding the length of a sequence and the cost of skipping sequence elements (sequentially), but should run faster on large arrays as more cores are added.&lt;/p&gt;
&lt;p&gt;#light&lt;/p&gt;
&lt;p&gt;open System.Threading&lt;/p&gt;
&lt;p&gt;let inline sumAsync (xs : 'a seq) =&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;let n = System.Environment.ProcessorCount&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;let slice = Seq.length xs / n&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;let sumSliceAsync start size =&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;async { return xs |&amp;gt; Seq.skip start |&amp;gt; Seq.take size |&amp;gt; Seq.sum }&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{1..n}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;|&amp;gt; Seq.map (fun n -&amp;gt; sumSliceAsync ((n-1)*slice) slice)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;|&amp;gt; Async.Parallel&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;|&amp;gt; Async.Run&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;|&amp;gt; Seq.sum&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9566801" width="1" height="1"&gt;</description></item><item><title>re: Idiomatic F# – Functional vs. Imperative</title><link>http://blogs.msdn.com/b/chrsmith/archive/2009/04/23/idiomatic-f-functional-vs-imperative.aspx#9566626</link><pubDate>Fri, 24 Apr 2009 15:48:11 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9566626</guid><dc:creator>Jason Kikel</dc:creator><description>&lt;p&gt;In general I like the functional style more, but I'm not sure your example is fair. &amp;nbsp;I can see what you are trying to demonstrate, but the situation is contrived. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;For example, the imperative solution doesn't have to have two separate thread pool functions. &amp;nbsp;The core difference between the imperative and functional examples really is whether the thread pool functionality is passed to a higher order function (List.map, List.iter) or whether the functions are manually invoked individually. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;We can create a sumElements function in the imperative solution and call it twice with different ranges without any of the negatives you describe (hidden data, redundancy, or losing code reuse).&lt;/p&gt;
&lt;p&gt;At the end of the day, jcmincke's snippet is likely the 'best' approach, but I feel that's outside of what you're trying to illustrate with this example.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9566626" width="1" height="1"&gt;</description></item><item><title>re: Idiomatic F# – Functional vs. Imperative</title><link>http://blogs.msdn.com/b/chrsmith/archive/2009/04/23/idiomatic-f-functional-vs-imperative.aspx#9566257</link><pubDate>Fri, 24 Apr 2009 10:46:21 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9566257</guid><dc:creator>jcmincke</dc:creator><description>&lt;p&gt;My 2 cents here:&lt;/p&gt;
&lt;p&gt;open System.Threading&lt;/p&gt;
&lt;p&gt;let sumArray (arr : int[]) =&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;let mid = (arr.Length / 2 )&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;let subArr1 = Array.sub arr 0 &amp;nbsp;mid&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;let subArr2 = Array.sub arr (mid) (arr.Length-mid)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;let task1 = async { return Array.sum subArr1 }&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;let task2 = async { return Array.sum subArr2 }&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;let partialSums = Async.Run (Async.Parallel [ task1; task2 ])&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;let r = (Array.get partialSums 0) + (Array.get partialSums 1)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;r&lt;/p&gt;
&lt;p&gt;Advantages:&lt;/p&gt;
&lt;p&gt;- Shorter code, async manages all threading aspects.&lt;/p&gt;
&lt;p&gt;- No side effect&lt;/p&gt;
&lt;p&gt;Regards&lt;/p&gt;
&lt;p&gt;J-C&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9566257" width="1" height="1"&gt;</description></item><item><title>re: Idiomatic F# – Functional vs. Imperative</title><link>http://blogs.msdn.com/b/chrsmith/archive/2009/04/23/idiomatic-f-functional-vs-imperative.aspx#9565502</link><pubDate>Fri, 24 Apr 2009 00:27:15 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9565502</guid><dc:creator>Keith Farmer</dc:creator><description>&lt;p&gt;I'd as soon use the PLINQ to handle the parallelization, but that wouldn't be useful to discussing F# imperatively. :)&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9565502" width="1" height="1"&gt;</description></item><item><title>re: Idiomatic F# – Functional vs. Imperative</title><link>http://blogs.msdn.com/b/chrsmith/archive/2009/04/23/idiomatic-f-functional-vs-imperative.aspx#9565487</link><pubDate>Fri, 24 Apr 2009 00:15:37 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9565487</guid><dc:creator>Tom Jakcson</dc:creator><description>&lt;p&gt;I really do like the second one, I think it's worth the ink for the reader.&lt;/p&gt;
&lt;p&gt;1. It's only longer because it's far more verbose about the meaning of the values (it gives them names instead of using them inline)&lt;/p&gt;
&lt;p&gt;2. It only appears to have more concepts because fewer concepts are buried in the code (See #1).&lt;/p&gt;
&lt;p&gt;I will admit, however, than the pipeline stuff at the end is a little dense.&lt;/p&gt;
&lt;p&gt;-tjackson&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9565487" width="1" height="1"&gt;</description></item><item><title>re: Idiomatic F# – Functional vs. Imperative</title><link>http://blogs.msdn.com/b/chrsmith/archive/2009/04/23/idiomatic-f-functional-vs-imperative.aspx#9565253</link><pubDate>Thu, 23 Apr 2009 21:42:37 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9565253</guid><dc:creator>MichaelGG</dc:creator><description>&lt;p&gt;Well, the top sample is more &amp;quot;throw away&amp;quot;. You could lighten up the bottom one by not adding let bindings for the indicies and delegates. Just write them inline in the list.&lt;/p&gt;
&lt;p&gt;I also wouldn't use List.map. I think I'd go with:&lt;/p&gt;
&lt;p&gt;&amp;quot; |&amp;gt; List.iter (ThreadPool.QueueUserWorkItem &amp;gt;&amp;gt; ignore) &amp;quot;&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9565253" width="1" height="1"&gt;</description></item></channel></rss>