1. Devin Martin
  2. FlipSideOut.Linqpad

Commits

Devin Martin  committed 66a4be3

Make memoize an extension method

  • Participants
  • Parent commits 6c5e07f
  • Branches master

Comments (0)

Files changed (4)

File FlipSideOut.Linqpad.Tests/FunctionalTests.cs

View file
                 return i + 1;
             };
 
-            var memoizedCountAction = Functional.Memoize(countAction);
+            var memoizedCountAction = countAction.Memoize();
 
             memoizedCountAction(1).Should().Be(2);
             invocationCount.Should().Be(1);

File FlipSideOut.Linqpad/FlipSideOut.Linqpad.csproj

View file
     <Compile Include="EnumerableExtensions.cs" />
     <Compile Include="FluentMathExtensions.cs" />
     <Compile Include="FluentRangeExtensions.cs" />
-    <Compile Include="Functional.cs" />
+    <Compile Include="FunctionalExtensions.cs" />
     <Compile Include="Histogram.cs" />
     <Compile Include="Quantile.cs" />
     <Compile Include="RandomEnumerables.cs" />

File FlipSideOut.Linqpad/Functional.cs

-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-
-namespace FlipSideOut.Linqpad
-{
-    public class Functional
-    {
-        public static Func<TInput, TReturn> Memoize<TInput, TReturn>(Func<TInput, TReturn> f)
-        {
-            var map = new Dictionary<TInput, TReturn>();
-            return a =>
-            {
-                TReturn value;
-                if (map.TryGetValue(a, out value))
-                    return value;
-                value = f(a);
-                map.Add(a, value);
-                return value;
-            };
-        }
-    }
-}

File FlipSideOut.Linqpad/FunctionalExtensions.cs

View file
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace FlipSideOut.Linqpad
+{
+    public static class FunctionalExtensions
+    {
+        public static Func<TInput, TReturn> Memoize<TInput, TReturn>(this Func<TInput, TReturn> f)
+        {
+            var map = new Dictionary<TInput, TReturn>();
+            return a =>
+            {
+                TReturn value;
+                if (map.TryGetValue(a, out value))
+                    return value;
+                value = f(a);
+                map.Add(a, value);
+                return value;
+            };
+        }
+    }
+}