The output fromstdinF :: F a String stdoutF :: F String a stderrF :: F String a
stdinF
is the characters received from the
program's standard input channel. For efficiency reasons,
you do not get one character at a time, but larger chunks of
characters. If you want the input as a stream of lines, you
can use
which puts together the chunks and splits them at the newlines.inputLinesSP :: SP String String
A simple example is a fudget that copies text from the keyboard to the screen with all letters converted to upper case:
It appliesstdoutF >==< (map toUpper >^=< stdinF)
toUpper
to all characters in the strings
output by stdinF
and then feeds the result to
stdoutF
.
Here is a fudget that reverses lines:
The precedences and associativities of the combinators are such that these fudgets can be written as:(stdoutF>=^<((++"\n").reverse))>==<(inputLinesSP>^^=<stdinF)
stdoutF >==< map toUpper >^=< stdinF stdoutF >=^< (++"\n").reverse >==< inputLinesSP >^^=< stdinF
These can be seen as servers, with a one-to-one correspondence between requests and responses. For convenience, the responses are paired with the file path from the request. The responses contain either an error message or the result of the request. The result is the contents of a file (readFileF :: F FilePath (FilePath,Either IOError String) writeFileF :: F (FilePath,String) (FilePath,Either IOError ()) readDirF :: F FilePath (FilePath,Either IOError [FilePath])
readFile
), a
directory listing (readDirF
), or ()
( writeFileF
).
The timer is initially idle. When it receivesdata Tick = Tick timerF :: F (Maybe (Int, Int)) Tick
Just
(i,d)
on its input, it begins ticking. The
first tick will be output after d milliseconds. Then,
ticks will appear regularly at i millisecond
intervals, unless i is 0, in which case only one tick
will be output. Sending Nothing
to the timer resets it
to the idle state.
As a simple example, here is a fudget that outputs,once a second, the number of seconds that have elapsed since it was activated:
countSP >^^=< timerF >=^^< putSP (Just (1000,1000)) nullSP where countSP = mapAccumlSP inc 0 inc n Tick = (n+1,n+1)