Line Filters in Elixir
A line filter is a common type of program that reads input on stdin, processes it, and then prints some derived result to stdout. grep
and sed
are common line filters.
Here’s an example line filter in Elixir that writes a capitalized version of all input text. You can use this pattern to write your own Elixir line filters.
Let’s break down the code:
We define a module
LineFilter
with amain
function.IO.stream(:stdio, :line)
creates a stream from the standard input, reading it line by line.We use
Stream.map(&String.trim/1)
to remove any trailing whitespace from each line.Stream.map(&String.upcase/1)
converts each line to uppercase.Finally,
Enum.each(&IO.puts/1)
prints each processed line to the standard output.
To try out our line filter, first make a file with a few lowercase lines.
Then use the line filter to get uppercase lines.
In Elixir, we don’t need to explicitly handle errors for IO operations as we did in the original example. Elixir’s design philosophy is “let it crash”, and the runtime will handle most common IO errors gracefully.
This Elixir version provides the same functionality as the original, using idiomatic Elixir constructs like streams and the pipe operator to create a concise and efficient line filter.