Unique
Saturday, August 20, 2011
A few days ago, I noticed this example from the
Racket website, for reporting “each unique
line from stdin
”:
;; Report each unique line from stdin
(let ([saw (make-hash)])
(for ([line (in-lines)])
(unless (hash-ref saw line #f)
(displayln line))
(hash-set! saw line #t)))
We can implement the same functionality in Factor, reading each unique line from an input stream:
: unique-lines ( -- )
lines members [ print ] each ;
The lines word acts on the “current input stream”, so we can use a file reader as an input stream to print out all unique lines in a file:
: unique-file ( path -- )
utf8 [ unique-lines ] with-file-reader ;
If we wanted to make this print and flush each unique line of input as it is read, we could have used the each-line word to implement it in a line-by-line fashion:
: unique-lines ( -- )
HS{ } clone '[
dup _ ?adjoin [
print flush
] [ drop ] if
] each-line ;