Mercurial > octave
comparison scripts/miscellaneous/memoize.m @ 31245:a887ffb997a7
New function memoize to optimize repetitive function calls (bug #60860).
* scripts/miscellaneous/clearAllMemoizedCaches.m: new function.
* scripts/miscellaneous/memoize.m: new function.
* scripts/miscellaneous/private/__memoize__.m: new function.
* scripts/miscellaneous/module.mk: add new functions to build system.
* scripts/+matlab/+lang/MemoizedFunction.m: new function.
* scripts/+matlab/+lang/module.mk: add new functions to build system.
author | Guillaume Flandin <guillaume.offline@gmail.com> |
---|---|
date | Tue, 27 Sep 2022 16:12:45 -0400 |
parents | |
children | 3dae836c598c |
comparison
equal
deleted
inserted
replaced
31244:80a0905905be | 31245:a887ffb997a7 |
---|---|
1 ######################################################################## | |
2 ## | |
3 ## Copyright (C) 2021 The Octave Project Developers | |
4 ## | |
5 ## See the file COPYRIGHT.md in the top-level directory of this | |
6 ## distribution or <https://octave.org/copyright/>. | |
7 ## | |
8 ## This file is part of Octave. | |
9 ## | |
10 ## Octave is free software: you can redistribute it and/or modify it | |
11 ## under the terms of the GNU General Public License as published by | |
12 ## the Free Software Foundation, either version 3 of the License, or | |
13 ## (at your option) any later version. | |
14 ## | |
15 ## Octave is distributed in the hope that it will be useful, but | |
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 ## GNU General Public License for more details. | |
19 ## | |
20 ## You should have received a copy of the GNU General Public License | |
21 ## along with Octave; see the file COPYING. If not, see | |
22 ## <https://www.gnu.org/licenses/>. | |
23 ## | |
24 ######################################################################## | |
25 | |
26 ## -*- texinfo -*- | |
27 ## @deftypefn {} {@var{mem_fcn_handle} =} memoize (@var{fcn_handle}) | |
28 ## | |
29 ## @seealso{clearAllMemoizedCaches} | |
30 ## @end deftypefn | |
31 | |
32 function mem_fcn_handle = memoize (fcn_handle) | |
33 | |
34 if (nargin != 1 || nargout > 1) | |
35 print_usage (); | |
36 endif | |
37 if (! isa (fcn_handle, "function_handle")) | |
38 error ("memoize: FCN_HANDLE must be a function handle"); | |
39 endif | |
40 | |
41 mem_fcn_handle = __memoize__ (fcn_handle); | |
42 | |
43 endfunction | |
44 | |
45 %!test | |
46 %! fcn1 = memoize (@sin); | |
47 %! assert (isa (fcn1, "matlab.lang.MemoizedFunction")); | |
48 %! fcn1 (pi); | |
49 %! fcn2 = memoize (@sin); | |
50 %! fcn2 (2*pi); | |
51 %! assert (isequal (fcn1, fcn2)) | |
52 | |
53 %!test | |
54 %! fcn = memoize (@rand); | |
55 %! a = fcn (); | |
56 %! b = fcn (); | |
57 %! assert (a, b); | |
58 %! fcn2 = memoize (@rand); | |
59 %! c = fcn2 (); | |
60 %! assert (a, c); | |
61 | |
62 %!test | |
63 %! fcn = memoize (@plus); | |
64 %! fcn.stats; | |
65 %! stats (fcn); | |
66 %! clearCache (fcn); | |
67 %! fcn.clearCache; | |
68 | |
69 # Test input validation | |
70 %!error memoize (); | |
71 %!error memoize (1, 2); | |
72 %!error [a, b] = memoize (1); | |
73 %!error memoize (1); |