|
| 1 | +--- |
| 2 | +title: YS Streaming Mode |
| 3 | +date: 2025-07-23 |
| 4 | +draft: false |
| 5 | +authors: [ingydotnet] |
| 6 | +categories: [Summer-of-YS] |
| 7 | +edit: blog/2025-07-23.md |
| 8 | +comments: true |
| 9 | +--- |
| 10 | + |
| 11 | +We've seen before that YS can work with YAML files that contain multiple |
| 12 | +documents. |
| 13 | +"Document" is the YAML term for a single top level node (object). |
| 14 | + |
| 15 | +YS returns the last document in the file by default. |
| 16 | + |
| 17 | +YS can also load all the documents in the file. |
| 18 | + |
| 19 | +This is called streaming mode, and we'll learn more about it today. |
| 20 | + |
| 21 | +<!-- more --> |
| 22 | + |
| 23 | + |
| 24 | +## Multiple Documents |
| 25 | + |
| 26 | +Like I said, YS returns the last document in the file by default: |
| 27 | + |
| 28 | +```bash |
| 29 | +$ ys -Y <<<' |
| 30 | +--- |
| 31 | +foo: 111 |
| 32 | +--- |
| 33 | +bar: 222 |
| 34 | +' |
| 35 | +bar: 222 |
| 36 | +``` |
| 37 | + |
| 38 | +This makes sense, because the other documents could be data sets, code |
| 39 | +definitions or YS modules that get used by the last document to produce its |
| 40 | +desired value: |
| 41 | + |
| 42 | +```bash |
| 43 | +$ ys -Y <<<' |
| 44 | +!YS-v0 |
| 45 | +--- !code |
| 46 | +foo =: 111 |
| 47 | +--- !data |
| 48 | +bar:: foo |
| 49 | +' |
| 50 | +bar: 111 |
| 51 | +``` |
| 52 | + |
| 53 | +But what if we want to load multiple documents? |
| 54 | + |
| 55 | +We can do that by using the `-s` flag: |
| 56 | + |
| 57 | +```bash |
| 58 | +$ ys -s -Y <<<' |
| 59 | +--- |
| 60 | +foo: 111 |
| 61 | +--- |
| 62 | +bar: 222 |
| 63 | +' |
| 64 | +--- |
| 65 | +foo: 111 |
| 66 | +--- |
| 67 | +bar: 222 |
| 68 | +``` |
| 69 | + |
| 70 | +What happens if some of the documents are code mode and others are data mode? |
| 71 | + |
| 72 | +```bash |
| 73 | +$ ys -s -Y <<<' |
| 74 | +!YS-v0 |
| 75 | +--- |
| 76 | +foo: bar |
| 77 | +--- !code |
| 78 | +foo =: 111 |
| 79 | +--- !data |
| 80 | +foo:: foo |
| 81 | +--- !code |
| 82 | +say: "Hello, $foo!" |
| 83 | +--- !code |
| 84 | +(1 .. 3) + (10 .. 12) |
| 85 | +--- !data |
| 86 | +bar:: foo |
| 87 | +' |
| 88 | +Hello, 111! |
| 89 | +--- |
| 90 | +foo: bar |
| 91 | +--- |
| 92 | +foo: 111 |
| 93 | +--- |
| 94 | +- 1 |
| 95 | +- 2 |
| 96 | +- 3 |
| 97 | +- 10 |
| 98 | +- 11 |
| 99 | +- 12 |
| 100 | +--- |
| 101 | +bar: 111 |
| 102 | +``` |
| 103 | + |
| 104 | +If you count carefully, you'll see that there are 7 documents in the input, but |
| 105 | +only 4 documents in the output. |
| 106 | + |
| 107 | +YS will only return the documents whose value is representable as JSON and only |
| 108 | +if the value is not `nil`. |
| 109 | + |
| 110 | +In one of the documents, we just printed a line with `say`. |
| 111 | +Let's see what the return value of `say` is: |
| 112 | + |
| 113 | +```bash |
| 114 | +$ ys -pe 'say: "Hello"' |
| 115 | +Hello |
| 116 | +nil |
| 117 | +``` |
| 118 | + |
| 119 | +It looks like `say` returns `nil`. |
| 120 | +That means the value of that document is `nil`. |
| 121 | +Since the value of that document is `nil`, YS will not load it. |
| 122 | + |
| 123 | +What about the variable assignment? |
| 124 | + |
| 125 | +```bash |
| 126 | +$ ys -pe 'foo =: 123' |
| 127 | +#<Var@21b42ece: 123> |
| 128 | +``` |
| 129 | + |
| 130 | +That returned a Clojure var object. |
| 131 | +Since this is not something that can be represented as JSON, YS will not |
| 132 | +load it. |
| 133 | + |
| 134 | + |
| 135 | +## The `stream` function and the `_` variable |
| 136 | + |
| 137 | +As YS evaluates a YAML file, it keeps track of the values of each document. |
| 138 | +Any document can access the values of the previous documents. |
| 139 | + |
| 140 | +The `stream` function returns a list of the values of the previous documents. |
| 141 | + |
| 142 | +The `_` variable contains the value of the previous document. |
| 143 | +It's short for `stream():last` or `stream.$`. |
| 144 | + |
| 145 | +Consider this example: |
| 146 | + |
| 147 | +```bash |
| 148 | +$ ys -Ye ' |
| 149 | +!YS-v0 |
| 150 | +--- |
| 151 | +foo: 111 |
| 152 | +bar: 222 |
| 153 | +--- !code |
| 154 | +merge stream().$:: |
| 155 | + baz: 333 |
| 156 | +--- !data |
| 157 | +number:: _.foo * _.bar * _.baz |
| 158 | +--- !code |
| 159 | +say: stream():yaml/dump' |
| 160 | +- foo: 111 |
| 161 | + bar: 222 |
| 162 | +- foo: 111 |
| 163 | + bar: 222 |
| 164 | + baz: 333 |
| 165 | +- number: 8205786 |
| 166 | +``` |
| 167 | + |
| 168 | +In the final document, we print the values of the previous 3 documents in YAML. |
| 169 | + |
| 170 | +The second to last document calculates the product of the values from the |
| 171 | +previous document, whose value is the result of a merge using the document |
| 172 | +before that. |
| 173 | + |
| 174 | +If you study that example carefully, you'll see how to use the `stream` function |
| 175 | +and the `_` variable. |
0 commit comments