Actually the hash was not unique. This uses the whole row from the symbol
table as a file name rather than just the column 2 (the hash column) , which
I think is going to be unique as long as I always save and restore the
entire symbol table?

Anyway, code below:

purge =: 1 : 0
 if. 0 ~: # (1!:0) 'C:\data\cache\symb.dat' do.
  10 s: (3!:2) 1!:1 <'C:\data\cache\symb.dat' NB. make sure symbol table as
we left it
 else.
  message 'No previous symbol table found.'
 end.
 NB.parameters =. (( (TAB ~: ]) *. (' ' ~: ]) *. (LF ~: ])) # ]) ((5!:5)
<'u' ), ": y
    hash =. (  [: , [: ; [: {. : ; (0: s: 2:) {~ [: 6&s: s: ) (3!:1) (( (TAB
~: ]) *. (' ' ~: ]) *. (LF ~: ])) # ]) ((5!:5) <'u' ), ": y
 hash =. (( (TAB ~: ]) *. (' ' ~: ]) *. (LF ~: ])) # ])  ": hash
 dir =. 'C:\data\cache\'
 NB.fname =. dir, 'cache', ": (  2: { [: ; (0: s: 2:) {~ [: 6&s: s: ) (3!:1)
parameters
 fname =. dir,hash
 if. 1 = # (1!:0) fname do.
  f =. u cache NB. don't know why his has to be here, but if it is not you
get a syntax error.
  (1!:55) <fname
  message 'Purged! : ', fname, '...', parameters
 else.
  message 'Not purged.. was not cached : ', fname, '...', parameters
 end.
    ((3!:1) 0 s: 10) 1!:2 <'C:\data\cache\symb.dat' NB. save the new symbol
table
)

cache =: 1 : 0
 if. 0 ~: # (1!:0) 'C:\data\cache\symb.dat' do.
  10 s: (3!:2) 1!:1 <'C:\data\cache\symb.dat' NB. make sure symbol table as
we left it
 else.
  message 'No previous symbol table found.'
 end.
 NB.parameters =. (( (TAB ~: ]) *. (' ' ~: ]) *. (LF ~: ])) # ]) ((5!:5)
<'u' ), ": y
    hash =. (  [: , [: ; [: {. : ; (0: s: 2:) {~ [: 6&s: s: ) (3!:1) (( (TAB
~: ]) *. (' ' ~: ]) *. (LF ~: ])) # ]) ((5!:5) <'u' ), ": y
 hash =. (( (TAB ~: ]) *. (' ' ~: ]) *. (LF ~: ])) # ])  ": hash
 dir =. 'C:\data\cache\'
 NB.fname =. dir, 'cache', ": (  2: { [: ; (0: s: 2:) {~ [: 6&s: s: ) (3!:1)
parameters
 fname =. dir,hash
 if. 0 = # (1!:0) fname do.
  message 'calculating result and caching...'
  data =. u  y
  ( 3!:1 data ) fwrite fname
 else.
  message 'using previously cached result...'
  data =. ( 3!:2 fread fname )
 end.
 ((3!:1) 0 s: 10) 1!:2 <'C:\data\cache\symb.dat' NB. save the new symbol
table
 data
)

On Thu, Apr 24, 2008 at 2:33 PM, Jorge Arredondo <
[EMAIL PROTECTED]> wrote:

> You can use 5!:5 to get the string representation of u, for instance:
>   a=: 1 : '(5!:5<''u'')'
>   '+/' -: +/ a
> 1
>
> On Thu, Apr 24, 2008 at 8:08 AM, Matthew Brand <[EMAIL PROTECTED]>
> wrote:
> > This is what I have in mind: (to run it you must create directory
> >  c:\data\cache or change the "dir" variable).
> >
> >  myadverb =: 1 : 0
> >  NB. y is a string already, but how do I convert the input verb "u" into
> a
> >  string?
> >  NB.parameters =. <convert verb u to a string> , y
> >  parameters =. y
> >  dir =. 'C:\data\cache\'
> >  fname =. dir, 'myadverb', ": ( 2: { [: ; (0: s: 2:) {~ [: 6&s: [: s: '
> '&~:
> >  # ]) (3!:1) parameters
> >  if. 0 = # (1!:0) fname do.
> >  ('calculating result and caching...') (1!:2) 2
> >  data =. u i. # y NB. would normally be data =. u y
> >  ( 3!:1 data ) fwrite fname
> >  else.
> >  ('using previously cached result...') (1!:2) 2
> >  data =. ( 3!:2 fread fname )
> >  end.
> >  data
> >  )
> >
> >  Examples:
> >  +/\ myadverb 'inputA'
> >  calculating result and caching...
> >  0 1 3 6 10 15
> >  +/\ myadverb 'inputB'
> >  calculating result and caching...
> >  0 1 3 6 10 15
> >  +/\ myadverb 'inputA'
> >  using previously cached result...
> >  0 1 3 6 10 15
> >  +/\ myadverb 'inputB'
> >  using previously cached result...
> >  0 1 3 6 10 15
> >
> >
> >  How can I get the text that is the program code for the input verb (u)
> so
> >  that I can generate the hash for it though? I.e. how do I turn u into
> the
> >  string '+/\' so that I can concatenate it with y?
> >
> >
> >  Thanks,
> >
> >
> > Matthew.
> >  ----------------------------------------------------------------------
> >  For information about J forums see http://www.jsoftware.com/forums.htm
> >
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
>
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to