Tuesday, August 6, 2013

uniq

I've used Factor to build several common unix programs including copy, cat, fortune, wc, move, and others.

Today, I wanted to show how to build the uniq program. If we look at the man page, we can see that it is used to:

Filter adjacent matching lines from INPUT (or standard input), writing to OUTPUT (or standard output).

Using the each-line combinator to output each line of text that is not identical to the previous line (using f to force the first line to always be printed):

: uniq-lines ( -- )
    f [
        2dup = [ dup print ] unless nip
    ] each-line drop ;

The input is optionally specified, so need a uniq-file word that will "uniq" the lines of a file, or read directly from the current input-stream (which will be standard input when run from the command line):

: uniq-file ( path/f -- )
    [
        utf8 [ uniq-lines ] with-file-reader
    ] [
        uniq-lines
    ] if* ;

Finally, our "main method" checks the command-line, writing our output to a file if a second command-line argument is provided:

: run-uniq ( -- )
    command-line get [ ?first ] [ ?second ] bi [
        utf8 [ uniq-file ] with-file-writer
    ] [
        uniq-file
    ] if* ;

MAIN: run-uniq

The code for this is on my GitHub.

1 comment:

Unknown said...

i think you could use factor to implement busybox again

which would shows the powerof factor and fits the needs of people