Functional Object Programming with Maple

lehalle's picture

As you can see, this technique implements quite full and simple functional objects for Maple.

It is very simple and efficient. All properties are private, you need to implement public get and set methods to make them public.

Public methods and stored in the last table structure of the procedure.

I'm deeply interested by comments...

retart;

Maple Equation

Functional object programming with Maple

Simplest functional object

simplest_fo:=proc(n) local this, set_;
this := [n];
set_:=proc( p); this[1]:=p; end proc;
table(['get'= (x->this[1]), 'set'= set_]);
end proc;

Maple Equation

foo:=simplest_fo( 2);

Maple Equation

foo['get']();

Maple Equation

foo['set'](5);

Maple Equation

foo['get']();

Maple Equation

Application to probability distributions

G:=(m,s)->(t->(exp(-(t-m)^2/(2*s^2))/(sqrt(2*Pi)*s)));

Maple Equation

structured_distribution:=proc( f, A, B)
table(['density'= (x->f(x)), 'repartition'=(x->Int(f(t),t=A..x)), 'plot'=(t->plot(f(x),x=A..B))]);
end proc:

SD:=structured_distribution( G(0,1), -infinity, infinity):

SD['density'](1);

Maple Equation

SD['repartition'](1);

Maple Equation

SD['plot']();

Maple Plot

This post was generated using the MaplePrimes File Manager

View 744_indexed-output.mw on MapleNet or Download 744_indexed-output.mw
View file details

Comments

JacquesC's picture

Via modules too

It is possible to do the same thing, with a cleaner syntax and more information hiding via Maple's modules. This is fully documented in the Advanced Programming Guide (and I believe the text of the examples is online as well).

Note that you have re-discovered something quite classical in the theory of programming languages, namely how to embed OO programming into a higher-order lambda-calculus with references. See for examples the excellent textbook Types and Programming Languages (Chapter 18).

lehalle's picture

Nothing re-discovered

Thanks for your comment.

I did not "re discorver" anything, because I totally aware of OO embedding into high level lambda calculus (my favorite programming language is ocaml).

The only point here is that from my point of view, it seems lighter than modules.

JacquesC's picture

I see

I prefer the modules way of doing it - but that is indeed a matter of taste. Internally, there is actually little difference between the two! The DAG for modules and proc are essentially the same.

My current favourite languages (in order) are Haskell, MetaOCaml, Maple, Scala, and Python.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
}