J provides scripts, which can be introduced using n :0, but scripts can
not contain further scripts. Hypothetically, maybe they could, using
some different convention -- perhaps n :1? And of course these would
need some different method for determining their end.
Anyways, currently we do not have them. But they are still interesting
to think about.
Here's an example implementation of nested blocks in J explicit
definitions:
defn=:2 :0
lns=.0 :n
if.0=L.lns do.
if.-.LF={:lns do.lns=.lns,LF end.
lns=.<;._2 lns
end.
lev=.(i.&0@:=&' 'S:0 lns),0
end=.sel=.I.(<'=.') = {:@;:S:0 lns
b=.1
whilst.1 e.b=.(end<#lns)*.end >&({&lev) sel do.
end=.end+b
end.
for_fix.|.end(],.-)sel+1 do.
ind=._1+0{blk=. (+i.)/fix
tmp=.m :(;(-.&a: blk { lns),each LF)
rpl=.<(ind{::lns),'(3!:2 a.{~',(":a.i.3!:1]5!:1<'tmp'),')5!:0'
lns=. a: blk} lns
lns=. rpl ind} lns
end.
m :(;(-.&a: lns),each LF)
)
And here's a trivial example of how it can be used:
trivi=:3 defn 0
avg=.
(+/ % #) y
avg\y
)
trivi 2 3 5 7
2 2.5 3.33333 4.25
In other words, nested blocks are introduced using =. at the end of
a line (that line can not end in a comment). Also, nested blocks are
delimited using indentation.
Finally note that these nested blocks must be the same kind of
explicit definition as the script as a whole.
Thoughts?
Thanks,
--
Raul
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm