Skip to content

Latest commit

 

History

History
52 lines (36 loc) · 1.36 KB

pipe-first.md

File metadata and controls

52 lines (36 loc) · 1.36 KB
title
Pipe First

Pipe first is the operator to apply a function to a value where data is passed as the first argument. -> is a convenient operator that allows you to "flip" your code inside-out. a(b) becomes b->a.

let result = value->function;

Imagine you have the following:

validateAge(getAge(parseData(person)))

This is slightly hard to read, since you need to read the code from the innermost part, to the outer parts. Use Pipe First to streamline it

person
  ->parseData
  ->getAge
  ->validateAge

Basically, parseData(person) is transformed into person->parseData, and getAge(person->parseData) is transformed into person->parseData->getAge, etc.

Pipe first operator always applies the value to the first argument of the function, even if that function takes more than one argument.

fn(one, two, three)

is the same as

one->fn(two, three)

This can be combined with labeled arguments. For example if a function foo is defined as:

let foo = (~two, ~three, data) => // { ... }

Note that the last argument isn't labelled, and it can be called with pipe-first like this:

data->foo(~two, ~three)

This section is documented under Melange's documentation as well: Pipe first.