code_junkie

How well does F# perform compared to C# [closed]

Nov 13th, 2011
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace ListPartitionTest
  7. {
  8. public static class IEnumerableExtensions
  9. {
  10. public static KeyValuePair<IEnumerable<T>, IEnumerable<T>> Partition<T>(this IEnumerable<T> items, Func<T, bool> f)
  11. {
  12. return items.Aggregate(
  13. new KeyValuePair<IEnumerable<T>, IEnumerable<T>>(Enumerable.Empty<T>(), Enumerable.Empty<T>()),
  14. (acc, i) =>
  15. {
  16. if (f(i))
  17. {
  18. return new KeyValuePair<IEnumerable<T>, IEnumerable<T>>(acc.Key.Concat(new[] { i }), acc.Value);
  19. }
  20. else
  21. {
  22. return new KeyValuePair<IEnumerable<T>, IEnumerable<T>>(acc.Key, acc.Value.Concat(new[] { i }));
  23. }
  24. });
  25. }
  26. }
  27.  
  28. class PrimeNumbers
  29. {
  30. public int Floor { get; private set; }
  31. public int Ceiling { get; private set; }
  32.  
  33. private IEnumerable<int> _sieve;
  34.  
  35. public PrimeNumbers(int floor, int ceiling)
  36. {
  37. Floor = floor;
  38. Ceiling = ceiling;
  39. }
  40.  
  41. public List<int> Go()
  42. {
  43. _sieve = Enumerable.Range(Floor, (Ceiling - Floor) + 1).ToList();
  44.  
  45. for (int i = (Floor < 2) ? 2 : Floor; i <= Math.Sqrt(Ceiling); i++)
  46. {
  47. _sieve = _sieve.Where(x => (x % i != 0 && x != i));
  48.  
  49. foreach (int x in _sieve)
  50. {
  51. Console.Write("{0}, ", x);
  52. }
  53. Console.WriteLine();
  54. }
  55.  
  56. return _sieve.ToList();
  57. }
  58. }
  59.  
  60. class Program
  61. {
  62. static void Main(string[] args)
  63. {
  64. System.Diagnostics.Stopwatch s = new System.Diagnostics.Stopwatch();
  65. int floor = 1;
  66. int ceiling = 10;
  67.  
  68. s.Start();
  69. PrimeNumbers p = new PrimeNumbers(floor, ceiling);
  70. p.Go();
  71. //foreach (int i in p.Go()) Console.Write("{0} ", i);
  72. s.Stop();
  73.  
  74. Console.WriteLine("n{0} to {1} completed in {2}", floor, ceiling, s.Elapsed.TotalMilliseconds);
  75.  
  76. Console.ReadLine();
  77. }
  78. }
  79. }
Add Comment
Please, Sign In to add comment