I am trying to write a routine which will be something like unix filter:
open file
process each line
close file here is the code(this may not compile yet :)). my question is how to refactor 'ffilter, 'ffilter1 & 'ffilter2 methods?? Thanks. (def filltbl (keys vals (o tbl (table)))
((map (fn (k v) (= tbl.k v)) keys vals)
tbl))
; 'func takes a hashtbl
(def ffilter2 (file func fldnames (o fldsep whitec))
(let nln 0
(w/infile in file
(whilet line (readline in)
(++ nln)
(func (filltbl (fldnames) (tokens line fldsep)))
))
nln))
; 'func takes a list
(def ffilter1 (file func (o fldsep whitec))
(let nln 0
(w/infile in file
(whilet line (readline in)
(++ nln)
(func (tokens line fldsep))))
nln))
; 'func directly works on line
(def ffilter (file func )
(let nln 0
(w/infile in file
(whilet line (readline in)
(++ nln)
(func line)))
nln))
;; some usage (def cat (f)
(filter f [pr _]))
(def grep (f pat)
(filter f [if (matchpat pat _) pr _]))
(def cut (f fldsep pos)
(filter1 f [pr (pos _)] fldsep))
(def loadcsv (f)
(filter2 f [pr _] #\,))
|