Raul Miller-4 wrote:
>
> [...]
> 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)
> )
>
Raul, this is not it. An implementation of ]: (Closure) is a bit more
complicated.
Let me explain:
"Monadic invocation of the closure sets the initial
value of a generator g to be u y and returns such initialized g."
means that closure maps a pair of verbs, u and v, into a generator.
Generator is verb v plus an internal state accessible and modifyable
to each instance of generator. u of course sets the initial value of
the internal state. You're right (as I have also anticipated this response
in
one of NB.'s in my post) that one could simply pass u y to Closure, but
it's just nicer to have u that will convert programmer's argument into
some internal state: the two might be considerably different.
Think of ? --- that's a generator, of random numbers, not a function that
returns a random number. You pass seed, then rng constructs from it
the internal state which may or may not be equal to seed.
Your code then should also have the Closure produce unique
instances of generators: each produced generator should have
a unique internal state.
Another way to think about closures and generators is in terms of OO
concepts:
- closure is creating an object.
- generator is that object,
- v is the method,
- ng is a private variable,
- u is the constructor.
In other words, closures allow for some rudimentary but very powerful
object-oriented programming.
I hope this helps.
--
View this message in context:
http://www.nabble.com/Closures-and-Generators-in-J-tp20753957s24193p20757267.html
Sent from the J Programming mailing list archive at Nabble.com.
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm