Transforming the Data

Learn about the major goal of transforming the data.

If you come from an object-oriented world, you’re used to programming with classes and their instances. When we code with objects, we’re thinking about the state. Much of our time is spent calling methods in objects and passing them to other objects. Based on these calls, objects update their own state and possibly the state of other objects. In this world, the class is king. It defines what each instance can do, and it implicitly controls the state of the data its instances hold. Our goal is data hiding.

But that’s not the real world. We want to get things done, not maintain the state. Let’s illustrate it through an example. Suppose we’re taking empty computer files and transforming them into files containing text. Soon, we’ll transform those files into a readable format. A web server somewhere will transform the request to download the book into an HTTP response containing the content. Put simply, we don’t want to hide data. We want to transform it.

Combine transformations with pipelines

Unix users are accustomed to the philosophy of small, focused command-line tools that can be combined in arbitrary ways. Each tool takes an input, transforms it, and writes the result in a format the next tool (or a human) can use.

This philosophy is incredibly flexible and leads to fantastic reuse. The Unix utilities can be combined in ways undreamed of by their authors. And each one multiplies the potential of the others. It’s also highly reliable. Each small program does one thing well, making it easier to test.

There’s another benefit. A command pipeline can operate in parallel. If we write grep Elixir *.pml | wc -l, the word-count program, wc, runs at the same time as the grep command. This is because wc consumes the output of grep.

Functions are data transformers

Elixir lets us solve the problem in the same way the Unix shell does. Rather than have command-line utilities, we have functions. And we can string them together as we please. The smaller and more focused those functions are, the more flexibility we have when combining them.

If we want, we can make these functions run in parallel. Elixir has a simple but powerful mechanism for passing messages between them. Here, we’re talking about the potential to run millions of them on a single machine and have hundreds of these machines interoperating.

Note: Bruce Tate commented on this paragraph with this thought: “Most programmers treat threads and processes as a necessary evil; Elixir developers feel they are an important simplification.”

As we get deeper into the course, this quote will make more sense.

This idea of transformation lies at the heart of functional programming: a function transforms its inputs into its output. Many of our instincts will be wrong. It’ll be frustrating because we’re going to feel like a complete beginners, and that’s part of the fun. But at some point, things will click. We’ll start thinking about problems in a different way, and we’ll be writing small chunks of code that can be used over and over, often in unexpected ways.