Arc Forumnew | comments | leaders | submitlogin
4 points by waterhouse 5134 days ago | link | parent

Macros: (fromfile f . body), (tofile f . body), (ontofile f . body)

'fromfile executes body in a context where stdin is an input-port that reads the contents of the file described by the string f; it closes the port afterwards. 'tofile is similar, but instead binds stdout so that it writes to the file f. 'ontofile is similar to 'tofile, except that output is appended to the file f.

These macros, while not strictly necessary given the existence of 'w/infile, 'w/outfile, and 'w/appendfile (which are implemented almost identically), make it nicer to read from and write to files. There are several macros in Arc that come in pairs, one binding a variable and the other supplying a usually anaphoric default variable name. Examples: 'rfn and 'afn, 'each and 'on, 'iflet and 'aif (although these aren't quite identical). I think this is a good pattern.

'tofile and 'ontofile are especially useful when you want to write to a file using 'pr, 'prn, 'prs, etc., none of which take an optional output-port argument; alternatives would be rebinding stdout using 'w/stdout, or collecting output with 'tostring and 'disp-ing it all at once (which may not work as a substitute if it's important that output be generated incrementally).

Credit for these names goes to fallintothis: http://arclanguage.org/item?id=12272

  (mac fromfile (f . body)
    (w/uniq gf
      `(w/infile ,gf ,f
         (w/stdin ,gf
           ,@body))))
  (mac tofile (f . body)
    (w/uniq gf
      `(w/outfile ,gf ,f
         (w/stdout ,gf
           ,@body))))
  (mac ontofile (f . body)
    (w/uniq gf
      `(w/appendfile ,gf ,f
         (w/stdout ,gf
           ,@body))))


1 point by akkartik 5134 days ago | link

I was calling ontofile w/prfile. Thanks for the better name and all the context.

-----