Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Workspace in J

39 views
Skip to first unread message

Eke van Batenburg

unread,
Mar 24, 1997, 3:00:00 AM3/24/97
to

A week ago the question arose in this newsgroup about the value of the WS
concept.
I submitted that I like it very much and that I miss it in J.
Chris offered some consolation by offering a nice couple of functions
(thanks Chris).

His reaction stimulated me to extend the functions a bit. I hope to
contribute to the lack of the WS (I still miss the opportunity to save
the status and resume) and submit them for whoever is interested to use
them (or to improve them).
The functions are wssave, wsload, wscopy and wsnl.

NB. -------------SAVE -----------
wssave=: 3 : 0
NB. Save all objects of certain types to file y.
NB. y[]: filename
NB. x: object type(s) (def.1 2 3 4)
NB. E: 2 wssave 'EVOLUTN'
NB. P: EvBatenburg 19-3-1997
(i.4)wssave y.
:
ok=.1
if. #1!:0<fl=.y.,'.jw' do. NB. If file exists...
('File ',fl,' exists. Continue?')(1!:2)2 NB. ask permission
ok=.-.+/'nN'=0{t=.(1!:1)1
end.

if.ok do. jcreate fl NB. Open file
t=.((4!:1)x.)-.<;._1 ' ok fl t x. y.'
(<t) jappend fl NB. Write names of all items x.
(5!:1 "1 t)jappend fl NB. and append their representation
end.
t
)

NB. ------------- COPY -----------
wscopy=: 3 : 0
NB. Copy items from file into workspace
NB. y[]: filename followed by items (all items if no specified)
NB. x: 0/1 for nonprotected/protected copy (def.1)
NB. E: 0 wscopy 'EVOLUTN A B C D'
NB. P: EvBatenburg 19-3-97
1 wscopy y.
:
fil=.'.jw',~>0{r=.<;._1 ' ',y. NB. filename
if. #1!:0<fil do. NB. If file exists
b=.(nms=.>jread fil;0)e.r=.1}.r
if. 0=#r do. NB. All items if no items specified
b=.1#~#r=.nms
end.
if.#t=.r-.nms do. NB. Report items not found
('Not found:',;' ',&.>t)(1!:2)2
end.
if. x.*.1 e.t=._1~:(4!:0)b#nms do. NB. Report protected items
('Protected:',;' ',&.>t#b#nms)(1!:2)2
b=.0 (t#b#i.#b)}b
end.
b=.0,b#i.#r=.b
while. #b=.}.b do. NB. Copy if items left
(t{nms)=:(jread fil;1+t=.0{b)5!:0
end.
;' ',&.>r#nms
else.
'File "',fil,'" not found.'
end.
)

NB. ------------- NAMELIST -----------
wsnl=: 3 : 0
NB. Name List of objects in file y or type y. objects in WS
NB. y[]: file name (active WS if '' or number)
NB. E: wsnl 'EVOLUTN' or wsnl '' or wsnl 0 3
NB. P: EvBatenburg 19-3-97
if. 0=#,y. do. y.=.i.4 end.
if. 2=3!:0 y. do.
fil=.'.jw',~>0{r=.<;._1 ' ',y. NB. filename
if. #1!:0<fil do. NB. Check and warn if existing file
;' ',&.>>jread fil;0
else.
'File "',fil,'" not found.'
end.
else.
;' ',&.>4!:1 y.
end.
)

NB. ------------- LOAD -----------
wsload=: 3 : 0
NB. Load objects from file y
NB. y[]: filename
NB. E: wsload'EVOLUTN'
NB. P: EvBatenburg 17-3-97
if. #1!:0<fl=.y.,'.jw' do. NB. Check if existing file
(4!:55)((4!:1)i.4)-.<'fl' NB. Erase current items
i=.1+#nm=.>jread fl;0 NB. First read names...
while. i=.<:i do.
((i-1){nm)=:(jread fl;i)5!:0 NB. and fix their values
end.
;' ',&.>nm
else.
'File "',fl,'" not found.'
end.
)

Eke van Batenburg

---------------------------------
F.H.D. van Batenburg
Institute for Theoretical Biology
Rijks University Leiden
Kaiserstraat 63
2321GP Leiden
The Netherlands
---------------------------------


0 new messages