Hi there,

just wanted to share some findings that maybe helpful for others.

RexxBlocks (executable Rexx code that can be also stated as literals by 
defining the code enclosed
with curly brackets) get by means of extensions the methods "reduce" and "map".

"reduce" iterates over a collection, supplying two arguments that the RexxBlock 
processes and
returns a single value. The first invocation supplies the first element in the 
collection as the
first argument and the second value as second argument, afterwards the first 
argument will be the
value returned from the previous invocation of reduce, the second argument will 
be the next element
in the collection. E.g.

    a=.array~of(1,2,3)              -- define a collection
    b={use arg a1, a2;return a1+a2} -- define a block
    a~reduce(b)=                    -- will show: [6]

it is possible to reduce the code in the block, by skipping the return keyword 
statement. [This is
because the new option "NOCOMMANDS" is in effect, which will cause 
strings/expressions not to be
passed on to the environment to be executed as a command, but will get their 
value assigned to the
rexx variable "RESULT". A RexxBlock will get a statement injected that will 
check, whether the
variable RESULT is set and if so will return its value to the invoker.] This 
makes coding simpler
and if one knows this behaviour it is perfectly understandable:

    a=.array~of(1,2,3)       -- define a collection
    b={use arg a1, a2;a1+a2} -- define a block
    a~reduce(b)=             -- will show: [6]

In the case of strings (and mutable buffers) one could define a string of words 
and use "reduceW"
(which should be renamed to "reduceWords" IMHO), which will first create an 
array of words, let the
reduce RexxBlock to its work and returns the result:

    a="1 2 3"                -- define a string of words
    b={use arg a1, a2;a1+a2} -- define a block
    a~reduceW(b)=            -- will show: [6]

There is in addition another variant for strings (and mutable buffers): one 
could have a string
turned into an array in which each character of the string is an element in the 
array (using
"reduceC", which should be named "reduceChars" IMHO), then the reduce RexxBlock 
works on these
elements and returns the result:

    a="123"                  -- define a string
    b={use arg a1, a2;a1+a2} -- define a block
    a~reduceC(b)=            -- will show: [6]


---

Finally, there is one specialty built-in for strings: if the first argument of 
a "reduce"-message is
a string, then Jean-Louis implementation does the following: the first argument 
is regarded as the
message that gets sent to the first element in the collection supplying the 
next element as an
argument (then the third collection element etc.):

    a=.array~of(1,2,3) -- define an array 
    a~reduce("+")=     -- will show: [6]

 

    a="1 2 3"        -- define a string of words
    a~reduceW("+")=  -- will show: [6]

 

    a="123"          -- define a string
    a~reduceC("+")=  -- will show: [6]


HTH,

---rony

P.S.: @Jean-Louis: in the case that one is supplying an initial value, the 
initial value should trail the message-name/RexxBlock-Argument. This way the 
first argument is always well defined to be the messae-name or the RexxBlock 
object.




------------------------------------------------------------------------------
Ridiculously easy VDI. With Citrix VDI-in-a-Box, you don't need a complex
infrastructure or vast IT resources to deliver seamless, secure access to
virtual desktops. With this all-in-one solution, easily deploy virtual 
desktops for less than the cost of PCs and save 60% on VDI infrastructure 
costs. Try it free! http://p.sf.net/sfu/Citrix-VDIinabox
_______________________________________________
Oorexx-devel mailing list
Oorexx-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/oorexx-devel

Reply via email to