Memoize your functions with an extension method

by rsutton 5. August 2009 04:32

Recently I made a post on how to create memoized functions using a class.  Well today I came across a post that shows how to do the same thing using an extension method, so I thought I would share the code.

 

    public static class Extensions

    {

        public static Func<A, R> Memoize<A, R>(this Func<A, R> f)

        {

          var map = new Dictionary<A, R>();

          return a =>

            {

              R value;

              if (map.TryGetValue(a, out value))

                return value;

              value = f(a);

              map.Add(a, value);

              return value;

            };

        }

    }

 

Then you use it like so.

 

    class TestIt

    {

        public int DoIt(string parameter)

        {

            // Do lots of processing...    

 

            return 0;    

        }

 

        public void Test()

        {

            // Wrap the function and then use the Memoize extension method.

            var func = new Func<string, int>(DoIt).Memoize();

 

            // The function can then be executed as it if were a normal function.

            // This is different than before because you had to call execute.

            var result = func("process me");

        }

    }

 

The only limitation is that you have to create different extension methods based on the number of parameters.

Tags:

Development

How to use the same email address for multiple twitter accounts

by rsutton 3. August 2009 03:15

As anyone who has multiple twitter accounts knows you have to have a unique email address for each account.  Well this is of course a pain if you are like me and use gmail for pretty much everything.  There is a work around for this which will allow you to use the same email address for multiple twitter accounts.  It involves using a little know part of the standard used for email addresses.  Basically if you add a plus sign after the first part of your address you can add anything you want after it and it will still go to your account.  Here are some examples.

theman@domain.com

theman+twitteracct1@domain.com

theman+twitteracct2@domain

If you are using gmail or some other email host that supports this portion of the standard all of these emails will go to theman@domain.com.  So in your twitter account settings you can use something like theman+twitter1@domain.com for the first account, then theman+twitter2@domain.com for the second and so on and so forth.  This is also useful for filtering, so instead of giving out your normal address to websites you can give out addresses like theman+spamsite@domain.com and then just create a filter in gmail that will send any emails directly to the trash.

 

P.S.  Please don’t send email to theman@domain.com because I don’t know who that is.

Tags:

Powered by BlogEngine.NET 1.4.5.0
Theme by Extensive SEO

Profile

Member of the Church of Jesus Christ of Latter-Day Saints, Developer and IT Professional