The Pipe Operator In Elixir

Be like Mario: get in the pipe

Nicholas Feitel
Level Up Coding
Published in
3 min readMay 18, 2020

--

My recent journey in programming out of school has taken me to some worthy places including volunteer/open-source projects, learning Node/Express, and even my still-obsession, a React-wrapper called Gatsby.

But lately, I’ve been checking out Elixir, a functional programming language whose utility in handling concurrency and errors make it a cool choice in this era of intense JavaScript love.

Now Elixir uses Ruby-like syntax which is friendly and mostly reads like English and Elixir shares both most of the syntax of Ruby and some of the cooler inherent features.

ex:# Print "Hello World" to the console in Elixirdef hello doIO.puts("Hello World") end# no import needed, IO just works like the native puts method in Ruby.# now let's return "Hello World" as the end of our functiondef ret_hello do"Hello World"end

Just like Ruby, Elixir has “implicit return”, the idea that whatever the return value of your last statement or variable is will be the return value of your function. This is super “clean” in terms of not having to remember where to return your values and just writing what you need.

This can also be done with arguments.

def add(x,y) dox + yend# returns the sum

No need to save the variable, which arguably you don’t have to do in JavaScript either, but it’s often convention to cleanly return a single variable rather than an expression.

But what about multiple functions? In Ruby or JavaScript, they might look something like this.

# the above exampledef add(x,y) dox + yend# new functiondef multiply_by_two(number) donumber * 2

--

--