Group Abstract Group Abstract

Message Boards Message Boards

4
|
4.2K Views
|
0 Replies
|
4 Total Likes
View groups...
Share
Share this post:

Closure, a variant of Mathematica OOP

Posted 8 years ago

Closure, following Wikipedia, called as lexical closure or function closure can be implemented as a variant of the Mathematica OOP. Here, the sample is a counter.

Module[{count = 0},
 counter[object] ^:= ++count;
 ]

This definition is encapsulated, let's define a global function simultaneously as follows.

count = -1; counter[x_] := x + 1;

Let's run the closure, "counter".

Table[counter[object], {10^3}]; counter[object]

This closure returns 1001.

Let's run the global function.

{counter[0], count}

This global function and variable returns, {1,-1}.

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