In this tutorial we will implement another Rosetta code sample: Accumulator.
Our task is to implement the function which will return another function accumulating passed parameters.
Let’s start with declaring our accumulator function. Note that ELENA is an object-oriented language and any function is in fact an object (or in our case – a symbol).
Or we could simplify the code using a generic closure (function symbol):
Note that our code cannot be used stand alone, because we do not declare “append” method. It is in fact an object extension (that’s why it starts with E).
So another function should be declared – it will combine the passed variable (supporting “append” method) with our accumulating function – creating a wrap group object:
To understand how this code works let’s recall what is a group object. In short group object overwrites SELF variable (but not $SELF one). Various group objects do it differently. Wrap one replaces SELF in the extender (the first one) with the content (the second one). To put it another way it wraps the extender around the content: when we call append method, Wrap group object redirects the message to its first member replacing SELF with the second one.
The use case will be the following:
Let’s optimize the code a bit, to allow our symbol accepting constant values as well
So we could drop the variable creating:
The full code is below: