Perhaps there’s no facility to do this in the J language, but when I write:
succession =: monad define
greg =. 1 2 3
tom =. {{ greg }}
tom y
)
and then call succession 'season 1'
, I get the following error:
|noun result was required: tom
| greg
My understanding is that the variable greg
is not available in the namespace of the nested verb tom
, and so greg
is assumed to be a verb. But verbs can’t be returned as values from verbs, hence the error.
Two unsatisfactory solutions come to mind:
- Add
greg
to the global environment using=:
instead of=.
. This is of course horribly error-prone. - Pass
greg
as the left argument totom
. This seems ugly/unnecessary, and it gets even messier iftom
eventually requires access to more local variables (kendall
,shiv
,roman
).
I believe a sane solution is to use an appropriate namespace:
t =: monad define
greg_loc_ =. 1 2 3
tom =. {{ greg_loc_ }}}
tom y
)
t 'hi'
1 2 3
2
you are correct that there is no good way to do this; j does not have closures
my style is to make nested definitions tacit wherever possible, which does avoid this problem
if you are going to use locales, i would suggest not to use named locales, but rather create a fresh locale and pass it in, viz.
succession =: monad define
l=. cocreate''
greg__l=. 1 2 3
tom=. l&{{ greg__x }}
tom y
)
but do note that you now have to delete the locale at some point; there is no such problem when you pass in a context array