In python, you can use this `if __name__=="__main__":` line in a module,
and it will only execute the code in that block if you are running the
module directly.
This allows you to treat the same file as both a standalone application,
and a library/module that can be imported into another program. (Or just as
often often, to run a test suite for just that module as the "standalone"
version.)
I tried to make the same thing in J
My first thought was to turn on the debugger and inspect the stack to see
whether or not we're inside of "load", but it turns out that load does not
appear in the stack trace unless you enabled the debugger *before* you
called load.
So instead, I wound up with this:
{{y
NB. standalone code goes here
}}^:('repl.ijs' {.@E.&.|. >{.}.ARGV)''
It works pretty well, assuming the file in question is named 'repl.ijs' ...
Now I can run my repl as a standalone application, or import it as a widget.
(Yes, I could just make another file for the standalone code, but I really
like the python style.)
Does anyone have a better approach?
If load_j_ incremented and decremented a global counter, then you could do
this with:
{{y
NB. standalone code goes here
}}^:(-.*load_depth)''
Thoughts?
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm