From 849e97f5f1cae62d864cf2e6940e7d83336b55bd Mon Sep 17 00:00:00 2001 From: Kevan Stannard Date: Fri, 15 Jan 2021 21:55:50 +1100 Subject: [PATCH 1/5] Add syntax lookup pipe operator --- misc_docs/syntax/operators_pipe.mdx | 45 +++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 misc_docs/syntax/operators_pipe.mdx diff --git a/misc_docs/syntax/operators_pipe.mdx b/misc_docs/syntax/operators_pipe.mdx new file mode 100644 index 000000000..2dd039199 --- /dev/null +++ b/misc_docs/syntax/operators_pipe.mdx @@ -0,0 +1,45 @@ +--- +id: "pipe" +keywords: ["pipe", "operator", "function", "argument"] +name: "->" +summary: "This is the `pipe` operator." +category: "operators" +--- + +The `pipe` operator provides an convenient syntax for passing a value into a function. Typically it's used to pass a value into a function as its first argument. + +### 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`. + +### References + +* [Pipe](/docs/manual/latest/pipe) From 7954447b646c9715f8ab2ca79070f6b1fb377e01 Mon Sep 17 00:00:00 2001 From: Kevan Stannard Date: Fri, 15 Jan 2021 22:01:45 +1100 Subject: [PATCH 2/5] Use operator symbol instead of name in content --- misc_docs/syntax/operators_pipe.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/misc_docs/syntax/operators_pipe.mdx b/misc_docs/syntax/operators_pipe.mdx index 2dd039199..9e61715d9 100644 --- a/misc_docs/syntax/operators_pipe.mdx +++ b/misc_docs/syntax/operators_pipe.mdx @@ -6,7 +6,7 @@ summary: "This is the `pipe` operator." category: "operators" --- -The `pipe` operator provides an convenient syntax for passing a value into a function. Typically it's used to pass a value into a function as its first argument. +The `->` operator provides an convenient syntax for passing a value into a function. Typically it's used to pass a value into a function as its first argument. ### Example From fcdaffc7ddd079e6463f64fce48a9d940281b843 Mon Sep 17 00:00:00 2001 From: Kevan Stannard Date: Sat, 16 Jan 2021 10:57:03 +1100 Subject: [PATCH 3/5] Update misc_docs/syntax/operators_pipe.mdx Co-authored-by: Patrick Ecker --- misc_docs/syntax/operators_pipe.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/misc_docs/syntax/operators_pipe.mdx b/misc_docs/syntax/operators_pipe.mdx index 9e61715d9..e0f74cc1e 100644 --- a/misc_docs/syntax/operators_pipe.mdx +++ b/misc_docs/syntax/operators_pipe.mdx @@ -6,7 +6,7 @@ summary: "This is the `pipe` operator." category: "operators" --- -The `->` operator provides an convenient syntax for passing a value into a function. Typically it's used to pass a value into a function as its first argument. +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 From 05e789a5220a3dab27f9878c53338bd727d2d758 Mon Sep 17 00:00:00 2001 From: Kevan Stannard Date: Sat, 16 Jan 2021 10:58:09 +1100 Subject: [PATCH 4/5] Update misc_docs/syntax/operators_pipe.mdx Co-authored-by: Patrick Ecker --- misc_docs/syntax/operators_pipe.mdx | 31 +++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/misc_docs/syntax/operators_pipe.mdx b/misc_docs/syntax/operators_pipe.mdx index e0f74cc1e..13f48ec1d 100644 --- a/misc_docs/syntax/operators_pipe.mdx +++ b/misc_docs/syntax/operators_pipe.mdx @@ -40,6 +40,37 @@ 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) From 48555dba070e24ee1337a78e5d6663c2c91e3593 Mon Sep 17 00:00:00 2001 From: Kevan Stannard Date: Sat, 16 Jan 2021 11:00:36 +1100 Subject: [PATCH 5/5] Formatting fix --- misc_docs/syntax/operators_pipe.mdx | 1 + 1 file changed, 1 insertion(+) diff --git a/misc_docs/syntax/operators_pipe.mdx b/misc_docs/syntax/operators_pipe.mdx index 13f48ec1d..7b746878f 100644 --- a/misc_docs/syntax/operators_pipe.mdx +++ b/misc_docs/syntax/operators_pipe.mdx @@ -71,6 +71,7 @@ logMsg(user, datestr, "example message".toUpperCase()); ``` + ### References * [Pipe](/docs/manual/latest/pipe)