Release 2.7.0: executable handbooks and native user-site workflows

This commit is contained in:
cflmy committed 2026-11-10 17:03:06 +08:00
1 parent aa0c4d0f55
commit d9aef957f7
562 files changed
+21064 -20387

No files matched your search

+14 -2
View File
@@ -4,11 +4,23 @@ markup: occlusion
## 加一
| 参数 | 类型 | 默认 | 说明 |
|---|---|---|---|
| n | any | 必填 | Numeric input advanced or calculated by this example |
| 返回 | 类型 | 说明 |
|---|---|---|
| result | any | 下文显式返回的计算值或结构化结果;分支存在时遵循实际命中的分支。 |
对于输入变量`n`,我们执行 **`n` = `n` + 1**,然后把更新后的值作为结果返回 *`n`*。
# main
| 返回 | 类型 | 说明 |
|---|---|---|
| result | any | 空值;按顺序执行本页的演示步骤,观察输出或已说明的副作用。 |
从 41 出发调用「加一」,把结果存进`r`并打印。
**`r` = > 加一 n=41**
> print text=`r`
我们先**r = [计算并保存 r](加一(n=41))**,供下一步使用。
随后我们**[显示当前步骤的结果](> print text=`r`)**。
+18 -14
View File
@@ -8,6 +8,10 @@ import:
# main
| 返回 | 类型 | 说明 |
|---|---|---|
| result | any | None; execute this page's documented steps in order and observe their output or stated effects |
贝尔态:一次可读的量子实验。
我们先做一个假设:如果对第一个量子比特施加 Hadamard 门,再用它控制第二个量子比特翻转,
@@ -22,39 +26,39 @@ import:
| 1 | H | 0 |
| 2 | CX | 0,1 |
**`电路` = > quantum.circuit qubits=2 steps=`步骤`**
**`概率` = > `电路`.probabilities**
We first **电路 = [Compute 电路](quantum.circuit(qubits=2, steps=`步骤`))** for the next step.
We first **概率 = [Compute 概率](> `电路`.probabilities)** for the next step.
> print text=`概率`
We now **[Display this step's result](> print text=`概率`)**.
---
我们期待什么?如果假设成立,`概率`应该只在「00」与「11」上有值,而且各占一半。
先把两个关键概率取出来,读者不必在长映射里找:
**`p00` = [00](access(概率, "00"))**
**`p11` = [11](access(概率, "11"))**
We first **p00 = [00](access(`概率`, "00"))** for the next step.
We first **p11 = [11](access(`概率`, "11"))** for the next step.
> print text=`p00`
> print text=`p11`
We now **[Display this step's result](> print text=`p00`)**.
We now **[Display this step's result](> print text=`p11`)**.
下面的断言不是装饰:如果电路写错,或者门接错,运行会直接失败。
1. `p00` > 0.49
> print text=bell-00-ok
We now **[Display this step's result](> print text="bell-00-ok")**.
2. *
> print text=bell-00-fail
We now **[Display this step's result](> print text="bell-00-fail")**.
1. `p11` > 0.49
> print text=bell-11-ok
We now **[Display this step's result](> print text="bell-11-ok")**.
2. *
> print text=bell-11-fail
We now **[Display this step's result](> print text="bell-11-fail")**.
---
图不是截图,而是这份文档运行后生成的派生物。电路图和概率图都来自同一个`电路`。
**`_` = > `电路`.draw path="bell-circuit.svg" theme="light"**
**`_` = > `电路`.draw kind="probs" path="bell-probs.svg" theme="light"**
We first **_ = [Compute _](> `电路`.draw path="bell-circuit.svg" theme="light")** for the next step.
We first **_ = [Compute _](> `电路`.draw kind="probs" path="bell-probs.svg" theme="light")** for the next step.
> print text=bell-draw-ok
We now **[Display this step's result](> print text="bell-draw-ok")**.
+12 -8
View File
@@ -8,6 +8,10 @@ import:
# main
| 返回 | 类型 | 说明 |
|---|---|---|
| result | any | None; execute this page's documented steps in order and observe their output or stated effects |
我们从一条曲线开始。下面这个公式既是排版出来的数学对象,
也是运行时可以直接求导、求根与绘图的`f`。
@@ -18,17 +22,17 @@ $$
对它求导,得到`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`
We first **df = [Compute df](math.diff(formula=`f`, var="x"))** for the next step.
We first **slope = [Compute slope](math.eval(formula=`df`, var="x", value=2))** for the next step.
We now **[Display this step's result](> print text=`df`)**.
We now **[Display this step's result](> print text=`slope`)**.
曲线与 x 轴的交点同样可以被机器找出来。这里不需要另写一段脚本。
**`roots` = [math.solve] formula=`f` var="x" min=-3 max=3**
> print text=`roots`
We first **roots = [Compute roots](math.solve(formula=`f`, var="x", min=-3, max=3))** for the next step.
We now **[Display this step's result](> print text=`roots`)**.
最后把公式画成图。图不是截图,而是本文档运行产生的派生物。
**`_svg` = [math.plot] formula=`f` var="x" min=-3 max=3 path="calculus-plot.svg"**
> print text=plot-ok
We first **_svg = [Compute _svg](math.plot(formula=`f`, var="x", min=-3, max=3, path="calculus-plot.svg"))** for the next step.
We now **[Display this step's result](> print text="plot-ok")**.
+94 -64
View File
@@ -1,86 +1,116 @@
---
markup: occlusion
title: 外联:把 Node 当作文档里的计算车间
description: 一次性执行、持久会话、显式传值,以及可读的结果。
title: 原生订单分析与 Node Intl 展示边界
import:
native: ../foreign-native-report/index.mq.md
foreign: lib/foreign.mq.md
table: lib/table.mq.md
result: lib/result.mq.md
---
# main
订单分析应先使用已经具备的原生能力。只有本地化货币展示交给 Node Intl;一次性执行和持久会话都是明确的外联边界,文件操作与通用数据处理不需要 Python 或外部读取器。
这份文档要回答一个很具体的问题:同一批订单,按区域汇总之后,总金额分别是多少?
数据已经写在文档里,计算交给 Node,结果回到文档里继续被阅读与断言。
从仓库根目录调用:`marqdo call examples/oa2026-showcase/foreign-node.mq.md main --require-ok --effects process.exec --fs-root . --json`。只需原生分析时,可直接 call 共享模块的 analyze_orders,不需要任何进程授权。
## main
| 返回 | 类型 | 说明 |
|---|---|---|
| result | map | Result:前后两份原生分析、实际排序结果与一次性/持久格式化结果 |
成功时 value 的字段如下。先检查最外层 ok,再按字段读取;这里列出的数据都是本轮实际计算或执行得到的值。
| value 中的字段 | 值的形态 | 直接含义 |
|---|---|---|
| before | 原生分析记录 | 五笔订单的总额 870、数量 5、平均值 174 与按区域保留的统计行 |
| after | 原生分析记录 | 追加八十元订单后,总额 950、数量 6、平均值约 158.3333;不做展示舍入 |
| initial_ranking | 原生行记录列表 | 首轮按区域总额降序排列;每行保留区域、数量、总额、均值与标准化跨度 |
| updated_ranking | 原生行记录列表 | 更新后的区域总额依次为 420、300、230;排序返回新列表 |
| one_shot | 两个展示文本字段 | 一次性进程返回的 total 与 average,只表示首轮报告的本地化展示 |
| persistent_initial | 两个展示文本字段 | 持久格式器对同一首轮报告得到的 total 与 average,可与一次性结果核对 |
| persistent_updated | 两个展示文本字段 | 持久格式器展示更新后的报告;950.00 与 158.33 是展示文本,不覆盖原生数值 |
本例先处理五笔订单:总额为八百七十,随后追加一笔华北的八十元订单,总额变为九百五十。分组、计数、平均值、追加和按区域总额排序全部使用 Marqdo 固有能力;不再在 Node 中维护另一份订单分析程序。共享模块中的 analyze_orders 是唯一的分析实现,其参数与字段表可以直接阅读和调用。
外联只承担确实缺失的本地化展示。第一次通过本文件的 format_once 演示标准输入上的 JSON 往返;两次共享 format_summary 分别展示原生修改前后的结果。每次共享调用都在同一内核内建立格式器、复用它格式化平均值并关闭资源,实际生命周期写在对应函数中,而不是把一个句柄藏进跨命令的 JSON 字符串。
总额排序也保留在原生流程中:先按 total 升序,再反转得到降序记录。排序不会改变原始订单或报告,每个返回行仍包含区域、订单数和总额。追加发生在 Marqdo 集合中,随后将重新计算出的值拷贝给展示边界,避免外部内核与原生数据各保留一份会悄悄分叉的业务状态。
调用需要 process.exec 和 Node,不读写用户文件,也不安装包。先检查共享格式化任务的 ok,再使用 value;一次性 JSON 协议失败则保留原生诊断。成功结果携带真正的原生数据和展示字符串,既能继续被其他函数使用,也能核对展示没有替代数值计算。查看返回结果比只读一条“完成”打印更能证明这一轮执行发生了什么。
`订单` =
| @ | 区域 | 金额 |
|---|------|------|
|---|---|---|
| 1 | 华东 | 120 |
| 2 | 华东 | 300 |
| 3 | 华南 | 90 |
| 4 | 华南 | 140 |
| 5 | 华北 | 220 |
先做一次性执行。`foreign.call` 把`订单`编码成 JSON 放到 stdin,
Node 读完、汇总、打印 JSON;外联再把 stdout 解析成 Marqdo 的值。
没有 shell,也没有隐式的 stdin 约定——传什么、读什么,都写在代码里。
We **before = [在原生标准库中完成首轮订单分析](native.analyze_orders(orders=`订单`, region_key="区域", amount_key="金额"))** and
**initial_ranking = [保留首轮按区域总额降序的原生行记录](table.reverse(list=table.sort_by(list=access(before, "regions"), key="total")))** and
**one_shot = [只把首轮报告的展示交给一次性进程](format_once(report=`before`))** and
**initial = [用共享持久内核流程展示同一份原生结果](native.format_summary(report=`before`, locale="zh-CN"))**。
1. not access(`initial`, "ok")
返回*initial*,保留真实运行失败。
`汇总代码` =
```javascript
const incoming = JSON.parse(require('fs').readFileSync(0, 'utf8'));
`追加` =
const byRegion = {};
for (const row of incoming) {
byRegion[row["区域"]] = (byRegion[row["区域"]] || 0) + Number(row["金额"]);
}
| 区域 | 金额 |
|---|---|
| 华北 | 80 |
console.log(JSON.stringify(byRegion));
We **orders = [在原生集合中追加新订单](table.append(list=`订单`, item=`追加`))** and
**after = [用同一份原生实现重新计算完整报告](native.analyze_orders(orders=`orders`, region_key="区域", amount_key="金额"))** and
**updated_ranking = [在原生集合中保留更新后的降序记录](table.reverse(list=table.sort_by(list=access(after, "regions"), key="total")))** and
**updated = [把更新后的原生结果交给展示边界](native.format_summary(report=`after`, locale="zh-CN"))**。
1. not access(`updated`, "ok")
返回*updated*,保留持久内核的失败出口。
We **persistent_initial = [保留首轮实际格式化文本](access(initial, "value"))** and **persistent_updated = [保留更新后的实际展示文本](access(updated, "value"))**。
`观察` =
| before | after | initial_ranking | updated_ranking | one_shot | persistent_initial | persistent_updated |
|---|---|---|---|---|---|---|
| `before` | `after` | `initial_ranking` | `updated_ranking` | `one_shot` | `persistent_initial` | `persistent_updated` |
完成原生分析与明确的展示外联后,我们返回*[本次实际观察到的数据与结果](result.ok(value=`观察`))*。
## format_once
| 参数 | 类型 | 默认 | 说明 |
|---|---|---|---|
| report | map | required | 原生分析结果,包含 total 与 average 数值 |
| locale | text | "zh-CN" | Node Intl 使用的展示语言区域 |
| 返回 | 类型 | 说明 |
|---|---|---|
| result | map | 直接返回 total/average 本地化文本;子进程或 JSON 失败保留原生诊断 |
使用 `report` 和 `locale`="zh-CN" 补足本地化货币展示。一次性外联只接收已经计算好的总额与平均值,不分组、不排序、不修改订单。传入数据由 Marqdo 的 input 通道编码为 JSON;Node 从标准输入流接收它,不读取任何路径,也不额外生成文件。
代码围栏明确写出进程如何消费输入、建立 Intl 格式器以及输出唯一的 JSON 记录。标准输出只承载这个协议值,诊断保留在错误通道;混入其他打印会导致 JSON 解析失败,而不是被当作部分成功。这里选择人民币展示,不声称进行汇率换算或货币精度计算。
调用需要 process.exec 与已安装的 Node,超时为五秒。foreign.call 在进程失败或输出不是合法 JSON 时给出原生诊断,因此本函数的成功值就是格式化记录,不再套一个假造的通过标记。这个一次性子进程结束后没有可复用状态;持久会话的创建、两次求值和关闭由共享原生报表手册中的 format_summary 展示。
`输入` =
| report | locale |
|---|---|
| `report` | `locale` |
`格式化代码` =
```node
let raw = "";
process.stdin.setEncoding("utf8");
process.stdin.on("data", chunk => { raw += chunk; });
process.stdin.on("end", () => {
const input = JSON.parse(raw);
const money = new Intl.NumberFormat(input.locale, {style: "currency", currency: "CNY"});
console.log(JSON.stringify({total: money.format(input.report.total), average: money.format(input.report.average)}));
});
```
**`汇总` = > 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](access(`第一次`, "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](access(`第二次`, "stdout"))
**> foreign.session_close id=`会话`**
接收完整输入后,我们返回*[执行有界的一次性展示边界并解析真实 JSON 返回](foreign.call(code=`格式化代码`, input=`输入`, timeout=5))*。
+15 -11
View File
@@ -6,6 +6,10 @@ description: 叙述、数据、计算与断言共享同一个 .mq.md 源文件
# main
| 返回 | 类型 | 说明 |
|---|---|---|
| result | any | None; execute this page's documented steps in order and observe their output or stated effects |
这份文档不是代码旁边的说明,也不是说明里贴的代码。下表既是给人读的订单,
也是运行时直接使用的记录集合;下面的句子既解释业务规则,也参与计算。
@@ -19,27 +23,27 @@ description: 叙述、数据、计算与断言共享同一个 .mq.md 源文件
逐行把「单价 × 数量」累加进`小计`。读者看到的是清单,机器执行的是同一张表。
**`小计` = 0**
We first **小计 = 0** for the next step.
- [行](遍历 订单 为 行)
**`小计` = `小计` + [单价](access(行, "单价")) * [数量](access(行, "数量"))**
We first **小计 = (`小计` + ([单价](access(`行`, "单价")) * [数量](access(`行`, "数量"))))** for the next step.
业务约定写在叙述里:提前不少于七天,整单按九五折;否则原价。
本单最早提前 14 天。
**`最早提前` = 14**
We first **最早提前 = 14** for the next step.
1. `最早提前` >= 7
**`折后` = `小计` * 95 / 100**
We first **折后 = ((`小计` * 95) / 100)** for the next step.
2. *
**`折后` = `小计`**
We first **折后 = `小计`** for the next step.
把结果、依据和判断一起写进文档。下面的断言如果跑不通,文档本身就是错的。
**`含税` = `折后` * 106 / 100**
> print text=`小计`
> print text=`折后`
> print text=`含税`
We first **含税 = ((`折后` * 106) / 100)** for the next step.
We now **[Display this step's result](> print text=`小计`)**.
We now **[Display this step's result](> print text=`折后`)**.
We now **[Display this step's result](> print text=`含税`)**.
1. `含税` < 10000
> print text=within-cap
We now **[Display this step's result](> print text="within-cap")**.
2. *
> print text=over-cap
We now **[Display this step's result](> print text="over-cap")**.