Skip to content

Commit 2312098

Browse files
committed
Document pipe operator
1 parent 08ba882 commit 2312098

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

docs/New Operators.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,51 @@ print(eq <= 0) --> true
135135
print(lt <= 0) --> true
136136
print(gt <= 0) --> false
137137
```
138+
139+
## Pipe Operator
140+
The pipe operator allows you represent data pipelines more succinctly.
141+
```pluto
142+
local { http, json } = require "pluto:*"
143+
144+
http.request("https://httpbin.org/get")
145+
|> json.decode
146+
|> dumpvar
147+
|> print
148+
149+
--> {
150+
--> ["args"] = {},
151+
--> ["headers"] = {
152+
--> ["Host"] = string(11) "httpbin.org",
153+
--> ["User-Agent"] = string(56) "Mozilla/5.0 (compatible; Soup Library; +https://soup.do)",
154+
--> ["X-Amzn-Trace-Id"] = string(40) "Root=1-65e05b66-6aa8b7c94e7580774a804c24",
155+
--> ["Accept-Encoding"] = string(13) "deflate, gzip",
156+
--> },
157+
--> ["origin"] = string(13) "1.2.3.4",
158+
--> ["url"] = string(23) "https://httpbin.org/get",
159+
--> }
160+
```
161+
The HTTP-to-print pipeline here would otherwise be written like this:
162+
```pluto
163+
print(dumpvar(json.decode(http.request("https://httpbin.org/get"))))
164+
```
165+
166+
### Additional Arguments
167+
It is also possible to provide additional arguments for the righthand side of the pipe operator:
168+
```pluto
169+
local producer = || -> "10"
170+
171+
producer()
172+
|> tonumber|16|
173+
|> print --> 16
174+
```
175+
176+
### Anonymous Functions
177+
The righthand side of the pipe operator can also be an anonymous function, allowing for more advanced usage like this:
178+
```pluto
179+
local producer = || -> 42
180+
181+
producer()
182+
|> |res| -> print($"The result was {res}")
183+
184+
--> The result was 42
185+
```

src/theme/pluto.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Prism.languages.pluto = {
44
'function': [
55
/\b(?!in\s)(?!\d)(?!return)(?!case)(?!function)(?!local)(?!new)\w+(?=\s*(?:\??\())/, // func()
66
/\b(?!in\s)(?!\d)(?!return)(?!case)(?!function)(?!local)(?!not)\w+(?=\s*(?:\??[{"]))/, // func "", func {}
7+
/(?<=\|>)\s[\w.]+/, // |> func
78
/\b(function|enum|class)\b/,
89
/\b(debug|table|string|number|io|os|coroutine|_VERSION|_PVERSION|_PSOUP|json\.null)\b/, // standard library + type hints
910
],

0 commit comments

Comments
 (0)