File tree Expand file tree Collapse file tree 2 files changed +49
-0
lines changed
Expand file tree Collapse file tree 2 files changed +49
-0
lines changed Original file line number Diff line number Diff line change @@ -135,3 +135,51 @@ print(eq <= 0) --> true
135135print(lt <= 0) --> true
136136print(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+ ```
Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ Prism.languages.pluto = {
44 'function' : [
55 / \b (? ! i n \s ) (? ! \d ) (? ! r e t u r n ) (? ! c a s e ) (? ! f u n c t i o n ) (? ! l o c a l ) (? ! n e w ) \w + (? = \s * (?: \? ? \( ) ) / , // func()
66 / \b (? ! i n \s ) (? ! \d ) (? ! r e t u r n ) (? ! c a s e ) (? ! f u n c t i o n ) (? ! l o c a l ) (? ! n o t ) \w + (? = \s * (?: \? ? [ { " ] ) ) / , // func "", func {}
7+ / (?< = \| > ) \s [ \w . ] + / , // |> func
78 / \b ( f u n c t i o n | e n u m | c l a s s ) \b / ,
89 / \b ( d e b u g | t a b l e | s t r i n g | n u m b e r | i o | o s | c o r o u t i n e | _ V E R S I O N | _ P V E R S I O N | _ P S O U P | j s o n \. n u l l ) \b / , // standard library + type hints
910 ] ,
You can’t perform that action at this time.
0 commit comments