Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(107)

Side by Side Diff: appengine/findit/libs/math/functions.py

Issue 2548603003: Adding memoized functions (Closed)
Patch Set: Addressing nits Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 # Copyright 2016 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
4
5
6 class Function(object):
7 """Base class for mathematical functions.
8
9 The ``callable`` interface is sufficient for when you only ever need to
10 invoke a function. But many times we want to have more information about
11 the function, such as getting its domain or range or knowing whether
12 it's sparse. In addition, we often want to adjust the computational
13 representation of functions (e.g., adding memoization). So this class
14 provides a base class for functions supporting all these sorts of
15 operations in addition to being callable.
16 """
17 def __init__(self, f):
18 self._f = f
19
20 def __call__(self, x):
21 return self._f(x)
22
23 def map(self, g):
24 """Return a new function that applies ``g`` after ``self``.
25
26 Args:
27 g (callable): the function to post-compose.
28
29 Returns:
30 An object of the same type as ``self`` which computes ``lambda x:
31 g(self(x))``. N.B., although mathematically we have the equivalence:
32 ``SomeFunction(f).map(g) == SomeFunction(lambda x: g(f(x)))``;
33 operationally the left- and right-hand sides may differ. For
34 example, with the ``MemoizedFunction`` class, the left-hand side
35 will memoize the intermediate ``f(x)`` values whereas the right-hand
36 side will not. If that's not desired, be sure to call ``Unmemoize``
37 (or similar type coercions) before mapping.
38 """
39 return self.__class__(lambda x: g(self(x)))
40
41
42 class MemoizedFunction(Function):
stgao 2016/12/05 19:30:56 Do we want the memoization work across different i
wrengr 2016/12/05 21:23:51 Storing the memos with the instance is fine for ou
43 """A function which memoizes its value for all arguments."""
44 def __init__(self, f):
45 super(MemoizedFunction, self).__init__(f)
46 self._memos = {}
47
48 def _ClearMemos(self):
49 """Discard all memoized results of this function."""
50 self._memos = {}
51
52 def __call__(self, x):
stgao 2016/12/05 19:30:56 Here, we support one parameter only. Do the usecas
wrengr 2016/12/05 21:23:52 Yep. The use cases all take a single argument, and
53 try:
54 return self._memos[x]
55 except KeyError:
56 fx = self._f(x)
57 self._memos[x] = fx
58 return fx
OLDNEW
« no previous file with comments | « no previous file | appengine/findit/libs/math/test/functions_test.py » ('j') | appengine/findit/libs/math/test/functions_test.py » ('J')

Powered by Google App Engine
This is Rietveld 408576698