Line Filters in Idris

Our example demonstrates a line filter in Idris that reads input from stdin, processes it, and prints a derived result to stdout. This particular filter capitalizes all input text.

module Main

import System.File.ReadWrite
import Data.String

-- The main function is the entry point of our program
main : IO ()
main = do
    -- We'll use getContents to read all input from stdin
    contents <- getContents
    -- Process each line of the input
    let processedLines = map processLine (lines contents)
    -- Print the processed lines
    putStrLn (unlines processedLines)
  where
    -- Function to process each line (convert to uppercase)
    processLine : String -> String
    processLine = toUpper

This program does the following:

  1. We import necessary modules for file I/O and string manipulation.

  2. In the main function, we use getContents to read all input from stdin.

  3. We then use map to apply our processLine function to each line of the input.

  4. The processLine function simply converts each line to uppercase using toUpper.

  5. Finally, we print the processed lines to stdout using putStrLn.

To try out our line filter, first make a file with a few lowercase lines:

$ echo 'hello'   > /tmp/lines
$ echo 'filter' >> /tmp/lines

Then use the line filter to get uppercase lines:

$ cat /tmp/lines | idris line-filters.idr -o line-filters && ./line-filters
HELLO
FILTER

Note that in Idris, we first need to compile our program to an executable before we can run it. The -o flag specifies the output file name.

This example demonstrates how to create a simple line filter in Idris, showcasing basic I/O operations and string manipulation. While Idris doesn’t have a built-in buffered scanner like Go, we can achieve similar functionality using the standard library functions.