> It looks as if one is able to define any Rexx code as a literal by using
> curly brackets around them.
> These literals can then be run/executed later.
>

yes, but not all blocks are closures.
Only a block starting with ::cl[osure] will let create a closure. This
block has an associated directory of variables. No need to create a
snapshot of the variables for the other kinds blocks.

v=1 ; {}~variables=
[The NIL object id#_537657582]

v=1 ; {::cl}~variables=
type: The Directory class: (3 items)

    # 1: index=[RC]   -> item=[0]
    # 2: index=[SIGL] -> item=[364]
    # 3: index=[V]    -> item=[1]


>
> Do you have a brief problem, example where one can see what is needed for
> a "bare-bone" closure and
> how it helps solve some problems in an easier way than with what is
> currently available in ooRexx?
>
> Or maybe reformulated: what is special about the closures, that makes it
> easier for the programmer
> to take advantage of them, rather than using what ooRexx has already? What
> do closures allow for,
> that is not really possible now?
>

Well, it's a matter of code organization. There is nothing you can't do
without closures. It's just you can have more compact code, assuming you
like this kind of code :-)

Ex1 :
In this example, you create functions on-the-fly from a common block.

range = { use arg min, max ; return { ::closure expose min max ; use arg
num ; return min <= num & num <= max }}
from5to8 = range~(5, 8)
from20to30 = range~(20, 30)
say from5to8~(6) -- 1
say from5to8~(9) -- 0
say from20to30~(6) -- 0
say from20to30~(25) -- 1


Ex2 :
In this example, a closure is needed to have access to the 'translation'
variable. You can't pass it by parameter, because it's not you who calls
the block.

translation = .Directory~new
translation["quick"] = "slow"
translation["lazy"] = "nervous"
translation["brown"] = "yellow"
translation["dog"] = "cat"
translation~setMethod("UNKNOWN", "return arg(1)")
say "The quick brown fox jumps over the lazy dog"~mapW{::cl expose
translation ; translation[arg(1)]}
::requires "extension/extensions.cls"


Ex3 :
In this example, the 'partial' method returns a closure which remember the
parameter 10.

add10 = "+"~partial(10)
say add10~(1) -- 11
------------------------------------------------------------------------------
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