# Pure functioned Closure, a variant of the Mathematica OOP

**URL:** <https://community.wolfram.com/t/pure-functioned-closure-a-variant-of-the-mathematica-oop/10793>\
**Category:** Ideas\
**Tags:** mathematica\
**Created:** [July 5, 2017, 4:28am UTC](https://community.wolfram.com/t/pure-functioned-closure-a-variant-of-the-mathematica-oop/10793 "2017-07-05T04:28:43Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![kobayashikorio](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/kobayashikorio/32/5875_2.png) [@kobayashikorio](https://community.wolfram.com/u/kobayashikorio)\
**Post date:** [July 5, 2017, 4:28am UTC](https://community.wolfram.com/t/pure-functioned-closure-a-variant-of-the-mathematica-oop/10793/1 "2017-07-05T04:28:43Z")

</div>

In the previous post, [“Closure”](https://wolfram.discourse.group/t/10748/1) as a variant of the [Mathematica OOP](https://wolfram.discourse.group/t/8988/1) was presented. Next, i introduce a closure which method is written by the pure function. This sample shows also a initialization method for the OOP instance.

First code is a class definition,

```
class[nam_[init_]] := Module[{local = init},
  nam & ^:= ++local;
  ]

```

Second code is a construction of instances with local value initialization as,

```
class[counter1[0]]; class[counter2[100]];

```

Then we can call the initialized instance as a pure function,

```
{counter1 &, counter2 &, counter1 &, counter2 &}

```

We can get the results as,

```
{1, 101, 2, 102}

```

Let’s examine the “counter1”

```
?counter1

```

We’ll get as,

Global `counter1

counter1&^:=++local$231

Counter1 was encapsulated by OOP, and is still a global symbol, we can do as,

```
{counter1 = 0, counter1 &, counter1, counter1 &}

```

The result is,

{0, 3, 0, 4}

---

<div class="post-metadata">

**Author:** ![kubapod](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/kubapod/32/18662_2.png) [@kubapod](https://community.wolfram.com/u/kubapod)\
**Post date:** [July 5, 2017, 7:06am UTC](https://community.wolfram.com/t/pure-functioned-closure-a-variant-of-the-mathematica-oop/10793/2 "2017-07-05T07:06:12Z")

</div>

Sorry if I missed the point, but why? Isn’t `Function[counter1]` returning a value unexpected? What is wrong with `class[nam_[init_]] := Module[{local = init}, nam[] := ++local;]` and `counter1[]` later?

---

<div class="post-metadata">

**Author:** ![kobayashikorio](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/kobayashikorio/32/5875_2.png) [@kobayashikorio](https://community.wolfram.com/u/kobayashikorio)\
**Post date:** [July 5, 2017, 9:34am UTC](https://community.wolfram.com/t/pure-functioned-closure-a-variant-of-the-mathematica-oop/10793/3 "2017-07-05T09:34:27Z")

</div>

Hi,  
not “nam[] := ++local;”,  
but nam & ^:= ++local;" .

---

<div class="post-metadata">

**Author:** ![kubapod](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/kubapod/32/18662_2.png) [@kubapod](https://community.wolfram.com/u/kubapod)\
**Post date:** [July 5, 2017, 9:54am UTC](https://community.wolfram.com/t/pure-functioned-closure-a-variant-of-the-mathematica-oop/10793/4 "2017-07-05T09:54:11Z")

</div>

I saw what you wrote. My question is: why not `nam[]:=..`? Calling the method with `counter1[]` is more natural than `Function[counter1]` or equivalently `counter1 &`.

---

<div class="post-metadata">

**Author:** ![kobayashikorio](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/kobayashikorio/32/5875_2.png) [@kobayashikorio](https://community.wolfram.com/u/kobayashikorio)\
**Post date:** [July 5, 2017, 12:18pm UTC](https://community.wolfram.com/t/pure-functioned-closure-a-variant-of-the-mathematica-oop/10793/5 "2017-07-05T12:18:15Z")

</div>

HI, Podkalicki,

Its a good question on the characteristics of the OOP. Please try “? counter1, or ? counter2”. You will find the backyard of OOP and the meaning of UpSet. Unfortunately, the Mathematica help document for the UpSet is too insufficient to understand the incredibly strong power of that function.

---

<div class="post-metadata">

**Author:** ![kubapod](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/kubapod/32/18662_2.png) [@kubapod](https://community.wolfram.com/u/kubapod)\
**Post date:** [July 7, 2017, 9:20am UTC](https://community.wolfram.com/t/pure-functioned-closure-a-variant-of-the-mathematica-oop/10793/6 "2017-07-07T09:20:51Z")

</div>

I understand what UpSet is doing. I just don’t see any advantage of it with respect to my example with `SetDelayed`. The more than later you need to call `Function[counter]` to run a function while it is not consistent with how pure functions work, you’d expect something like `Function[counter][]` at least. So I don’t see the point of `&` here.

Just to emphasize, I’m not very experienced with OOP and those notes are only from MMA point of view. If you could elaborate why `UpSet` is better here and why `Function[counter] -> value` makes sense? I would appreciate that.

Best

---

<div class="post-metadata">

**Author:** ![kobayashikorio](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/kobayashikorio/32/5875_2.png) [@kobayashikorio](https://community.wolfram.com/u/kobayashikorio)\
**Post date:** [July 7, 2017, 1:19pm UTC](https://community.wolfram.com/t/pure-functioned-closure-a-variant-of-the-mathematica-oop/10793/7 "2017-07-07T13:19:01Z")

</div>

This sample was intended to show the functions of UpSet which is the key of OOP style. So, the closure “counter” is intending to show the OOP style extendable-ness not to show the other advantages. So, this sample use the pure-function as un-named-ness then we can call the encapsulated function with the "&"ed object name only.

Regards,
