On Sat, Nov 29, 2008 at 9:33 PM, Viktor Cerovski
<[EMAIL PROTECTED]> wrote:
> J could be extended such that it allows us to work with generators/objects,
> for instance via closure operation, ]:
>
> Definiton:
>
> (vm : vd) ]: u (Closure)
I think you mean
u Closure v
Hypothetically, 'Closure' could be replaced with ]: in some future
version of J, but before we even think about that, we should make
sure we understand what it would mean in the current version of J.
> 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.
This is not clear to me. A generator is not a value, and you have
not clearly specified the relationship between this generator and
this value.
Also, monadic invocation implies that you are talking about a
verb, but your discussion implies that g is a verb and not a
noun. This conflicts with J's syntax. Instead, I would have
the original conjunction return and adverb and have the
initial state of the generator be the argument to that adverb.
> 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.
I think you meant that last line to be
m return.
It's not clear to me why you have required the left argument
be overloaded, using both its monadic and dyadic
definitions in different parts of this process. Why not
just eliminate u, and require the original u y be
passed in during the initialization phase, and
use the original u and v passed to the conjunction to
handle the vd and vm mechanisms?
In other words, using your original u, y, vm, and vd,
the invocation would look like:
(u y) vm Closure vd
> 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
As I mentioned before, this is syntactically invalid, but you
could have
g=: 0 ] Closure +
Or, if you prefer
G=: ] Closure +
g=: 0 G
> 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.
I think that this difficulty is entirely due to the syntactic
conflict required by its result.
Here's how I might implement my proposed alternative
"Closure":
serialize=: 1 :0
r=. 5!:5<'u'
if. 10 41-:a.i._2{.r do.
'((3!:2 a.{~',(":a.i.3!:1]5!:1<'u'),')5!:0)'
else.
'(',r,')'
end.
)
Closure=:2 :0
linu=:u serialize
linv=:v serialize
L1=: 'L=. cocreate '''''
L2=: 'state__L=: m'
body=: linu,'state__x=: state__x ',linv,' y'
L3=: 'L&(4 :''',(body#~1+body=''''),''')'
1 :(L1;L2;L3)
)
Anyways, I think that this is doable.
But I'm not sure if this is very useful.
Why not just write the code explicitly?
Who would use this? What would
they use it for?
--
Raul
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm