J could be extended such that it allows us to work with generators/objects,
for instance via closure operation, ]:
Definiton:
(vm : vd) ]: u (Closure)
defines a closure. Monadic invocation of the closure sets the initial
value of a generator g to be u y and returns such initialized g.
Monadic invocation of the generator g evalues the following sequence:
m =. ng vd y
ng =. vm m
return. m
where ng is the next value of the generator's state.
Note that the next value produced by g is not ng,
thus the generator's state is hidden in general.
A closure defining a generator that returns the next integer (Graham's
problem) could be defined now as:
G =: (] : +) ]: ]
and should work as follows:
g =: G 0 NB. make and initialize a generator g
g
(] : +) ]: 0 NB. this, e.g, could also serve as a definiton
g 1
1
g
(] : +) ]: 1
g 1
2
g 1
3
etc.
Although implementation of ]: is straightforward at implementational
level of J via keeping the internal state ng of g together with the
definition of g, writing of ]: in J, as a conjunction, seems to me to
be very difficult.
--
View this message in context:
http://www.nabble.com/Closures-and-Generators-in-J-tp20753957s24193p20753957.html
Sent from the J Programming mailing list archive at Nabble.com.
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm