From time to time people ask on this site or elsewhere whether modules in Maple can be used the same way as objects in object oriented languages. The answer is yes and no. Yes - because OOP behavior can be simulated with modules - certainly not with full blown functionality, but still. No - because that is usually not the best way to do things in Maple. Here is an example of creating a type and an 'object' exploring it, with few 'methods'.
New type can be created as
`type/Point`:=x->type(x,list(algebraic)):
For example,
type([1,2,3],Point);
true
type([1,[2,3]],Point);
falsePointList constructor can be done as
PointList:=module() export ModuleApply; option package;
ModuleApply:=proc() local L,n;
if nargs=0 then error "either a dimension,
or a sequence of points should be entered"
elif nargs=1 and args::nonnegint then n:=args; L:=NULL
elif type([args],list(Point)) then n:=nops(args[1]);
if andmap(x->nops(x)=n,[args]) then L:=[args]
else error "Points should have the same dimension" fi
else error "correct calls are PointList(n) where n
is a nonnegative integer specifying the dimension,
or PointList(p) where p is a sequence of points" fi;
module() export element,elements,length,add;
option package;
element:=table(L);
length:='nops(op(op(element)))';
elements:='convert(element,list)';
add:=proc()
unprotect(element);
if nargs=n and type([args],Point) then
assign(element[length+1],[args])
elif type([args],list(Point))
and andmap(x->nops(x)=n,[args]) then
assign(seq(element[length+1]=args[k],k=1..nargs))
else error "points should be %1-dimensional", n fi;
protect(element)
end end end end:For example,
a:=PointList(2):
a:-add(2,5);
a:-add([1,2],[3,4]);
a:-length;
3
a:-elements;
[[2, 5], [1, 2], [3, 4]]
a:-element[2];
[1, 2]
b:=PointList([2,5],[1,2],[3,4]):
b:-add(8,9);
b:-elements;
[[2, 5], [1, 2], [3, 4], [8, 9]]This is just an example. PointList lacks many usual list methods. Other examples can be found in ?Stack, ?Queue, and reading PrintProc(SimpleStack); PrintProc(SimpleQueue); PrintProc(MeteredStack); PrintProc(BoundedStack); where PrintProc is the procedure for printing procedures written by Joe Riel.
_____________
Alec Mihailovs
http://mihailovs.com/Alec/
Comments
ModuleApply
i think there is mistake in moduleapply
elif nargs=1 and args::nonnegint then n:=args; L:=NULL
instead
elif nargs=1 and type(args, nonnegint) then n:=args; L:=NULL
i was trying anythinh but only second works...
Maple version
You, probably, use one of earlier Maple versions. It works in Maple 10. Both typechecking constructions are equivalent in Maple 10 (inside
if), with::being slightly faster,t:=RandomTools:-Generate(list(integer,1000000)): f1:=proc() local n; if args::nonnegint then n:=1 else n:=0 fi end: f2:=proc() local n; if type(args,nonnegint) then n:=1 else n:=0 fi end: gc(); time(map(f2,t)); 15.078 gc(); time(map(f1,t)); 13.015I did more time testing and the results are consistent. That's why and because it is also shorter, I prefer using
::__________
Alec Mihailovs
http://mihailovs.com/Alec/