Re: [Jprogramming] function within function; scope
Dear Raul and Henry thank you for your comprehensive list of ideas and explanation. It helped. Ruda March 20, 2020 11:22 PM, "Raul Miller" wrote: > There are several ways to accomplish what I think you're asking for: > > Algebraic simplification: > g=:3 :0 > -y > ) > > Pass the intermediate result as a parameter: > > g=:3 :0 > b=. 2*y > f=. 4 :'y-x' > b f y > ) > > Or > > g=:3 :0 > b=. 2*y > f=. b 1 :'y-m' > f y > ) > > Serialize the desired value and incorporate it in the definition of f > > g=:3 :0 > b=. 2*y > f=. 3 :('y-',5!:5<'b') > f y > ) > > Define f tacitly: > > g=:3 :0 > b=. 2*y > f=. ] - b"_ > f y > ) > > If you want 'b' to be incorporated dynamically (so that updates > propagate into f, then you should pass it as a parameter or pull a > stunt like this: > > g=:3 :0 > b=. 2*y > f=. ] - ".@'b' > f y > ) > > You could also use locales, which might be handy if things get > complex, but are way more complicated than this example seems to call > for. > > g=:3 :0 > loc=. cocreate'' > b__loc=. 2*y > f=. loc 1 :'y - b__m' > r=. f y > coerase loc > r > ) > > There's other ways, also -- mostly variations on parameter passing. > > I hope this helps, > > -- > Raul > > On Fri, Mar 20, 2020 at 5:59 PM Rudolf Sykora wrote: > >> Dear list, >> >> although I seem to be able to define a function within another function, >> it seems that the inner function does not see private variables from the >> outer one. What is then the correct way to write something like >> >> g =: 3 : 0 >> b =. 2*y >> f =. 3 : 'y-b' >> f y >> ) >> >> g 2 >> >> and get -2 as a result? >> >> Here one can change b =. to b =: , but this makes b completely public, >> and that's not particularly nice... >> >> Thanks >> Ruda. >> -- >> For information about J forums see http://www.jsoftware.com/forums.htm > > -- > For information about J forums see http://www.jsoftware.com/forums.htm -- For information about J forums see http://www.jsoftware.com/forums.htm
Re: [Jprogramming] function within function; scope
Hmm... I'd phrase that differently: J's blocks are a strict (or maybe 'flat') abstraction that do not inherit from other blocks. Local names (as opposed to locale names) are specific to the block they were created in, That said, the underlying concept is, I think, the same. Thanks, -- Raul On Fri, Mar 20, 2020 at 6:20 PM Henry Rich wrote: > > J has no concept of block scope. locales provide namespaces, so public > assignments are not truly global but only visible within the locale; but > for something small like what you are talking about you just can't do it. > > Henry Rich > > PS You CAN actually get to g's b from the function f, using a feature > added within the last year. But I'm not sure Ken would approve so I'm > keeping it to myself. > > hr > > On 3/20/2020 5:59 PM, Rudolf Sykora wrote: > > Dear list, > > > > > > although I seem to be able to define a function within another function, > > it seems that the inner function does not see private variables from the > > outer one. What is then the correct way to write something like > > > > g =: 3 : 0 > > b =. 2*y > > f =. 3 : 'y-b' > > f y > > ) > > > > g 2 > > > > and get -2 as a result? > > > > Here one can change b =. to b =: , but this makes b completely public, > > and that's not particularly nice... > > > > > > Thanks > > Ruda. > > -- > > For information about J forums see http://www.jsoftware.com/forums.htm > > > -- > This email has been checked for viruses by AVG. > https://www.avg.com > > -- > For information about J forums see http://www.jsoftware.com/forums.htm -- For information about J forums see http://www.jsoftware.com/forums.htm
Re: [Jprogramming] function within function; scope
There are several ways to accomplish what I think you're asking for: Algebraic simplification: g=:3 :0 -y ) Pass the intermediate result as a parameter: g=:3 :0 b=. 2*y f=. 4 :'y-x' b f y ) Or g=:3 :0 b=. 2*y f=. b 1 :'y-m' f y ) Serialize the desired value and incorporate it in the definition of f g=:3 :0 b=. 2*y f=. 3 :('y-',5!:5<'b') f y ) Define f tacitly: g=:3 :0 b=. 2*y f=. ] - b"_ f y ) If you want 'b' to be incorporated dynamically (so that updates propagate into f, then you should pass it as a parameter or pull a stunt like this: g=:3 :0 b=. 2*y f=. ] - ".@'b' f y ) You could also use locales, which might be handy if things get complex, but are way more complicated than this example seems to call for. g=:3 :0 loc=. cocreate'' b__loc=. 2*y f=. loc 1 :'y - b__m' r=. f y coerase loc r ) There's other ways, also -- mostly variations on parameter passing. I hope this helps, -- Raul On Fri, Mar 20, 2020 at 5:59 PM Rudolf Sykora wrote: > > Dear list, > > > although I seem to be able to define a function within another function, > it seems that the inner function does not see private variables from the > outer one. What is then the correct way to write something like > > g =: 3 : 0 > b =. 2*y > f =. 3 : 'y-b' > f y > ) > > g 2 > > and get -2 as a result? > > Here one can change b =. to b =: , but this makes b completely public, > and that's not particularly nice... > > > Thanks > Ruda. > -- > For information about J forums see http://www.jsoftware.com/forums.htm -- For information about J forums see http://www.jsoftware.com/forums.htm
Re: [Jprogramming] function within function; scope
J has no concept of block scope. locales provide namespaces, so public assignments are not truly global but only visible within the locale; but for something small like what you are talking about you just can't do it. Henry Rich PS You CAN actually get to g's b from the function f, using a feature added within the last year. But I'm not sure Ken would approve so I'm keeping it to myself. hr On 3/20/2020 5:59 PM, Rudolf Sykora wrote: Dear list, although I seem to be able to define a function within another function, it seems that the inner function does not see private variables from the outer one. What is then the correct way to write something like g =: 3 : 0 b =. 2*y f =. 3 : 'y-b' f y ) g 2 and get -2 as a result? Here one can change b =. to b =: , but this makes b completely public, and that's not particularly nice... Thanks Ruda. -- For information about J forums see http://www.jsoftware.com/forums.htm -- This email has been checked for viruses by AVG. https://www.avg.com -- For information about J forums see http://www.jsoftware.com/forums.htm