Reason
  • Docs
  • Try
  • API
  • Community
  • Blog
  • Languages iconEnglish
    • 日本語
    • Deutsch
    • Español
    • Français
    • 한국어
    • Português (Brasil)
    • Русский
    • Українська
    • 中文
    • 繁體中文
    • Help Translate
  • GitHub

›JavaScript

Intro

  • What & Why
  • Getting started

Setup

  • Installation
  • Editor Plugins
  • Format (refmt)

Language Basics

  • Overview
  • Let Bindings
  • Primitives
  • Basic Structures
  • Types
  • Records
  • Variants
  • Options and nullability
  • Functions
  • Recursion
  • Destructuring
  • Pattern Matching
  • Mutable Bindings
  • Loops
  • Modules

Advanced Features

  • JSX
  • External
  • Exception
  • Object

JavaScript

  • Melange
  • Interop
  • Syntax Cheatsheet
  • Pipe First
  • Promise
  • Libraries
  • Converting from JS

Extra

  • Frequently Asked Questions
  • Extra Goodies
  • REPL (rtop)
Edit

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.

← Syntax CheatsheetPromise →