mirror of
https://github.com/zaphar/ucg.git
synced 2025-07-23 18:29:50 -04:00
DOCS: Add reduce expressions to the grammar and reference.
This commit is contained in:
parent
e46496c666
commit
a062332b0a
@ -303,6 +303,38 @@ let tpl_filter = macro(name, val) => {
|
|||||||
filter tpl_filter.result test_tpl == { quux = "baz" };
|
filter tpl_filter.result test_tpl == { quux = "baz" };
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Reduce expressions
|
||||||
|
|
||||||
|
Reduce expressions start with the reduce keyword followed by a symbol referencing a macro a dot and the output field, then an expression for the accumulator and finally the tuple or list to process.
|
||||||
|
|
||||||
|
**Tuples**
|
||||||
|
|
||||||
|
```
|
||||||
|
let test_tpl = {
|
||||||
|
foo = "bar",
|
||||||
|
quux = "baz",
|
||||||
|
};
|
||||||
|
let tpl_reducer = macro(acc, name, val) => {
|
||||||
|
result = acc{
|
||||||
|
keys = self.keys + [name],
|
||||||
|
vals = self.vals + [val],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
reduce tpl_reducer.result {keys = [], vals = []}, test_tpl == {keys = ["foo", "quux"], vals = ["bar", "baz"]};
|
||||||
|
```
|
||||||
|
|
||||||
|
**Lists**
|
||||||
|
|
||||||
|
```
|
||||||
|
let list1 = [1, 2, 3, 4];
|
||||||
|
let list_reducer = macro(acc, item) => {
|
||||||
|
result = acc + item,
|
||||||
|
};
|
||||||
|
|
||||||
|
list_reducer.result 0, list1 == 0 + 1 + 2 + 3 + 4;
|
||||||
|
```
|
||||||
|
|
||||||
Include expressions
|
Include expressions
|
||||||
-------------------
|
-------------------
|
||||||
|
|
||||||
|
@ -50,6 +50,7 @@ include_keyword: "include" ;
|
|||||||
as_keyword: "as" ;
|
as_keyword: "as" ;
|
||||||
macro_keyword: "macro" ;
|
macro_keyword: "macro" ;
|
||||||
map_keyword: "map" ;
|
map_keyword: "map" ;
|
||||||
|
reduce_keyword: "map" ;
|
||||||
filter_keyword: "filter" ;
|
filter_keyword: "filter" ;
|
||||||
module_keyword: "module" ;
|
module_keyword: "module" ;
|
||||||
mod_keyword: "mod" ;
|
mod_keyword: "mod" ;
|
||||||
@ -121,8 +122,8 @@ module_def: module_keyword, tuple, fatcomma, lbrace, [ { statement } ], rbrace ;
|
|||||||
#### Copy and Call Expression
|
#### Copy and Call Expression
|
||||||
|
|
||||||
```
|
```
|
||||||
copy_expression: bareword, tuple ;
|
copy_expr: bareword, tuple ;
|
||||||
call_expression: bareword, lparen, [arglist], rparen ;
|
call_expr: bareword, lparen, [arglist], rparen ;
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Format Expression
|
#### Format Expression
|
||||||
@ -135,7 +136,9 @@ format_expr: str, percent, lparen, [arglist], rparen ;
|
|||||||
|
|
||||||
```
|
```
|
||||||
func_op_kind: map_keyword | filter_keyword ;
|
func_op_kind: map_keyword | filter_keyword ;
|
||||||
processing_expr: func_op_kind, bareword, dot, bareword, expr ;
|
map_or_filter_expr: func_op_kind, bareword, dot, bareword, expr ;
|
||||||
|
reduce_expr: reduce_keyword, bareword, dot, bareword, expr, expr ;
|
||||||
|
processing_expr: map_or_filter_expr | reduce_expr
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Include Expression
|
#### Include Expression
|
||||||
@ -151,11 +154,11 @@ non_operator_expr: literal
|
|||||||
| grouped
|
| grouped
|
||||||
| macrodef
|
| macrodef
|
||||||
| module_def
|
| module_def
|
||||||
| format_expression
|
| format_expr
|
||||||
| include_expression
|
| include_expr
|
||||||
| copy_expression
|
| copy_expr
|
||||||
| processing_expr
|
| processing_expr
|
||||||
| call_expression ;
|
| call_expr ;
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Operator Expressions
|
#### Operator Expressions
|
||||||
|
Loading…
x
Reference in New Issue
Block a user