Over the years I've known J, the question of how to make J implement a repl
/ input loop has come up numerous times.
Back in 2014, I sat down and figured out how to make one:
NB. -----------------------------------------
readln =: [: (1!:01) 1:
donext =: [: (9!:29) 1: [ 9!:27
main =: verb define
echo ''
echo 'main loop. type ''bye'' to exit.'
echo '--------------------------------'
while. (s:'`bye') ~: s:<input=:readln'' do.
echo ".input
end.
echo '--------------------------------'
echo 'loop complete. returning to j.'
NB. or put ( exit'' ) here to exit j.
)
donext 'main _'
NB. -----------------------------------------
And since then, numerous people have asked how to do this because it was
presumably a hard thing to do, and I would always point them to this
example.
Yesterday on stream I was talking about making a REPL, and saw it's now
possible to just put the readln ((1!:1)1) call in a loop:
NB. -----------------------------------------
repl =: {{
src =. ''
while. 1 do.
src =. CRLF -.~ 1!:1]1 NB. readline
if. src -: 'bye' do. 0$0 return. end.
res =. ". src
echo res
end. }}
NB. -----------------------------------------
Am I crazy to think this used to be difficult?
If not, what changed, and when?
-Michal
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm