id | keywords | name | summary | category | ||||
---|---|---|---|---|---|---|---|---|
pipe |
| -> | 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.
<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()));