Skip to content

Latest commit

 

History

History
78 lines (55 loc) · 1.67 KB

operators_pipe.mdx

File metadata and controls

78 lines (55 loc) · 1.67 KB
idkeywordsnamesummarycategory
pipe
pipe
operator
function
argument
->
This is the `pipe` operator.
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"]}>

letdieRoll=size=> { Math.Int.random(1, size) } letdieRollMessage= (value, name) => { "Hi "++name++", you rolled a "++Int.toString(value) } letmessage=dieRoll(6)->dieRollMessage("Marshall")
functiondieRoll(size){returnCore__Math.Int.random(1,size);}functiondieRollMessage(value,name){return"Hi "+name+", you rolled a "+value.toString();}varmessage=dieRollMessage(dieRoll(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:

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

letlogMsg= (user: string, datestr: string, msg: string): unit=> { Console.log(`${user}|${datestr}|${msg}`) } letdatestr="01-01-2021"letuser="admin"// Here, we put the result of toUpperCase into the last position// denoted with an _String.toUpperCase("example message")->logMsg(user, datestr, _)
functionlogMsg(user,datestr,msg){console.log(user+"|"+datestr+"|"+msg);}vardatestr="01-01-2021";varuser="admin";((function(__x){logMsg(user,datestr,__x);})("example message".toUpperCase()));

References

close