Download code

From LiteratePrograms

Jump to: navigation, search

Back to Functional_object_(Matlab)

Download for Windows: zip

Download for UNIX: zip, tar.gz, tar.bz2

script_to_test_2.m

 1 % Copyright (c) 2008 the authors listed at the following URL, and/or
 2 % the authors of referenced articles or incorporated external code:
 3 % http://en.literateprograms.org/Functional_object_(Matlab)?action=history&offset=20080704102026
 4 % 
 5 % Permission is hereby granted, free of charge, to any person obtaining
 6 % a copy of this software and associated documentation files (the
 7 % "Software"), to deal in the Software without restriction, including
 8 % without limitation the rights to use, copy, modify, merge, publish,
 9 % distribute, sublicense, and/or sell copies of the Software, and to
10 % permit persons to whom the Software is furnished to do so, subject to
11 % the following conditions:
12 % 
13 % The above copyright notice and this permission notice shall be
14 % included in all copies or substantial portions of the Software.
15 % 
16 % THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 % EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 % MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 % IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 % CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 % TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 % SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 % 
24 % Retrieved from: http://en.literateprograms.org/Functional_object_(Matlab)?oldid=13870
25 
26 z = test_embedded2(1);
27 z(2,3)
28 


script_to_test_3.m

 1 % Copyright (c) 2008 the authors listed at the following URL, and/or
 2 % the authors of referenced articles or incorporated external code:
 3 % http://en.literateprograms.org/Functional_object_(Matlab)?action=history&offset=20080704102026
 4 % 
 5 % Permission is hereby granted, free of charge, to any person obtaining
 6 % a copy of this software and associated documentation files (the
 7 % "Software"), to deal in the Software without restriction, including
 8 % without limitation the rights to use, copy, modify, merge, publish,
 9 % distribute, sublicense, and/or sell copies of the Software, and to
10 % permit persons to whom the Software is furnished to do so, subject to
11 % the following conditions:
12 % 
13 % The above copyright notice and this permission notice shall be
14 % included in all copies or substantial portions of the Software.
15 % 
16 % THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 % EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 % MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 % IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 % CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 % TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 % SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 % 
24 % Retrieved from: http://en.literateprograms.org/Functional_object_(Matlab)?oldid=13870
25 
26 z = test_embedded3(1);
27 z(2)
28 z(3)
29 


functional_object.m

 1 % Copyright (c) 2008 the authors listed at the following URL, and/or
 2 % the authors of referenced articles or incorporated external code:
 3 % http://en.literateprograms.org/Functional_object_(Matlab)?action=history&offset=20080704102026
 4 % 
 5 % Permission is hereby granted, free of charge, to any person obtaining
 6 % a copy of this software and associated documentation files (the
 7 % "Software"), to deal in the Software without restriction, including
 8 % without limitation the rights to use, copy, modify, merge, publish,
 9 % distribute, sublicense, and/or sell copies of the Software, and to
10 % permit persons to whom the Software is furnished to do so, subject to
11 % the following conditions:
12 % 
13 % The above copyright notice and this permission notice shall be
14 % included in all copies or substantial portions of the Software.
15 % 
16 % THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 % EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 % MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 % IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 % CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 % TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 % SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 % 
24 % Retrieved from: http://en.literateprograms.org/Functional_object_(Matlab)?oldid=13870
25 
26 function z = functional_object( v1, ..., vN)
27 % FUNCTIONAL_OBJECT -

28 this = struct('v1', v1, ..., 'vN', vN);
29 z    = struct('get_v', @get_v, 'set_v', @set_v, etc);
30 
31 function f = get_fields
32     f = fieldnames(this);
33 end
34 function v = get_v(n)
35     f = get_fields();
36     v = this.(f{n});
37 end
38 function set_v(n, v)
39     f = get_fields();
40     this.(f{n}) = v;
41 end
42 ...
43 end
44 


inherit.m

 1 % Copyright (c) 2008 the authors listed at the following URL, and/or
 2 % the authors of referenced articles or incorporated external code:
 3 % http://en.literateprograms.org/Functional_object_(Matlab)?action=history&offset=20080704102026
 4 % 
 5 % Permission is hereby granted, free of charge, to any person obtaining
 6 % a copy of this software and associated documentation files (the
 7 % "Software"), to deal in the Software without restriction, including
 8 % without limitation the rights to use, copy, modify, merge, publish,
 9 % distribute, sublicense, and/or sell copies of the Software, and to
10 % permit persons to whom the Software is furnished to do so, subject to
11 % the following conditions:
12 % 
13 % The above copyright notice and this permission notice shall be
14 % included in all copies or substantial portions of the Software.
15 % 
16 % THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 % EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 % MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 % IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 % CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 % TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 % SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 % 
24 % Retrieved from: http://en.literateprograms.org/Functional_object_(Matlab)?oldid=13870
25 
26 function z = inherit( objects, varargin)
27 % INHERIT - automated inheritance

28 this = struct('value', {varargin}, 'objects', {objects});
29 for o=1:length(objects)
30     this.(objects{o}) = feval(objects{o}, varargin);
31 end
32 z    = struct('get', @get_this, 'invoke', @hinvoke);
33 
34 function [v, os] = hinvoke( method_name, varargin)
35     v  = {};
36     os = {};
37     % For each parent object

38     for o=1:length(this.objects)
39         % Find existing methods with this name

40         idx = strmatch(method_name, fieldnames(this.(objects{o})), 'exact');
41         if ~isempty(idx)
42             % If possible, apply it (and store the name of the used object)

43             os{end+1} = this.objects{o};
44             if isempty(varargin)
45                 v{end+1}  = feval(this.(this.objects{o}).(method_name));
46             else
47                 v{end+1}  = feval(this.(this.objects{o}).(method_name), varargin);
48             end
49         end
50     end
51 end
52 function t = get_this
53     t = this;
54 end
55 end
56 


test_embedded1.m

 1 % Copyright (c) 2008 the authors listed at the following URL, and/or
 2 % the authors of referenced articles or incorporated external code:
 3 % http://en.literateprograms.org/Functional_object_(Matlab)?action=history&offset=20080704102026
 4 % 
 5 % Permission is hereby granted, free of charge, to any person obtaining
 6 % a copy of this software and associated documentation files (the
 7 % "Software"), to deal in the Software without restriction, including
 8 % without limitation the rights to use, copy, modify, merge, publish,
 9 % distribute, sublicense, and/or sell copies of the Software, and to
10 % permit persons to whom the Software is furnished to do so, subject to
11 % the following conditions:
12 % 
13 % The above copyright notice and this permission notice shall be
14 % included in all copies or substantial portions of the Software.
15 % 
16 % THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 % EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 % MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 % IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 % CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 % TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 % SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 % 
24 % Retrieved from: http://en.literateprograms.org/Functional_object_(Matlab)?oldid=13870
25 
26 function z = test_embedded1( a)
27 % TEST_EMBEDDED1 - simple test of embedded function

28 z = ALPHA(a,2);
29 
30 function u = ALPHA(v,w)
31    u = v+w;
32 end
33 end
34 


test_embedded2.m

 1 % Copyright (c) 2008 the authors listed at the following URL, and/or
 2 % the authors of referenced articles or incorporated external code:
 3 % http://en.literateprograms.org/Functional_object_(Matlab)?action=history&offset=20080704102026
 4 % 
 5 % Permission is hereby granted, free of charge, to any person obtaining
 6 % a copy of this software and associated documentation files (the
 7 % "Software"), to deal in the Software without restriction, including
 8 % without limitation the rights to use, copy, modify, merge, publish,
 9 % distribute, sublicense, and/or sell copies of the Software, and to
10 % permit persons to whom the Software is furnished to do so, subject to
11 % the following conditions:
12 % 
13 % The above copyright notice and this permission notice shall be
14 % included in all copies or substantial portions of the Software.
15 % 
16 % THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 % EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 % MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 % IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 % CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 % TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 % SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 % 
24 % Retrieved from: http://en.literateprograms.org/Functional_object_(Matlab)?oldid=13870
25 
26 function z = test_embedded2( a)
27 % TEST_EMBEDDED2 - simple test of embedded function

28 z = @ALPHA;
29 
30 function u = ALPHA(v,w)
31    u = v+w;
32 end
33 end
34 


test_embedded3.m

 1 % Copyright (c) 2008 the authors listed at the following URL, and/or
 2 % the authors of referenced articles or incorporated external code:
 3 % http://en.literateprograms.org/Functional_object_(Matlab)?action=history&offset=20080704102026
 4 % 
 5 % Permission is hereby granted, free of charge, to any person obtaining
 6 % a copy of this software and associated documentation files (the
 7 % "Software"), to deal in the Software without restriction, including
 8 % without limitation the rights to use, copy, modify, merge, publish,
 9 % distribute, sublicense, and/or sell copies of the Software, and to
10 % permit persons to whom the Software is furnished to do so, subject to
11 % the following conditions:
12 % 
13 % The above copyright notice and this permission notice shall be
14 % included in all copies or substantial portions of the Software.
15 % 
16 % THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 % EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 % MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 % IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 % CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 % TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 % SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 % 
24 % Retrieved from: http://en.literateprograms.org/Functional_object_(Matlab)?oldid=13870
25 
26 function z = test_embedded3( a)
27 % TEST_EMBEDDED3 - simple test of embedded function

28 z = @ALPHA_A;
29 function u = ALPHA_A(x)
30    u = ALPHA(a,x);
31 end
32 function u = ALPHA(v,w)
33    u = v+w;
34 end
35 end
36 


use_inheritance_script.m

 1 % Copyright (c) 2008 the authors listed at the following URL, and/or
 2 % the authors of referenced articles or incorporated external code:
 3 % http://en.literateprograms.org/Functional_object_(Matlab)?action=history&offset=20080704102026
 4 % 
 5 % Permission is hereby granted, free of charge, to any person obtaining
 6 % a copy of this software and associated documentation files (the
 7 % "Software"), to deal in the Software without restriction, including
 8 % without limitation the rights to use, copy, modify, merge, publish,
 9 % distribute, sublicense, and/or sell copies of the Software, and to
10 % permit persons to whom the Software is furnished to do so, subject to
11 % the following conditions:
12 % 
13 % The above copyright notice and this permission notice shall be
14 % included in all copies or substantial portions of the Software.
15 % 
16 % THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 % EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 % MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 % IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 % CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 % TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 % SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 % 
24 % Retrieved from: http://en.literateprograms.org/Functional_object_(Matlab)?oldid=13870
25 
26 z = inherit({'object1', 'object2'}, 2);
27 z.get()
28 z.invoke('set', 3);
29 z.invoke('get');
30 z.get()
31 z.invoke('add',1)
32 z.invoke('substract',1)
33 


object12.m

 1 % Copyright (c) 2008 the authors listed at the following URL, and/or
 2 % the authors of referenced articles or incorporated external code:
 3 % http://en.literateprograms.org/Functional_object_(Matlab)?action=history&offset=20080704102026
 4 % 
 5 % Permission is hereby granted, free of charge, to any person obtaining
 6 % a copy of this software and associated documentation files (the
 7 % "Software"), to deal in the Software without restriction, including
 8 % without limitation the rights to use, copy, modify, merge, publish,
 9 % distribute, sublicense, and/or sell copies of the Software, and to
10 % permit persons to whom the Software is furnished to do so, subject to
11 % the following conditions:
12 % 
13 % The above copyright notice and this permission notice shall be
14 % included in all copies or substantial portions of the Software.
15 % 
16 % THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 % EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 % MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 % IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 % CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 % TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 % SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 % 
24 % Retrieved from: http://en.literateprograms.org/Functional_object_(Matlab)?oldid=13870
25 
26 function z = object12( v)
27 % OBJECT12 - addition and substraction

28 this = struct('v', v, 'add', object1(v), 'substract', object2(v));
29 z    = struct('substract', this.substract.substract, 'add',this.add.add, ...
30               'get', @get, 'set', @(x)([set(v), this.add.set(v), this.substract.set(v)]));
31 
32 function v = set(v)
33     this.v = v;
34 end
35 function v = get
36     v = this.v;
37 end
38 end
39 


my_light_object.m

 1 % Copyright (c) 2008 the authors listed at the following URL, and/or
 2 % the authors of referenced articles or incorporated external code:
 3 % http://en.literateprograms.org/Functional_object_(Matlab)?action=history&offset=20080704102026
 4 % 
 5 % Permission is hereby granted, free of charge, to any person obtaining
 6 % a copy of this software and associated documentation files (the
 7 % "Software"), to deal in the Software without restriction, including
 8 % without limitation the rights to use, copy, modify, merge, publish,
 9 % distribute, sublicense, and/or sell copies of the Software, and to
10 % permit persons to whom the Software is furnished to do so, subject to
11 % the following conditions:
12 % 
13 % The above copyright notice and this permission notice shall be
14 % included in all copies or substantial portions of the Software.
15 % 
16 % THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 % EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 % MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 % IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 % CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 % TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 % SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 % 
24 % Retrieved from: http://en.literateprograms.org/Functional_object_(Matlab)?oldid=13870
25 
26 function z = my_light_object( mode, varargin)
27 % MY_LIGHT_OBJECT - a light struct object

28 % two versions embedded:

29 %

30 % 1. functional:

31 % mlo = my_light_object('init', 'alpha', 1, 'beta', 2)

32 % my_light_object('get', mlo, 'alpha')

33 % my_light_object('set', mlo, 'beta', 8, 'garzol', 12)

34 %

35 % 2. struct object

36 % mlo = my_light_object('create', 'alpha', 1, 'beta', 2)

37 % mlo.get('alpha')

38 % mlo.set('beta', 8, 'garzol', 12)

39 % mlo.get()

40 switch lower(mode)
41     case 'init'
42         %<* init a function mlo

43         z = [];
44         for i=1:2:length(varargin)-1
45             z.(varargin{i}) = varargin{i+1};
46         end
47         %>*

48     case 'get'
49         %<* Get attribute

50         if length(varargin)==1
51             z = varargin{1};
52             return
53         end
54         z = {};
55         for i=2:length(varargin)
56             z{end+1} = varargin{1}.(varargin{i});
57         end
58         if length(z)==1
59             z = z{1};
60         end
61         %>*

62     case 'set'
63         %<* set attribute

64         z = varargin{1};
65         for i=2:2:length(varargin)-1
66             z.(varargin{i})= varargin{i+1};
67         end
68         %>*

69     case 'create'
70         %<* object version

71         this = my_light_object('init', varargin{:});
72         z = struct('get', @get_, 'set', @set_);
73         %>*

74     otherwise
75         error('my_light_object:mode', 'mode <%s> is unknown', mode);

76 end
77 
78 %%** Internals

79 %<* Embedded get

80 function v = get_( varargin)
81 v = my_light_object('get', this, varargin{:});
82 end
83 %>*

84 %<* Embedded set

85 function set_(varargin)
86 this = my_light_object('set', this, varargin{:});
87 end
88 %>*

89 
90 end
91 


polymorphism_example.m

 1 % Copyright (c) 2008 the authors listed at the following URL, and/or
 2 % the authors of referenced articles or incorporated external code:
 3 % http://en.literateprograms.org/Functional_object_(Matlab)?action=history&offset=20080704102026
 4 % 
 5 % Permission is hereby granted, free of charge, to any person obtaining
 6 % a copy of this software and associated documentation files (the
 7 % "Software"), to deal in the Software without restriction, including
 8 % without limitation the rights to use, copy, modify, merge, publish,
 9 % distribute, sublicense, and/or sell copies of the Software, and to
10 % permit persons to whom the Software is furnished to do so, subject to
11 % the following conditions:
12 % 
13 % The above copyright notice and this permission notice shall be
14 % included in all copies or substantial portions of the Software.
15 % 
16 % THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 % EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 % MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 % IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 % CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 % TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 % SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 % 
24 % Retrieved from: http://en.literateprograms.org/Functional_object_(Matlab)?oldid=13870
25 
26 function z = polymorphism_example( v)
27 % POLYMORPHISM EXAMPLE -

28 this.value = v;
29 z = struct('get',@get,'set',@set,'to_string',@to_string);
30 function v = get
31     v = this.value;
32 end
33 function set( v)
34     this.value = v;
35 end
36 
37 function str = to_string(pref, v)
38     if nargin<1
39         pref = '';
40     end
41     if nargin<2
42         v = this.value;
43     end
44     if isstr(v)
45         str = [pref '-str:' v ':'];
46     elseif isnumeric(v)
47         if length(v)==1
48             str = [pref '-num:' num2str(v) ':'];
49         else
50             str = [pref '-vnum:' sprintf('%f:',v)];

51         end
52     elseif isstruct(v)
53         fnames = fieldnames(v);
54         str = '';
55         for f=1:length(fnames)
56             str = sprintf('%s%s\n', str, to_string(['struct:' fnames{f}], v.(fnames{f})));

57         end
58         str = str(1:end-1);
59     elseif iscell(v)
60         str = '';
61         for f=1:length(v)
62             str = sprintf('%s%s\n', str, to_string('cell', v{f}));

63         end
64         str = str(1:end-1);
65     elseif isa(v, 'function_handle')
66         str = '@???';
67     else
68         str = '***UNKNOWN***';
69     end
70 end
71 end
72 


script_pe.m

 1 % Copyright (c) 2008 the authors listed at the following URL, and/or
 2 % the authors of referenced articles or incorporated external code:
 3 % http://en.literateprograms.org/Functional_object_(Matlab)?action=history&offset=20080704102026
 4 % 
 5 % Permission is hereby granted, free of charge, to any person obtaining
 6 % a copy of this software and associated documentation files (the
 7 % "Software"), to deal in the Software without restriction, including
 8 % without limitation the rights to use, copy, modify, merge, publish,
 9 % distribute, sublicense, and/or sell copies of the Software, and to
10 % permit persons to whom the Software is furnished to do so, subject to
11 % the following conditions:
12 % 
13 % The above copyright notice and this permission notice shall be
14 % included in all copies or substantial portions of the Software.
15 % 
16 % THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 % EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 % MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 % IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 % CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 % TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 % SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 % 
24 % Retrieved from: http://en.literateprograms.org/Functional_object_(Matlab)?oldid=13870
25 
26 pe=polymorphism_example( 5)
27 pe.to_string()
28 pe.set(1:10); pe.to_string()
29 pe.set('garzol'); pe.to_string()
30 pe.set({'garzol', 4}); pe.to_string()
31 pe.set(struct('alpha',{{'garzol', 4}},'beta',1:3)); pe.to_string()
32 


object1.m

 1 % Copyright (c) 2008 the authors listed at the following URL, and/or
 2 % the authors of referenced articles or incorporated external code:
 3 % http://en.literateprograms.org/Functional_object_(Matlab)?action=history&offset=20080704102026
 4 % 
 5 % Permission is hereby granted, free of charge, to any person obtaining
 6 % a copy of this software and associated documentation files (the
 7 % "Software"), to deal in the Software without restriction, including
 8 % without limitation the rights to use, copy, modify, merge, publish,
 9 % distribute, sublicense, and/or sell copies of the Software, and to
10 % permit persons to whom the Software is furnished to do so, subject to
11 % the following conditions:
12 % 
13 % The above copyright notice and this permission notice shall be
14 % included in all copies or substantial portions of the Software.
15 % 
16 % THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 % EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 % MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 % IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 % CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 % TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 % SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 % 
24 % Retrieved from: http://en.literateprograms.org/Functional_object_(Matlab)?oldid=13870
25 
26 function z = object1( v)
27 % OBJECT1 - addition

28 this.v = v;
29 z      = struct('add', @ADD, 'get', @get, 'set', @set);
30 
31 function v = set(v)
32     this.v = v;
33 end
34 function v = get
35     v = this.v;
36 end
37 function u = ADD( x)
38     u = x+this.v;
39 end
40 end
41 


object2.m

 1 % Copyright (c) 2008 the authors listed at the following URL, and/or
 2 % the authors of referenced articles or incorporated external code:
 3 % http://en.literateprograms.org/Functional_object_(Matlab)?action=history&offset=20080704102026
 4 % 
 5 % Permission is hereby granted, free of charge, to any person obtaining
 6 % a copy of this software and associated documentation files (the
 7 % "Software"), to deal in the Software without restriction, including
 8 % without limitation the rights to use, copy, modify, merge, publish,
 9 % distribute, sublicense, and/or sell copies of the Software, and to
10 % permit persons to whom the Software is furnished to do so, subject to
11 % the following conditions:
12 % 
13 % The above copyright notice and this permission notice shall be
14 % included in all copies or substantial portions of the Software.
15 % 
16 % THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 % EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 % MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 % IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 % CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 % TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 % SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 % 
24 % Retrieved from: http://en.literateprograms.org/Functional_object_(Matlab)?oldid=13870
25 
26 function z = object2( v)
27 % OBJECT2 - substraction

28 this.v = v;
29 z      = struct('substract', @SUBSTRACT, 'get', @get, 'set', @set);
30 
31 function v = set(v)
32     this.v = v;
33 end
34 function v = get
35     v = this.v;
36 end
37 function u = SUBSTRACT( x)
38     u = x-this.v;
39 end
40 end
41 


script4mlo.m

 1 % Copyright (c) 2008 the authors listed at the following URL, and/or
 2 % the authors of referenced articles or incorporated external code:
 3 % http://en.literateprograms.org/Functional_object_(Matlab)?action=history&offset=20080704102026
 4 % 
 5 % Permission is hereby granted, free of charge, to any person obtaining
 6 % a copy of this software and associated documentation files (the
 7 % "Software"), to deal in the Software without restriction, including
 8 % without limitation the rights to use, copy, modify, merge, publish,
 9 % distribute, sublicense, and/or sell copies of the Software, and to
10 % permit persons to whom the Software is furnished to do so, subject to
11 % the following conditions:
12 % 
13 % The above copyright notice and this permission notice shall be
14 % included in all copies or substantial portions of the Software.
15 % 
16 % THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 % EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 % MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 % IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 % CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 % TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 % SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 % 
24 % Retrieved from: http://en.literateprograms.org/Functional_object_(Matlab)?oldid=13870
25 
26 %% How to build simple MATLAB object

27 
28 %% Functionnal version

29 mlo = my_light_object('init', 'alpha', 1, 'beta', 2)
30 my_light_object('get', mlo, 'alpha')
31 my_light_object('set', mlo, 'beta', 8, 'garzol', 12)
32 
33 %% Struct object version

34 mlo = my_light_object('create', 'alpha', 1, 'beta', 2)
35 mlo.get('alpha')
36 mlo.set('beta', 8, 'garzol', 12)
37 mlo.get()
38 


Views
Personal tools