Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A good example is the sieve of eratosthenes.</p> <p>A co-worker and I wrote similar sieves in C# and F#. The performance of the C# version was almost 10 times slower than it's functional counterpart written by my co-worker.</p> <p>There were probably some inefficiencies in the C# version that could have been cleaned up, but the F# version is noticeably faster.</p> <p>This sort of problem lends itself to being written in a functional language..</p> <p>Hope this helps some.</p> <p><strong>Edit -</strong></p> <p>Here's one of the C# examples using similar functionality to F#'s List.Partition. I'll keep looking for the F# example. I have hundreds of projects that it could be in, it's just a matter of sorting through all of my stuff to find it (I save everything I ever experiment with, so this could be time consuming.. lol)</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ListPartitionTest { public static class IEnumerableExtensions { public static KeyValuePair&lt;IEnumerable&lt;T&gt;, IEnumerable&lt;T&gt;&gt; Partition&lt;T&gt;(this IEnumerable&lt;T&gt; items, Func&lt;T, bool&gt; f) { return items.Aggregate( new KeyValuePair&lt;IEnumerable&lt;T&gt;, IEnumerable&lt;T&gt;&gt;(Enumerable.Empty&lt;T&gt;(), Enumerable.Empty&lt;T&gt;()), (acc, i) =&gt; { if (f(i)) { return new KeyValuePair&lt;IEnumerable&lt;T&gt;, IEnumerable&lt;T&gt;&gt;(acc.Key.Concat(new[] { i }), acc.Value); } else { return new KeyValuePair&lt;IEnumerable&lt;T&gt;, IEnumerable&lt;T&gt;&gt;(acc.Key, acc.Value.Concat(new[] { i })); } }); } } class PrimeNumbers { public int Floor { get; private set; } public int Ceiling { get; private set; } private IEnumerable&lt;int&gt; _sieve; public PrimeNumbers(int floor, int ceiling) { Floor = floor; Ceiling = ceiling; } public List&lt;int&gt; Go() { _sieve = Enumerable.Range(Floor, (Ceiling - Floor) + 1).ToList(); for (int i = (Floor &lt; 2) ? 2 : Floor; i &lt;= Math.Sqrt(Ceiling); i++) { _sieve = _sieve.Where(x =&gt; (x % i != 0 &amp;&amp; x != i)); foreach (int x in _sieve) { Console.Write("{0}, ", x); } Console.WriteLine(); } return _sieve.ToList(); } } class Program { static void Main(string[] args) { System.Diagnostics.Stopwatch s = new System.Diagnostics.Stopwatch(); int floor = 1; int ceiling = 10; s.Start(); PrimeNumbers p = new PrimeNumbers(floor, ceiling); p.Go(); //foreach (int i in p.Go()) Console.Write("{0} ", i); s.Stop(); Console.WriteLine("\n{0} to {1} completed in {2}", floor, ceiling, s.Elapsed.TotalMilliseconds); Console.ReadLine(); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload