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);
                                false
PointList 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/

Please Wait...