Message Boards Message Boards

GROUPS:

Object Oriented Programing - Another type of Class definition

Posted 16 days ago
199 Views
|
0 Replies
|
3 Total Likes
|

Some my articles has been submitted to this community on the Object Oriented Programing (OOP) that is composed of "doubled delayed function definition." The definition key functions are "SetDelayed", "Module", and "UpSet."

Recently, I defined a new type of Class definition of OOP using Pure Function, has same effect as the Class using "SetDelayed" but more than simple.

Following is the Wolfram language OOP using "Function."

First is a definition of Class including local value and two methods.

f = Function[x, Module[{local = 0},
    get[x] ^:= local;
    set[x] ^:= Function[x, local = x]]];

Here, we constructs two instances, as,

{f[a], f[b]}

then, get local value of each instance "a" and "b", as,

{get[a], get[b]}

You can get the results. {0, 0} Then, we can change the local value of each instance as.

{set[a][1], set[b][2]};

So, we can get modified local values as.

{get[a], get[b]}

You can get the modified local values. {1, 2}

A simple example using OOP is,

DynamicModule[{
    n = 10,
    speed = 0.01,
    speedRange = {1., 4.},
    bg = ListLinePlot[
    Table[{Sin[u], Sin[2 u]}, {u, 0, 2 Pi, Pi/100.}], Axes -> None]},

  CLASS DEFINITION;
  new = Function[x, Module[{u = 0, v = 1},
       setv[x] ^:= Function[x, v = x];
       step[x] ^:= (u++; Point[{Sin[u*v], Sin[2 u*v]}])
       ]];

  CREATE INSTANCES;
  objectList = Table[Unique[], {n}];
  Map[new[#] &, objectList];
  Map[setv[#][speed*RandomReal[speedRange]] &, objectList];

  SHOW GRAPHICS;
  movingPoints := 
  Graphics[{PointSize[Large], Map[step[#] &, objectList]}];
  Animate[t;
    Show[{bg, movingPoints}, PlotRange -> 1.2], {t, 0, 100, 1}, 
  AnimationRunning -> False]
  ]

Enjoy.

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract