diff --git a/misc_docs/syntax/operators_pipe.mdx b/misc_docs/syntax/operators_pipe.mdx new file mode 100644 index 000000000..7b746878f --- /dev/null +++ b/misc_docs/syntax/operators_pipe.mdx @@ -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 + + + +```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"); +``` + + + +Which produces a message such as `Hello Marshall, you rolled a 3`. + +You can also explicitly define the argument position of a piped value by using the `_` placeholder: + + + +```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()); +``` + + + +### References + +* [Pipe](/docs/manual/latest/pipe)