Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add syntax lookup pipe operator #192

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions misc_docs/syntax/operators_pipe.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
---
id: "pipe"
keywords: ["pipe", "operator", "function", "argument"]
name: "->"
summary: "This is the `pipe` operator."
category: "operators"
---

The `->` operator passes a value into the first argument position of a function. Typically it's used to chain multiple function calls together in a "pipeline like" fashion.

### Example

<CodeTab labels={["ReScript", "JS Output"]}>

```res
let dieRoll = size => {
Js.Math.random_int(1, size)
}

let dieRollMessage = (value, name) => {
"Hi " ++ name ++ ", you rolled a " ++ Js.Int.toString(value)
}

let message = dieRoll(6)->dieRollMessage("Marshall")
```

```js
function dieRoll(size) {
return Js_math.random_int(1, size);
}

function dieRollMessage(value, name) {
return "Hi " + name + ", you rolled a " + value.toString();
}

var message = dieRollMessage(Js_math.random_int(1, 6), "Marshall");
```

</CodeTab>

Which produces a message such as `Hello Marshall, you rolled a 3`.

kevanstannard marked this conversation as resolved.
Show resolved Hide resolved
You can also explicitly define the argument position of a piped value by using the `_` placeholder:

<CodeTab labels={["ReScript", "JS Output"]}>

```res example
let logMsg = (user: string, datestr: string, msg: string): unit => {
Js.log(`${user}|${datestr}|${msg}`)
}

let datestr = "01-01-2021"
let user = "admin"

// Here, we put the result of toUpperCase into the last position
// denoted with an _
Js.String2.toUpperCase("example message")->logMsg(user, datestr, _)
```

```js
function logMsg(user, datestr, msg) {
console.log(user + "|" + datestr + "|" + msg);

}

var datestr = "01-01-2021";

var user = "admin";

logMsg(user, datestr, "example message".toUpperCase());
```

</CodeTab>

### References

* [Pipe](/docs/manual/latest/pipe)