Memoization decorator for functions taking a nonzero number of arguments.

    References:
        http://code.activestate.com/recipes/578231-fastest-memoization-decorator
968235
def memoize_nonzero(func):
    
    class _memorizer(dict):
        def __init__(self, func):
            self.func = func
        def __call__(self, *args):
            return self[args]
        def __missing__(self, key):
            ret = self[key] = self.func(*key)
            return ret
    return _memorizer(func)
180218
def memoize(obj):
    
    cache = obj.cache = {}

    @functools.wraps(obj)
    def memoizer(*args, **kwargs):
        key = str(args) + str(kwargs)
        if key not in cache:
            cache[key] = obj(*args, **kwargs)
        # only keep the most recent 100 entries
        if len(cache) > 100:
            cache.popitem(last=False)
        return cache[key]
    return memoizer
950502
def decorator(f):
    """"""
    @functools.wraps(f)
    def decoratored_function(*args, **kwargs):
        if args and len(args) == 1:
            return f(*args, **kwargs)

        if args:
            raise TypeError(
                "This decorator only accepts extra keyword arguments.")

        return lambda g: f(g, **kwargs)

    return decoratored_function
1005865
public static <T1, T2, R> Function2<T1, T2, R> memoizeBiFunction(final BiFunction<T1, T2, R> fn) {
        Function1<Tuple2<T1, T2>, R> memoise2 = memoizeFunction((final Tuple2<T1, T2> pair) -> fn.apply(pair._1(), pair._2()));
        return (t1, t2) -> memoise2.apply(tuple(t1, t2));
    }
878104
public static <T1, T2, T3, R> Function3<T1, T2, T3, R> memoizeTriFunction(final Function3<T1, T2, T3, R> fn, final Cacheable<R> cache) {
      Function1<Tuple3<T1, T2, T3>, R> memoise2 = memoizeFunction((final Tuple3<T1, T2, T3> triple) -> fn.apply(triple._1(), triple._2(), triple._3()), cache);
        return (t1, t2, t3) -> memoise2.apply(tuple(t1, t2, t3));
    }
219981
def memoize(function):
    
    cache = {}
    @functools.wraps(function)
    def _memoize(*args):
        if args in cache:
            return cache[args]
        result = function(*args)
        cache[args] = result
        return result
    return function
61742
public static <T1, T2, T3, T4, R> Function4<T1, T2, T3, T4, R> memoizeQuadFunction(final Function4<T1, T2, T3, T4, R> fn) {
      Function1<Tuple4<T1, T2, T3, T4>, R> memoise2 = memoizeFunction((final Tuple4<T1, T2, T3, T4> quad) -> fn.apply(quad._1(), quad._2(), quad._3(), quad._4()));
        return (t1, t2, t3, t4) -> memoise2.apply(tuple(t1, t2, t3, t4));
    }
82355
def memoized(func):
    
    cache = {}
    @wraps(func)
    def memoized_function(*args):
        """The decorated function.
        """
        try:
            return cache[args]
        except KeyError:
            value = func(*args)
            try:
                cache[args] = value
            except MemoryError:
                cache.clear()
                gc.collect()
            return value
    return memoized_function
248294
def operations():
    
    def decorator(func):
        @wraps(func)
        def wrapped_func(*args, **kwargs):

            self = args[0]
            assert self.__can_use, "User operation queue only in 'with' block"

            def defaults_dict():
                f_args, varargs, keywords, defaults = inspect.getargspec(func)
                defaults = defaults or []
                return dict(zip(f_args[-len(defaults)+len(args[1:]):], defaults[len(args[1:]):]))

            route_args = dict(defaults_dict().items() + kwargs.items())

            func(*args, **kwargs)
            self.operations.append((func.__name__, args[1:], route_args, ))

        return wrapped_func
    def decorate(clazz):

        for attr in clazz.__dict__:
            if callable(getattr(clazz, attr)):
                setattr(clazz, attr, decorator(getattr(clazz, attr)))
        def __init__(self):  # simple parameter-less constructor
            self.operations = []
            self.__can_use = True
        def invalidate(self):
            self.__can_use = False
        clazz.__init__ = __init__
        clazz.invalidate = invalidate
        return clazz
    return decorate
1082012
public static <T, R> Function1<T, R> memoizeFunction(final Function<T, R> fn) {
        final Map<T, R> lazy = new ConcurrentHashMap<>();
        LazyImmutable<R> nullR = LazyImmutable.def();
        return t -> t==null? nullR.computeIfAbsent(()->fn.apply(null)) : lazy.computeIfAbsent(t, fn);
    }
1358656
def _prepare_memoization_key(args, kwargs):
    
    key_list = []
    for arg in args:
        try:
            hash(arg)
            key_list.append(arg)
        except:
            key_list.append(repr(arg))
    for (k, v) in kwargs.items():
        try:
            hash(k)
            hash(v)
            key_list.append((k, v))
        except:
            key_list.append((repr(k), repr(v)))
    return tuple(key_list)
371742
def times(self, func, *args):
        
        n = self.obj
        i = 0
        while n is not 0:
            n -= 1
            func(i)
            i += 1

        return self._wrap(func)
708730
def cached(size):
    """"""
    def decorator(func):
        cached_func = _Cached(func, size)
        return lambda *a, **kw: cached_func(*a, **kw)
    return decorator
821854
def decorator(cls, func_or_class):
        """"""
        resources = getattr(func_or_class, 'resources', [])
        resources.append(cls)
        func_or_class.resources = resources
        return func_or_class
661463
public static <T1, T2, R> BiFunction<T1, T2, R> memoize(final BiFunction<T1, T2, R> biFunc) {
    final Function<T1, Function<T2, R>> transformed = Memoizer.memoize(x -> Memoizer.memoize(y -> biFunc.apply(x, y)));
    return Transformations
        .<T1, T2, R>unCurryBiFunction()
        .apply(transformed);
  }
1280502
def _memo(f):
    """"""
    memos = {}
    def memoized(*args):
        try: return memos[args]
        except KeyError:
            result = memos[args] = f(*args)
            return result
    return memoized
1292146
function memoizeCapped(func) {
        var result = memoize(func, function(key) {
            if (cache.size === MAX_MEMOIZE_SIZE) {
                cache.clear();
            }
            return key;
        });

        var cache = result.cache;
        return result;
    }