I found a workaround...
I was mistakenly useing f as a dyad... f@:] fixes that.
fixing (f.) the verb before extending the closure was needed too.

   f=: + Closure 3
   h=: ([ , f@:] ) f. Closure 3
   h 2
3 5
   h 5
3 5 10
   h 0
3 5 10 10

A generic logging closure that will extend any function or other nonad to track 
its history should  be able to be defined as:
logger=: 1 : '([ , u@:]) f. Closure y'

but it doesn't.


----- Original Message ----
From: Pascal Jasmin <[EMAIL PROTECTED]>
To: Programming forum <[email protected]>
Sent: Sunday, October 29, 2006 9:30:08 AM
Subject: [Jprogramming]  passing object functions to other objects

It seems as though passing myverb__myobject to a modifier inside another class 
(numbered locale) breaks the references to its locale that myverb relies on.  
This also fails when passing an alias to myverb_mylocalenumber_ to a modifier 
in a class. (breaks on alias_base_ too)

Here's an extended application of my nonad/coroutine/closure class with more 
syntax sugar.  But, that illustrates this J 'bug' in classes...

   history=: ([ , {:@:[ + ]) Closure 3      

   history 2

3 5

   history 5

3 5 10

   history 0

3 5 10 10


f =: + Closure 3
   f 2
5
   (f bind +:) 3 NB. works as expected
11

   f
3 : '] STATE_31_ =: STATE_31_ function_31_ y'

   ] history =: ([ , f) Closure 3 NB. should produce identical results to 
original
] 3 : '] STATE_33_ =: STATE_33_ function_33_ y'

   history 3
|value error: f
|   ]STATE_33_=:STATE_33_     function_33_ y

   function_33_
[ , f

why does f get a value error? Passing f_base_ in the constructor also gets same 
error.



Need latest implementation below to test.  

coclass'nonad'
NB. Coroutines are functions with bonded state, that when called return a value 
NB. and update their bonded state, ready to be called a next time.  Other 
languages implement this similarly to putting the function into debug pause 
state.  
NB.This is a much simpler and more versatile approach: just save the state 
somewhere easy to find.
NB.The nonad can do everything a coroutine can.  A verb together with 
modifiable state are bound together, and the state can be changed on each call.


NB.CREATE:
NB. savedobject =. verb Nonad [noun Initialdata] or,
NB. savedobject =. resultverb`transfromverb Nonad [noun Initialdata]
NB. first version is equivalen to verb`}. Nonad [noun Initialdata]
NB.USE:
NB. resume__savedobject '' 
NB. after each call, the state is beheaded, or transform function passed as 2nd 
gerund is used.
Note 'Example'
   a1=: {. Nonad 1 2 3 4 5
+--+
|28|
+--+
   resume__a1 ''
1
   (resume__a1 + ]) 10 20
12 22
   (resume__a1 + ])("0) 10 20
13 24
   resume__a1 ''
5
   (i.0 0) -: resume__a1 ''
1
)

function=: i.0
transform=: }.

create=: 3 : 0
  STATE=: y
)
destroy=: codestroy


resume=: 3 : 0
if. 0=#STATE do. return. end.
(STATE=: transform STATE) ] function STATE
) 
NB. possible alternative: call y function STATE to use param to resume.


lazy=: 3 : 0
NB. lazy data structures
NB. not meant to be mixed and matched with resume_coroutine_ but it is the same 
code.
NB. this version is meant to be slighly faster and simpler.
NB. if verb, then new STATE=: f STATE
 if. 0=#STATE do. return. end.
 ] STATE=:  function STATE
)

lazyd=: 3 : 0
 NB. variation that uses dyad transform where x argument is (function STATE)
 if. 0=#STATE do. return. end.
 (STATE=: r transform STATE) ] r=.function STATE
)
lclosure=: 3 : '] STATE=: STATE function y'
cl=: 1 : 0 NB. alternate return: pack function with obj reference.
m=. ": >m
3 : ('] STATE_' , m , '_ =: STATE_', m , '_ function_' , m , '_  y' )
)

cocurrent 'base'

Nonad=: 1 : 0
lastobj =: y conew 'nonad'
function__lastobj =: u
if. 4!:0 <'function__lastobj' do. else. NB. if gerund... if not, just behead 
list
     '`function__lastobj transform__lastobj' =: function__lastobj 
    end.
lastobj 
)

Closure=: 2 : 0
o=.(u Nonad n)
NB. 3 : 'o cl_nonad_'
o cl_nonad_
)




----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm





----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to