summaryrefslogtreecommitdiff
path: root/lib/FuncExtensions.cs
blob: 252485a946083858411cc9d7eaa8ceee238166f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
using System;

namespace jive
{
  static public class FuncExtensions
  {
    static public readonly object mutex = new object();

    static public Func<T> memoize<T>(this Func<T> item) where T : class
    {
      T the_implementation = null;
      return () =>
      {
        if (null == the_implementation)
        {
          lock (mutex)
          {
            if (null == the_implementation)
            {
              the_implementation = item();
            }
          }
        }
        return the_implementation;
      };
    }
  }
}