<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">Hi. I thought I'd use Racket to solve a silly little scripting problem. <div><br></div><div>Script doesn't take arguments, instead looks for .csv files in its directory, reads them line by line, does some trivial replacement and writes the result to a new .csv file adding "-ms" suffix to its name. You can see my solution <a href="http://pastebin.com/CPb5gxEu">http://pastebin.com/CPb5gxEu</a> and below. I suppose you bearded hackers would roll-out a quick awk + sh script in a minute - seems like exactly the task suited for awk. Unfortunately I find I'm allergic to sh - entirely too much of a challenge for me. Plus .csv files I'm dealing with are on the order of >200Mb. <br><br>Writing it felt somewhat awkward or rather more so than I anticipated, hence this post. Can you see a better, more straightforward, canonical way of doing this sort of things? Feels like general-purpousness of the language shows and a dsl like scsh would allow more straightforward style. <div><br><div><div>#! /usr/bin/env racket</div><div>#lang racket</div><div><br></div><div>;; doesn't tell directories and files apart, can be dangerous</div><div>(define files</div><div> (filter</div><div> (lambda (e) (regexp-match? #rx"[^-ms]\\.csv" e))</div><div> (directory-list (current-directory))))</div><div><br></div><div>;; this really should be a straightforward pipe with processing in the middle</div><div>;; this wrapping of with--file procedures feels a little like writing cps by hand</div><div>;; (stdin stdout) now point to (in out)</div><div>(define (with-in-out-files proc in out)</div><div> (with-input-from-file in</div><div> (lambda ()</div><div> (with-output-to-file out proc #:mode 'text #:exists 'replace))))</div><div><br></div><div>(define (replace-dot-in-file in)</div><div> (define (dot->comma)</div><div> (for ([l (in-lines)])</div><div> (printf "~a\n" (regexp-replace #rx"\\." l ","))))</div><div> (define (out-filename file suffix)</div><div> (define file-ext (regexp-split #rx"\\." file))</div><div> (string-append (first file-ext) suffix "." (second file-ext)))</div><div> (with-in-out-files dot->comma in (out-filename in "-ms")))</div><div><br></div><div>;; good candidate to exploit that multi-core bazooka</div><div>;; should I be using futures / places / threads and </div><div>;; some kind of parallel map here? </div><div>(time (for-each replace-dot-in-file files))</div><div><br></div><div>Thanks</div><div><br></div><div><br></div><div><br></div></div></div></div></body></html>