examples: 新增 oa2026-showcase(2026 开源项目大赛作品介绍示例集)
五个可运行示例,覆盖 Marqdo 的核心能力面:
| 文件 | 展示 |
|------|------|
| add.mq.md | 叙述统一、粗体即代码、斜体即返回 |
| invoice.mq.md | 表格即数据、循环、分支、断言 |
| calculus.mq.md | 公式即变量、求导、求根、绘图 |
| bell.mq.md | 量子电路、概率、断言、SVG 派生物 |
| foreign-node.mq.md | 外联一次性执行、持久会话、显式传值 |
全部实测跑通(1.5.2),输出与 README「预期输出」一节一致:
- add → 42
- invoice → 960 / 912 / 966 / within-cap
- calculus → 3*x^2 - 3 / 9 / [-1.73, 0, 1.73] / plot-ok
- bell → {00: 0.5, 11: 0.5} / bell-00-ok / bell-11-ok / bell-draw-ok
- foreign-node → 区域汇总表 + 持久会话复读
仓库侧改动:
- examples/oa2026-showcase/*.svg 随示例入库(README 有说明,属固定产物)
- .gitignore 忽略根目录 bell-*.svg / calculus-*.svg —— 从仓库根运行示例时
draw/plot 会把 SVG 写到 cwd,那是运行产物不是源文件
- examples/marqdo-blog/db/posts.mq.md 首行正文去掉了内嵌 `#` 标题写法
This commit is contained in:
1 parent
aeb5a2a43d
commit
ecd7d5e433
11 files changed
+340
-1
No files matched your search
@@ -71,6 +71,11 @@ plugins/*/tmp/
|
||||
/examples/linalg-*/**/*.svg
|
||||
/examples/linalg-*/*.svg
|
||||
/examples/linalg-*.svg
|
||||
# oa2026 showcase: SVGs beside the examples ARE tracked; root-level ones are
|
||||
# run artifacts (draw/plot write to cwd when the example is run from root)
|
||||
/bell-*.svg
|
||||
/calculus-*.svg
|
||||
|
||||
# Plot SVGs emitted beside tests / notebooks when demos run
|
||||
/tests/**/*-plot-*.svg
|
||||
|
||||
|
||||
@@ -24,7 +24,7 @@ import data:ext/data/db.mq.md
|
||||
|
||||
| title | slug | summary | content | tag | created_at |
|
||||
|-------|------|---------|---------|-----|------------|
|
||||
| Hello Marqdo | hello | Welcome | # Hello\n\nExecutable documents. | intro | 2026-09-01 |
|
||||
| Hello Marqdo | hello | Welcome | Hello — executable documents. | intro | 2026-09-01 |
|
||||
| Artifact Web | artifact-web | Document first | Pages are mq.md files. | design | 2026-09-28 |
|
||||
|
||||
*rows*
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
# oa2026-showcase
|
||||
|
||||
用于 2026 开源项目大赛作品介绍文档的可运行示例。
|
||||
|
||||
| 文件 | 展示的能力 | 运行 |
|
||||
|------|------------|------|
|
||||
| `add.mq.md` | 叙述统一、粗体即代码、斜体即返回 | `marqdo run examples/oa2026-showcase/add.mq.md` |
|
||||
| `invoice.mq.md` | 表格即数据、循环、分支、断言 | `marqdo run examples/oa2026-showcase/invoice.mq.md` |
|
||||
| `calculus.mq.md` | 公式即变量、求导、求根、绘图 | `marqdo run examples/oa2026-showcase/calculus.mq.md` |
|
||||
| `bell.mq.md` | 量子电路、概率、断言、SVG 派生物 | `marqdo run examples/oa2026-showcase/bell.mq.md` |
|
||||
| `foreign-node.mq.md` | 外联一次性执行、持久会话、显式传值 | `marqdo run examples/oa2026-showcase/foreign-node.mq.md` |
|
||||
|
||||
## 预期输出
|
||||
|
||||
```text
|
||||
$ marqdo run examples/oa2026-showcase/add.mq.md
|
||||
42
|
||||
|
||||
$ marqdo run examples/oa2026-showcase/invoice.mq.md
|
||||
960
|
||||
912
|
||||
966
|
||||
within-cap
|
||||
|
||||
$ marqdo run examples/oa2026-showcase/calculus.mq.md
|
||||
3*x^2 - 3
|
||||
9
|
||||
[-1.7320508076, 0, 1.7320508076]
|
||||
plot-ok
|
||||
|
||||
$ marqdo run examples/oa2026-showcase/foreign-node.mq.md
|
||||
{华东: 420, 华北: 220, 华南: 230}
|
||||
华东: 420
|
||||
华南: 230
|
||||
华北: 220
|
||||
|
||||
华东: 420
|
||||
华北: 300
|
||||
华南: 230
|
||||
|
||||
$ marqdo run examples/oa2026-showcase/bell.mq.md
|
||||
{00: 0.5, 11: 0.5}
|
||||
0.5
|
||||
0.5
|
||||
bell-00-ok
|
||||
bell-11-ok
|
||||
bell-draw-ok
|
||||
```
|
||||
|
||||
`bell.mq.md` 运行后会生成 `bell-circuit.svg` 与 `bell-probs.svg`;
|
||||
`calculus.mq.md` 运行后会生成 `calculus-plot.svg`。
|
||||
@@ -0,0 +1,10 @@
|
||||
## 加一
|
||||
|
||||
对于输入变量`n`,我们执行 **`n` = `n` + 1**,然后把更新后的值作为结果返回 *`n`*。
|
||||
|
||||
# main
|
||||
|
||||
从 41 出发调用「加一」,把结果存进`r`并打印。
|
||||
|
||||
**`r` = > 加一 n=41**
|
||||
> print text=`r`
|
||||
@@ -0,0 +1,24 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="320" height="136" viewBox="0 0 320.0 136.0" data-theme="light" role="img" aria-label="quantum circuit"><defs>
|
||||
<linearGradient id="mq-bg" x1="0" y1="0" x2="1" y2="1">
|
||||
<stop offset="0%" stop-color="#eef3f9"/>
|
||||
<stop offset="55%" stop-color="#ffffff"/>
|
||||
<stop offset="100%" stop-color="#eef3f9"/>
|
||||
</linearGradient>
|
||||
<filter id="mq-glow" x="-40%" y="-40%" width="180%" height="180%">
|
||||
<feGaussianBlur stdDeviation="2.2" result="b"/>
|
||||
<feMerge><feMergeNode in="b"/><feMergeNode in="SourceGraphic"/></feMerge>
|
||||
</filter>
|
||||
<filter id="mq-soft" x="-20%" y="-20%" width="140%" height="140%">
|
||||
<feDropShadow dx="0" dy="1.5" stdDeviation="1.8" flood-color="#3d5afe" flood-opacity="0.35"/>
|
||||
</filter>
|
||||
</defs><rect width="320.0" height="136.0" rx="14" fill="url(#mq-bg)"/>
|
||||
<rect x="1.5" y="1.5" width="317.0" height="133.0" rx="12.5" fill="none" stroke="#e2eaf3" stroke-width="1" opacity="0.55"/><line x1="96" y1="36" x2="296" y2="36" stroke="#4a5a6a" stroke-width="3.2" stroke-linecap="round" opacity="0.28"/>
|
||||
<line x1="96" y1="36" x2="296" y2="36" stroke="#4a5a6a" stroke-width="1.8" stroke-linecap="round"/><rect x="51" y="25" width="38" height="22" rx="7" fill="#ffffff" stroke="#e2eaf3" stroke-width="1"/>
|
||||
<text x="70" y="36" text-anchor="middle" dominant-baseline="middle" font-family="ui-monospace,Menlo,Consolas,monospace" font-size="13" font-weight="700" letter-spacing="0.02em" fill="#15202b">q0</text><line x1="96" y1="100" x2="296" y2="100" stroke="#4a5a6a" stroke-width="3.2" stroke-linecap="round" opacity="0.28"/>
|
||||
<line x1="96" y1="100" x2="296" y2="100" stroke="#4a5a6a" stroke-width="1.8" stroke-linecap="round"/><rect x="51" y="89" width="38" height="22" rx="7" fill="#ffffff" stroke="#e2eaf3" stroke-width="1"/>
|
||||
<text x="70" y="100" text-anchor="middle" dominant-baseline="middle" font-family="ui-monospace,Menlo,Consolas,monospace" font-size="13" font-weight="700" letter-spacing="0.02em" fill="#15202b">q1</text><rect x="116" y="16" width="40" height="40" rx="8" fill="#3d5afe" stroke="#ffffff" stroke-width="1.1" data-gate-family="clifford" filter="url(#mq-soft)" opacity="0.98"/>
|
||||
<text x="136" y="36" text-anchor="middle" dominant-baseline="middle" font-family="ui-monospace,Menlo,Consolas,monospace" font-size="14" font-weight="700" fill="#ffffff">H</text><line x1="216" y1="36" x2="216" y2="100" stroke="#1a237e" stroke-width="2.25" data-gate-family="clifford"/>
|
||||
<circle cx="216" cy="36" r="5.2" fill="#1a237e" filter="url(#mq-glow)"/>
|
||||
<circle cx="216" cy="100" r="13" fill="#3d5afe" stroke="#1a237e" stroke-width="1.6" filter="url(#mq-soft)" opacity="0.96"/>
|
||||
<line x1="216" y1="87" x2="216" y2="113" stroke="#1a237e" stroke-width="2"/>
|
||||
<line x1="203" y1="100" x2="229" y2="100" stroke="#1a237e" stroke-width="2"/></svg>
|
||||
|
After Width: | Height: | Size: 2.7 KiB |
@@ -0,0 +1,14 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="152" height="180" viewBox="0 0 152.0 180.0" data-theme="light" role="img" aria-label="measurement probabilities"><defs>
|
||||
<linearGradient id="mq-bg" x1="0" y1="0" x2="1" y2="1">
|
||||
<stop offset="0%" stop-color="#eef3f9"/>
|
||||
<stop offset="55%" stop-color="#ffffff"/>
|
||||
<stop offset="100%" stop-color="#eef3f9"/>
|
||||
</linearGradient>
|
||||
<filter id="mq-glow" x="-40%" y="-40%" width="180%" height="180%">
|
||||
<feGaussianBlur stdDeviation="2.2" result="b"/>
|
||||
<feMerge><feMergeNode in="b"/><feMergeNode in="SourceGraphic"/></feMerge>
|
||||
</filter>
|
||||
<filter id="mq-soft" x="-20%" y="-20%" width="140%" height="140%">
|
||||
<feDropShadow dx="0" dy="1.5" stdDeviation="1.8" flood-color="#3d5afe" flood-opacity="0.35"/>
|
||||
</filter>
|
||||
</defs><rect width="152.0" height="180.0" rx="14" fill="url(#mq-bg)"/><line x1="36" y1="144" x2="136" y2="144" stroke="#4a5560" stroke-width="1.2"/><line x1="36" y1="24" x2="36" y2="144" stroke="#4a5560" stroke-width="1.2"/><text x="8" y="28" font-family="ui-monospace,Menlo,Consolas,monospace" font-size="10" fill="#15202b">1</text><text x="8" y="148" font-family="ui-monospace,Menlo,Consolas,monospace" font-size="10" fill="#15202b">0</text><rect x="40" y="83.99999999999999" width="36" height="60.000000000000014" rx="4" fill="#3d5afe" stroke="#1a237e" stroke-width="1" filter="url(#mq-soft)"/><text x="58" y="162" text-anchor="middle" font-family="ui-monospace,Menlo,Consolas,monospace" font-size="11" fill="#15202b">00</text><text x="58" y="78.99999999999999" text-anchor="middle" font-family="ui-monospace,Menlo,Consolas,monospace" font-size="10" fill="#5a6a7a">0.50</text><rect x="88" y="83.99999999999999" width="36" height="60.000000000000014" rx="4" fill="#3d5afe" stroke="#1a237e" stroke-width="1" filter="url(#mq-soft)"/><text x="106" y="162" text-anchor="middle" font-family="ui-monospace,Menlo,Consolas,monospace" font-size="11" fill="#15202b">11</text><text x="106" y="78.99999999999999" text-anchor="middle" font-family="ui-monospace,Menlo,Consolas,monospace" font-size="10" fill="#5a6a7a">0.50</text></svg>
|
||||
|
After Width: | Height: | Size: 2.0 KiB |
@@ -0,0 +1,58 @@
|
||||
---
|
||||
title: 贝尔态:两个量子比特如何一起回答
|
||||
description: 从一条假设开始,搭电路、看概率、画图,并用断言验收。
|
||||
import quantum:ext/quantum/quantum.mq.md
|
||||
---
|
||||
|
||||
# main
|
||||
|
||||
贝尔态:一次可读的量子实验。
|
||||
|
||||
我们先做一个假设:如果对第一个量子比特施加 Hadamard 门,再用它控制第二个量子比特翻转,
|
||||
那么测量结果不会出现「一个 0、一个 1」,只会出现「00」或「11」。
|
||||
|
||||
下面的表不是配置,而是实验步骤本身。读者先读实验设计,再读执行结果。
|
||||
|
||||
`步骤` =
|
||||
|
||||
| 步骤 | gate | qubits |
|
||||
|------|------|--------|
|
||||
| 1 | H | 0 |
|
||||
| 2 | CX | 0,1 |
|
||||
|
||||
**`电路` = > quantum.circuit qubits=2 steps=`步骤`**
|
||||
**`概率` = > `电路`.probabilities**
|
||||
|
||||
> print text=`概率`
|
||||
|
||||
---
|
||||
|
||||
我们期待什么?如果假设成立,`概率`应该只在「00」与「11」上有值,而且各占一半。
|
||||
先把两个关键概率取出来,读者不必在长映射里找:
|
||||
|
||||
**`p00` = [00](概率)**
|
||||
**`p11` = [11](概率)**
|
||||
|
||||
> print text=`p00`
|
||||
> print text=`p11`
|
||||
|
||||
下面的断言不是装饰:如果电路写错,或者门接错,运行会直接失败。
|
||||
|
||||
1. `p00` > 0.49
|
||||
> print text=bell-00-ok
|
||||
2. *
|
||||
> print text=bell-00-fail
|
||||
|
||||
1. `p11` > 0.49
|
||||
> print text=bell-11-ok
|
||||
2. *
|
||||
> print text=bell-11-fail
|
||||
|
||||
---
|
||||
|
||||
图不是截图,而是这份文档运行后生成的派生物。电路图和概率图都来自同一个`电路`。
|
||||
|
||||
**`_` = > `电路`.draw path="bell-circuit.svg" theme="light"**
|
||||
**`_` = > `电路`.draw kind="probs" path="bell-probs.svg" theme="light"**
|
||||
|
||||
> print text=bell-draw-ok
|
||||
@@ -0,0 +1,17 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 520 360" width="520" height="360" role="img" aria-label="function plot">
|
||||
<defs>
|
||||
<marker id="arrow" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||
<path d="M 0 0 L 10 5 L 0 10 z" fill="#4b5563"/>
|
||||
</marker>
|
||||
</defs>
|
||||
<style>
|
||||
.grid { stroke: #e5e7eb; stroke-width: 1; }
|
||||
.axis { stroke: #4b5563; stroke-width: 1.5; }
|
||||
.tick { stroke: #4b5563; stroke-width: 1; }
|
||||
.curve { fill: none; stroke: #1f6feb; stroke-width: 2; }
|
||||
.pt { fill: #1f6feb; }
|
||||
.axis-label { font: 12px ui-sans-serif, system-ui, sans-serif; fill: #374151; font-weight: 600; }
|
||||
.tick-label { font: 10px ui-sans-serif, system-ui, sans-serif; fill: #6b7280; }
|
||||
</style>
|
||||
<line x1="36.00" y1="36" x2="36.00" y2="324" class="grid"/><line x1="110.67" y1="36" x2="110.67" y2="324" class="grid"/><line x1="185.33" y1="36" x2="185.33" y2="324" class="grid"/><line x1="260.00" y1="36" x2="260.00" y2="324" class="grid"/><line x1="334.67" y1="36" x2="334.67" y2="324" class="grid"/><line x1="409.33" y1="36" x2="409.33" y2="324" class="grid"/><line x1="484.00" y1="36" x2="484.00" y2="324" class="grid"/><line x1="36" y1="289.09" x2="484" y2="289.09" class="grid"/><line x1="36" y1="252.73" x2="484" y2="252.73" class="grid"/><line x1="36" y1="216.36" x2="484" y2="216.36" class="grid"/><line x1="36" y1="180.00" x2="484" y2="180.00" class="grid"/><line x1="36" y1="143.64" x2="484" y2="143.64" class="grid"/><line x1="36" y1="107.27" x2="484" y2="107.27" class="grid"/><line x1="36" y1="70.91" x2="484" y2="70.91" class="grid"/><line x1="36" y1="180.00" x2="484" y2="180.00" class="axis" marker-end="url(#arrow)"/><line x1="260.00" y1="324" x2="260.00" y2="36" class="axis" marker-end="url(#arrow)"/><text x="488.00" y="184.00" class="axis-label">x</text><text x="266.00" y="40.00" class="axis-label">y</text><line x1="36.00" y1="176.00" x2="36.00" y2="184.00" class="tick"/><text x="36.00" y="194.00" text-anchor="middle" class="tick-label">-3</text><line x1="110.67" y1="176.00" x2="110.67" y2="184.00" class="tick"/><text x="110.67" y="194.00" text-anchor="middle" class="tick-label">-2</text><line x1="185.33" y1="176.00" x2="185.33" y2="184.00" class="tick"/><text x="185.33" y="194.00" text-anchor="middle" class="tick-label">-1</text><line x1="334.67" y1="176.00" x2="334.67" y2="184.00" class="tick"/><text x="334.67" y="194.00" text-anchor="middle" class="tick-label">1</text><line x1="409.33" y1="176.00" x2="409.33" y2="184.00" class="tick"/><text x="409.33" y="194.00" text-anchor="middle" class="tick-label">2</text><line x1="484.00" y1="176.00" x2="484.00" y2="184.00" class="tick"/><text x="484.00" y="194.00" text-anchor="middle" class="tick-label">3</text><line x1="256.00" y1="289.09" x2="264.00" y2="289.09" class="tick"/><text x="252.00" y="289.09" text-anchor="end" dominant-baseline="middle" class="tick-label">-15</text><line x1="256.00" y1="252.73" x2="264.00" y2="252.73" class="tick"/><text x="252.00" y="252.73" text-anchor="end" dominant-baseline="middle" class="tick-label">-10</text><line x1="256.00" y1="216.36" x2="264.00" y2="216.36" class="tick"/><text x="252.00" y="216.36" text-anchor="end" dominant-baseline="middle" class="tick-label">-5</text><line x1="256.00" y1="143.64" x2="264.00" y2="143.64" class="tick"/><text x="252.00" y="143.64" text-anchor="end" dominant-baseline="middle" class="tick-label">5</text><line x1="256.00" y1="107.27" x2="264.00" y2="107.27" class="tick"/><text x="252.00" y="107.27" text-anchor="end" dominant-baseline="middle" class="tick-label">10</text><line x1="256.00" y1="70.91" x2="264.00" y2="70.91" class="tick"/><text x="252.00" y="70.91" text-anchor="end" dominant-baseline="middle" class="tick-label">15</text><text x="252.00" y="194.00" class="tick-label">0</text><path d="M 36.00 310.91 L 38.24 305.73 L 40.48 300.67 L 42.72 295.72 L 44.96 290.89 L 47.20 286.18 L 49.44 281.57 L 51.68 277.07 L 53.92 272.69 L 56.16 268.41 L 58.40 264.24 L 60.64 260.18 L 62.88 256.22 L 65.12 252.36 L 67.36 248.61 L 69.60 244.96 L 71.84 241.40 L 74.08 237.95 L 76.32 234.60 L 78.56 231.34 L 80.80 228.17 L 83.04 225.11 L 85.28 222.13 L 87.52 219.25 L 89.76 216.45 L 92.00 213.75 L 94.24 211.13 L 96.48 208.61 L 98.72 206.17 L 100.96 203.81 L 103.20 201.53 L 105.44 199.34 L 107.68 197.23 L 109.92 195.20 L 112.16 193.25 L 114.40 191.38 L 116.64 189.58 L 118.88 187.86 L 121.12 186.22 L 123.36 184.64 L 125.60 183.14 L 127.84 181.71 L 130.08 180.35 L 132.32 179.06 L 134.56 177.83 L 136.80 176.67 L 139.04 175.57 L 141.28 174.54 L 143.52 173.57 L 145.76 172.67 L 148.00 171.82 L 150.24 171.03 L 152.48 170.30 L 154.72 169.62 L 156.96 169.00 L 159.20 168.44 L 161.44 167.93 L 163.68 167.47 L 165.92 167.06 L 168.16 166.70 L 170.40 166.39 L 172.64 166.12 L 174.88 165.90 L 177.12 165.73 L 179.36 165.60 L 181.60 165.51 L 183.84 165.46 L 186.08 165.46 L 188.32 165.49 L 190.56 165.56 L 192.80 165.67 L 195.04 165.81 L 197.28 165.98 L 199.52 166.19 L 201.76 166.43 L 204.00 166.70 L 206.24 167.01 L 208.48 167.33 L 210.72 167.69 L 212.96 168.07 L 215.20 168.48 L 217.44 168.91 L 219.68 169.36 L 221.92 169.84 L 224.16 170.33 L 226.40 170.84 L 228.64 171.38 L 230.88 171.92 L 233.12 172.48 L 235.36 173.06 L 237.60 173.65 L 239.84 174.25 L 242.08 174.86 L 244.32 175.49 L 246.56 176.12 L 248.80 176.75 L 251.04 177.39 L 253.28 178.04 L 255.52 178.69 L 257.76 179.35 L 260.00 180.00 L 262.24 180.65 L 264.48 181.31 L 266.72 181.96 L 268.96 182.61 L 271.20 183.25 L 273.44 183.88 L 275.68 184.51 L 277.92 185.14 L 280.16 185.75 L 282.40 186.35 L 284.64 186.94 L 286.88 187.52 L 289.12 188.08 L 291.36 188.62 L 293.60 189.16 L 295.84 189.67 L 298.08 190.16 L 300.32 190.64 L 302.56 191.09 L 304.80 191.52 L 307.04 191.93 L 309Line truncated
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 6.8 KiB |
@@ -0,0 +1,32 @@
|
||||
---
|
||||
title: 一份可执行的求导讲义
|
||||
description: 公式、求导、求根、求值与绘图,在同一条叙述里完成。
|
||||
import math:lib/math.mq.md
|
||||
---
|
||||
|
||||
# main
|
||||
|
||||
我们从一条曲线开始。下面这个公式既是排版出来的数学对象,
|
||||
也是运行时可以直接求导、求根与绘图的`f`。
|
||||
|
||||
`f` =
|
||||
$$
|
||||
x^3 - 3*x
|
||||
$$
|
||||
|
||||
对它求导,得到`df`;再让`df`在 x = 2 处取值,得到切线斜率。
|
||||
|
||||
**`df` = [math.diff] formula=`f` var="x"**
|
||||
**`slope` = [math.eval] formula=`df` var="x" value=2**
|
||||
> print text=`df`
|
||||
> print text=`slope`
|
||||
|
||||
曲线与 x 轴的交点同样可以被机器找出来。这里不需要另写一段脚本。
|
||||
|
||||
**`roots` = [math.solve] formula=`f` var="x" min=-3 max=3**
|
||||
> print text=`roots`
|
||||
|
||||
最后把公式画成图。图不是截图,而是本文档运行产生的派生物。
|
||||
|
||||
**`_svg` = [math.plot] formula=`f` var="x" min=-3 max=3 path="calculus-plot.svg"**
|
||||
> print text=plot-ok
|
||||
@@ -0,0 +1,84 @@
|
||||
---
|
||||
title: 外联:把 Node 当作文档里的计算车间
|
||||
description: 一次性执行、持久会话、显式传值,以及可读的结果。
|
||||
import foreign:lib/foreign.mq.md
|
||||
---
|
||||
|
||||
# main
|
||||
|
||||
这份文档要回答一个很具体的问题:同一批订单,按区域汇总之后,总金额分别是多少?
|
||||
数据已经写在文档里,计算交给 Node,结果回到文档里继续被阅读与断言。
|
||||
|
||||
`订单` =
|
||||
|
||||
| @ | 区域 | 金额 |
|
||||
|---|------|------|
|
||||
| 1 | 华东 | 120 |
|
||||
| 2 | 华东 | 300 |
|
||||
| 3 | 华南 | 90 |
|
||||
| 4 | 华南 | 140 |
|
||||
| 5 | 华北 | 220 |
|
||||
|
||||
先做一次性执行。`foreign.call` 把`订单`编码成 JSON 放到 stdin,
|
||||
Node 读完、汇总、打印 JSON;外联再把 stdout 解析成 Marqdo 的值。
|
||||
没有 shell,也没有隐式的 stdin 约定——传什么、读什么,都写在代码里。
|
||||
|
||||
`汇总代码` =
|
||||
```javascript
|
||||
const incoming = JSON.parse(require('fs').readFileSync(0, 'utf8'));
|
||||
|
||||
const byRegion = {};
|
||||
for (const row of incoming) {
|
||||
byRegion[row["区域"]] = (byRegion[row["区域"]] || 0) + Number(row["金额"]);
|
||||
}
|
||||
|
||||
console.log(JSON.stringify(byRegion));
|
||||
```
|
||||
|
||||
**`汇总` = > foreign.call code=`汇总代码` input=`订单`**
|
||||
> print text=`汇总`
|
||||
|
||||
---
|
||||
|
||||
一次性执行适合「算完就走」。如果想让同一个 Node 内核保留变量,
|
||||
就显式开启会话、显式设值、显式求值、显式关闭——状态不会藏在某个看不见的 cell 里。
|
||||
|
||||
**`会话` = > foreign.session_open lang="node"**
|
||||
**> foreign.session_set id=`会话` name="orders" value=`订单`**
|
||||
|
||||
`排序代码` =
|
||||
```javascript
|
||||
const ranked = Object.entries(
|
||||
orders.reduce((acc, row) => {
|
||||
acc[row["区域"]] = (acc[row["区域"]] || 0) + Number(row["金额"]);
|
||||
return acc;
|
||||
}, {})
|
||||
).sort((a, b) => b[1] - a[1]);
|
||||
|
||||
console.log(ranked.map(([region, total]) => `${region}: ${total}`).join("\n"));
|
||||
```
|
||||
|
||||
**`第一次` = > foreign.session_eval id=`会话` code=`排序代码`**
|
||||
> print text=[stdout](`第一次`)
|
||||
|
||||
内核状态是持续的。下面这段代码在同一个`orders`上追加一条华北订单,
|
||||
再重新排序;不需要重新传一遍整张表。
|
||||
|
||||
`追加代码` =
|
||||
```javascript
|
||||
orders.push({ 区域: "华北", 金额: 80 });
|
||||
|
||||
const rankedAgain = Object.entries(
|
||||
orders.reduce((acc, row) => {
|
||||
acc[row["区域"]] = (acc[row["区域"]] || 0) + Number(row["金额"]);
|
||||
return acc;
|
||||
}, {})
|
||||
).sort((a, b) => b[1] - a[1]);
|
||||
|
||||
console.log(rankedAgain.map(([region, total]) => `${region}: ${total}`).join("\n"));
|
||||
```
|
||||
|
||||
**`第二次` = > foreign.session_eval id=`会话` code=`追加代码`**
|
||||
> print text=[stdout](`第二次`)
|
||||
|
||||
**> foreign.session_close id=`会话`**
|
||||
@@ -0,0 +1,44 @@
|
||||
---
|
||||
title: 一张订单,一份可执行的业务说明
|
||||
description: 叙述、数据、计算与断言共享同一个 .mq.md 源文件。
|
||||
---
|
||||
|
||||
# main
|
||||
|
||||
这份文档不是代码旁边的说明,也不是说明里贴的代码。下表既是给人读的订单,
|
||||
也是运行时直接使用的记录集合;下面的句子既解释业务规则,也参与计算。
|
||||
|
||||
`订单` =
|
||||
|
||||
| @ | 客户 | 单价 | 数量 | 提前天数 |
|
||||
|---|------|------|------|----------|
|
||||
| 1 | 青山实验室 | 120 | 3 | 10 |
|
||||
| 2 | 远望中心 | 80 | 5 | 4 |
|
||||
| 3 | 星尘工坊 | 200 | 1 | 14 |
|
||||
|
||||
逐行把「单价 × 数量」累加进`小计`。读者看到的是清单,机器执行的是同一张表。
|
||||
|
||||
**`小计` = 0**
|
||||
- [行](订单)
|
||||
**`小计` = `小计` + [单价](行) * [数量](行)**
|
||||
|
||||
业务约定写在叙述里:提前不少于七天,整单按九五折;否则原价。
|
||||
本单最早提前 14 天。
|
||||
|
||||
**`最早提前` = 14**
|
||||
1. `最早提前` >= 7
|
||||
**`折后` = `小计` * 95 / 100**
|
||||
2. *
|
||||
**`折后` = `小计`**
|
||||
|
||||
把结果、依据和判断一起写进文档。下面的断言如果跑不通,文档本身就是错的。
|
||||
|
||||
**`含税` = `折后` * 106 / 100**
|
||||
> print text=`小计`
|
||||
> print text=`折后`
|
||||
> print text=`含税`
|
||||
|
||||
1. `含税` < 10000
|
||||
> print text=within-cap
|
||||
2. *
|
||||
> print text=over-cap
|
||||
Reference in new issue
Block a user