source: project/wiki/eggref/5/srfi-197 @ 39196

Last change on this file since 39196 was 39196, checked in by gnosis, 2 months ago

Initial checkin of SRFI-197 documentaton

File size: 15.2 KB
Line 
1== 197: Pipeline Operators
2=== Abstract
3Many functional languages provide pipeline operators, like Clojure's -> or OCaml's |>. Pipelines are a simple, terse, and readable way to write deeply-nested expressions. This SRFI defines a family of chain and nest pipeline operators, which can rewrite nested expressions like {{(a b (c d (e f g)))}} as a sequence of operations: {{(chain g (e f _) (c d _) (a b _))}}.
4=== Rationale
5Deeply-nested expressions are a common problem in all functional languages, especially Lisps. Excessive nesting can result in deep indentation and parenthesis-matching
6errors.
7
8<enscript highlight="scheme">
9; Quick, how many close parentheses are there?
10(eta (zeta (epsilon (delta (gamma (beta (alpha)))))))
11</enscript>
12
13Additionally, some expressions sound more natural when written inside out, as a sequence of steps from start to finish.
14
15<enscript highlight="scheme">
16; This recipe looks
 backwards.
17(bake (pour (mix (add eggs (add sugar (add flour bowl))))) (fahrenheit 350))
18</enscript>
19
20Many functional languages solve this by introducing pipeline operators. This SRFI defines a chain operator inspired by Clojure's threading macros, but with _ as an
21argument placeholder, a notation also used in SRFI 156.
22
23<enscript highlight="scheme">
24(chain (alpha) (beta _) (gamma _) (delta _) (epsilon _) (zeta _) (eta _))
25
26(chain bowl
27       (add flour _)
28       (add sugar _)
29       (add eggs _)
30       (mix _)
31       (pour _)
32       (bake _ (fahrenheit 350)))
33</enscript>
34
35Pipelines are especially useful for nested list and vector operations.
36
37<enscript highlight="scheme">
38(chain xs
39       (map (lambda (x) (+ x 1) _)
40       (filter odd? _)
41       (fold * 1 _))
42</enscript>
43
44Scheme already provides an idiomatic way to chain expressions in let* and SRFI 2 {{and-let*}}, but the primary advantage of chain is terseness and the accompanying
45readability. This focus on readability and reduced nesting is similar in spirit to SRFI 156 and SRFI 26.
46
47Compared to an equivalent {{let*}} expression, chain removes two levels of parenthesis nesting, does not define any intermediate variables, and allows mixing single and
48multiple return values.
49
50To demonstrate the difference in verbosity, here is the let* equivalent of the recipe expression:
51
52<enscript highlight="scheme">
53(let* ((x bowl)
54       (x (add flour x))
55       (x (add sugar x))
56       (x (add eggs x))
57       (x (mix x))
58       (x (pour x)))
59  (bake x (fahrenheit 350)))
60</enscript>
61
62Like let*, chain guarantees evaluation order. In fact, {{(chain a (b _) (c _))}} expands to something like {{(let* ((x (b a)) (x (c x))) x)}}, not {{(c (b a))}}, and so chain is not suitable for pipelines containing syntax like if or let.
63
64For pipelines containing complex syntax, the nest and nest-reverse operators look like chain but are guaranteed to expand to nested forms, not let* forms. nest nests in the opposite direction of chain, so {{(nest (a _) (b _) c)}} expands to {{(a (b c))}}.
65=== Specification
66==== chain
67
68<procedure>(chain <initial-value> [<placeholder> [<ellipsis>]] <step> ...)</procedure>
69
70==== Syntax
71
72<parameter><initial-value></parameter>
73
74{{<initial-value>}} is an expression.
75
76
77<parameter><placeholder></parameter>
78
79
80<parameter><ellipsis></parameter>
81
82
83{{<placeholder>}} and {{<ellipsis>}} are literal symbols; these are the placeholder symbol and ellipsis symbol. If {{<placeholder>}} or {{<ellipsis>}} are not present, they default to _ and ..., respectively.
84
85<parameter><step></parameter>
86
87The syntax of {{<step>}} is (<datum> ...), where each {{<datum>}} is either the placeholder symbol, the ellipsis symbol, or an expression. A {{<step>}} must contain at least one {{<datum>}}. The ellipsis symbol is only allowed at the end of a {{<step>}}, and it must immediately follow a placeholder symbol.
88==== Semantics
89chain evaluates each {{<step>}} in order from left to right, passing the result of each step to the next.
90
91Each {{<step>}} is evaluated as an application, and the return value(s) of that application are passed to the next step as its pipeline values. {{<initial-value>}} is the pipeline value of the first step. The return value(s) of chain are the return value(s) of the last step.
92
93The placeholder symbols in each {{<step>}} are replaced with that step's pipeline values, in the order they appear. It is an error if the number of placeholders for a step does not equal the number of pipeline values for that step, unless the step contains no placeholders, in which case it will ignore its pipeline values.
94
95<enscript highlight="scheme">
96(chain x (a b _)) ; => (a b x)
97(chain (a b) (c _ d) (e f _)) ; => (let* ((x (a b)) (x (c x d))) (e f x))
98(chain (a) (b _ _) (c _)) ; => (let*-values (((x1 x2) (a)) ((x) (b x1 x2))) (c x))
99</enscript>
100
101If a {{<step>}} ends with a placeholder symbol followed by an ellipsis symbol, that placeholder sequence is replaced with all remaining pipeline values that do not have a matching placeholder.
102
103<enscript highlight="scheme">
104(chain (a) (b _ c _ ...) (d _))
105; => (let*-values (((x1 . x2) (a)) ((x) (apply b x1 c x2))) (d x))
106</enscript>
107
108chain and all other SRFI 197 macros support custom placeholder symbols, which can help to preserve hygiene when used in the body of a syntax definition that may insert a {{_}} or {{...}}.
109
110<enscript highlight="scheme">
111(chain (a b) <> (c <> d) (e f <>))
112 ; => (let* ((x (a b)) (x (c x d))) (e f x))
113(chain (a) - --- (b - c - ---) (d -))
114; => (let*-values (((x1 . x2) (a)) ((x) (apply b x1 c x2))) (d x))
115</enscript>
116==== chain-and
117
118<procedure>(chain-and <initial-value> [<placeholder>] <step> ...)</procedure>
119
120===== Syntax
121
122<parameter><initial-value></parameter>
123
124{{<initial-value>}} is an expression.
125
126
127<parameter><placeholder></parameter>
128
129{{<placeholder>}} is a literal symbol; this is the placeholder symbol. If {{<placeholder>}} is not present, the placeholder symbol is {{_}}.
130
131
132<parameter><step></parameter>
133
134The syntax of {{<step>}} is (<datum> ... [<_> <datum> ...]), where {{<_>}} is the placeholder symbol.
135===== Semantics
136A variant of chain that short-circuits and returns {{#f}} if any step returns {{#f}}. chain-and is to chain as SRFI 2 {{and-let*}} is to {{let*}}.
137
138Each {{<step>}} is evaluated as an application. If the step evaluates to {{#f}}, the remaining steps are not evaluated, and chain-and returns {{#f}}. Otherwise, the return value of the step is passed to the next step as its pipeline value. {{<initial-value>}} is the pipeline value of the first step. If no step evaluates to {{#f}}, the return value of chain-and is the return value of the last step.
139
140The {{<_>}} placeholder in each {{<step>}} is replaced with that step's pipeline value. If a {{<step>}} does not contain {{<_>}}, it will ignore its pipeline value, but chain-and will still check whether that pipeline value is {{#f}}.
141
142Because chain-and checks the return value of each step, it does not support steps with multiple return values. It is an error if a step returns more than one value.
143==== chain-when
144
145<procedure>(chain-when <initial-value> [<placeholder>] ([<guard>] <step>) ...)</procedure>
146
147===== Syntax
148{{<initial-value>}} and {{<guard>}} are expressions. {{<placeholder>}} is a literal symbol; this is the placeholder symbol. If {{<placeholder>}} is not present, the placeholder symbol is _. The syntax of {{<step>}} is (<datum> ... [<_> <datum> ...]), where {{<_>}} is the placeholder symbol.
149===== Semantics
150A variant of chain in which each step has a guard expression and will be skipped if the guard expression evaluates to {{#f}}.
151===== Example
152<enscript highlight="scheme">
153(define (describe-number n)
154  (chain-when '()
155    ((odd? n) (cons "odd" _))
156    ((even? n) (cons "even" _))
157    ((zero? n) (cons "zero" _))
158    ((positive? n) (cons "positive" _))))
159
160(describe-number 3) ; => '("positive" "odd")
161(describe-number 4) ; => '("positive" "even")
162</enscript>
163===== Description
164Each {{<step>}} is evaluated as an application. The return value of the step is passed to the next step as its pipeline value. {{<initial-value>}} is the pipeline value of the first step.
165
166The {{<_>}} placeholder in each {{<step>}} is replaced with that step's pipeline value. If a {{<step>}} does not contain {{<_>}}, it will ignore its pipeline value
167
168If a step's {{<guard>}} is present and evaluates to {{#f}}, that step will be skipped, and its pipeline value will be reused as the pipeline value of the next step. The return value of chain-when is the return value of the last non-skipped step, or {{<initial-value>}} if all steps are skipped.
169
170Because chain-when may skip steps, it does not support steps with multiple return values. It is an error if a step returns more than one value.
171==== chain-lambda
172
173<procedure>(chain-lambda [<placeholder> [<ellipsis>]] <step> ...)</procedure>
174
175===== Syntax
176
177<parameter><placeholder></parameter>
178
179
180<parameter><ellipsis></parameter>
181
182{{<placeholder>}} and {{<ellipsis>}} are literal symbols these are the placeholder symbol and ellipsis symbol. If {{<placeholder>}} or {{<ellipsis>}} are not present, they default to _ and ..., respectively.
183
184
185<parameter><step></parameter>
186
187The syntax of {{<step>}} is (<datum> ...), where each {{<datum>}} is either the placeholder symbol, the ellipsis symbol, or an expression. A {{<step>}} must contain at least one {{<datum>}}. The ellipsis symbol is only allowed at the end of a {{<step>}}, and it must immediately follow a placeholder symbol.
188===== Semantics
189Creates a procedure from a sequence of chain steps. When called, a {{chain-lambda}} procedure evaluates each {{<step>}} in order from left to right, passing the result of each step to the next.
190
191<enscript highlight="scheme">
192(chain-lambda (a _) (b _)) ; => (lambda (x) (let* ((x (a x))) (b x)))
193(chain-lambda (a _ _) (b c _)) ; => (lambda (x1 x2) (let* ((x (a x1 x2))) (b c x)))
194</enscript>
195
196Each {{<step>}} is evaluated as an application, and the return value(s) of that application are passed to the next step as its pipeline values. The procedure's arguments are the pipeline values of the first step. The return value(s) of the procedure are the return value(s) of the last step.
197
198The placeholder symbols in each {{<step>}} are replaced with that step's pipeline values, in the order they appear. It is an error if the number of placeholders for a step does not equal the number of pipeline values for that step, unless the step contains no placeholders, in which case it will ignore its pipeline values.
199
200If a {{<step>}} ends with a placeholder symbol followed by an ellipsis symbol, that placeholder sequence is replaced with all remaining pipeline values that do not have a matching placeholder.
201
202The number of placeholders in the first {{<step>}} determines the arity of the procedure. If the first step ends with an ellipsis symbol, the procedure is variadic.
203==== nest
204
205<procedure>(nest [<placeholder>] <step> ... <initial-value>)</procedure>
206
207===== Syntax
208
209<parameter><placeholder></parameter>
210
211{{<placeholder>}} is a literal symbol; this is the placeholder symbol. If {{<placeholder>}} is not present, the placeholder symbol is _. The syntax of {{<step>}} is {{(<datum> ... <_> <datum> ...)}}, where {{<_>}} is the placeholder symbol. {{<initial-value>}} is expression.
212===== Semantics
213nest is similar to chain, but sequences its steps in the opposite order. Unlike chain, nest literally nests expressions; as a result, it does not provide the same strict evaluation order guarantees as chain.
214
215<enscript highlight="scheme">
216(nest (a b _) (c d _) e) ; => (a b (c d e))
217</enscript>
218
219A nest expression is evaluated by lexically replacing the {{<_>}} in the last {{<step>}} with {{<initial-value>}}, then replacing the {{<_>}} in the next-to-last {{<step>}} with that replacement, and so on until the {{<_>}} in the first {{<step>}} has been replaced. It is an error if the resulting final replacement is not an expression, which is then evaluated and its values are returned.
220
221Because it produces an actual nested form, nest can build expressions that chain cannot. For example, nest can build a quoted data structure:
222
223<enscript highlight="scheme">
224(nest '_ (1 2 _) (3 _ 5) (_) 4) ; => '(1 2 (3 (4) 5))
225</enscript>
226
227nest can also safely include special forms like if, let, lambda, or parameterize in a pipeline.
228
229A custom placeholder can be used to safely nest nest expressions.
230
231<enscript highlight="scheme">
232(nest (nest _2 '_2 (1 2 3 _2) _ 6)
233      (_ 5 _2)
234      4)
235; => '(1 2 3 (4 5 6))
236</enscript>
237==== nest-reverse
238
239<procedure>(nest-reverse <initial-value> [<placeholder>] <step> ...)</procedure>
240
241===== Syntax
242
243<parameter><initial-value></parameter>
244
245{{<initial-value>}} is an expression. {{<placeholder>}} is a literal symbol; this is the placeholder symbol. If {{<placeholder>}} is not present, the placeholder symbol is _.
246
247The syntax of {{<step>}} is (<datum> ... <_> <datum> ...), where {{<_>}} is the placeholder symbol.
248===== Semantics
249nest-reverse is variant of nest that nests in reverse order, which is the same order as chain.
250
251<enscript highlight="scheme">
252(nest-reverse e (c d _) (a b _)) ; => (a b (c d e))
253</enscript>
254
255A nest-reverse expression is evaluated by lexically replacing the {{<_>}} in the first {{<step>}} with {{<initial-value>}}, then replacing the {{<_>}} in the second {{<step>}} with that replacement, and so on until the {{<_>}} in the last {{<step>}} has been replaced. It is an error if the resulting final replacement is not an expression, which is then evaluated and its values are returned.
256=== Implementation
257A sample implementation is available on GitHub. This repository contains two portable SRFI 197 implementations, one in R7RS-small and syntax-rules, the other in R6RS and syntax-case. The only dependency of either implementation is SRFI 2. It includes an R7RS library wrapper and a test script.
258=== Acknowledgements
259Thanks to the participants in the SRFI 197 mailing list who helped me refine this SRFI, including Marc Nieper-Wißkirchen, Linus Björnstam, Shiro Kawai, Lassi Kortela, and John Cowan.
260
261Marc provided a paragraph that has been included (with only minor changes) in the Semantics section of the nest and nest-reverse macros.
262
263Thanks to Rich Hickey for Clojure and the original implementation of Clojure threading macros, and to Paulus Esterhazy for the (EPL licensed) threading macros
264documentation page, which was a source of inspiration and some of the examples in this document.
265=== Author
266==== by Adam Nelson
267==== Ported to Chicken Scheme 5 by Sergey Goldgaber
268=== Copyright
269© 2020 Adam Nelson.
270
271Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
272
273The above copyright notice and this permission notice (including the next paragraph) shall be included in all copies or substantial portions of the Software.
274
275THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
276=== Version history
277==== 0.1 - Ported to Chicken Scheme 5
Note: See TracBrowser for help on using the repository browser.