Excerpt


Some things in programming make me feel at peace with the world. One such thing is the pipeline (or pipe) operator.
It’s so simple, but so elegant. Today, I will act as your local functional programming hype-man, and go over how it works and why it’s great (for the uninitiated).
The pipe operator is a staple in languages like Elixir and OCaml, but curiously, the first place we can really take a look at it is in Linux and the Unix philosophy.
## The origin of the pipe
The pipe operator is a simple concept. It takes the output of one program or function, and passes it as input to another.
If you’ve used the terminal before you’ll likely be familiar with the | symbol. You can do some cool things with it.
For example, let’s say you want to download a JSON file from a website, pull out certain data, and pretty print it in the terminal.
Here’s a command which fetches JSON data about cats (run at your own risk!):
```plain text
curl -X 'GET' 'https://cataas.com/api/cats' -H 'accept: application/json' | jq '.'
```
You can do this by using the program curl to fetch the data, and then jq to format and print the JSON. You can use the pipe operator (|) to join them together (with relevant arguments).
What’s wonderful about this is the composability. Yes yes, I’m just throwing big words around, but it’s truly quite elegant.
curl does not know jq exists.
jq does not know curl exists.
Neither of these programs knows what kind of data the other one outputs, or what it even does. Yet, you can get perfect results by simply piping the output of one program into the input of another.
That being said, the Unix pipe is not really a “functional” feature in the programming paradigm sense.
But, functional programming languages are what took this pipe and brought it into general-purpose programming.
## Pipes, functions, and more

In imperative, object-oriented (or even multi-paradigm) languages you can achieve the same kind of effect as the pipe with method chaining.
Here’s an example of turning a string to uppercase and reversing it in JavaScript:
```plain text
const myString = "A wizard is never late."
const result = myString.toUpperCase().split('').reverse().join('');
```
Granted, this might be a little bit of an unfair example. In JavaScript you have to convert the string to an array first to reverse it, and then join it back together.
This method1().method2().method3() is the standard approach you might be familiar with. But, what would this look like using a pipe operator instead?
Here’s the same example using the pipe operator in Elixir:
```plain text
my_string = "A wizard is never late."
result = my_string |> String.upcase() |> String.reverse()
```
Now come on, tell me that doesn’t look pretty. Method chaining feels a little bit more difficult to parse at times for me at least, especially as the methods start adding up.
I also want to be clear that this isn’t a post to hate on JavaScript; I actually (shock) even enjoy the language. I just wanted to talk about pipe operators because they’re cool.
In fact, JavaScript even has a proposed pipeline operator which may (one day) be added to the language. The same example might look something like this:
```plain text
const result = "A wizard is never late."
|> (s) => s.toUpperCase()
|> (s) => s.split('').reverse().join('');
```
The beauty of functional programming is that it has a lot of features which end up being added to mainstream languages because people see value in them. Just as people see value in other paradigms, and adopt them accordingly.
What I’m hoping is that they’ll be a nice entry point for you to check out a functional programming language and their features.
If you’re interested, I’d definitely recommend giving Elixir a try if you’re into dynamic languages. Or, if you want something statically-typed, give OCaml a go. It’s also got some object features which might make you feel at home.
Thanks for reading!
If you want to read more posts on functional programming, PL development, and my self-learning approach to these things, feel free to subscribe.
Subscribe