Arc Forumnew | comments | leaders | submitlogin
3 points by almkglor 5904 days ago | link | parent

Should be possible to abstract a layer around file-table for that (untested):

  (def file-table-w/read (path)
    (let ft (file-table path)
      (add-attachments
        '= (fn (v k)
             (= (ft k) (tostring (write v))))
        'keys (fn () (keys ft))
        (annotate 'table
          (fn (k) (read (ft k)))))))
Of course, the above doesn't memoize, so repeated calls to ftab!fname will return different ((iso ftb!fname ftb!fname) => t, (is ftb!fname ftb!fname) => nil) objects.

And of course, you're not supposed to write circular objects with the above.

And then someone will want to use temload and friends... hmm. Need a good way of abstracting the abstractions...



2 points by eds 5903 days ago | link

Thanks. It might be nice to have that on Anarki.

EDIT: And it also might be nice to have the path parameter be optional (and default to ".").

-----

3 points by almkglor 5903 days ago | link

Hmm. What I'm currently planning is to have 'tofile and 'fromfile tagged arguments instead:

  (def file-table-w/read (path)
    (file-table path
                'tofile    [tostring (write _)]
                'fromfile  read))
Then memoization will be done on the file-contents (ct and mt) tables instead of actual file-table objects.

It would be useful also to have non-memoized versions, accessible via 'nocache:

  (def grep* (rex path)
    (zap re rex)
    (accum collect
      (ontable k v (file-table path 'nocache)
        (if (re-match rex v) (collect k)))))
'nocache would be useful for such cases where you want to scan through files but not cache their actual contents.

It might also be useful to store cached contents only for a certain time, to preserve memory (but then gc will get run anyway).

p.s. supporting tagged options will make an optional path argument difficult. I suppose I can check if the first argument is a string, though, and treat it as the path if it is.

-----