Pipeline Operator And Functions In Elixir And Elm

Karlo Smid
2 min readMay 22, 2020

--

TL;DR

Let’s explain the most exciting Elixir and Elm operator and how it operates with functions. This post is part of the functional language series.

We have already learned how to define functions in Elixir and Elm. The function is a black box with input and output. We day that output is a function of the input. To solve problems, the programmer has to use multiply functions.

The program is a pipeline of chained functions.

The output of the previous function is the input of the next one. Elixir and Elm have an operator for that feature, a pipeline:

|>

Elixir example of pipe in IEx:

Here we pipe two math functions: a square root and power. Squareroot has only one input, while pow has two inputs, number, and actual power. Both functions have only one output.

We “pipe” number two as input to :math.sqrt() function, and result of that function we pipe into :math.pow(2) function. This is why :math.sqrt() has empty parenthesis, while :math.pow(2) has only number 2 as input.

A similar example in Elm Repl:

Here we first pipe -4 to abs which results in 4, and then we pipe that to sqrt. We do this because we can only take sqrt of positive numbers.

Remember

  • what pipes the pipe operator into the function
  • pipe operator syntax

Originally published at https://blog.tentamen.eu on May 22, 2020.

--

--

Karlo Smid
Karlo Smid

Written by Karlo Smid

Founder of Tentamen, software testing agency.

No responses yet