For anyone interested, adverbs "cache" and "purge" defined below are very
useful for auto-caching the results of lengthy calculations.

" u cache y " executes the monad u as u y and stores the result in a
uniquely named (?) binary file, unless it has already been exectued with the
current parameters u and y, in which case it does not bother to redo the
calculation but just reads the result from the cache.

"u purge y" will remove the cache file from the disk and the next execution
of " u cache y' will result in execution of u y and storage of the result.

Can anyone confirm that the hash code is unique and that I don't need to
worry about the "color" column in the symbol table?

Thanks,
Matthew.

Code and examples:


QUIET =: 0
message =: 3 : 0
 if. -. QUIET do.
  y (1!:2) 2
 end.
)

  purge =: 1 : 0
 parameters =. (( (TAB ~: ]) *. (' ' ~: ]) *. (LF ~: ])) # ]) ((5!:5) <'u'
), ": y
 dir =. 'C:\data\cache\'
 fname =. dir, 'cache', ": (  2: { [: ; (0: s: 2:) {~ [: 6&s: s: ) (3!:1)
parameters
 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.

)

   cache =: 1 : 0
 parameters =. (( (TAB ~: ]) *. (' ' ~: ]) *. (LF ~: ])) # ]) ((5!:5) <'u'
), ": y
    dir =. 'C:\data\cache\'
 fname =. dir, 'cache', ": (  2: { [: ; (0: s: 2:) {~ [: 6&s: s: ) (3!:1)
parameters
 NB. fname =. u cachefname y
 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.
 data
)

NB. Example, function is +/\ and y is i.10000. It is not useful here
because it does not take long to do +/\ i.10000, but it is useful
for functions which take ages to execute.

   (+/\) cache i.10000
calculating result and caching...
0 1 3 6 10 15 21 28 36 45 55 66 78 91 105 120 136 153 171 190 210 231 253
276 300 325 351 378 406 435 465 496 528 561 595 630 666 703 741 780 820 861
903 946 990 1035 1081 1128 1176 1225 1275 1326 1378 1431 1485 1540 1596 1653
1711 1770 1830 1891 1953 2016 2080 2145 2211 2278 2346 2415 2485 2556 2628
2701 2775 2850 2926 3003 3081 3160 3240 3321 3403 3486 3570 3655 3741 3828
3916 4005 4095 4186 4278 4371 4465 4560 4656 4753 4851 4950 5050 5151 5253
5356 5460 5565 5671 5778 5886 5995 6105 6216 6328 6441 6555 6670 6786 6903
7021 7140 7260 7381 7503 7626 7750 7875 8001 8128 8256 8385 8515 8646 8778
8911 9045 9180 9316 9453 9591 9730 9870 10011 10153 10296 10440 10585 10731
10878 11026 11175 11325 11476 11628 11781 11935 12090 12246 12403 12561
12720 12880 13041 13203 13366 13530 13695 13861 14028 14196 14365 14535
14706 14878 15051 15225 15400 15576 15753 15931 16110 16290 16471 16653
16836 17020 17205 17391 17578 17766 17955 18145 18336 18528 18721 18915
19110 19306 19503 19701 19900 2010...
   (+/\) cache i.10000
using previously cached result...
0 1 3 6 10 15 21 28 36 45 55 66 78 91 105 120 136 153 171 190 210 231 253
276 300 325 351 378 406 435 465 496 528 561 595 630 666 703 741 780 820 861
903 946 990 1035 1081 1128 1176 1225 1275 1326 1378 1431 1485 1540 1596 1653
1711 1770 1830 1891 1953 2016 2080 2145 2211 2278 2346 2415 2485 2556 2628
2701 2775 2850 2926 3003 3081 3160 3240 3321 3403 3486 3570 3655 3741 3828
3916 4005 4095 4186 4278 4371 4465 4560 4656 4753 4851 4950 5050 5151 5253
5356 5460 5565 5671 5778 5886 5995 6105 6216 6328 6441 6555 6670 6786 6903
7021 7140 7260 7381 7503 7626 7750 7875 8001 8128 8256 8385 8515 8646 8778
8911 9045 9180 9316 9453 9591 9730 9870 10011 10153 10296 10440 10585 10731
10878 11026 11175 11325 11476 11628 11781 11935 12090 12246 12403 12561
12720 12880 13041 13203 13366 13530 13695 13861 14028 14196 14365 14535
14706 14878 15051 15225 15400 15576 15753 15931 16110 16290 16471 16653
16836 17020 17205 17391 17578 17766 17955 18145 18336 18528 18721 18915
19110 19306 19503 19701 19900 2010...
   (+/\) purge i.10000
Purged! : C:\data\cache\cache_605535552...+/...




On Thu, Apr 24, 2008 at 2:55 PM, Matthew Brand <[EMAIL PROTECTED]>
wrote:

> Thanks Jorge.  Here is the final program below for anyone interested.
>
> myadverb =: 1 : 0
> parameters =. (( (TAB ~: ]) *. (' ' ~: ]) *. (LF ~: ])) # ]) ((5!:5) <'u'
> ), 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 y
> ( 3!:1 data ) fwrite fname
> else.
> ('using previously cached result...') (1!:2) 2
> data =. ( 3!:2 fread fname )
> end.
> data
> )
>
>
>   On 4/24/08, 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