1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
|
<?xml version="1.0" encoding="utf-8"?>
<search>
<entry>
<title>10 Tips for Productive FSharp Scripting</title>
<url>/2024/10/18/10-Tips-for-Productive-FSharp-Scripting/</url>
<content><![CDATA[<p>Scott Hanselman recently had a <a href="http://www.hanselman.com/blog/InteractiveCodingWithCAndFREPLsScriptCSOrTheVisualStudioInteractiveWindow.aspx">nice post on C# and F# REPLs</a>, which reminded me of the time I started using F# scripts. Over time, I found out a couple of small tricks, which helped make the experience productive. I found about them mainly by accident, so I figured, let’s see if I can list them in one place! Some of these are super simple, some probably a bit obscure, but hopefully, one of them at least will make your path towards scripting nirvana an easier one…</p>
<blockquote>
<p>Note: these tips are not necessarily ordered by usefulness. For that matter, there might or might not be exactly 10 of them :)</p>
</blockquote>
<h2 id="Tip-1-Use-fsx-Files-for-Interactive-Coding"><a class="header-anchor" href="#Tip-1-Use-fsx-Files-for-Interactive-Coding">¶</a>Tip 1: Use <code>.fsx</code> Files for Interactive Coding</h2>
<p>You can use the F# Interactive 2 ways: you can directly type code into FSI, the F# Interactive window, or you can write code in an <code>.fsx</code> file, and select pieces of the code you want to execute. I recommend the second approach, for at least two reasons. First, FSI is a very primitive environment, <code>.fsx</code> files provide a much richer experience (IntelliSense). Then this encourages writing clean scripts you can reuse later.</p>
<blockquote>
<p>This is not specific to scripts, but… if you are on Visual Studio, do yourself a service and install the <a href="http://fsprojects.github.io/VisualFSharpPowerTools/">Visual F# Power Tools</a> - you’ll get nice things such as better code highlighting, refactoring, and more.</p>
</blockquote>
<p>To execute code interactively, simply type code in an <code>.fsx</code> file, select a block of code, and hit Alt + Enter. The selected code will be evaluated, and the result will show up in the FSI window. In Visual Studio, you can also select code and right-click “Execute in Interactive”, but shortcuts are way faster.</p>
<blockquote>
<p>You can also execute a single-line with Alt + '. I rarely use this option, but this can save you time because you don’t need to select the entire line of code.</p>
</blockquote>
<blockquote>
<p>In case the keyboard shortcuts to send code to FSI do not work anymore (ReSharper used to over-write them in the past), you can reset them in Visual Studio, by going to Tools / Options / Environment / Keyboard. The 2 commands you need to map are EditorContextMenus.CodeWindow.ExecuteInInteractive and EditorContextMenus.CodeWindow.ExecuteLineInInteractive.</p>
</blockquote>
<p>You can also use these shortcuts from a regular <code>.fs</code> file, which can be handy if you want to validate that a piece of code is behaving the way you want.</p>
<blockquote>
<p>Interactive coding is by far my main usage for scripts - I use it extensively to prototype designs, run dumb tasks, or explore data or libraries. I realized recently that a few of my C# friends use LinqPad for the same purpose.</p>
</blockquote>
<h2 id="Tip-2-What-is-it"><a class="header-anchor" href="#Tip-2-What-is-it">¶</a>Tip 2: What is <code>it</code>?</h2>
<p>While I encourage working primarily from <code>.fsx</code> files, the FSI window is also very helpful. I use it primarily for small verifications. For instance, I might have in my script file code like this:</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">let add x y =</span><br><span class="line"> x + y</span><br></pre></td></tr></table></figure>
<p>Once I send it for evaluation into FSI, I will see the following show up in FSI:</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">val add : x:int -> y:int -> int</span><br><span class="line">></span><br></pre></td></tr></table></figure>
<p>My function <code>add</code> is now in memory, in my FSI session; I can start typing in the FSI window and use it:</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">> add 1 2;;</span><br><span class="line">val it : int = 3</span><br><span class="line">></span><br></pre></td></tr></table></figure>
<p>Enter does not trigger execution in FSI. The <code>;;</code> indicates to FSI “Please execute everything I just typed, up to that point”. This is useful if you want to type multiple lines of code in FSI, and execute them as a block.</p>
<blockquote>
<p><code>it</code>: in our <code>add 1 2</code> example, the result showed up as <code>it</code>. We simply ran add, but didn’t assign the result to anything. <code>it</code> now contains the result, until we run another expression. If you want to re-use that value, you can assign it in FSI, by doing for instance <code>let x = it;;</code>.′</p>
</blockquote>
<blockquote>
<p>Once a value is loaded in your FSI session, it will remain there, available to you until you shadow it (in the example above, <code>x</code> will remain available, until I run for instance <code>let x = 42;;</code>). This is extremely convenient: for instance, you can load a data file once <code>let data = File.ReadAllLines path</code>, and keep using <code>data</code> for as long as you want, without having to reload it between code changes.</p>
</blockquote>
<blockquote>
<p>FSI often shows an abbreviated version of values for large items. For instance, <code>[1..999]</code> will show up as <code>val it : int list = [1; 2; 3; 4; 5; 6; 7; 8; 9; 10; 11; 12; 13; 14; 15; 16; 17; 18; 19; 20; 21; 22; 23; 24; 25; 26; 27; 28; 29; 30; 31; 32; 33; 34; 35; 36; 37; 38; 39; 40; 41; 42; 43; 44; 45; 46; 47; 48; 49; 50; 51; 52; 53; 54; 55; 56; 57; 58; 59; 60; 61; 62; 63; 64; 65; 66; 67; 68; 69; 70; 71; 72; 73; 74; 75; 76; 77; 78; 79; 80; 81; 82; 83; 84; 85; 86; 87; 88; 89; 90; 91; 92; 93; 94; 95; 96; 97; 98; 99; 100; ...]</code> - note the … at the end, which indicate that there is more.</p>
</blockquote>
<p>What if you inadvertently started a very long computation, or an infinite loop? In Visual Studio, you can either kill the session entirely, by right-clicking over the FSI window and selecting “Reset Interactive Session” or Ctrl + Alt + R, or cancel the latest evaluation you requested (“Cancel Interactive Evaluation”, or Ctrl + Break.).</p>
<h2 id="Tip-3-Run-Scripts-from-the-Command-Line"><a class="header-anchor" href="#Tip-3-Run-Scripts-from-the-Command-Line">¶</a>Tip 3: Run Scripts from the Command Line</h2>
<p>Besides interactive scripting, you can also run a script from the command line, by using <code>FSI.exe</code>:</p>
<p><code>>fsi.exe "C:\myscript.fsx"</code></p>
<blockquote>
<p><code>FSI.exe</code> is typically located at <code>C:\Program Files (x86)\Microsoft SDKs\F#\4.0\Framework\v4.0</code>. You can also install it separately, see <a href="http://fsharp.org/">fsharp.org/use</a> section for instructions for various platforms.</p>
</blockquote>
<p>You can define different behaviors in your script, depending on whether it is run interactively or from the command line, like this:</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">#if INTERACTIVE</span><br><span class="line">let msg = "Interactive"</span><br><span class="line">#else</span><br><span class="line">let msg = "Not Interactive"</span><br><span class="line">#endif</span><br><span class="line"></span><br><span class="line">printfn "%s" msg</span><br></pre></td></tr></table></figure>
<p><em>Updated, Sep 19: thanks Matt Klein for <a href="http://stackoverflow.com/q/39581342/114519">pointing the issue</a>.</em></p>
<p>For more information on FSI from the command line, <a href="https://msdn.microsoft.com/en-us/library/dd233175.aspx">check the reference page here</a>.</p>
<p><em>Updated, Feb 20: <a href="https://twitter.com/genTauro42">Ramon Soto Mathiesen</a> points out that <a href="https://twitter.com/genTauro42/status/696407757835132928">Tip 9 also applies to the command line</a>.</em></p>
<h2 id="Tip-4-Use-Relative-Paths"><a class="header-anchor" href="#Tip-4-Use-Relative-Paths">¶</a>Tip 4: Use Relative Paths</h2>
<p>Sometimes, your script will reference another resource; for instance, you need to read the contents of a <code>.txt</code> file somewhere. You can use absolute path, as in:</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">File.ReadAllLines @"C:/data/myfile.txt"</span><br></pre></td></tr></table></figure>
<blockquote>
<p>Pre-pending a string with <code>@</code> makes it a verbatim string, and ignore escape sequences, such as <code>\</code>.</p>
</blockquote>
<blockquote>
<p>Use <code>/</code> rather than <code>\</code>, so that path work both on Windows and Mono.</p>
</blockquote>
<p>However, if that resource lives in a location relative to your script, consider using relative path, so that you can move your script folder around without breaking it.</p>
<p>Relative paths can be a bit tricky; for instance, running the following code interactively…</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">System.Environment.CurrentDirectory</span><br></pre></td></tr></table></figure>
<p>… produces a potentially unexpected result in FSI:</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">val it : string = "C:\Users\Mathias Brandewinder\AppData\Local\Temp"</span><br><span class="line">></span><br></pre></td></tr></table></figure>
<p>You can avoid these issues by using built-in constants, which refer respectively to the directory where the script lives, the script file name, and the current line of the script:</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">__SOURCE_DIRECTORY__</span><br><span class="line">__SOURCE_FILE__</span><br><span class="line">__LINE__</span><br></pre></td></tr></table></figure>
<p>So if your folder structure was along these lines…</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">root</span><br><span class="line"> /src/script.fsx</span><br><span class="line"> /data/data.txt</span><br></pre></td></tr></table></figure>
<p>… you could refer to the data file <code>data.txt</code> from your script like this:</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">let path = System.IO.Path.Combine(__SOURCE_DIRECTORY__,"..","data/data.txt")</span><br><span class="line">System.IO.File.ReadAllText path</span><br></pre></td></tr></table></figure>
<h2 id="Tip-5-Including-Assemblies"><a class="header-anchor" href="#Tip-5-Including-Assemblies">¶</a>Tip 5: Including Assemblies</h2>
<p>By default, FSI loads <code>FSharp.Core</code> and nothing else. If you want to use <code>System.DateTime</code>, you will need to first <code>open System</code> in your script. If you want to use an assembly that is not part of the standard .NET distribution, you will need to reference it first using <code>#r</code>. Imagine for instance that you installed the Nuget package <code>fsharp.data</code>; to use it in your script, you would do something like:</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">#r @"../packages/FSharp.Data.2.2.5/lib/net40/FSharp.Data.dll"</span><br><span class="line">open FSharp.Data</span><br></pre></td></tr></table></figure>
<blockquote>
<p>When you execute <code>open System</code> in interactive, don’t worry if nothing seems to happen: the only result is a new <code>></code> showing up in FSI.</p>
</blockquote>
<p>For assemblies that are part of .NET but not referenced by default, you can use a shorter version:</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">#r @"System.Xaml"</span><br><span class="line">open System.Xaml</span><br></pre></td></tr></table></figure>
<blockquote>
<p>In Visual Studio, you can right-click a reference from Solution Explorer, and send to F# interactive. You can then directly open it, and start using it in FSI.</p>
</blockquote>
<p><em>Updated, Feb 20: <a href="https://twitter.com/sergey_tihon">Sergey Tihon</a> shared an interesting comment, explaining where Tip 5 can sometimes go wrong. I’d say, try Tip 5 first, but be aware that this might at times not quite work:</em></p>
<blockquote>
<p><a href="https://twitter.com/brandewinder">@brandewinder</a> don’t load assemblies like in Tip 5 ) <a href="https://t.co/Owft1NmPoo">https://t.co/Owft1NmPoo</a></p>
<p>— Sergey Tihon (@sergey_tihon) <a href="https://twitter.com/sergey_tihon/status/696395229285523456">February 7, 2016</a></p>
</blockquote>
<p><em>Updated, Feb 20: <a href="https://twitter.com/dsyme">F# open source contributor Don Syme</a> share a related nice trick:</em></p>
<blockquote>
<p><a href="https://twitter.com/jeroldhaas">@jeroldhaas</a> <a href="https://twitter.com/sergey_tihon">@sergey_tihon</a> <a href="https://twitter.com/brandewinder">@brandewinder</a> Use <a href="https://twitter.com/hashtag/I?src=hash">#I</a> <strong>SOURCE_DIRECTORY</strong>, it is wondrous, very satisfying. All relative paths then work</p>
<p>— Don Syme (@dsyme) <a href="https://twitter.com/dsyme/status/696429115184955393">February 7, 2016</a></p>
</blockquote>
<h2 id="Tip-6-Use-Paket"><a class="header-anchor" href="#Tip-6-Use-Paket">¶</a>Tip 6: Use <code>Paket</code></h2>
<p>The Nuget package manager is useful to consume existing packages. However, by default, Nuget stores assemblies in a folder that includes the package version number. This is very impractical for a script. In our example above, if <code>fsharp.data</code> gets an update, our script reference will be broken once we update the Nuget package:</p>
<p><code>#r @"../packages/FSharp.Data.2.2.5/lib/net40/FSharp.Data.dll"</code></p>
<p>Fixing the script requires manually editing the version number in the path, which quickly becomes a pain. <a href="https://fsprojects.github.io/Paket/">Paket</a> provides a better experience, because it stores packages without the version number, in this case, under:</p>
<p><code>#r @"../packages/FSharp.Data/lib/net40/FSharp.Data.dll"</code></p>
<p>Your scripts will now gracefully handle version number changes.</p>
<p>If you end up consuming numerous packages, you can make your life even easier, by referencing paths where assemblies might be searched for, using <code>#I</code>:</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">#I @"../packages/</span><br><span class="line">#r @"FSharp.Data/lib/net40/FSharp.Data.dll"</span><br></pre></td></tr></table></figure>
<blockquote>
<p>If your primary goal is to “just script”, consider using <a href="https://atom.io/">Atom</a> or <a href="https://code.visualstudio.com/">VSCode</a>, with the <a href="http://ionide.io/">Ionide plugin</a>. You can create and run free-standing F# scripts, with beautiful <a href="http://ionide.io/#paket-integration">Paket integration</a>.</p>
</blockquote>
<h2 id="Tip-7-Include-Files"><a class="header-anchor" href="#Tip-7-Include-Files">¶</a>Tip 7: Include Files</h2>
<p>You might want to use the code from an existing file in your script. Suppose that we have a code file <code>Code.fs</code> somewhere, looking like this:</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">namespace Mathias</span><br><span class="line"></span><br><span class="line">module Common =</span><br><span class="line"> let hello name = sprintf "Hello, %s" name</span><br></pre></td></tr></table></figure>
<p>You can use that code from your script, by using the <code>#load</code> directive:</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">#load "Code.fs"</span><br><span class="line">open Mathias.Common</span><br><span class="line">hello "World"</span><br></pre></td></tr></table></figure>
<blockquote>
<p>You might have to close and re-open the script file if you end up changing the contents of the file.</p>
</blockquote>
<blockquote>
<p>If the file you are attempting to load contains references to other assemblies or files, you might get an error on the <code>#load</code> statement: “One or more errors in loaded files. The namespace or module … is not defined”. Simply reference the missing assemblies above the <code>#load</code> statement, so that your script uses the same dependencies as the file it refers to.</p>
</blockquote>
<h2 id="Tip-8-Profile-your-Code-with-time"><a class="header-anchor" href="#Tip-8-Profile-your-Code-with-time">¶</a>Tip 8: Profile your Code with #time</h2>
<p>Another handy directive, <code>#time</code>, turns on basic profiling. Once it is executed, for every block of code you send for execution you will see timing and garbage collection information. For instance, running this code…</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">#time</span><br><span class="line">[| 1 .. 10000000 |] |> Array.map (fun x -> x * x)</span><br></pre></td></tr></table></figure>
<p>… will produce the following in FSI:</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">--> Timing now on</span><br><span class="line"></span><br><span class="line">Real: 00:00:00.887, CPU: 00:00:00.828, GC gen0: 2, gen1: 2, gen2: 2</span><br><span class="line">val it : int [] =</span><br><span class="line"> [|1; 4; 9; 16; 25; 36; 49; // snipped for brevity</span><br></pre></td></tr></table></figure>
<p>We get the wall time and CPU time it took, as well as some information about garbage collection in generations 0, 1 and 2. This would not replace a full-blown profiler, but this is an awfully convenient tool to figure out quickly if there are obvious ways to improve a piece of code.</p>
<p>Note that every time you execute <code>#time</code>, the timer will be switched from on to off, or vice-versa. This is not always convenient; you can also explicitly set it to the desired state, like this:</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">#time "on"</span><br><span class="line">// everything now is timed</span><br><span class="line">#time "off"</span><br></pre></td></tr></table></figure>
<blockquote>
<p>If you are interested in profiling, you should take a look at <a href="http://www.privateeye.io/">PrivateEye</a>; check out <a href="https://twitter.com/gregyoung">Greg Young</a>’s <a href="https://vimeo.com/131637366">talk at NDC Oslo 2015</a> to get a feel for what it does.</p>
</blockquote>
<h2 id="Tip-9-Turn-64-bits-on"><a class="header-anchor" href="#Tip-9-Turn-64-bits-on">¶</a>Tip 9: Turn 64-bits on</h2>
<p>Hat tip to <a href="https://twitter.com/rickasaurus">Rick Minerich</a> for that one. I’ll refer you to his blog post to see how to <a href="http://richardminerich.com/2013/03/setting-up-fsharp-interactive-for-machine-learning-with-large-datasets/">set FSI to 64 bits to handle large datasets</a>.</p>
<h2 id="Tip-10-Bonus-Material"><a class="header-anchor" href="#Tip-10-Bonus-Material">¶</a>Tip 10: Bonus Material</h2>
<p>Did you know that you could…</p>
<ul>
<li><a href="https://channel9.msdn.com/Events/Visual-Studio/Visual-Studio-2015-Final-Release-Event/Six-Quick-Picks-from-Visual-F-40">debug an F# script? (around 0:12:35 in)</a></li>
<li><a href="http://www.swensensoftware.com/fseye">inspect the objects in your FSI session with FsEye?</a></li>
<li>change the FSI font size in Tools/Options/Environment/Fonts and Colors/Show Settings for/F# Interactive?</li>
<li>add your own pretty-printer to FSI, <a href="https://github.com/mathnet/mathnet-numerics/blob/master/src/FSharp/MathNet.Numerics.fsx">like this</a>?</li>
<li>mess with your coworkers’ mental sanity, by executing <code>(*</code> (opening a multiline comment) in FSI? (credit: <a href="https://twitter.com/tomaspetricek">Tomas</a>)</li>
<li>simplify loading references with Visual Studio and Power Tools? (credit: <a href="https://twitter.com/kitlovesfsharp">Kit Eason</a>, see details in comments section).</li>
</ul>
<p>And again… if you are not using the <a href="http://fsprojects.github.io/VisualFSharpPowerTools/">Visual F# Power Tools</a>, you are missing out:</p>
<blockquote>
<p>“Don’t let your friends try <a href="https://twitter.com/hashtag/fsharp?src=hash">#fsharp</a> without installing <a href="https://twitter.com/FSPowerTools">@FSPowerTools</a>.” <a href="https://twitter.com/dsyme">@dsyme</a> at <a href="https://twitter.com/hashtag/ndclondon?src=hash">#ndclondon</a></p>
<p>— Tomas Petricek (@tomaspetricek) <a href="https://twitter.com/tomaspetricek/status/687934127627186176">January 15, 2016</a></p>
</blockquote>
<p>That’s what I got! I am sure I forgot some - do you have a useful or favorite trick to share?</p>
]]></content>
<tags>
<tag>Technique</tag>
<tag>Archive</tag>
</tags>
</entry>
<entry>
<title>A Brief History of Just-In-Time</title>
<url>/2023/09/28/A-Brief-History-of-Just-In-Time/</url>
<content><![CDATA[<p>Software systems have been using “just-in-time” compilation (JIT) techniques since the<br>
1960s. Broadly, JIT compilation includes any translation performed dynamically, after a<br>
program has started execution. We examine the motivation behind JIT compilation and<br>
constraints imposed on JIT compilation systems, and present a classification scheme for<br>
such systems. This classification emerges as we survey forty years of JIT work, from<br>
1960–2000.</p>
<ul>
<li><a href="/papers/A%20Brief%20History%20of%20Just-In-Time.pdf">论文链接</a></li>
</ul>
<p>主要介绍了JIT的历史和发展过程, 从上世纪六十年代开始, 一直到现在的第四代JIT系统, 大概有三点:</p>
<ul>
<li>介绍了JIT每一代出现的动机和约束条件</li>
<li>介绍了对各种JIT技术的分类方式
<blockquote>
<ol>
<li>Invocation</li>
<li>Executability</li>
<li>Concurrency</li>
</ol>
</blockquote>
</li>
<li>介绍了各个时期JIT技术的实现方式和优缺点</li>
</ul>
<p>除此之外还介绍了JIT的工具包以及它们在三个不同的方面的支持程度, 分别是</p>
<ul>
<li>Binary code generation.</li>
<li>Cache coherence.</li>
<li>Execution.</li>
</ul>
<p>总体来说,这篇论文算是一个概览,可以当成一个JIT相关论文的查询目录了,它把这过程中的所有研究全部以时间线的方式梳理起来,最后总结分类。</p>
]]></content>
<tags>
<tag>Technique</tag>
</tags>
</entry>
<entry>
<title>Adolescent Onset of Acute Heart Failure</title>
<url>/2023/11/21/Adolescent-Onset-of-Acute-Heart-Failure/</url>
<content><![CDATA[<p>心力衰竭一般是常见于成人的,主要继发于高血压和冠状动脉疾病。 青少年的病因很广泛,包括心肌炎、心肌病、结构性心脏病、营养缺乏、代谢紊乱、高血压危象和心律失常等。在发达国家,先天性心脏病和心肌病是儿童心力衰竭的两大主要原因,而在发展中国家,主要原因包括感染和严重贫血。 尽管原因不同,但青少年心力衰竭的表现与成人相似。常见症状包括呼吸困难、外周水肿、端坐呼吸、咳嗽、喘息和继发于水肿的阵发性夜间呼吸困难,但也可能包括非特异性症状,如疲劳、恶心、厌食或液体潴留导致的体重增加。表现形式差异很大,包括无明显症状、充血性心力衰竭体征,极端情况下表现为心源性休克。同样,体检也可能存在很大差异。如果患者患有代偿性心力衰竭,则可以进行相对良性的检查。</p>
<p>心力衰竭的一些不易被察觉但高度相关的检查结果包括有:心动过速、心音异常(奔马律、舒张期杂音或病理性收缩期杂音)、呼吸急促和肝肿大。胸部X光片和心电图(ECG)可能显示非特异性发现。胸片可显示心脏肥大伴肺充血。心电图结果通常是非特异性的。 这篇文章对青少年心力衰竭的罕见和常见原因进行了概述。</p>
<ul>
<li><a href="/papers/Approach%20to%20Idiopathic%20Anaphylaxis%20in%20Adolescents.pdf">原始文章</a></li>
</ul>
<h2 id="其他"><a class="header-anchor" href="#其他">¶</a>其他</h2>
<p>端坐呼吸或端坐呼吸是平躺时发生的呼吸短促,导致患者不得不支撑在床上或坐在椅子上睡觉。它通常被视为心力衰竭的晚期表现,是由于液体重新分布到中央循环,导致肺毛细血管压力增加并导致呼吸困难所致。它也见于腹部肥胖或肺部疾病的情况。端坐呼吸与平呼吸相反,平呼吸是指坐或站直时呼吸急促会加重。</p>
]]></content>
<tags>
<tag>Medicine</tag>
</tags>
</entry>
<entry>
<title>Advanced C binding using ocaml-ctypes and dune</title>
<url>/2024/09/15/Advanced-C-binding-using-ocaml-ctypes-and-dune/</url>
<content><</code> C API.</p>
<p>I had already decided to use the very elegant <code>[ocaml-ctypes](https://github.com/ocamllabs/ocaml-ctypes)</code> module for the SRT binding so I went with it and created a <code>[ocaml-sys-socket](https://github.com/toots/ocaml-sys-socket)</code> module using it as well. It was a very interesting experience that I would like to describe here!</p>
<h1>ocaml-ctypes</h1>
<p>The idea behind OCaml ctypes is to create a binding against a C library without having to write C code, or as least as possible. The most straight-forward way of using it is via <code>[libffi](https://github.com/libffi/libffi)</code> , providing access to dynamically-loaded libraries.</p>
<p>The second way of using it is by letting the module generate the basic C stubs required to build and link against a shared library. This is the mode that we’re going to use here. In this mode, the programmer has to describe the C headers of the library they intent to bind to using dedicated OCaml modules, operators and types. From that description, ocaml-ctypes is able to generate the required glue for the binding.</p>
<p>One advantage of using ocaml-ctypes is that the created bindings make as few assumptions as possible about the <a href="https://caml.inria.fr/pub/docs/manual-ocaml/intfc.html">OCaml C interfacing API</a>. This is pretty nice, in particular since the <a href="https://github.com/ocaml/ocaml">OCaml compiler</a> is moving pretty quickly these days (which is awesome!) and also if, perhaps one day, <a href="https://github.com/ocaml-multicore/ocaml-multicore">support for multi-core</a> is added to the compiler, which will undoubtedly change the C interface API quite a bit.</p>
<h1>dune</h1>
<p><code>[dune](https://github.com/ocaml/dune)</code> (formally <code>jbuilder</code> ) is a build system for OCaml projects that has recently raised to much popularity, particularly due to its tight integration with the rest of the OCaml ecosystem, such as <code>[ocamlfind](http://projects.camlcity.org/projects/findlib.html)</code> and <code>[opam](https://opam.ocaml.org/)</code> .</p>
<p>My personal motto in programming in general is that <em>“Simple things should be simple, but complex things should be possible”</em>. <code>dune</code> certainly does not fit into that category but, rather, makes some complex things extremely easy to setup. It’s the kind of tool that will make your life incredibly easier when what you intent to do fits well within their workflow but might not be easy to bend to some very specific niche use. We will see one such case below.</p>
<p>At any rate, it’s been an amazing experience getting to learn how to use <code>dune</code> and the resulting code and build system is remarkably short and elegant, yet very powerful.</p>
<h1>socket.h</h1>
<p><code>socket.h</code> is the Unix header that describes the C API to various socket operations, IP version 4 and 6 as well as unix file sockets. There is also a windows API mimicking it, which makes most code using it easily portable to windows.</p>
<p>Most network-based C libraries refer to <code>socket.h</code> to describe the type of socket that can be used with their API so it’s an important entry point for a lot of network operations and one that would be nice to support as generically as possible in OCaml.</p>
<p>The catch, though, is that, most likely for historical reasons¹, the <a href="https://pubs.opengroup.org/onlinepubs/7908799/xns/syssocket.h.html">POSIX specifications</a> only <em>partially</em> defines some of the required data structures and types, which makes it possible to write C code using them but does not give enough information to write C bindings without having to use the compiler to parse the actual system-specific headers of the running host.</p>
<p>For instance, here’s how the <code>sockaddr</code> structure is specified:</p>
<p>The <em><sys/socket.h></em> header defines the sockaddr structure that includes at least the following members:sa_family_t sa_family address family<br>
char sa_data[] socket address (variable-length data)</p>
<p>Likewise, here’s what is specified about the size of the <code>socklen_t</code> data type:</p>
<p><em><sys/socket.h></em> makes available a type, socklen_t, which is an unsigned opaque integral type of length of at least 32 bits.</p>
<p>Thus, in order to know the exact offset of <code>sa_family</code> inside the <code>sockaddr</code> structure or the actual size of a <code>socklen_t</code> integer, one has to include the OS-specific header, parse its definitions for that specific OS and, only then, is it possible to compute that offset or data size. Let’s see how it’s done in our binding now!</p>
<h1>Putting it together</h1>
<p>The C binding requires 4 separate passes:</p>
<ul>
<li>The <code>[constants](https://github.com/toots/ocaml-sys-socket/tree/master/src/sys-socket/constants)</code> pass, which computes and exports some specific constant and data sizes, computed from the C headers</li>
<li>The <code>[types](https://github.com/toots/ocaml-sys-socket/tree/master/src/sys-socket/types)</code> pass, which, given the system-specific constants and sizes exported in the previous phase, defines the actual C data structure bindings.</li>
<li>The <code>[stubs](https://github.com/toots/ocaml-sys-socket/tree/master/src/sys-socket/stubs)</code> pass, where we define the actual bindings to the C functions that we wish to export in our API.</li>
<li>Finally, the <a href="https://github.com/toots/ocaml-sys-socket/blob/master/src/sys-socket/sys_socket.mli">last pass</a> does a cleanup of the <code>stubs</code> pass to export a relevant and OCaml- (and <code>ocaml-ctypes</code>) specific public API that is to be used by users of the module.</li>
</ul>
<p><code>dune</code> makes each of these steps fairly easy to integrate into the next one, defining compilation elements and binaries to build before moving to the next pass.</p>
<h2 id="Constants-pass"><a class="header-anchor" href="#Constants-pass">¶</a>Constants pass</h2>
<p>During that pass, we compute and export all required C values defined in the headers. We also add our own constants, which give us the sizes that the POSIX specifications leave up to the OS. Here’s the OCaml code for it:</p>
<figure class="highlight ocaml"><table><tr><td class="code"><pre><span class="line"><span class="keyword">module</span> <span class="type">Def</span> (<span class="type">S</span> : <span class="type">Cstubs</span>.<span class="type">Types</span>.<span class="type">TYPE</span>) = <span class="keyword">struct</span></span><br><span class="line"> <span class="keyword">let</span> af_inet = <span class="type">S</span>.constant <span class="string">"AF_INET"</span> <span class="type">S</span>.<span class="built_in">int</span></span><br><span class="line"> <span class="keyword">let</span> af_inet6 = <span class="type">S</span>.constant <span class="string">"AF_INET6"</span> <span class="type">S</span>.<span class="built_in">int</span></span><br><span class="line"> <span class="keyword">let</span> af_unix = <span class="type">S</span>.constant <span class="string">"AF_UNIX"</span> <span class="type">S</span>.<span class="built_in">int</span></span><br><span class="line"> <span class="keyword">let</span> af_unspec = <span class="type">S</span>.constant <span class="string">"AF_UNSPEC"</span> <span class="type">S</span>.<span class="built_in">int</span></span><br><span class="line"> <span class="keyword">let</span> sa_data_len = <span class="type">S</span>.constant <span class="string">"SA_DATA_LEN"</span> <span class="type">S</span>.<span class="built_in">int</span></span><br><span class="line"> <span class="keyword">let</span> sa_family_len = <span class="type">S</span>.constant <span class="string">"SA_FAMILY_LEN"</span> <span class="type">S</span>.<span class="built_in">int</span></span><br><span class="line"> <span class="keyword">let</span> sock_dgram = <span class="type">S</span>.constant <span class="string">"SOCK_DGRAM"</span> <span class="type">S</span>.<span class="built_in">int</span></span><br><span class="line"> <span class="keyword">let</span> sock_stream = <span class="type">S</span>.constant <span class="string">"SOCK_STREAM"</span> <span class="type">S</span>.<span class="built_in">int</span></span><br><span class="line"> <span class="keyword">let</span> sock_seqpacket = <span class="type">S</span>.constant <span class="string">"SOCK_STREAM"</span> <span class="type">S</span>.<span class="built_in">int</span></span><br><span class="line"> <span class="keyword">let</span> socklen_t_len = <span class="type">S</span>.constant <span class="string">"SOCKLEN_T_LEN"</span> <span class="type">S</span>.<span class="built_in">int</span></span><br><span class="line"> <span class="keyword">let</span> ni_maxserv = <span class="type">S</span>.constant <span class="string">"NI_MAXSERV"</span> <span class="type">S</span>.<span class="built_in">int</span></span><br><span class="line"> <span class="keyword">let</span> ni_maxhost = <span class="type">S</span>.constant <span class="string">"NI_MAXHOST"</span> <span class="type">S</span>.<span class="built_in">int</span></span><br><span class="line"> <span class="keyword">let</span> ni_numerichost = <span class="type">S</span>.constant <span class="string">"NI_NUMERICHOST"</span> <span class="type">S</span>.<span class="built_in">int</span></span><br><span class="line"> <span class="keyword">let</span> ni_numericserv = <span class="type">S</span>.constant <span class="string">"NI_NUMERICSERV"</span> <span class="type">S</span>.<span class="built_in">int</span></span><br><span class="line"><span class="keyword">end</span></span><br></pre></td></tr></table></figure>
<p>Pretty straightforward! Some of these constants are defined by the POSIX headers and some are custom defined for our needs, for instance <code>SOCKLEN_T_LEN</code> . Here’s how they are extracted, using the <code>dune</code> build configuration for <code>[gen_constants_c](https://github.com/toots/ocaml-sys-socket/blob/master/src/sys-socket/generator/gen_constants_c.ml)</code>:</p>
<figure class="highlight ocaml"><table><tr><td class="code"><pre><span class="line"><span class="keyword">let</span> c_headers = <span class="string">"</span></span><br><span class="line"><span class="string">#ifdef _WIN32</span></span><br><span class="line"><span class="string"> #include <winsock2.h></span></span><br><span class="line"><span class="string"> #include <ws2tcpip.h></span></span><br><span class="line"><span class="string">#else</span></span><br><span class="line"><span class="string"> #include <sys/socket.h></span></span><br><span class="line"><span class="string"> #include <sys/un.h></span></span><br><span class="line"><span class="string"> #include <netdb.h></span></span><br><span class="line"><span class="string">#endif</span></span><br><span class="line"><span class="string">#define SA_DATA_LEN (sizeof(((struct sockaddr*)0)->sa_data))</span></span><br><span class="line"><span class="string">#define SA_FAMILY_LEN (sizeof(((struct sockaddr*)0)->sa_family))</span></span><br><span class="line"><span class="string">#define SOCKLEN_T_LEN (sizeof(socklen_t))</span></span><br><span class="line"><span class="string">#ifndef NI_MAXHOST</span></span><br><span class="line"><span class="string"> #define NI_MAXHOST 1025</span></span><br><span class="line"><span class="string">#endif</span></span><br><span class="line"><span class="string">#ifndef NI_MAXSERV</span></span><br><span class="line"><span class="string"> #define NI_MAXSERV 32</span></span><br><span class="line"><span class="string">#endif</span></span><br><span class="line"><span class="string">"</span></span><br><span class="line"><span class="keyword">let</span> <span class="literal">()</span> =</span><br><span class="line"> <span class="keyword">let</span> fname = <span class="type">Sys</span>.argv.(<span class="number">1</span>) <span class="keyword">in</span></span><br><span class="line"> <span class="keyword">let</span> oc = open_out_bin fname <span class="keyword">in</span></span><br><span class="line"> <span class="keyword">let</span> format =</span><br><span class="line"> <span class="type">Format</span>.formatter_of_out_channel oc</span><br><span class="line"> <span class="keyword">in</span></span><br><span class="line"> <span class="type">Format</span>.fprintf format <span class="string">"%s@\n"</span> c_headers;</span><br><span class="line"> <span class="type">Cstubs</span>.<span class="type">Types</span>.write_c format (<span class="keyword">module</span> <span class="type">Sys_socket_constants</span>.<span class="type">Def</span>);</span><br><span class="line"> <span class="type">Format</span>.pp_print_flush format <span class="literal">()</span>;</span><br><span class="line"> close_out oc</span><br></pre></td></tr></table></figure>
<p>This OCaml code makes use of <code>ocaml-ctypes</code> to build a binary that exports the OCaml interface defined by <code>Sys_socket_constants.Def</code> . Once compiled, its output looks like this:</p>
<figure class="highlight ocaml"><table><tr><td class="code"><pre><span class="line"><span class="keyword">include</span> <span class="type">Ctypes</span></span><br><span class="line"><span class="keyword">let</span> lift x = x</span><br><span class="line"><span class="keyword">open</span> <span class="type">Ctypes_static</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> <span class="keyword">rec</span> field : <span class="keyword">type</span> t a. t typ -> <span class="built_in">string</span> -> a typ -> (a, t) field =</span><br><span class="line"> <span class="keyword">fun</span> s fname ftype -> <span class="keyword">match</span> s, fname <span class="keyword">with</span></span><br><span class="line"> | <span class="type">View</span> { ty }, _ -></span><br><span class="line"> <span class="keyword">let</span> { ftype; foffset; fname } = field ty fname ftype <span class="keyword">in</span></span><br><span class="line"> { ftype; foffset; fname }</span><br><span class="line"> | _ -> failwith (<span class="string">"Unexpected field "</span>^ fname)</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> <span class="keyword">rec</span> seal : <span class="keyword">type</span> a. a typ -> <span class="built_in">unit</span> = <span class="keyword">function</span></span><br><span class="line"> | <span class="type">Struct</span> { tag; spec = <span class="type">Complete</span> _ } -></span><br><span class="line"> raise (<span class="type">ModifyingSealedType</span> tag)</span><br><span class="line"> | <span class="type">Union</span> { utag; uspec = <span class="type">Some</span> _ } -></span><br><span class="line"> raise (<span class="type">ModifyingSealedType</span> utag)</span><br><span class="line"> | <span class="type">View</span> { ty } -> seal ty</span><br><span class="line"> | _ -></span><br><span class="line"> raise (<span class="type">Unsupported</span> <span class="string">"Sealing a non-structured type"</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">type</span> <span class="symbol">'a</span> const = <span class="symbol">'a</span></span><br><span class="line"><span class="keyword">let</span> constant (<span class="keyword">type</span> t) name (t : t typ) : t = <span class="keyword">match</span> t, name <span class="keyword">with</span></span><br><span class="line"> | <span class="type">Ctypes_static</span>.<span class="type">Primitive</span> <span class="type">Cstubs_internals</span>.<span class="type">Int</span>, <span class="string">"NI_NUMERICSERV"</span> -></span><br><span class="line"> <span class="number">8</span></span><br><span class="line"> | <span class="type">Ctypes_static</span>.<span class="type">Primitive</span> <span class="type">Cstubs_internals</span>.<span class="type">Int</span>, <span class="string">"NI_NUMERICHOST"</span> -></span><br><span class="line"> <span class="number">2</span></span><br><span class="line"> | <span class="type">Ctypes_static</span>.<span class="type">Primitive</span> <span class="type">Cstubs_internals</span>.<span class="type">Int</span>, <span class="string">"NI_MAXHOST"</span> -></span><br><span class="line"> <span class="number">1025</span></span><br><span class="line"> | <span class="type">Ctypes_static</span>.<span class="type">Primitive</span> <span class="type">Cstubs_internals</span>.<span class="type">Int</span>, <span class="string">"NI_MAXSERV"</span> -></span><br><span class="line"> <span class="number">32</span></span><br><span class="line"> | <span class="type">Ctypes_static</span>.<span class="type">Primitive</span> <span class="type">Cstubs_internals</span>.<span class="type">Int</span>, <span class="string">"SOCKLEN_T_LEN"</span> -></span><br><span class="line"> <span class="number">4</span></span><br><span class="line"> | <span class="type">Ctypes_static</span>.<span class="type">Primitive</span> <span class="type">Cstubs_internals</span>.<span class="type">Int</span>, <span class="string">"SOCK_STREAM"</span> -></span><br><span class="line"> <span class="number">1</span></span><br><span class="line"> | <span class="type">Ctypes_static</span>.<span class="type">Primitive</span> <span class="type">Cstubs_internals</span>.<span class="type">Int</span>, <span class="string">"SOCK_STREAM"</span> -></span><br><span class="line"> <span class="number">1</span></span><br><span class="line"> | <span class="type">Ctypes_static</span>.<span class="type">Primitive</span> <span class="type">Cstubs_internals</span>.<span class="type">Int</span>, <span class="string">"SOCK_DGRAM"</span> -></span><br><span class="line"> <span class="number">2</span></span><br><span class="line"> | <span class="type">Ctypes_static</span>.<span class="type">Primitive</span> <span class="type">Cstubs_internals</span>.<span class="type">Int</span>, <span class="string">"SA_FAMILY_LEN"</span> -></span><br><span class="line"> <span class="number">1</span></span><br><span class="line"> | <span class="type">Ctypes_static</span>.<span class="type">Primitive</span> <span class="type">Cstubs_internals</span>.<span class="type">Int</span>, <span class="string">"SA_DATA_LEN"</span> -></span><br><span class="line"> <span class="number">14</span></span><br><span class="line"> | <span class="type">Ctypes_static</span>.<span class="type">Primitive</span> <span class="type">Cstubs_internals</span>.<span class="type">Int</span>, <span class="string">"AF_UNSPEC"</span> -></span><br><span class="line"> <span class="number">0</span></span><br><span class="line"> | <span class="type">Ctypes_static</span>.<span class="type">Primitive</span> <span class="type">Cstubs_internals</span>.<span class="type">Int</span>, <span class="string">"AF_UNIX"</span> -></span><br><span class="line"> <span class="number">1</span></span><br><span class="line"> | <span class="type">Ctypes_static</span>.<span class="type">Primitive</span> <span class="type">Cstubs_internals</span>.<span class="type">Int</span>, <span class="string">"AF_INET6"</span> -></span><br><span class="line"> <span class="number">30</span></span><br><span class="line"> | <span class="type">Ctypes_static</span>.<span class="type">Primitive</span> <span class="type">Cstubs_internals</span>.<span class="type">Int</span>, <span class="string">"AF_INET"</span> -></span><br><span class="line"> <span class="number">2</span></span><br><span class="line"> | _, s -> failwith (<span class="string">"unmatched constant: "</span>^ s)</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> enum (<span class="keyword">type</span> a) name ?typedef ?unexpected (alist : (a * <span class="built_in">int64</span>) <span class="built_in">list</span>) =</span><br><span class="line"> <span class="keyword">match</span> name <span class="keyword">with</span></span><br><span class="line"> | s -></span><br><span class="line"> failwith (<span class="string">"unmatched enum: "</span>^ s)</span><br></pre></td></tr></table></figure>
<p>The files used to describe how to build this binary using <code>dune</code> are located in a separate <code>[generator](https://github.com/toots/ocaml-sys-socket/tree/master/src/sys-socket/generator)</code> directory. Here’s the entry to build this one:</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">(executable</span><br><span class="line"> (name gen_constants_c)</span><br><span class="line"> (modules gen_constants_c)</span><br><span class="line"> (libraries sys-socket.constants ctypes.stubs))</span><br><span class="line"></span><br><span class="line">(rule</span><br><span class="line"> (targets gen_constants.c)</span><br><span class="line"> (deps (:gen ./gen_constants_c.exe))</span><br><span class="line"> (action (run %{gen} %{targets})))</span><br><span class="line"></span><br><span class="line">(rule</span><br><span class="line"> (targets gen_constants_c)</span><br><span class="line"> (deps (:c_code ./gen_constants.c))</span><br><span class="line"> (action (run %{ocaml-config:c_compiler} -I %{lib:ctypes:} -I %{ocaml-config:standard_library} -o %{targets} %{c_code})))</span><br></pre></td></tr></table></figure>
<p>This executable is compiled during the next phase. Let’s move into it now!</p>
<h2 id="Types-pass"><a class="header-anchor" href="#Types-pass">¶</a>Types pass</h2>
<p>During that phase, we use the constants exported during the previous phase to describe the various C structures and types. This is by far the most complex part of the code, making use of first-class modules and several OCaml tricks.</p>
<p>First, let’s look at how we tell <code>dune</code> that we need to generate the <code>.ml</code> file exporting our required constants from the previous pass:</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">(rule</span><br><span class="line"> (targets sys_socket_generated_constants.ml)</span><br><span class="line"> (deps (:exec ../generator/exec.sh)</span><br><span class="line"> (:gen ../generator/gen_constants_c))</span><br><span class="line"> (action (with-stdout-to %{targets}</span><br><span class="line"> (system "%{exec} %{ocaml-config:system} %{gen}"))))</span><br></pre></td></tr></table></figure>
<p>With only this information, if the code refers to a <code>Sys_socket_generated_constants</code> module, <code>dune</code> will know that this module needs to be generated and how to do it. We will explain later the use of the <code>exec.sh</code> wrapper here.</p>
<p>Now that we can make use of the exported constants in our OCaml code, let’s see how we define the <code>Socklen</code> module, exporting abstract types and interface to use <code>socklen_t</code> integers:</p>
<figure class="highlight ocaml"><table><tr><td class="code"><pre><span class="line"><span class="keyword">module</span> <span class="type">Constants</span> = <span class="type">Sys_socket_constants</span>.<span class="type">Def</span>(<span class="type">Sys_socket_generated_constants</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">module</span> <span class="keyword">type</span> <span class="type">Socklen</span> = <span class="keyword">functor</span> (<span class="type">S</span> : <span class="type">Cstubs</span>.<span class="type">Types</span>.<span class="type">TYPE</span>) -> <span class="keyword">sig</span></span><br><span class="line"> <span class="keyword">type</span> socklen</span><br><span class="line"> <span class="keyword">val</span> socklen_t : socklen <span class="type">S</span>.typ</span><br><span class="line"> <span class="keyword">val</span> int_of_socklen : socklen -> <span class="built_in">int</span></span><br><span class="line"> <span class="keyword">val</span> socklen_of_int : <span class="built_in">int</span> -> socklen</span><br><span class="line"><span class="keyword">end</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> socklen : (<span class="keyword">module</span> <span class="type">Socklen</span>) =</span><br><span class="line"> <span class="keyword">match</span> <span class="type">Constants</span>.socklen_t_len <span class="keyword">with</span></span><br><span class="line"> | <span class="number">4</span> -> (<span class="keyword">module</span> <span class="keyword">functor</span> (<span class="type">S</span> : <span class="type">Cstubs</span>.<span class="type">Types</span>.<span class="type">TYPE</span>) -> <span class="keyword">struct</span></span><br><span class="line"> <span class="keyword">type</span> socklen = <span class="type">Unsigned</span>.uint32</span><br><span class="line"> <span class="keyword">let</span> socklen_t = <span class="type">S</span>.uint32_t</span><br><span class="line"> <span class="keyword">let</span> int_of_socklen = <span class="type">Unsigned</span>.<span class="type">UInt32</span>.to_int</span><br><span class="line"> <span class="keyword">let</span> socklen_of_int = <span class="type">Unsigned</span>.<span class="type">UInt32</span>.of_int</span><br><span class="line"> <span class="keyword">end</span>)</span><br><span class="line"> | <span class="number">8</span> -> (<span class="keyword">module</span> <span class="keyword">functor</span> (<span class="type">S</span> : <span class="type">Cstubs</span>.<span class="type">Types</span>.<span class="type">TYPE</span>) -> <span class="keyword">struct</span></span><br><span class="line"> <span class="keyword">type</span> socklen = <span class="type">Unsigned</span>.uint64</span><br><span class="line"> <span class="keyword">let</span> socklen_t = <span class="type">S</span>.uint64_t</span><br><span class="line"> <span class="keyword">let</span> int_of_socklen = <span class="type">Unsigned</span>.<span class="type">UInt64</span>.to_int</span><br><span class="line"> <span class="keyword">let</span> socklen_of_int = <span class="type">Unsigned</span>.<span class="type">UInt64</span>.of_int</span><br><span class="line"> <span class="keyword">end</span>)</span><br><span class="line"> | _ -> <span class="keyword">assert</span> <span class="literal">false</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">module</span> <span class="type">Socklen</span> = (<span class="keyword">val</span> socklen : <span class="type">Socklen</span>)</span><br></pre></td></tr></table></figure>
<p>As you can see, we make use of first-order modules and the size of the <code>socklen_t</code> integer to define the right API for the compiling host. Now let’s see how we define the <code>sockaddr</code> interface:</p>
<figure class="highlight ocaml"><table><tr><td class="code"><pre><span class="line"><span class="keyword">module</span> <span class="keyword">type</span> <span class="type">SaFamily</span> = <span class="keyword">sig</span></span><br><span class="line"> <span class="keyword">type</span> sa_family</span><br><span class="line"> <span class="keyword">val</span> int_of_sa_family : sa_family -> <span class="built_in">int</span></span><br><span class="line"> <span class="keyword">val</span> sa_family_of_int : <span class="built_in">int</span> -> sa_family</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">module</span> <span class="type">T</span> : <span class="keyword">functor</span> (<span class="type">S</span> : <span class="type">Cstubs</span>.<span class="type">Types</span>.<span class="type">TYPE</span>) -> <span class="keyword">sig</span></span><br><span class="line"> <span class="keyword">val</span> t : sa_family <span class="type">S</span>.typ</span><br><span class="line"> <span class="keyword">end</span></span><br><span class="line"><span class="keyword">end</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> saFamily : (<span class="keyword">module</span> <span class="type">SaFamily</span>) =</span><br><span class="line"> <span class="keyword">match</span> <span class="type">Constants</span>.sa_family_len <span class="keyword">with</span></span><br><span class="line"> | <span class="number">1</span> -> (<span class="keyword">module</span> <span class="keyword">struct</span></span><br><span class="line"> <span class="keyword">type</span> sa_family = <span class="type">Unsigned</span>.uint8</span><br><span class="line"> <span class="keyword">let</span> int_of_sa_family = <span class="type">Unsigned</span>.<span class="type">UInt8</span>.to_int</span><br><span class="line"> <span class="keyword">let</span> sa_family_of_int = <span class="type">Unsigned</span>.<span class="type">UInt8</span>.of_int </span><br><span class="line"> <span class="keyword">module</span> <span class="type">T</span> (<span class="type">S</span> : <span class="type">Cstubs</span>.<span class="type">Types</span>.<span class="type">TYPE</span>) = <span class="keyword">struct</span></span><br><span class="line"> <span class="keyword">let</span> t = <span class="type">S</span>.uint8_t</span><br><span class="line"> <span class="keyword">end</span></span><br><span class="line"> <span class="keyword">end</span>)</span><br><span class="line">...</span><br><span class="line"></span><br><span class="line"><span class="keyword">module</span> <span class="type">SaFamily</span> = (<span class="keyword">val</span> saFamily : <span class="type">SaFamily</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">module</span> <span class="type">Def</span> (<span class="type">S</span> : <span class="type">Cstubs</span>.<span class="type">Types</span>.<span class="type">TYPE</span>) = <span class="keyword">struct</span></span><br><span class="line"> <span class="keyword">include</span> <span class="type">Constants</span></span><br><span class="line"></span><br><span class="line"> <span class="keyword">include</span> <span class="type">Socklen</span>(<span class="type">S</span>)</span><br><span class="line"></span><br><span class="line"> <span class="keyword">include</span> <span class="type">SaFamily</span></span><br><span class="line"></span><br><span class="line"> <span class="keyword">module</span> <span class="type">SaFamilyT</span> = <span class="type">SaFamily</span>.<span class="type">T</span>(<span class="type">S</span>)</span><br><span class="line"></span><br><span class="line"> <span class="keyword">let</span> sa_family_t = <span class="type">S</span>.typedef <span class="type">SaFamilyT</span>.t <span class="string">"sa_family_t"</span></span><br><span class="line"></span><br><span class="line"> <span class="keyword">module</span> <span class="type">Sockaddr</span> = <span class="keyword">struct</span></span><br><span class="line"> <span class="keyword">type</span> t = <span class="built_in">unit</span></span><br><span class="line"> <span class="keyword">let</span> t = <span class="type">S</span>.structure <span class="string">"sockaddr"</span></span><br><span class="line"> <span class="keyword">let</span> sa_family = <span class="type">S</span>.field t <span class="string">"sa_family"</span> sa_family_t</span><br><span class="line"> <span class="keyword">let</span> sa_data = <span class="type">S</span>.field t <span class="string">"sa_data"</span> (<span class="type">S</span>.<span class="built_in">array</span> sa_data_len <span class="type">S</span>.<span class="built_in">char</span>)</span><br><span class="line"> <span class="keyword">let</span> <span class="literal">()</span> = <span class="type">S</span>.seal t</span><br><span class="line"> <span class="keyword">end</span></span><br><span class="line"> </span><br><span class="line"> ...</span><br><span class="line"><span class="keyword">end</span></span><br></pre></td></tr></table></figure>
<p>Here, too, we make use of the size of <code>sa_family</code> as exported previously to define the right structure fields.</p>
<p>Next step, we need to compile this interface again to export the right offset for the various structures that have been defined. That’s <code>dune</code>’s job again!</p>
<p>First, the generator code:</p>
<figure class="highlight ocaml"><table><tr><td class="code"><pre><span class="line"><span class="keyword">let</span> c_headers = <span class="string">"</span></span><br><span class="line"><span class="string">#ifdef _WIN32</span></span><br><span class="line"><span class="string"> #include <winsock2.h></span></span><br><span class="line"><span class="string"> #include <ws2tcpip.h></span></span><br><span class="line"><span class="string">#else</span></span><br><span class="line"><span class="string"> #include <sys/socket.h></span></span><br><span class="line"><span class="string"> #include <sys/un.h></span></span><br><span class="line"><span class="string"> #include <netinet/in.h></span></span><br><span class="line"><span class="string"> #include <netdb.h></span></span><br><span class="line"><span class="string">#endif</span></span><br><span class="line"><span class="string">"</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> <span class="literal">()</span> =</span><br><span class="line"> <span class="keyword">let</span> fname = <span class="type">Sys</span>.argv.(<span class="number">1</span>) <span class="keyword">in</span></span><br><span class="line"> <span class="keyword">let</span> oc = open_out_bin fname <span class="keyword">in</span></span><br><span class="line"> <span class="keyword">let</span> format =</span><br><span class="line"> <span class="type">Format</span>.formatter_of_out_channel oc</span><br><span class="line"> <span class="keyword">in</span></span><br><span class="line"> <span class="type">Format</span>.fprintf format <span class="string">"%s@\n"</span> c_headers;</span><br><span class="line"> <span class="type">Cstubs</span>.<span class="type">Types</span>.write_c format (<span class="keyword">module</span> <span class="type">Sys_socket_types</span>.<span class="type">Def</span>);</span><br><span class="line"> <span class="type">Format</span>.pp_print_flush format <span class="literal">()</span>;</span><br><span class="line"> close_out oc</span><br></pre></td></tr></table></figure>
<p>And the build instructions:</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">(executable</span><br><span class="line"> (name gen_types_c)</span><br><span class="line"> (modules gen_types_c)</span><br><span class="line"> (libraries sys-socket.types ctypes.stubs))</span><br><span class="line"></span><br><span class="line">(rule</span><br><span class="line"> (targets gen_types.c)</span><br><span class="line"> (deps (:gen ./gen_types_c.exe))</span><br><span class="line"> (action (run %{gen} %{targets})))</span><br><span class="line"></span><br><span class="line">(rule</span><br><span class="line"> (targets gen_types_c)</span><br><span class="line"> (deps (:c_code ./gen_types.c))</span><br><span class="line"> (action (run %{ocaml-config:c_compiler} -I %{lib:ctypes:} -I %{ocaml-config:standard_library} -o %{targets} %{c_code})))</span><br></pre></td></tr></table></figure>
<p>Once, compiled, the exported <code>.ml</code> looks like this:</p>
<figure class="highlight ocaml"><table><tr><td class="code"><pre><span class="line"><span class="keyword">include</span> <span class="type">Ctypes</span></span><br><span class="line"><span class="keyword">let</span> lift x = x</span><br><span class="line"><span class="keyword">open</span> <span class="type">Ctypes_static</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> <span class="keyword">rec</span> field : <span class="keyword">type</span> t a. t typ -> <span class="built_in">string</span> -> a typ -> (a, t) field =</span><br><span class="line"> <span class="keyword">fun</span> s fname ftype -> <span class="keyword">match</span> s, fname <span class="keyword">with</span></span><br><span class="line">...</span><br><span class="line"> | <span class="type">Struct</span> ({ tag = <span class="string">"sockaddr"</span>} <span class="keyword">as</span> s'), <span class="string">"sa_data"</span> -></span><br><span class="line"> <span class="keyword">let</span> f = {ftype; fname; foffset = <span class="number">2</span>} <span class="keyword">in</span></span><br><span class="line"> (s'.fields <- <span class="type">BoxedField</span> f :: s'.fields; f)</span><br><span class="line"> | <span class="type">Struct</span> ({ tag = <span class="string">"sockaddr"</span>} <span class="keyword">as</span> s'), <span class="string">"sa_family"</span> -></span><br><span class="line"> <span class="keyword">let</span> f = {ftype; fname; foffset = <span class="number">1</span>} <span class="keyword">in</span></span><br><span class="line"> (s'.fields <- <span class="type">BoxedField</span> f :: s'.fields; f)</span><br><span class="line"> | <span class="type">View</span> { ty }, _ -></span><br><span class="line"> <span class="keyword">let</span> { ftype; foffset; fname } = field ty fname ftype <span class="keyword">in</span></span><br><span class="line"> { ftype; foffset; fname }</span><br><span class="line"> | _ -> failwith (<span class="string">"Unexpected field "</span>^ fname)</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> <span class="keyword">rec</span> seal : <span class="keyword">type</span> a. a typ -> <span class="built_in">unit</span> = <span class="keyword">function</span></span><br><span class="line">...</span><br><span class="line"> | <span class="type">Struct</span> ({ tag = <span class="string">"sockaddr_storage"</span>; spec = <span class="type">Incomplete</span> _ } <span class="keyword">as</span> s') -></span><br><span class="line"> s'.spec <- <span class="type">Complete</span> { size = <span class="number">128</span>; align = <span class="number">8</span> }</span><br><span class="line"> | <span class="type">Struct</span> ({ tag = <span class="string">"sockaddr"</span>; spec = <span class="type">Incomplete</span> _ } <span class="keyword">as</span> s') -></span><br><span class="line"> s'.spec <- <span class="type">Complete</span> { size = <span class="number">16</span>; align = <span class="number">1</span> }</span><br><span class="line"> | <span class="type">Struct</span> { tag; spec = <span class="type">Complete</span> _ } -></span><br><span class="line"> raise (<span class="type">ModifyingSealedType</span> tag)</span><br><span class="line"> | <span class="type">Union</span> { utag; uspec = <span class="type">Some</span> _ } -></span><br><span class="line"> raise (<span class="type">ModifyingSealedType</span> utag)</span><br><span class="line"> | <span class="type">View</span> { ty } -> seal ty</span><br><span class="line"> | _ -></span><br><span class="line"> raise (<span class="type">Unsupported</span> <span class="string">"Sealing a non-structured type"</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">type</span> <span class="symbol">'a</span> const = <span class="symbol">'a</span></span><br><span class="line"><span class="keyword">let</span> constant (<span class="keyword">type</span> t) name (t : t typ) : t = <span class="keyword">match</span> t, name <span class="keyword">with</span></span><br><span class="line"> | _, s -> failwith (<span class="string">"unmatched constant: "</span>^ s)</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> enum (<span class="keyword">type</span> a) name ?typedef ?unexpected (alist : (a * <span class="built_in">int64</span>) <span class="built_in">list</span>) =</span><br><span class="line"> <span class="keyword">match</span> name <span class="keyword">with</span></span><br><span class="line"> | s -></span><br><span class="line"> failwith (<span class="string">"unmatched enum: "</span>^ s)</span><br></pre></td></tr></table></figure>
<p>As you can see, this exports all the offsets required to access the fields inside a <code>sockaddr_t</code> structure. We’re now ready to move to the final stage, which is the actual binding stubs!</p>
<h2 id="Binding-stubs"><a class="header-anchor" href="#Binding-stubs">¶</a>Binding stubs</h2>
<p>First step in this pass, just like with the previous ones, we need to configure <code>dune</code> to be able to build the exported <code>.ml</code> code from the <code>types</code> pass:</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">(rule</span><br><span class="line"> (targets sys_socket_generated_types.ml)</span><br><span class="line"> (deps (:exec ../generator/exec.sh)</span><br><span class="line"> (:gen ../generator/gen_types_c))</span><br><span class="line"> (action (with-stdout-to %{targets}</span><br><span class="line"> (system "%{exec} %{ocaml-config:system} %{gen}"))))</span><br></pre></td></tr></table></figure>
<p>And we can now define the proper bindings. Here’s how it looks like:</p>
<figure class="highlight ocaml"><table><tr><td class="code"><pre><span class="line"><span class="keyword">open</span> <span class="type">Ctypes</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">module</span> <span class="type">Def</span> (<span class="type">F</span> : <span class="type">Cstubs</span>.<span class="type">FOREIGN</span>) = <span class="keyword">struct</span></span><br><span class="line"> <span class="keyword">open</span> <span class="type">F</span></span><br><span class="line"></span><br><span class="line"> <span class="keyword">module</span> <span class="type">Types</span> = <span class="type">Sys_socket_types</span>.<span class="type">Def</span>(<span class="type">Sys_socket_generated_types</span>)</span><br><span class="line"></span><br><span class="line"> <span class="keyword">open</span> <span class="type">Types</span></span><br><span class="line"></span><br><span class="line"> <span class="keyword">let</span> getnameinfo = foreign <span class="string">"getnameinfo"</span> (ptr sockaddr_t @-> socklen_t @-> ptr <span class="built_in">char</span> @-> socklen_t @-> ptr <span class="built_in">char</span> @-> socklen_t @-> <span class="built_in">int</span> @-> (returning <span class="built_in">int</span>))</span><br><span class="line"></span><br><span class="line">...</span><br><span class="line"><span class="keyword">end</span></span><br></pre></td></tr></table></figure>
<p>As you can see, we’re exporting the <code>getnameinfo</code> function, taking various arguments, including a pointer to a <code>sockaddr_t</code> structure and a couple of <code>socklen_t</code> integers, making use of all the various data types and structures previously defined. The exact specifications of this function can be found <a href="https://pubs.opengroup.org/onlinepubs/009695399/functions/getnameinfo.html">here</a>. We can now define out top-level API…</p>
<h2 id="Final-API"><a class="header-anchor" href="#Final-API">¶</a>Final API</h2>
<p>Building upon the previous modules, we export various OCaml idiomatic APIs that the binding user can now use to build new bindings against the <code>socket.h</code> APIs.</p>
<p>Just like with the previous steps, first we need to configure the build system:</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">(rule</span><br><span class="line"> (targets sys_socket_generated_stubs.ml)</span><br><span class="line"> (deps (:gen ./generator/gen_stubs.exe))</span><br><span class="line"> (action (run %{gen} ml %{targets})))</span><br><span class="line"></span><br><span class="line">(rule</span><br><span class="line"> (targets sys_socket_generated_stubs.c)</span><br><span class="line"> (deps (:gen ./generator/gen_stubs.exe))</span><br><span class="line"> (action (run %{gen} c %{targets})))</span><br></pre></td></tr></table></figure>
<p>This time, we need <code>ocaml-ctypes</code> to generate two compilation units: a <code>.ml</code> file describing the API exported during the <code>stubs</code> phase, as well as the C code to glue it with the C APIs. Here’s the code for that generator:</p>
<figure class="highlight ocaml"><table><tr><td class="code"><pre><span class="line"><span class="keyword">let</span> c_headers = <span class="string">"</span></span><br><span class="line"><span class="string">#ifdef _WIN32</span></span><br><span class="line"><span class="string"> #include <winsock2.h></span></span><br><span class="line"><span class="string"> #include <ws2tcpip.h></span></span><br><span class="line"><span class="string">#else</span></span><br><span class="line"><span class="string"> #include <sys/socket.h></span></span><br><span class="line"><span class="string"> #include <netinet/in.h></span></span><br><span class="line"><span class="string"> #include <arpa/inet.h></span></span><br><span class="line"><span class="string"> #include <netdb.h></span></span><br><span class="line"><span class="string">#endif</span></span><br><span class="line"><span class="string">#include <string.h></span></span><br><span class="line"><span class="string">"</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> <span class="literal">()</span> =</span><br><span class="line"> <span class="keyword">let</span> mode = <span class="type">Sys</span>.argv.(<span class="number">1</span>) <span class="keyword">in</span></span><br><span class="line"> <span class="keyword">let</span> fname = <span class="type">Sys</span>.argv.(<span class="number">2</span>) <span class="keyword">in</span></span><br><span class="line"> <span class="keyword">let</span> oc = open_out_bin fname <span class="keyword">in</span></span><br><span class="line"> <span class="keyword">let</span> format =</span><br><span class="line"> <span class="type">Format</span>.formatter_of_out_channel oc</span><br><span class="line"> <span class="keyword">in</span></span><br><span class="line"> <span class="keyword">let</span> fn =</span><br><span class="line"> <span class="keyword">match</span> mode <span class="keyword">with</span></span><br><span class="line"> | <span class="string">"ml"</span> -> <span class="type">Cstubs</span>.write_ml</span><br><span class="line"> | <span class="string">"c"</span> -></span><br><span class="line"> <span class="type">Format</span>.fprintf format <span class="string">"%s@\n"</span> c_headers;</span><br><span class="line"> <span class="type">Cstubs</span>.write_c</span><br><span class="line"> | _ -> <span class="keyword">assert</span> <span class="literal">false</span></span><br><span class="line"> <span class="keyword">in</span></span><br><span class="line"> fn ~concurrency:<span class="type">Cstubs</span>.unlocked format ~prefix:<span class="string">"sys_socket"</span> (<span class="keyword">module</span> <span class="type">Sys_socket_stubs</span>.<span class="type">Def</span>);</span><br><span class="line"> <span class="type">Format</span>.pp_print_flush format <span class="literal">()</span>;</span><br><span class="line"> close_out oc</span><br></pre></td></tr></table></figure>
<p>The exported <code>.ml</code> and <code>.c</code> files are omitted here for simplicity but the reader can generated them themselves from the <code>[ocaml-sys-socket](https://github.com/toots/ocaml-sys-socket)</code> repository if they are curious about their actual content.</p>
<p>We can now export our top-level API:</p>
<figure class="highlight ocaml"><table><tr><td class="code"><pre><span class="line"><span class="keyword">open</span> <span class="type">Ctypes</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">include</span> <span class="type">Sys_socket_types</span>.<span class="type">SaFamily</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">include</span> <span class="type">Sys_socket_stubs</span>.<span class="type">Def</span>(<span class="type">Sys_socket_generated_stubs</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">type</span> socklen = <span class="type">Types</span>.socklen</span><br><span class="line"><span class="keyword">let</span> socklen_t = <span class="type">Types</span>.socklen_t</span><br><span class="line"><span class="keyword">let</span> int_of_socklen = <span class="type">Types</span>.int_of_socklen</span><br><span class="line"><span class="keyword">let</span> socklen_of_int = <span class="type">Types</span>.socklen_of_int</span><br><span class="line"></span><br><span class="line"><span class="keyword">module</span> <span class="type">Sockaddr</span> = <span class="keyword">struct</span></span><br><span class="line"> <span class="keyword">include</span> <span class="type">Types</span>.<span class="type">Sockaddr</span></span><br><span class="line"> <span class="keyword">let</span> from_sockaddr_storage = from_sockaddr_storage t</span><br><span class="line"> <span class="keyword">let</span> sa_data_len = <span class="type">Types</span>.sa_data_len</span><br><span class="line"><span class="keyword">end</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> getnameinfo sockaddr_ptr =</span><br><span class="line"> <span class="keyword">let</span> maxhost = <span class="type">Types</span>.ni_maxhost <span class="keyword">in</span></span><br><span class="line"> <span class="keyword">let</span> s = allocate_n <span class="built_in">char</span> ~count:maxhost <span class="keyword">in</span></span><br><span class="line"> <span class="keyword">let</span> maxserv = <span class="type">Types</span>.ni_maxserv <span class="keyword">in</span></span><br><span class="line"> <span class="keyword">let</span> p = allocate_n <span class="built_in">char</span> ~count:maxserv <span class="keyword">in</span></span><br><span class="line"> <span class="keyword">match</span> getnameinfo sockaddr_ptr (socklen_of_int (sizeof sockaddr_t))</span><br><span class="line"> s (socklen_of_int maxhost) </span><br><span class="line"> p (socklen_of_int maxserv)</span><br><span class="line"> (<span class="type">Types</span>.ni_numerichost <span class="keyword">lor</span></span><br><span class="line"> <span class="type">Types</span>.ni_numericserv) <span class="keyword">with</span></span><br><span class="line"> | <span class="number">0</span> -></span><br><span class="line"> <span class="keyword">let</span> host =</span><br><span class="line"> <span class="keyword">let</span> length =</span><br><span class="line"> <span class="type">Unsigned</span>.<span class="type">Size_t</span>.to_int</span><br><span class="line"> (strnlen s (<span class="type">Unsigned</span>.<span class="type">Size_t</span>.of_int maxhost))</span><br><span class="line"> <span class="keyword">in</span></span><br><span class="line"> string_from_ptr s ~length</span><br><span class="line"> <span class="keyword">in</span></span><br><span class="line"> <span class="keyword">let</span> port =</span><br><span class="line"> <span class="keyword">let</span> length =</span><br><span class="line"> <span class="type">Unsigned</span>.<span class="type">Size_t</span>.to_int</span><br><span class="line"> (strnlen p (<span class="type">Unsigned</span>.<span class="type">Size_t</span>.of_int maxserv))</span><br><span class="line"> <span class="keyword">in</span></span><br><span class="line"> <span class="keyword">let</span> port =</span><br><span class="line"> string_from_ptr p ~length</span><br><span class="line"> <span class="keyword">in</span></span><br><span class="line"> <span class="keyword">try</span></span><br><span class="line"> int_of_string port</span><br><span class="line"> <span class="keyword">with</span> _ -></span><br><span class="line"> <span class="keyword">match</span> getservbyname p null <span class="keyword">with</span></span><br><span class="line"> | ptr <span class="keyword">when</span> is_null ptr -> failwith <span class="string">"getnameinfo"</span></span><br><span class="line"> | ptr -></span><br><span class="line"> <span class="type">Unsigned</span>.<span class="type">UInt16</span>.to_int</span><br><span class="line"> (ntohs (!@ (ptr |-> <span class="type">Types</span>.<span class="type">Servent</span>.s_port)))</span><br><span class="line"> <span class="keyword">in</span></span><br><span class="line"> host, port</span><br><span class="line"> | _ -> failwith <span class="string">"getnameinfo"</span></span><br><span class="line"></span><br><span class="line">...</span><br></pre></td></tr></table></figure>
<figure class="highlight ocaml"><table><tr><td class="code"><pre><span class="line"><span class="keyword">open</span> <span class="type">Ctypes</span></span><br><span class="line"></span><br><span class="line"><span class="comment">(** Ctypes routines for C type socklen_t. *)</span></span><br><span class="line"><span class="keyword">type</span> socklen</span><br><span class="line"><span class="keyword">val</span> socklen_t : socklen typ</span><br><span class="line"><span class="keyword">val</span> int_of_socklen : socklen -> <span class="built_in">int</span></span><br><span class="line"><span class="keyword">val</span> socklen_of_int : <span class="built_in">int</span> -> socklen</span><br><span class="line"></span><br><span class="line"><span class="comment">(** Generic sockaddr_t structure. *)</span></span><br><span class="line"><span class="keyword">module</span> <span class="type">Sockaddr</span> : <span class="keyword">sig</span></span><br><span class="line"> <span class="keyword">type</span> t</span><br><span class="line"> <span class="keyword">val</span> t : t structure typ</span><br><span class="line"> <span class="keyword">val</span> sa_family : (sa_family, t structure) field</span><br><span class="line"> <span class="keyword">val</span> sa_data : (<span class="built_in">char</span> carray, t structure) field</span><br><span class="line"> <span class="keyword">val</span> sa_data_len : <span class="built_in">int</span></span><br><span class="line"></span><br><span class="line"> <span class="keyword">val</span> from_sockaddr_storage : <span class="type">SockaddrStorage</span>.t structure ptr -> t structure ptr</span><br><span class="line"><span class="keyword">end</span></span><br><span class="line"></span><br><span class="line"><span class="comment">(** IP address conversion functions. *)</span></span><br><span class="line"><span class="keyword">val</span> getnameinfo : sockaddr ptr -> <span class="built_in">string</span> * <span class="built_in">int</span></span><br><span class="line"></span><br><span class="line">...</span><br></pre></td></tr></table></figure>
<p>That’s it! We now have <code>ocaml-ctypes</code> specific data types and structures that can be used to interface with the host’s native <code>socket.h</code> APIs. Note that we also worked on top of the original low-level binding to <code>getnameinfo</code> to export a higher-level function more idiomatic to the OCaml language.</p>
<h1>Lagniappe: cross-compilation to Windows</h1>
<p>On windows platforms, <code>liquidsoap</code> is compiled using <code>[ocaml-cross-windows](https://github.com/ocaml-cross/opam-cross-windows)</code> and, since windows does have compatible socket APIs, we wanted to also look at cross-compiling for the windows target, which is where we hit a snag on the current <code>dune</code> support.</p>
<p>The problem is that, at each intermediary steps, in the case of a cross-compilation, the compiled binaries need to use the target’s OS headers and not the host’s headers, otherwise we end up using offsets specific to e.g. Debian but for a windows binary.</p>
<p>In this case, this means that the compiled <code>.exe</code> binaries need to be windows binaries and that we need to execute them as windows native binaries, using <code>[wine](https://www.winehq.org/)</code> .</p>
<p><code>dune</code> has a truly amazing <a href="https://dune.readthedocs.io/en/latest/cross-compilation.html">support for cross-compiling</a>, which we do not cover here, but, unfortunately, its primitives for building and executing binaries do not yet cover this use case. Thus we had to trick it into compiling things the way we wanted to do, which why we are using the <code>exec.sh</code> wrapper. Here’s its code:</p>
<figure class="highlight sh"><table><tr><td class="code"><pre><span class="line"><span class="meta">#!/bin/sh</span></span><br><span class="line"></span><br><span class="line">SYSTEM=<span class="variable">$1</span></span><br><span class="line">CMD=<span class="variable">$2</span></span><br><span class="line">ARG=<span class="variable">$3</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> <span class="built_in">test</span> <span class="string">"<span class="variable">${SYSTEM}</span>"</span> = <span class="string">"mingw"</span>; <span class="keyword">then</span></span><br><span class="line"> wine <span class="variable">$CMD</span> <span class="variable">$ARG</span></span><br><span class="line"><span class="keyword">elif</span> <span class="built_in">test</span> <span class="string">"<span class="variable">${SYSTEM}</span>"</span> = <span class="string">"mingw64"</span>; <span class="keyword">then</span></span><br><span class="line"> wine64 <span class="variable">$CMD</span> <span class="variable">$ARG</span></span><br><span class="line"><span class="keyword">else</span></span><br><span class="line"> <span class="variable">$CMD</span> <span class="variable">$ARG</span></span><br></pre></td></tr></table></figure>
<p>Now, you can go back to the previous <code>dune</code> files and see how this wrapper allows to execute binaries according to the system that the corresponding <code>ocamlopt</code> compiler has been configured to build for.</p>
<h1>Conclusion</h1>
<p>It’s been a fun time working on this binding! It’s amazing to see the level of details that can be built through <code>ocaml-ctypes</code> using their provided primitives. Ultimately, the binding is very clean and elegant, with very few low-level assumptions.</p>
<p>Likewise, the simplicity and power of the <code>dune</code> build system makes this very fluid to build. Without it, each of the described steps above would have been much more painful to execute and compile.</p>
<p>[1]: My bet is that, at the time the POSIX specifications were being written, there we already several inconsistent <code>socket.h</code> headers out in the wild among the various historical UNIX flavors…</p>
]]></content>
<tags>
<tag>Technique</tag>
<tag>Archive</tag>
</tags>
</entry>
<entry>
<title>Building custom fibers library in FSharp</title>
<url>/2024/10/18/Building-custom-fibers-library-in-FSharp/</url>
<content><![CDATA[<p>Over the course of last few months on this blog post, I’ve been sharing about internals and how-to of different concurrency patters. We discussed how to <a href="https://www.bartoszsypytkowski.com/build-your-own-actor-model/">implement our own actors</a> and specific <a href="https://www.bartoszsypytkowski.com/thread-safety-with-affine-thread-pools/">affinity-based thread pool</a>. Today we’ll focus of the most dominant pattern present in modern programming nowadays: fibers, also known as coroutines, futures, tasks, green threads or user-space threads.</p>
<p>The general idea is simple - we want a fine-grained concurrency primitive, that will let us easily compose chain of operations in sequential manner. Of course we could use threads here, but the question is: are threads fine-grained? In many managed languages with OS threads exposed, they can be quite heavy eg. by default in .NET each thread takes around 1MB of memory and requires calling kernel code to cooperate with other threads, which is an expensive operation on its own.</p>
<p>What we’re after, are more lightweight structures (less than 1kB), that can live fully in a user space, so that we can have even millions of them cooperating frequently with each other without heavy performance penalties.</p>
<p>Before we begin, I think it’s good to discuss different designs. We’ll cover several different topics to be able to make more informed decisions, that we’re up to apply to our own solution.</p>
<h3 id="Preemptive-vs-cooperative-scheduler"><a class="header-anchor" href="#Preemptive-vs-cooperative-scheduler">¶</a>Preemptive vs cooperative scheduler</h3>
<p>Scheduler is a subsystem, which direct responsibility is to assign CPU core processing power to a particular fiber. It’s also responsible for coordinating fibers execution. The two most common categories of schedulers are preemptive and cooperative.</p>
<p>A preemptive scheduler is the one, that’s always in control of fiber execution. It’s able to decide on its own, when fiber can be started and stopped. The most obvious example of such is a thread scheduler existing on most operating systems.</p>
<p>Preemptive scheduler usually works in one of two ways:</p>
<ul>
<li>Time based scheduler takes a quant of CPU time and gives it to a given fiber, which ten can execute its logic until it reaches its execution time limit (of course, it can finish earlier). This is how OS thread scheduler, but also how Go goroutine scheduler works.</li>
<li>Another variant is step-based scheduler, which splits fiber’s function body into series of (more or less equal) steps. Then each fiber is given a number of steps to execute before preemption occurs. Example of such is Erlang’s BEAM - it simply allows each process to execute up to 2000 “reductions”, where each reduction is basically a function call. <em>And since in Erlang there are no loops, only tail-recursive functions, this approach works well for long-living iterative processes as well.</em></li>
</ul>
<p>One of the problems with preemptive schedulers is that they usually need some kind of involvement from the compiler or hosting virtual machine in order to work. For this reason, most of the fiber libraries use cooperative schedulers to perform their work.</p>
<p>A cooperative scheduler doesn’t have a concept of preemption - once started by the scheduler, a fiber will execute until it doesn’t give back the control willingly. This is often done with dedicated programming constructs, and often is known as yielding, parking or awaiting.</p>
<p>In cooperative variant, a fiber body is usually split into series of discrete steps, between which fiber gives control back to the scheduler.</p>
<p>Keep in mind that these two are not mutually exclusive - a preemptive scheduler often provides a way for a fiber to return control back to it when it’s known that fiber won’t be executing any longer eg. because it has been put to sleep for a while.</p>
<h3 id="Stackless-vs-stackful"><a class="header-anchor" href="#Stackless-vs-stackful">¶</a>Stackless vs. stackful</h3>
<p>A concept, that’s somewhat related to a topic above is the idea of stackless and stackful coroutines.</p>
<p>A stackful variant is aware of underlying execution stack and can preserve/restore parts of it when yielding/continuing a fiber. Examples of this approach could be Go, Lua, Python asyncio and in the future, also <a href="https://www.youtube.com/watch?v=NV46KFV1m-4&ref=bartoszsypytkowski.com">Java Loom</a> project. Implementing such option (if it’s not implemented by a runtime already) usually requires diving deep into low-level internals, since execution stack is not something that most managed languages offers the users to play with, and doing so without coordination with runtime can cause problems - like determining liveness of objects for GC purposes.</p>
<p>Stackless coroutine usually captures locals that we want to preserve as part of callback object (lambda), that is allocated as an object on the heap and scheduled on yield continuation. These steps are usually visible directly in code (eg. await in C#, Rust and JavaScript, but also joints of Scala for-comprehensions, bang-suffix in F# or Haskell do-notation), but sometimes can be implicit like in case of Kotlin. Take into account that while many languages offer syntax support for those constructs, it’s not explicitly necessary to work - take a look at JavaScript and <code>Promise.then</code> as an example.</p>
<p>Stackless coroutines usually construct their logic around one of two concepts:</p>
<ol>
<li>Finite state machines - this variant is usually faster and can be encoded manually (example of such case is Akka actors), but for a human eye it usually doesn’t really read as a sequential step-by-step program execution, unless it has some support from the compiler itself (see: C# and Rust).</li>
<li>Monadic sequencing via bind/flatMap operator, which is very popular in functional languages. While we cover it in more details in the rest of this blog post, for now it’s enough to say that it’s a way to chain callback-based behaviors together in a way, that resembles standard sequential code.</li>
</ol>
<p>For sure one of the advantages of stackful coroutines is that they’re <em>mono-colored</em>: you can yield/continue coroutine execution from within any other function, while in the stackless variant splits your world into <em>two-colored</em> functions - synchronous and asynchronous - where async one can be only called and yielded safely (without blocking underlying OS thread) from within another async function.</p>
<h3 id="Eager-vs-lazy-fibers"><a class="header-anchor" href="#Eager-vs-lazy-fibers">¶</a>Eager vs lazy fibers</h3>
<p>We already mentioned two important events in fiber execution life cycle - starting and parking. Here I briefly discuss about different design decisions on when to start a fiber execution.</p>
<p>Eager execution means, that fiber is started automatically after its creation. An example of such are Scala <code>Future[A]</code> and JavaScript <code>Promise</code>. Since execution process starts right away, we’re willingly resign from a certain degree of control over how or when to execute given fiber. Usually this is solved by wrapping a fiber creation into another function or lambda.</p>
<p>Lazy execution is much more common and preferred way of work, as it allows us to separate place where we want to define our asynchronous sequence of steps from the place, where the execution details are defined. It’s used in C# TPL as well as pretty much in all functional languages implementations (excluding Scala futures mentioned earlier).</p>
<h3 id="Interruption"><a class="header-anchor" href="#Interruption">¶</a>Interruption</h3>
<p>There are also few decisions regarding premature escaping the fiber execution, also known as interruption/cancelation: one of them requires passing special object - a token - between method calls and explicit checking for its completion. It is how C# Tasks work. However putting such requirement onto the API user can be cumbersome and error-prone option. Therefore pretty much every other coroutine library either allows to direcly interrupt a fiber or (like in case of F# Async) passes cancelation tokens and check if they were triggered under the hood.</p>
<h2 id="Implementation"><a class="header-anchor" href="#Implementation">¶</a>Implementation</h2>
<p>Since we talked a bit about various approaches, let’s get to the meat of this blog post: implementing our own coroutine library in F#. So, what properties will it have?:</p>
<ol>
<li>We use cooperative scheduling (we don’t want to tweak the compiler) of stackless fibers with support from F# computation expression for nice syntax.</li>
<li>We use simple approach by defining custom <code>bind</code> operator with support from F# computation expressions. No state machines.</li>
<li>We’ll use lazy invocation.</li>
<li>We’ll make use of implicitly passed cancelation tokens. We’ll handle them directly inside the linking code.</li>
</ol>
<p>All of these give us in very similar approach to that found inside of native F# <code>Async</code> data type. To begin with, we’ll simply define the shape of our fiber.</p>
<p>Underneath, pretty much every cooperative stackless coroutine approach uses callbacks to drive the flow of synchronous segments of code to be executed one after another. So what we need is a callback which takes a result of previous coroutine and schedules in within some context of execution:</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">type</span> <span class="title class_">Fiber</span><span class="operator"><</span><span class="symbol">'a</span><span class="operator">></span> <span class="operator">=</span> Fiber <span class="keyword">of</span> (ExecutionContext <span class="operator">-></span> FiberCallback<span class="operator"><</span><span class="symbol">'a</span><span class="operator">></span> <span class="operator">-></span> <span class="type">unit</span>)</span><br></pre></td></tr></table></figure>
<p>Here we’ll represent Fiber as a simple single-case discriminated union. We could as well define other specialized cases, like:</p>
<ul>
<li>Situation when coroutine is executed immediately and doesn’t need to be awaited on: think about variant of <code>ValueTask</code> from C# Task Parallel Library.</li>
<li>Case when coroutine fails - in that case we might want to store an artificial tracing context that would allow us to create nicely-formatted “stack traces”: since .NET Core 2.1, C# already provides similar solution however AFAIK it’s been solved differently.</li>
</ul>
<p>Ok, but what are <code>ExecutionContext</code> and <code>FiberCallback<'a></code>? Let’s start from callback. We can represent it as follows:</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">type</span> <span class="title class_">FiberCallback</span><span class="operator"><</span><span class="symbol">'a</span><span class="operator">></span> <span class="operator">=</span> FiberResult<span class="operator"><</span><span class="symbol">'a</span><span class="operator">></span> <span class="operator">-></span> unit</span><br></pre></td></tr></table></figure>
<p>It’s just a simple function, which takes result of previous fiber execution and handles it. What’s the <code>FiberResult<'a></code> then?</p>
<p>Our fiber can complete successfully (returning a value) or fail with an exception. We’ll be conservative here and won’t go into more typed world of <a href="http://degoes.net/articles/bifunctor-io?ref=bartoszsypytkowski.com">IO bifunctor</a>. We can easy define these possible outputs in F# using <code>Result<'a, exn></code>.</p>
<p>Question is: is that exhaustive? Well… no. As we already mentioned, there’s a 3rd state, often overlooked or conflated with failure: a canceled fiber. A canceled fiber doesn’t produce any output - since it was canceled before completion. In F# we already know how to represent an absence of value - simply use an option. Therefore our ultimate Fiber result type could look like this:</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">type</span> <span class="title class_">FiberResult</span><span class="operator"><</span><span class="symbol">'a</span><span class="operator">></span> <span class="operator">=</span> Result<span class="operator"><</span><span class="symbol">'a</span>, exn<span class="operator">></span> option</span><br></pre></td></tr></table></figure>
<p>Now, the <code>ExecutionContext</code>. While it can be compound of many different capabilities throughout the system - even to serve as functional equivalent of dependency injection - here I’ll use it only for implicit passing of specific scheduler info and cancelation tokens from one fiber to another.</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">type</span> <span class="title class_">ExecutionContext</span> <span class="operator">=</span> IScheduler <span class="operator">*</span> Cancel</span><br></pre></td></tr></table></figure>
<p><code>IScheduler</code> interface is used to abstract component responsible for running our fibers. At the moment all we need is an ability to schedule fiber execution:</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="meta">[<Interface>]</span></span><br><span class="line"><span class="keyword">type</span> <span class="title class_">IScheduler</span> <span class="operator">=</span> </span><br><span class="line"> <span class="keyword">abstract</span> Schedule<span class="operator">:</span> (<span class="type">unit</span> <span class="operator">-></span> <span class="type">unit</span>) <span class="operator">-></span> <span class="type">unit</span></span><br></pre></td></tr></table></figure>
<p>While the name and signature imply multithreaded execution model, it doesn’t have to be the case. We can even implement scheduler which will simulate everything on a single core.</p>
<p>For now, we can simply implement a scheduler API on top of our standard .NET thread pool:</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">module</span> Scheduler</span><br><span class="line"></span><br><span class="line"><span class="keyword">open</span> System.Threading</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> shared <span class="operator">=</span> </span><br><span class="line"> { <span class="keyword">new</span> ISchedule <span class="keyword">with</span></span><br><span class="line"> <span class="keyword">member</span> __.Schedule fn <span class="operator">=</span> </span><br><span class="line"> ThreadPool.QueueUserWorkItem(WaitCallback (<span class="keyword">fun</span> _ <span class="operator">-></span> fn())) <span class="operator">|></span> <span class="built_in">ignore</span> }</span><br></pre></td></tr></table></figure>
<h3 id="Cancellation"><a class="header-anchor" href="#Cancellation">¶</a>Cancellation</h3>
<p>Now it’s a time for cancellation tokens. Of course we could just make use of a flag - conceptually working like native .NET <code>CancellationToken</code>. However given implicit cancellation, it may not be enough. Example:</p>
<blockquote>
<p>Imagine, that inside our fiber we’re scheduling the race between two other fibers ie. one writing data to a file and other which will complete after timeout. Now, whenever one of them completes first, we want to cancel another one to stop wasting resources for result that no longer matters.</p>
</blockquote>
<p>This simple scenario is similar to what .NET <code>Task.WhenAny</code> is used - with a difference that, unlike TPL, we want to actually cancel other executing tasks instead of letting them run (potentially forever) :D</p>
<p>Now, since our cancellation is not explicit, we need to deal with few things:</p>
<ol>
<li>Whenever parent fiber is cancelled, all child fibers it spawned are also cancelled.</li>
<li>Whenever we cancel a fiber that loose the race, we don’t want to accidentally cancel a token of its parent.</li>
</ol>
<p>This behavior implies at least using two separate tokens, however in practice it will be more pragmatic to make our <code>Cancel</code> token work as a tree hierarchy - this way we can easily keep track of things and support more complex scenarios.</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="meta">[<Sealed;AllowNullLiteral>]</span></span><br><span class="line"><span class="keyword">type</span> <span class="title class_">Cancel</span>(parent<span class="operator">:</span> Cancel) <span class="operator">=</span></span><br><span class="line"> <span class="keyword">let</span> <span class="keyword">mutable</span> flag<span class="operator">:</span> <span class="type">int</span> <span class="operator">=</span> <span class="number">0</span></span><br><span class="line"> <span class="keyword">let</span> <span class="keyword">mutable</span> children<span class="operator">:</span> Cancel <span class="type">list</span> <span class="operator">=</span> []</span><br><span class="line"> <span class="keyword">new</span>() <span class="operator">=</span> Cancel(<span class="literal">null</span>)</span><br><span class="line"> <span class="comment">/// Check if token was cancelled</span></span><br><span class="line"> <span class="keyword">member</span> __.Cancelled <span class="operator">=</span> flag <span class="operator">=</span> <span class="number">1</span></span><br><span class="line"> <span class="comment">/// Remove child token</span></span><br><span class="line"> <span class="keyword">member</span> <span class="keyword">private</span> __.RemoveChild(child) <span class="operator">=</span> </span><br><span class="line"> <span class="keyword">let</span> <span class="keyword">rec</span> loop child <span class="operator">=</span></span><br><span class="line"> <span class="keyword">let</span> children' <span class="operator">=</span> children</span><br><span class="line"> <span class="keyword">let</span> nval <span class="operator">=</span> children' <span class="operator">|></span> List.filter ((<span class="operator"><></span>) child)</span><br><span class="line"> <span class="keyword">if</span> <span class="built_in">not</span> (obj.ReferenceEquals(children', Interlocked.CompareExchange(<span class="operator">&</span>children, nval, children')))</span><br><span class="line"> <span class="keyword">then</span> loop child</span><br><span class="line"> <span class="keyword">if</span> <span class="built_in">not</span> (List.isEmpty children) <span class="keyword">then</span> loop child</span><br><span class="line"> <span class="comment">/// Create a new child token and return it.</span></span><br><span class="line"> <span class="keyword">member</span> this.AddChild () <span class="operator">=</span></span><br><span class="line"> <span class="keyword">let</span> <span class="keyword">rec</span> loop child <span class="operator">=</span></span><br><span class="line"> <span class="keyword">let</span> children' <span class="operator">=</span> children</span><br><span class="line"> <span class="keyword">if</span> (obj.ReferenceEquals(children', Interlocked.CompareExchange(<span class="operator">&</span>children, child<span class="operator">::</span>children', children')))</span><br><span class="line"> <span class="keyword">then</span> child</span><br><span class="line"> <span class="keyword">else</span> loop child</span><br><span class="line"> loop (Cancel this)</span><br><span class="line"> <span class="comment">/// Cancel a token</span></span><br><span class="line"> <span class="keyword">member</span> this.Cancel() <span class="operator">=</span></span><br><span class="line"> <span class="keyword">if</span> Interlocked.Exchange(<span class="operator">&</span>flag, <span class="number">1</span>) <span class="operator">=</span> <span class="number">0</span> <span class="keyword">then</span></span><br><span class="line"> <span class="keyword">for</span> child <span class="keyword">in</span> Interlocked.Exchange(<span class="operator">&</span>children, []) <span class="keyword">do</span> child.Cancel()</span><br><span class="line"> <span class="keyword">if</span> <span class="built_in">not</span> (isNull parent) <span class="keyword">then</span> parent.RemoveChild(this)</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>The general idea is simple: every new cancellation token (except root) may have a parent and a list of children. Canceling parent means canceling its children as well. After cancellation, we need to unpin child from its parent (therefore need for <code>RemveChild</code> operation) to avoid memory leaks.</p>
<h5 id="Lock-free-updates"><a class="header-anchor" href="#Lock-free-updates">¶</a>Lock-free updates</h5>
<p>What might be confusing for some in the code above, are recursive loops inside of <code>AddChild</code>/<code>RemoveChild</code> operations. This is a good place to introduce lock-free algorithms: we use atomic operations from <a href="https://www.bartoszsypytkowski.com/building-custom-fibers-library-in-f/docs.microsoft.com/en-us/dotnet/api/system.threading.interlocked">Interlocked</a> class to make sure that we can replace field references within a single CPU instruction, therefore making such field update safe without synchronized access. This is also known as Compare-And-Swap semantics.</p>
<p>This alone however is not enough, as <code>Interlocked.CompareExchange(&field, new', old)</code> can only safely replace a single field with new value if it contained an old one. This means that you cannot safely add or remove element to the list. So what can we do?</p>
<ol>
<li>We’re taking a value from the field.</li>
<li>Update that value.</li>
<li>Conditionally put it back again. What if in the meantime the field was already replaced by another concurrently running thread? In that case <code>Interlocked.CompareExchange</code> will return field value other that the one we read in step 1. This is why we compare its result with the variable we expected.</li>
<li>If the expectation fails, we’ll retry - hence a recursive loop. Eventually even in high contention scenarios we should be able to complete after few retries. Given cheap and idempotent update operation, this still will be way faster than trying to call kernel code to obtain mutex/semaphore lock.</li>
</ol>
<p>While this may sound like something error prone - we can potentially add the same element multiple times - in practice it’s safe, because our collection here is an immutable data structure. Adding the same element multiple times without updating the reference will always produce the same result.</p>
<h3 id="Back-on-track…"><a class="header-anchor" href="#Back-on-track…">¶</a>Back on track…</h3>
<p>Now we have pretty much all core structures. We’re ready to start building our fiber operators. Starting from the basic ones - a successfully completed fiber and the failed one:</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">let</span> success r <span class="operator">=</span> Fiber <span class="operator"><|</span> <span class="keyword">fun</span> (_, c) next <span class="operator">-></span> </span><br><span class="line"> <span class="keyword">if</span> c.Cancelled <span class="keyword">then</span> next <span class="literal">None</span> <span class="keyword">else</span> next (<span class="literal">Some</span> (<span class="literal">Ok</span> r))</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> fail ex <span class="operator">=</span> Fiber <span class="operator"><|</span> <span class="keyword">fun</span> (_, c) next <span class="operator">-></span> </span><br><span class="line"> <span class="keyword">if</span> c.Cancelled <span class="keyword">then</span> next <span class="literal">None</span> <span class="keyword">else</span> next (<span class="literal">Some</span> (<span class="literal">Error</span> ex))</span><br></pre></td></tr></table></figure>
<p>Here, we simply pass a result/error to our Fiber callback:</p>
<ul>
<li>Cancelled fiber call <code>next</code> callback with <code>None</code> - as fibers cancelled before completion produce output .</li>
<li>Successful call results in passing <code>Some (Ok result)</code> to a callback…</li>
<li>… while failed result can be identified with <code>Some (Error exception)</code>.</li>
</ul>
<p>You’ll be able to see a cancellation check made here as preamble of pretty much every operator body, which we’ll define. While it may sound cumbersome remember: we do that so that users of our fibers won’t have to :)</p>
<p>Next very important operation is result mapping - we want to map result of one fiber into something else, returning another (lazy) fiber:</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">let</span> mapResult (fn<span class="operator">:</span> <span class="type">Result</span><span class="operator"><</span><span class="symbol">'a</span><span class="operator">></span> <span class="operator">-></span> <span class="type">Result</span><span class="operator"><</span><span class="symbol">'b</span><span class="operator">></span>) (Fiber call) <span class="operator">=</span> Fiber <span class="operator"><|</span> <span class="keyword">fun</span> (s, c) next <span class="operator">-></span></span><br><span class="line"> <span class="keyword">if</span> c.Cancelled <span class="keyword">then</span> next <span class="literal">None</span></span><br><span class="line"> <span class="keyword">else</span> </span><br><span class="line"> <span class="keyword">try</span> </span><br><span class="line"> call (s, c) (<span class="keyword">fun</span> result <span class="operator">-></span></span><br><span class="line"> <span class="keyword">if</span> c.Cancelled <span class="keyword">then</span> next <span class="literal">None</span></span><br><span class="line"> <span class="keyword">else</span> next (Option.map fn result))</span><br><span class="line"> <span class="keyword">with</span> e <span class="operator">-></span> next (<span class="literal">Some</span> (<span class="literal">Error</span> e))</span><br></pre></td></tr></table></figure>
<p>We can use this function to compose more traditionally-looking <code>map</code> function…</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">let</span> map (fn<span class="operator">:</span> <span class="symbol">'a</span> <span class="operator">-></span> <span class="symbol">'b</span>) fiber <span class="operator">=</span> mapResult (Result.map fn) fiber</span><br></pre></td></tr></table></figure>
<p>… however <code>mapResult</code> is more powerful - you could easily imagine using to apply failure recovery (a.k.a <code>try</code>/<code>catch</code> semantics) by simply mapping <code>Error exception</code> → <code>Ok recoveredValue</code>:</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">let</span> catch fn fiber <span class="operator">=</span> mapResult (<span class="keyword">function</span> <span class="literal">Error</span> e <span class="operator">-></span> fn e <span class="operator">|</span> ok <span class="operator">-></span> ok) fiber</span><br></pre></td></tr></table></figure>
<p>Another must-have function is binding operator (also know as <code>flatMap</code> in other languages like Scala, or <code>Promise.then</code> in JavaScript). It gives us the ability to compose fibers together - we’ll also use it when we come up to building a computation expression for our fibers.</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">let</span> bind (fn<span class="operator">:</span> <span class="symbol">'a</span> <span class="operator">-></span> Fiber<span class="operator"><</span><span class="symbol">'b</span><span class="operator">></span>) (Fiber call) <span class="operator">=</span> Fiber <span class="operator"><|</span> <span class="keyword">fun</span> (s, c) next <span class="operator">-></span></span><br><span class="line"> <span class="keyword">if</span> c.Cancelled <span class="keyword">then</span> next <span class="literal">None</span> </span><br><span class="line"> <span class="keyword">else</span> </span><br><span class="line"> <span class="keyword">try</span></span><br><span class="line"> call (s, c) (<span class="keyword">fun</span> result <span class="operator">-></span></span><br><span class="line"> <span class="keyword">if</span> c.Cancelled <span class="keyword">then</span> next <span class="literal">None</span></span><br><span class="line"> <span class="keyword">else</span> <span class="keyword">match</span> result <span class="keyword">with</span></span><br><span class="line"> <span class="operator">|</span> <span class="literal">Some</span> (<span class="literal">Ok</span> r) <span class="operator">-></span></span><br><span class="line"> <span class="keyword">let</span> (Fiber call2) <span class="operator">=</span> fn r</span><br><span class="line"> call2 (s, c) next <span class="comment">// pass `next` callback over to next fiber</span></span><br><span class="line"> <span class="operator">|</span> <span class="literal">None</span> <span class="operator">-></span> next <span class="literal">None</span></span><br><span class="line"> <span class="operator">|</span> <span class="literal">Some</span> (<span class="literal">Error</span> e) <span class="operator">-></span> next (<span class="literal">Some</span>(<span class="literal">Error</span> e)))</span><br><span class="line"> <span class="keyword">with</span> e <span class="operator">-></span> next (<span class="literal">Some</span>(<span class="literal">Error</span> e))</span><br></pre></td></tr></table></figure>
<p>It’s simple - we execute one fiber from within another, passing the <code>next</code> callback from outer function as an argument to inner one.</p>
<h3 id="Fiber-computation-expressions"><a class="header-anchor" href="#Fiber-computation-expressions">¶</a>Fiber computation expressions</h3>
<p>With these few functions we’re already prepared to build a basic computation expression, that will enable us programming with fibers in pleasant way:</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="meta">[<Struct>]</span></span><br><span class="line"><span class="keyword">type</span> <span class="title class_">FiberBuilder</span> <span class="operator">=</span></span><br><span class="line"> <span class="keyword">member</span> <span class="keyword">inline</span> __.Zero <span class="operator">=</span> Fiber.success (Unchecked.defaultof<span class="operator"><</span>_<span class="operator">></span>)</span><br><span class="line"> <span class="keyword">member</span> <span class="keyword">inline</span> __.ReturnFrom fib <span class="operator">=</span> fib</span><br><span class="line"> <span class="keyword">member</span> <span class="keyword">inline</span> __.Return value <span class="operator">=</span> Fiber.success value</span><br><span class="line"> <span class="keyword">member</span> <span class="keyword">inline</span> __.Bind(fib, fn) <span class="operator">=</span> Fiber.bind fn fib</span><br><span class="line"></span><br><span class="line"><span class="meta">[<AutoOpen>]</span></span><br><span class="line"><span class="keyword">module</span> FiberBuilder <span class="operator">=</span></span><br><span class="line"></span><br><span class="line"> <span class="keyword">let</span> fib <span class="operator">=</span> FiberBuilder()</span><br></pre></td></tr></table></figure>
<p>While in F# <a href="https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/computation-expressions?ref=bartoszsypytkowski.com">there are many more operators</a> we could pack into our computation expression, these are basic ones that will let it work. With such construct, we’ll be able to write programs like:</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">let</span> <span class="keyword">inline</span> millis n <span class="operator">=</span> TimeSpan.FromMilliseconds (float n)</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> program<span class="operator">:</span> Fiber<span class="operator"><</span><span class="type">int</span><span class="operator">></span> <span class="operator">=</span> <span class="keyword">fib</span> {</span><br><span class="line"> <span class="keyword">let</span> a <span class="operator">=</span> <span class="keyword">fib</span> {</span><br><span class="line"> <span class="keyword">do!</span> Fiber.delay (millis <span class="number">1000</span>) <span class="comment">// create some artificial delay</span></span><br><span class="line"> <span class="keyword">return</span> <span class="number">3</span></span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">let!</span> b <span class="operator">=</span> a <span class="operator">|></span> Fiber.timeout (millis <span class="number">3000</span>) <span class="comment">// execute task within specified timeout</span></span><br><span class="line"> <span class="keyword">return</span> b }</span><br></pre></td></tr></table></figure>
<p>Sure, we have neither delay nor timeout operators at the moment, but at least you know where are we heading now :)</p>
<h3 id="Delayed-execution"><a class="header-anchor" href="#Delayed-execution">¶</a>Delayed execution</h3>
<p>In order to implement delays, we could theoretically just call <code>Thread.Sleep</code> and get over it, but this approach is devastating from any coroutine library point of view. Most user-space thread libraries work by using a predefined fixed pool of OS-level threads and scheduling coroutines on them - you can read more about building thread pools <a href="https://www.bartoszsypytkowski.com/thread-safety-with-affine-thread-pools/">here</a>.</p>
<p>However, <code>Thread.Sleep(timeout)</code> doesn’t know thread pooling mechanism - all it knows about is that we called suspending current OS thread of execution. This means, that this thread will not be awoken by kernel until timeout completes. What it means, is that none of our fibers will be able to use that thread. This is bad, because usually thread pools are made to fit in-line with number of machine CPU cores. In practice, <code>Thread.Sleep</code> may keep one of our CPU cores idle, wasting machine power in the process.</p>
<p>For this reason we usually want to build a suspendable fibers, that will respect our thread pool. This however cannot be done without cooperation with scheduler itself. Therefore, we need to extend API of our scheduler:</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">type</span> <span class="title class_">IScheduler</span> <span class="operator">=</span></span><br><span class="line"> <span class="keyword">abstract</span> Schedule<span class="operator">:</span> (<span class="type">unit</span> <span class="operator">-></span> <span class="type">unit</span>) <span class="operator">-></span> <span class="type">unit</span></span><br><span class="line"> <span class="keyword">abstract</span> Delay<span class="operator">:</span> TimeSpan <span class="operator">*</span> (<span class="type">unit</span> <span class="operator">-></span> <span class="type">unit</span>) <span class="operator">-></span> <span class="type">unit</span></span><br></pre></td></tr></table></figure>
<p>And our simple implementation of it as well:</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">let</span> shared <span class="operator">=</span> </span><br><span class="line"> { <span class="keyword">new</span> IScheduler <span class="keyword">with</span></span><br><span class="line"> <span class="keyword">member</span> __.Schedule fn <span class="operator">=</span> <span class="operator">....</span></span><br><span class="line"> <span class="keyword">member</span> this.Delay (timeout<span class="operator">:</span> TimeSpan, fn) <span class="operator">=</span> </span><br><span class="line"> <span class="keyword">let</span> <span class="keyword">mutable</span> t <span class="operator">=</span> Unchecked.defaultof<span class="operator"><</span>Timer<span class="operator">></span></span><br><span class="line"> <span class="keyword">let</span> callback <span class="operator">=</span> <span class="keyword">fun</span> _ <span class="operator">-></span> </span><br><span class="line"> t.Dispose()</span><br><span class="line"> fn()</span><br><span class="line"> ()</span><br><span class="line"> t <span class="operator"><-</span> <span class="keyword">new</span> Timer(callback, <span class="literal">null</span>, int timeout.TotalMilliseconds, Timeout.Infinite)</span><br><span class="line"> }</span><br></pre></td></tr></table></figure>
<p>We’ll use a .NET timers here to implement our delays. With these in our hands, our<code>Fiber.delay</code> operation is trivial to implement:</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">let</span> delay (timeout)<span class="operator">:</span> Fiber<span class="operator"><</span><span class="type">unit</span><span class="operator">></span> <span class="operator">=</span></span><br><span class="line"> Fiber <span class="operator"><|</span> <span class="keyword">fun</span> (s, c) next <span class="operator">-></span></span><br><span class="line"> <span class="keyword">if</span> c.Cancelled <span class="keyword">then</span> next <span class="literal">None</span></span><br><span class="line"> <span class="keyword">else</span> s.Delay(timeout, <span class="keyword">fun</span> () <span class="operator">-></span> </span><br><span class="line"> <span class="keyword">if</span> c.Cancelled </span><br><span class="line"> <span class="keyword">then</span> next <span class="literal">None</span> </span><br><span class="line"> <span class="keyword">else</span> next (<span class="literal">Some</span> (<span class="literal">Ok</span> ())))</span><br></pre></td></tr></table></figure>
<h3 id="Composing-parallel-fibers"><a class="header-anchor" href="#Composing-parallel-fibers">¶</a>Composing parallel fibers</h3>
<p>We’re slowly getting to the end. What I left for this blog post was to implement two basic operators, that are prevalent in most coroutine libraries:</p>
<ul>
<li><code>Fiber.parallel</code> which will schedule multiple fibers to run in parallel and returns a fiber which aggregates their results.</li>
<li>Running two fibers in parallel and returning the result of whichever completes first, while cancelling a second one. We already discussed this approach before. Here I’ll call it <code>Fiber.race</code>.</li>
</ul>
<h4 id="Aggregating-parallel-results"><a class="header-anchor" href="#Aggregating-parallel-results">¶</a>Aggregating parallel results</h4>
<p>We’ll start from building a parallel operator, which will change our array of fibers into fiber with an array of results. But let’s define the semantics of that operation first:</p>
<ul>
<li>Our result fiber completes only when all of the aggregated fibers completed with successful result.</li>
<li>If any of the fibers fails, the resulting fiber also fails.</li>
<li>If any of the fibers fails or get cancelled, all pending ones are also cancelled.</li>
</ul>
<p>The core skeleton of that operation could look like following:</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">let</span> parallel (fibers<span class="operator">:</span> Fiber<span class="operator"><</span><span class="symbol">'a</span><span class="operator">></span>[])<span class="operator">:</span> Fiber<span class="operator"><</span><span class="symbol">'a</span>[]<span class="operator">></span> <span class="operator">=</span></span><br><span class="line"> Fiber <span class="operator"><|</span> <span class="keyword">fun</span> (s, c) next <span class="operator">-></span></span><br><span class="line"> <span class="keyword">if</span> c.Cancelled <span class="keyword">then</span> next <span class="literal">None</span></span><br><span class="line"> <span class="keyword">else</span> </span><br><span class="line"> <span class="keyword">let</span> child <span class="operator">=</span> c.AddChild()</span><br><span class="line"> <span class="keyword">let</span> successes <span class="operator">=</span> Array.zeroCreate remaining</span><br><span class="line"> <span class="keyword">let</span> <span class="keyword">mutable</span> remaining <span class="operator">=</span> Array.length fibs</span><br><span class="line"> fibers <span class="operator">|></span> Array.iteri (<span class="keyword">fun</span> idx (Fiber call) <span class="operator">-></span></span><br><span class="line"> s.Schedule (<span class="keyword">fun</span> () <span class="operator">-></span> <span class="comment">(* to be defined *)</span>)</span><br><span class="line"> )</span><br></pre></td></tr></table></figure>
<p>Here, we create a dedicated cancellation token, an array of results and a countdown counter - we’re going to decrement it every time one of our fibers completes to know when we’re ready to return a complete result. I’ve left a placeholder for a lambda body that we actually want to schedule. We’re going to fill it right away:</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="comment">// defined above: s.Schedule <| fun () -></span></span><br><span class="line">call (s, child) (<span class="keyword">fun</span> result <span class="operator">-></span> </span><br><span class="line"><span class="keyword">match</span> result <span class="keyword">with</span></span><br><span class="line"><span class="operator">|</span> <span class="literal">Some</span> (<span class="literal">Ok</span> success) <span class="operator">-></span></span><br><span class="line"> <span class="comment">// fill the result array</span></span><br><span class="line"> successes.[idx] <span class="operator"><-</span> success</span><br><span class="line"> <span class="keyword">if</span> c.Cancelled <span class="operator">&&</span> Interlocked.Exchange(<span class="operator">&</span>remaining, <span class="number">-1</span>) <span class="operator">></span> <span class="number">0</span> <span class="keyword">then</span></span><br><span class="line"> next <span class="literal">None</span></span><br><span class="line"> <span class="keyword">elif</span> Interlocked.Decrement(<span class="operator">&</span>remaining) <span class="operator">=</span> <span class="number">0</span> <span class="keyword">then</span></span><br><span class="line"> <span class="comment">// if all results have been returned, call the `next` callback</span></span><br><span class="line"> <span class="keyword">if</span> c.Cancelled <span class="keyword">then</span> next <span class="literal">None</span></span><br><span class="line"> <span class="keyword">else</span> next (<span class="literal">Some</span> (<span class="literal">Ok</span> successes))</span><br><span class="line"><span class="operator">|</span> <span class="literal">Some</span> (<span class="literal">Error</span> fail) <span class="operator">-></span></span><br><span class="line"> <span class="keyword">if</span> Interlocked.Exchange(<span class="operator">&</span>remaining, <span class="number">-1</span>) <span class="operator">></span> <span class="number">0</span> <span class="keyword">then</span> </span><br><span class="line"> child.Cancel() <span class="comment">// we failed, cancel other fibers</span></span><br><span class="line"> <span class="keyword">if</span> c.Cancelled <span class="keyword">then</span> next <span class="literal">None</span></span><br><span class="line"> <span class="keyword">else</span> next (<span class="literal">Some</span> (<span class="literal">Error</span> fail))</span><br><span class="line"><span class="operator">|</span> <span class="literal">None</span> <span class="operator">-></span></span><br><span class="line"> <span class="keyword">if</span> Interlocked.Exchange(<span class="operator">&</span>remaining, <span class="number">-1</span>) <span class="operator">></span> <span class="number">0</span> <span class="keyword">then</span> next <span class="literal">None</span>)</span><br></pre></td></tr></table></figure>
<p>As you probably noticed, we’re using <code>Interlocked</code> class again - that’s because now we have multiple fibers running in parallel, therefore our access to shared mutable values is not thread safe. This includes <code>remaining</code> counter decrement operation. This however doesn’t apply to <code>successes.[i] <- success</code> - since every fiber knows and touches only its own index within result array, there’s no worry that any other will try to push its result in the same place.</p>
<p>What you also can see, we’re using a <code>-1</code> here as a magic value - we’ll use it on the counter as a flag to determine if any of the fibers failed/was cancelled - and if so, which one of them will call the <code>next</code> callback.</p>
<h4 id="Racing-to-completion"><a class="header-anchor" href="#Racing-to-completion">¶</a>Racing to completion</h4>
<p>With first operator (<code>Fiber.parallel</code>) ready, now it’s the time to implement <code>Fiber.race</code>. Since I’ve discussed it behavior multiple times in this post already, let’s dive straight into the code:</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">let</span> race (Fiber left) (Fiber right)<span class="operator">:</span> Fiber<span class="operator"><</span>Choice<span class="operator"><</span><span class="symbol">'a</span>, <span class="symbol">'b</span><span class="operator">>></span> <span class="operator">=</span></span><br><span class="line"> Fiber <span class="operator"><|</span> <span class="keyword">fun</span> (s, c) next <span class="operator">-></span></span><br><span class="line"> <span class="keyword">if</span> c.Cancelled <span class="keyword">then</span> next <span class="literal">None</span></span><br><span class="line"> <span class="keyword">else</span> </span><br><span class="line"> <span class="keyword">let</span> <span class="keyword">mutable</span> flag <span class="operator">=</span> <span class="number">0</span></span><br><span class="line"> <span class="keyword">let</span> cancelChild <span class="operator">=</span> c.AddChild()</span><br><span class="line"> <span class="keyword">let</span> run fiber choice <span class="operator">=</span></span><br><span class="line"> <span class="comment">(* to be described *)</span></span><br><span class="line"> run left Choice1Of2</span><br><span class="line"> run right Choice2Of2</span><br></pre></td></tr></table></figure>
<p>So again, we want to have shared mutable flag, which we’ll use to determine, which of the fibers finished as a first one to be able to call fiber’s callback safely and cancel the other. You may see, that our returned fiber uses <code>Choice<,></code> type - this means, that our left and right fibers can have results of different types. We’ll use that soon, but first we need to complete our <code>run</code> function body:</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">let</span> run fiber choice <span class="operator">=</span></span><br><span class="line"> s.Schedule (<span class="keyword">fun</span> () <span class="operator">-></span></span><br><span class="line"> fiber (s, cancelChild) (<span class="keyword">fun</span> result <span class="operator">-></span></span><br><span class="line"> <span class="keyword">if</span> Interlocked.Exchange(<span class="operator">&</span>flag, <span class="number">1</span>) <span class="operator">=</span> <span class="number">0</span> <span class="keyword">then</span></span><br><span class="line"> cancelChild.Cancel()</span><br><span class="line"> <span class="keyword">if</span> c.Cancelled <span class="keyword">then</span> next <span class="literal">None</span></span><br><span class="line"> <span class="keyword">else</span> <span class="keyword">match</span> result <span class="keyword">with</span></span><br><span class="line"> <span class="operator">|</span> <span class="literal">None</span> <span class="operator">-></span> next <span class="literal">None</span></span><br><span class="line"> <span class="operator">|</span> <span class="literal">Some</span>(<span class="literal">Ok</span> v) <span class="operator">-></span> next (<span class="literal">Some</span>(<span class="literal">Ok</span>(choice v)))</span><br><span class="line"> <span class="operator">|</span> <span class="literal">Some</span>(<span class="literal">Error</span> e) <span class="operator">-></span> next (<span class="literal">Some</span>(<span class="literal">Error</span> e))))</span><br></pre></td></tr></table></figure>
<p>What we do here is simply trying to race to “reserve” out flag variable - the winner gets his result mapped to corresponding choice, while looser gets cancelled.</p>
<p>What’s interesting, we can now combine our <code>race</code> and <code>delay</code> functions to easily implement timeout mechanism:</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">let</span> timeout (t<span class="operator">:</span> TimeSpan) fiber <span class="operator">=</span></span><br><span class="line"> Fiber <span class="operator"><|</span> <span class="keyword">fun</span> (s, c) next <span class="operator">-></span></span><br><span class="line"> <span class="keyword">let</span> (Fiber call) <span class="operator">=</span> race (delay t) fiber</span><br><span class="line"> call (s, c) (<span class="keyword">fun</span> result <span class="operator">-></span></span><br><span class="line"> <span class="keyword">if</span> c.Cancelled <span class="keyword">then</span> next <span class="literal">None</span></span><br><span class="line"> <span class="keyword">else</span> <span class="keyword">match</span> result <span class="keyword">with</span></span><br><span class="line"> <span class="operator">|</span> <span class="literal">None</span> <span class="operator">-></span> next <span class="literal">None</span></span><br><span class="line"> <span class="operator">|</span> <span class="literal">Some</span>(<span class="literal">Ok</span> (Choice1Of2 _)) <span class="operator">-></span> next <span class="literal">None</span> <span class="comment">// timeout won</span></span><br><span class="line"> <span class="operator">|</span> <span class="literal">Some</span>(<span class="literal">Ok</span> (Choice2Of2 v)) <span class="operator">-></span> next (<span class="literal">Some</span>(<span class="literal">Ok</span> v))</span><br><span class="line"> <span class="operator">|</span> <span class="literal">Some</span>(<span class="literal">Error</span> e) <span class="operator">-></span> next (<span class="literal">Some</span>(<span class="literal">Error</span> e))</span><br><span class="line"> )</span><br></pre></td></tr></table></figure>
<p>The one last thing left for us, is to be able to run out fibers on the main thread - otherwise we’d start our program, schedule fibers to run in the background and then close the program without waiting for the results.</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">let</span> blocking (s<span class="operator">:</span> IScheduler) (cancel<span class="operator">:</span> Cancel) (Fiber fn) <span class="operator">=</span></span><br><span class="line"> <span class="keyword">use</span> waiter <span class="operator">=</span> <span class="keyword">new</span> ManualResetEventSlim(<span class="literal">false</span>)</span><br><span class="line"> <span class="keyword">let</span> <span class="keyword">mutable</span> res <span class="operator">=</span> <span class="literal">None</span></span><br><span class="line"> s.Schedule(<span class="keyword">fun</span> () <span class="operator">-></span> fn (s, cancel) (<span class="keyword">fun</span> result <span class="operator">-></span></span><br><span class="line"> <span class="keyword">if</span> <span class="built_in">not</span> cancel.Cancelled <span class="keyword">then</span></span><br><span class="line"> Interlocked.Exchange(<span class="operator">&</span>res, <span class="literal">Some</span> result) <span class="operator">|></span> <span class="built_in">ignore</span></span><br><span class="line"> waiter.Set()))</span><br><span class="line"> waiter.Wait()</span><br><span class="line"> res.Value</span><br></pre></td></tr></table></figure>
<p>It’s simple - we’ll use standard synchronization primitives provided by .NET runtime, to hold current OS thread until we complete. Sure it’s blocking an OS thread, but we’ll eventually need that if we don’t want our program’s main function to finish before all fibers inside the thread pool complete.</p>
<h3 id="Simulating-real-environment-in-tests"><a class="header-anchor" href="#Simulating-real-environment-in-tests">¶</a>Simulating real environment in tests</h3>
<p>In theory, we could be done here. But, if you managed to read up to this point, we may want to cover one last scenario. Imagine that we’d want to test our fibers. However running tests using standard thread pool scheduler can lead to funky issues:</p>
<ul>
<li>Sometimes you may trigger some race conditions in your code, that only happen in specific situations (like high CPU contention) and are almost impossible to reproduce during debug sessions.</li>
<li>Other times you may have some lengthy delays/timeouts in your code, like waiting for seconds or even minutes before continuing. Guess what: now your test will wait for just as long.</li>
</ul>
<p>These are not new problems. They are well known in world of concurrent and distributed systems. What we need, is a simulation of execution environment. If you want to listen more about that concept, I could recommend you <a href="https://www.youtube.com/watch?v=4fFDFbi3toc&ref=bartoszsypytkowski.com">this presentaton</a>. To run our test predictably, we’ll create a dedicated test scheduler, which will run our code in deterministic fashion (on a single core) and in a way that’s detached from other invariants eg. actual physical clock and random number generator.</p>
<p>The idea here is simple - our scheduler will operate on notion of virtual timeline. When we’ll try to schedule a new function - to trigger either immediately or after some timeout - we’ll store it inside an ordered collection, a timeline. Some of the technical decisions we also made for purposes of this implementation:</p>
<ul>
<li>Whenever a fiber is going to schedule multiple parallel executions “at the same time”, we’ll put them all into a single bucket on a timeline. Later on I’ll cover, why this is useful.</li>
<li>We’ll assume, that single operation execution is instantaneous. It means, it doesn’t advance our scheduler’s clock. We do it only for delayed executions.</li>
</ul>
<p>After describing the concept behind the algorithm, the actual implementation really shouldn’t be that surprising:</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">type</span> <span class="title class_">TestScheduler</span>(now<span class="operator">:</span> DateTime) <span class="operator">=</span></span><br><span class="line"> <span class="keyword">let</span> <span class="keyword">mutable</span> running <span class="operator">=</span> <span class="literal">false</span></span><br><span class="line"> <span class="keyword">let</span> <span class="keyword">mutable</span> currentTime <span class="operator">=</span> now.Ticks</span><br><span class="line"> <span class="keyword">let</span> <span class="keyword">mutable</span> timeline <span class="operator">=</span> Map.empty</span><br><span class="line"> <span class="keyword">let</span> schedule delay fn <span class="operator">=</span> <span class="comment">(* to be defined *)</span></span><br><span class="line"> <span class="keyword">let</span> <span class="keyword">rec</span> run () <span class="operator">=</span> <span class="comment">(* to be defined *)</span></span><br><span class="line"> <span class="keyword">interface</span> IScheduler <span class="keyword">with</span></span><br><span class="line"> <span class="keyword">member</span> this.Schedule fn <span class="operator">=</span> </span><br><span class="line"> schedule <span class="number">0</span>L fn</span><br><span class="line"> <span class="keyword">if</span> <span class="built_in">not</span> running <span class="keyword">then</span></span><br><span class="line"> running <span class="operator"><-</span> <span class="literal">true</span></span><br><span class="line"> run ()</span><br><span class="line"> <span class="keyword">member</span> this.Delay (timeout<span class="operator">:</span> TimeSpan, fn) <span class="operator">=</span> schedule timeout.Ticks fn</span><br></pre></td></tr></table></figure>
<p>We’re using a <code>running</code> flag here to not try to invoke <code>run</code> multiple times in nested manner - this would cause non-tailable recursion and potential stack overflow in more expensive tests.</p>
<p>The schedule function is pretty simple - calculate expected execution time for a function, then add that function to be executed at that point in time.</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">let</span> schedule delay fn <span class="operator">=</span> </span><br><span class="line"> <span class="keyword">let</span> at <span class="operator">=</span> currentTime <span class="operator">+</span> delay</span><br><span class="line"> timeline <span class="operator"><-</span></span><br><span class="line"> <span class="keyword">match</span> Map.tryFind at timeline <span class="keyword">with</span></span><br><span class="line"> <span class="operator">|</span> <span class="literal">None</span> <span class="operator">-></span> Map.add at [fn] timeline </span><br><span class="line"> <span class="operator">|</span> <span class="literal">Some</span> fns <span class="operator">-></span> Map.add at (fn<span class="operator">::</span>fns) timeline</span><br></pre></td></tr></table></figure>
<p>Given all of the code we already survived in this blog post, run loop should be pretty simple:</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">let</span> <span class="keyword">rec</span> run () <span class="operator">=</span></span><br><span class="line"> <span class="keyword">match</span> Seq.tryHead timeline <span class="keyword">with</span></span><br><span class="line"> <span class="operator">|</span> <span class="literal">None</span> <span class="operator">-></span> running <span class="operator"><-</span> <span class="literal">false</span></span><br><span class="line"> <span class="operator">|</span> <span class="literal">Some</span> (KeyValue(time, bucket)) <span class="operator">-></span></span><br><span class="line"> timeline <span class="operator"><-</span> Map.remove time timeline</span><br><span class="line"> currentTime <span class="operator"><-</span> time</span><br><span class="line"> <span class="keyword">for</span> fn <span class="keyword">in</span> List.rev bucket <span class="keyword">do</span> </span><br><span class="line"> fn () </span><br><span class="line"> run ()</span><br></pre></td></tr></table></figure>
<p>We’ll try to pick the first entry from the timeline - since here we use F# map, which is sorted in ascending order, we know that first entry is the one with the shortest execution timeout. We update our “current” time to match the expected one we calculated earlier, and finally we execute all functions scheduled at that time and repeat the loop all over until we eventually run out of scheduled actions.</p>
<p>Now here’s the trick - we use <code>List.rev</code> to execute functions in the same order in which they were scheduled, because we want our tests to be deterministic and our bugs to be reproducible. However this is not the only strategy - since we know that functions in the same bucket could as well be executing in parallel, we could shuffle them around in different permutations for early discovery of some data races! I’ll won’t dive into it, but leave that idea as food for thoughts for you.</p>
<p>One last note about the test scheduler is that isolating it from the actual physical clock means, we cannot trust our time functions (like <code>DateTime.UtcNow</code>) any longer. This shouldn’t really be an issue though - because relying on physical time would potentially make our tests indeterministic, we didn’t want to use it anyway, right?</p>
<p>However, we need to be able to obtain current time from the scheduler, so we need to extend its API:</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">type</span> <span class="title class_">IScheduler</span> <span class="operator">=</span></span><br><span class="line"> <span class="keyword">abstract</span> UtcNow<span class="operator">:</span> <span class="type">unit</span> <span class="operator">-></span> <span class="type">unit</span></span><br><span class="line"> <span class="comment">// ... other methods</span></span><br><span class="line"> </span><br><span class="line"><span class="keyword">type</span> <span class="title class_">TestScheduler</span>() <span class="operator">=</span></span><br><span class="line"> <span class="keyword">let</span> <span class="keyword">mutable</span> currentTime <span class="operator">=</span> DateTime.UtcNow.Ticks</span><br><span class="line"> <span class="comment">// ... rest of the implementation</span></span><br><span class="line"> <span class="keyword">interface</span> IScheduler <span class="keyword">with</span></span><br><span class="line"> <span class="keyword">member</span> __.UtcNow() <span class="operator">=</span> DateTime(currentTime)</span><br><span class="line"> <span class="comment">// ... other methods</span></span><br></pre></td></tr></table></figure>
<p>And that’s all. As always, if you got confused or have a problems along the way, you can get the entire code <a href="https://gist.github.com/Horusiath/9c790691130150b524aaa9ab426ed982?ref=bartoszsypytkowski.com">here</a>. I wanted to thank to Anthony Lloyd for his initial work on porting Scala <a href="https://zio.dev/?ref=bartoszsypytkowski.com">ZIO</a> library to F#, which brought me an inspiration to write this piece.</p>
]]></content>
<tags>
<tag>Technique</tag>
<tag>Archive</tag>
</tags>
</entry>
<entry>
<title>DDD 中的 Ubiquitous Language</title>
<url>/2025/03/14/DDD-%E4%B8%AD%E7%9A%84-Ubiquitous-Languages/</url>
<content><![CDATA[<p>Ubiquitous Language(通用语言)是 Domain-Driven Design (DDD) 中的一个核心概念。它是一种共享的、统一的语言,用于描述和讨论领域模型中的概念和规则。Ubiquitous Language 的目的是确保开发团队和业务专家之间的沟通更加高效和准确,从而减少误解和错误。</p>
<p>一般来说,Ubiquitous Language 有以下特点:</p>
<ol>
<li>共享:Ubiquitous Language 是由开发团队和业务专家共同创建和使用的。它不仅用于代码和文档,还用于日常的沟通和讨论。</li>
<li>统一:在整个项目中,Ubiquitous Language 应该是一致的。无论是在代码、文档、会议还是白板上,都应该使用相同的术语和概念。</li>
<li>精确:Ubiquitous Language 应该尽可能精确,避免模糊和歧义。每个术语都应该有明确的定义和含义。</li>
<li>演进:Ubiquitous Language 是动态的,会随着项目的进展和业务需求的变化而演进。团队应该定期审查和更新它。</li>
</ol>
<p>基于此,它可以用在这些地方:</p>
<ol>
<li>命名:在代码中使用 Ubiquitous Language 来命名类、方法、变量等。例如,如果业务专家使用“订单”来描述一个概念,那么在代码中也应该使用“Order”而不是“Purchase”或“Transaction”。</li>
<li>文档:在文档中使用 Ubiquitous Language 来描述系统的设计、架构和功能。这有助于业务专家更容易理解技术文档。</li>
<li>沟通:在团队会议、讨论和白板会议中使用 Ubiquitous Language。这有助于确保所有参与者都在讨论同一个概念。</li>
<li>测试:在编写测试用例时,使用 Ubiquitous Language 来描述测试的前提条件、步骤和预期结果。这有助于确保测试覆盖了业务需求。</li>
</ol>
<p>假设正在开发一个电子商务系统,业务专家使用“订单”来描述用户购买的商品集合。团队可以在代码中使用“Order”来命名相关的类和方法:</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">type</span> <span class="title class_">Order</span> <span class="operator">=</span> {</span><br><span class="line"> items<span class="operator">:</span> OrderItem <span class="type">list</span></span><br><span class="line"> customer<span class="operator">:</span> Customer</span><br><span class="line"> orderDate<span class="operator">:</span> DateTime</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">type</span> <span class="title class_">OrderItem</span> <span class="operator">=</span> {</span><br><span class="line"> product<span class="operator">:</span> Product</span><br><span class="line"> quantity<span class="operator">:</span> <span class="type">int</span></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">type</span> <span class="title class_">Customer</span> <span class="operator">=</span> {</span><br><span class="line"> name<span class="operator">:</span> <span class="type">string</span></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">type</span> <span class="title class_">Product</span> <span class="operator">=</span> {</span><br><span class="line"> name<span class="operator">:</span> <span class="type">string</span></span><br><span class="line"> price<span class="operator">:</span> <span class="type">float</span></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> addItem (order<span class="operator">:</span> Order) (item<span class="operator">:</span> OrderItem) <span class="operator">=</span></span><br><span class="line"> { order <span class="keyword">with</span> items <span class="operator">=</span> order.items <span class="operator">@</span> [item] }</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> getTotalAmount (order<span class="operator">:</span> Order) <span class="operator">=</span></span><br><span class="line"> <span class="keyword">let</span> total <span class="operator">=</span> <span class="number">0.0</span></span><br><span class="line"> <span class="keyword">for</span> item <span class="keyword">in</span> order.items <span class="keyword">do</span></span><br><span class="line"> total <span class="operator"><-</span> total <span class="operator">+</span> item.price</span><br><span class="line"> total</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> getPrice (item<span class="operator">:</span> OrderItem) <span class="operator">=</span></span><br><span class="line"> item.product.price <span class="operator">*</span> item.quantity</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> getTotalPrice (order<span class="operator">:</span> Order) <span class="operator">=</span></span><br><span class="line"> <span class="keyword">let</span> total <span class="operator">=</span> <span class="number">0.0</span></span><br><span class="line"> <span class="keyword">for</span> item <span class="keyword">in</span> order.items <span class="keyword">do</span></span><br><span class="line"> total <span class="operator"><-</span> total <span class="operator">+</span> getPrice(item)</span><br><span class="line"> total</span><br></pre></td></tr></table></figure>
<p>在这个示例中使用了“Order”、“OrderItem”、“Customer”和“Product”等术语,这些术语都是 Ubiquitous Language 的一部分,可以提高代码与业务需求的一致性。</p>
]]></content>
<tags>
<tag>Technique</tag>
</tags>
</entry>
<entry>
<title>Dealing with complex dependency injection in FSharp</title>
<url>/2024/10/18/Dealing-with-complex-dependency-injection-in-FSharp/</url>
<content><![CDATA[<p>Today, we’re going to cover different ways of encapsulating capabilities and supplying them between functions using functional programming techniques which can be realized in F#.</p>
<p>Managing code dependencies in object oriented languages in 2020 is pretty much one sided problem: dependency injection has won, people use dedicated frameworks to handle that for them, which 99.9% of the time operate using runtime reflection. Of course now you need to learn them as well, potentially misconfigure them and fail at runtime or maybe even discover that not every problem is a stateless web service, but it still better (?) than what we had in the past, and what more can we possibly do anyway?</p>
<p>On the other side in functional space, there’s no one opinionated solution or approach - various things have been proposed, usually depending on features that languages and compiler have to offer. And since pretty much all functional languages offer this thing known as partial application, for many years it was the most common answer for the problem of managing dependencies.</p>
<p>In short we’re talking about dependency injection by function parameter, like:</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="comment">// foo requires 2 dependencies to serve the incoming request</span></span><br><span class="line"><span class="keyword">let</span> foo bar baz request <span class="operator">=</span> <span class="operator">???</span></span><br><span class="line"><span class="comment">// we're providing dependencies by partial application</span></span><br><span class="line"><span class="keyword">let</span> wired <span class="operator">=</span> foo dependency1 dependency2</span><br><span class="line"><span class="comment">// now wired can serve request directly without calling </span></span><br><span class="line"><span class="comment">// dependencies every time</span></span><br><span class="line"><span class="keyword">let</span> response <span class="operator">=</span> wired request</span><br></pre></td></tr></table></figure>
<p>It’s very simple, doesn’t require reflection or dedicated library. However there are several pain points coming with this approach - visible especially as our code base grows and become more complex. However latest approaches popularized by libraries like Scala <a href="https://zio.dev/?ref=bartoszsypytkowski.com">ZIO</a> or Haskell’s <a href="https://youtu.be/idU7GdlfP9Q?t=1394&ref=bartoszsypytkowski.com">Polysemy</a> challenge this approach.</p>
<h2 id="Partial-application"><a class="header-anchor" href="#Partial-application">¶</a>Partial application</h2>
<p>There are some design decisions, when partial application doesn’t always give a clear answer. Example:</p>
<blockquote>
<p>Imagine using a set of methods, that are closely related and - in object oriented world - encapsulated within a single object, like database query/execute or different logging methods (debug/info/warning/error).</p>
</blockquote>
<p>Now, given that our function needs to use potentially more than one of these, how should we pass our arguments?:</p>
<ol>
<li>Functional purist path - pass every dependency as a separate function parameter: <code>let doSmth logError logInfo = ??</code>. While it allows us to precisely describe what this function uses, it would of course lead to explosion of function parameters. Additionally every time you need new function in your existing code, you need to partially apply it at all call sites.</li>
<li>Describe operations using ADT (algebraic data types) and inject a function that will work as an interpreter for them: <code>let doSmth (log: LogEvent -> unit) = ??</code>. While it’s easy to mock (you don’t need to implement everything, only pattern match on cases that matter for a particular test) and reduces params affinity, it also comes with a lot of indirection, that may lead to harder to grasp, especially during debugging. Sometimes a performance penalty is also to be expected.</li>
<li>Fallback to objects/interfaces and pass them as methods: <code>let doSmth (logger: ILogger) = ??</code>. While interfaces may simplify dependency tree, it’s not always obvious when to use it. Mocking story is also more painful + interfaces are not inferred by F# compiler.</li>
</ol>
<p>These are quite common options I’ve seen in the wild - each having their own advantages and disadvantages. Which one to use? Good question, as in practice with codebases that are old enough, you usually see 2 or even all 3 of them mixed together. This can lead to some confusion and obscurity over time.</p>
<p>What’s worse, none of these cases really solves problem of dependency management - all they do is just try to reduce it to a manageable scope. Eventually you’ll end up manually wiring - by partial application - dozens of functions and managing all of the dependencies between them by hand.</p>
<h3 id="Example"><a class="header-anchor" href="#Example">¶</a>Example</h3>
<p>In order to get better understanding, we’ll use a fairly simple example - changing user password:</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">let</span> fetchUser (db<span class="operator">:</span> IDbConnection) userId <span class="operator">=</span> </span><br><span class="line"> db.QueryFirstAsync(Sql.FetchUser, {<span class="operator">|</span> userId <span class="operator">=</span> userid<span class="operator">|</span> })</span><br><span class="line"> </span><br><span class="line"><span class="keyword">let</span> updateUser (db<span class="operator">:</span> IDbConnection) user <span class="operator">=</span> db.ExecuteAsync(Sql.UpdateUser, user)</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> changePass (logger<span class="operator">:</span> ILogger) fetch update <span class="operator">=</span> <span class="keyword">fun</span> req <span class="operator">-></span> <span class="keyword">task</span> {</span><br><span class="line"> <span class="keyword">let!</span> user <span class="operator">=</span> fetch req.UserId</span><br><span class="line"> <span class="keyword">if</span> user.Hash <span class="operator">=</span> bcrypt user.Salt req.OldPass <span class="keyword">then</span></span><br><span class="line"> <span class="keyword">let</span> salt <span class="operator">=</span> generateSalt ()</span><br><span class="line"> <span class="keyword">let</span> user' <span class="operator">=</span> { user <span class="keyword">with</span> Salt <span class="operator">=</span> salt; Hash <span class="operator">=</span> bcrypt salt req.NewPass }</span><br><span class="line"> <span class="keyword">do!</span> update user'</span><br><span class="line"> logger.LogInformation <span class="string">"Password change: user %i"</span> user.Id</span><br><span class="line"> <span class="keyword">return</span> <span class="literal">Ok</span> ()</span><br><span class="line"> <span class="keyword">else</span> </span><br><span class="line"> logger.LogError <span class="string">"Password change unauthorized: user %i"</span> user.Id</span><br><span class="line"> <span class="keyword">return</span> <span class="literal">Error</span> <span class="string">"Old password is invalid"</span></span><br><span class="line">}</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>Here we have a fairly short snippet with some dependencies? But how many in practice?:</p>
<ul>
<li>Number of parameters suggest 3, but depending on our choices it could be 4 (if we decide to pass log error and info separately) or 2 (if we conflate fetch/update into dedicated interface).</li>
<li>It’s not hard to imagine that in the future our <code>bcrypt</code> hashing function may turn out to be configurable - maybe even per each user. That may need a configurable parameter.</li>
<li>Maybe aside of the logger we may be needing a separate telemetry mechanism to count number of incoming request or password validation failures? That means another parameter.</li>
<li>Salt generation is pseudo-random process - it we want our function to be deterministic, we should probably parametrize it over explicitly passed <code>Random</code> as well.</li>
</ul>
<p>As you see, what seemed to be simple task at the beginning can quickly blow up out of proportion. As the number of arguments grows, the more nasty our wiring code eventually becomes. Quite common pattern is to hide all of that nastiness under the carpet a.k.a. composition root. However this doesn’t have to be the case.</p>
<p>Below we’ll cover another approach for dealing with dependencies - inspired by Scala <a href="https://medium.com/@pascal.mengelt/what-are-the-benefits-of-the-zio-modules-with-zlayers-3bf6cc064a9b?ref=bartoszsypytkowski.com">ZIO</a> library - using incremental steps, from first principles to monadic bindings.</p>
<h2 id="Managing-dependencies-beyond-partial-application"><a class="header-anchor" href="#Managing-dependencies-beyond-partial-application">¶</a>Managing dependencies beyond partial application</h2>
<p>Let’s start from how our code from above will eventually look like at the end of this step:</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">let</span> changePass env <span class="operator">=</span> <span class="keyword">fun</span> req <span class="operator">-></span> <span class="keyword">task</span> {</span><br><span class="line"> <span class="keyword">let!</span> user <span class="operator">=</span> Db.fetchUser env req.UserId</span><br><span class="line"> <span class="keyword">if</span> user.Hash <span class="operator">=</span> bcrypt user.Salt req.OldPass <span class="keyword">then</span></span><br><span class="line"> <span class="keyword">let</span> salt <span class="operator">=</span> Random.bytes env <span class="number">32</span></span><br><span class="line"> <span class="keyword">do!</span> Db.updateUser <span class="keyword">env</span> { user <span class="keyword">with</span> Salt <span class="operator">=</span> salt; Hash <span class="operator">=</span> bcrypt salt req.NewPass }</span><br><span class="line"> Log.info env <span class="string">"Changed password for user %i"</span> user.Id</span><br><span class="line"> <span class="keyword">return</span> <span class="literal">Ok</span> ()</span><br><span class="line"> <span class="keyword">else</span> </span><br><span class="line"> Log.error env <span class="string">"Password change unauthorized: user %i"</span> user.Id</span><br><span class="line"> <span class="keyword">return</span> <span class="literal">Error</span> <span class="string">"Old password is invalid"</span></span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>As you may notice, all of our partially applied parameters disappeared, replaced by some single cryptic <code>env</code> parameter. We’ll get there soon. We also packed similar capabilities into corresponding modules (<code>Db</code>/<code>Log</code>/<code>Random</code>). Lets start from defining them:</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="meta">[<Interface>]</span></span><br><span class="line"><span class="keyword">type</span> <span class="title class_">ILogger</span> <span class="operator">=</span></span><br><span class="line"> <span class="keyword">abstract</span> Debug<span class="operator">:</span> <span class="type">string</span> <span class="operator">-></span> <span class="type">unit</span></span><br><span class="line"> <span class="keyword">abstract</span> <span class="literal">Error</span><span class="operator">:</span> <span class="type">string</span> <span class="operator">-></span> <span class="type">unit</span> </span><br><span class="line"></span><br><span class="line"><span class="meta">[<Interface>]</span> <span class="keyword">type</span> <span class="title class_">ILog</span> <span class="operator">=</span> <span class="keyword">abstract</span> Logger<span class="operator">:</span> ILogger</span><br><span class="line"></span><br><span class="line"><span class="keyword">module</span> Log <span class="operator">=</span></span><br><span class="line"> <span class="keyword">let</span> debug (env<span class="operator">:</span> #ILog) fmt <span class="operator">=</span> Printf.kprintf env.Logger.Debug fmt</span><br><span class="line"> <span class="keyword">let</span> error (env<span class="operator">:</span> #ILog) fmt <span class="operator">=</span> Printf.kprintf env.Logger.<span class="literal">Error</span> fmt</span><br></pre></td></tr></table></figure>
<p>Now we can say something more about <code>env</code>. The secret is in <code>#ILog</code> signature, which means that our environment can be any generic type implementing <code>ILog</code> interface. As soon you’ll see, this approach is highly composable, but before that we’ll need another module:</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="meta">[<Interface>]</span></span><br><span class="line"><span class="keyword">type</span> <span class="title class_">IDatabase</span> <span class="operator">=</span></span><br><span class="line"> <span class="keyword">abstract</span> Query<span class="operator">:</span> <span class="type">string</span> <span class="operator">*</span> <span class="symbol">'i</span> <span class="operator">-></span> Task<span class="operator"><</span><span class="symbol">'o</span><span class="operator">></span></span><br><span class="line"> <span class="keyword">abstract</span> Execute<span class="operator">:</span> <span class="type">string</span> <span class="operator">*</span> <span class="symbol">'i</span> <span class="operator">-></span> Task</span><br><span class="line"> </span><br><span class="line"><span class="meta">[<Interface>]</span> <span class="keyword">type</span> <span class="title class_">IDb</span> <span class="operator">=</span> <span class="keyword">abstract</span> Database<span class="operator">:</span> IDatabase</span><br><span class="line"></span><br><span class="line"><span class="keyword">module</span> Db <span class="operator">=</span> </span><br><span class="line"> <span class="keyword">let</span> fetchUser (env<span class="operator">:</span> #IDb) userId <span class="operator">=</span> </span><br><span class="line"> env.Database.Query(Sql.FetchUser, {<span class="operator">|</span> userId <span class="operator">=</span> userId <span class="operator">|</span>})</span><br><span class="line"> <span class="keyword">let</span> updateUser (env<span class="operator">:</span> #IDb) user <span class="operator">=</span> env.Database.Execute(Sql.UpdateUser, user)</span><br></pre></td></tr></table></figure>
<p>Now what will happen if we use functions from both <code>Log</code> and <code>Db</code> modules? As it turns out, F# compiler can properly infer generic constraints over these interfaces. The result <code>env</code> type constraint is inferred to be an union - just like set union, which also means that it handles duplicates for us - of all constraints of other functions using <code>env</code> in its scope:</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">let</span> foo env <span class="operator">=</span> <span class="comment">// env :> IDb and env :> ILog</span></span><br><span class="line"> <span class="keyword">let</span> user <span class="operator">=</span> Db.fetchUser env <span class="number">123</span> <span class="comment">// env :> IDb</span></span><br><span class="line"> Log.debug env <span class="string">"User: %A"</span> user <span class="comment">// env :> ILog</span></span><br></pre></td></tr></table></figure>
<p>Now why did we use two separate interfaces (<code>ILog</code>/<code>ILogger</code>) instead of making environment implement <code>ILogger</code> directly? This is more practical approach that will let us isolate capabilities of particular modules rather than putting them flat into our environment. Example:</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">module</span> Log <span class="operator">=</span></span><br><span class="line"> <span class="keyword">let</span> live <span class="operator">:</span> ILogger <span class="operator">=</span> <span class="operator">??</span> <span class="comment">// create logger interface</span></span><br><span class="line"></span><br><span class="line"><span class="meta">[<Struct>]</span></span><br><span class="line"><span class="keyword">type</span> <span class="title class_">AppEnv</span> <span class="operator">=</span> </span><br><span class="line"> <span class="keyword">interface</span> ILog <span class="keyword">with</span> <span class="keyword">member</span> _.Logger <span class="operator">=</span> Log.live</span><br><span class="line"> <span class="keyword">interface</span> IDb <span class="keyword">with</span> <span class="keyword">member</span> _.Database <span class="operator">=</span> Db.live connectionString</span><br><span class="line"> </span><br><span class="line">foo (AppEnv())</span><br></pre></td></tr></table></figure>
<p>We cannot eagerly provide a specific implementation of <code>ILog</code>/<code>IDb</code>, because they’re yet to be defined as part of by our environment type (which may need to implement many interfaces). To maintain module encapsulation <code>Log</code> module shouldn’t be aware of existence or implementation of <code>IDb</code> interface and vice versa for <code>Db</code> module. What we can do however is to provide <code>live</code> implementation of <code>ILogger</code>, which encapsulates capabilities required by the <code>Log</code> module. This way we don’t need to know details of <code>ILogger</code> when defining our environment type.</p>
<p>Strong sides of this approach?:</p>
<ul>
<li>We only need to provide a single environment object instead of (potentially) unbounded list of parameters. Since it’s always one, it’s easier to generalize and compose other functions over it.</li>
<li>Unlike in reflection-based dependency injection frameworks - everything is still safe and checked by the compiler. If our environment type will not implement an interface required somewhere in the call chain, our code will simply not compile.</li>
<li>It’s still fairly easy to unit test - each function defines only the generic type constraints that it uses in its own call tree, NOT all of the constraints required by the application.</li>
<li>New dependencies are added implicitly - if your code uses module that requires additional capability, it will be automatically inferred by the compiler and bubble up to our environment type definition. No need to add new function parameters or to pass new argument. Also - unlike the object oriented IoC containers - there’s no need to add new dependency as a field or constructor argument.</li>
<li>It gives some opinionated approach on what should be a dependency - less thinking of <em>“should that be a function or interface?”</em> or <em>“if these two functions correspond to the same capability, should they be passed separately?”</em>, which arguably may be a good thing.</li>
<li>It doesn’t impose specific restrictions on libraries and frameworks.</li>
</ul>
<p>Now we could as well stop here - IMHO this approach is already good and useful for most cases. We can also try to push it further. As you’ve seen, our code now requires quite a lot of <code>env</code> passing around. Could we do something about this? It turns out that yes, we could.</p>
<h2 id="Reader-monad"><a class="header-anchor" href="#Reader-monad">¶</a>Reader monad</h2>
<p>Before we continue: what we’re going to cover now is less useful in terms of current state of F# ecosystem for the reasons I’ll mention later.</p>
<p>The pattern we’ll use here is known as a <a href="https://fsharpforfunandprofit.com/posts/elevated-world-6/?ref=bartoszsypytkowski.com">Reader Monad</a>. While it’s useful in certain situations, it’s not widely used - IMO it’s fault lies in the name itself, which somehow managed to sound both borderline meaningless and scary in ears of many developers.</p>
<p>The rest of this blog post will be introduction to this style in F#, however focused solely around problem of dependency management - we’ll ignore other aspects of monads.</p>
<p>We’ll going to reuse our environment type from above, but now encode it directly into another type we’ll call <code>Effect</code>. Since I’ve mentioned that our pattern has M-word in it, you can safely assume that our handler’s logic will be defined as a lazy sequence of steps to be executed (sounds almost like async/await). In F# we’ll sugar them by using custom computation expression (I’m going to call it <code>effect { ... }</code>) returning our effect type, which we’ll define as:</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="meta">[<Struct>]</span> <span class="keyword">type</span> <span class="title class_">Effect</span><span class="operator"><</span><span class="symbol">'env</span>, <span class="symbol">'out</span><span class="operator">></span> <span class="operator">=</span> Effect <span class="keyword">of</span> (<span class="symbol">'env</span> <span class="operator">-></span> <span class="symbol">'out</span>)</span><br></pre></td></tr></table></figure>
<p>Where:</p>
<ul>
<li><code>env</code> is our environment type we already talked about above.</li>
<li><code>out</code> defines a returned value type of our effect.</li>
</ul>
<p>Eventually, with this type in hand our simple request handler will be looking like that:</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">let</span> changePass req <span class="operator">=</span> <span class="keyword">effect</span> {</span><br><span class="line"> <span class="keyword">let!</span> user <span class="operator">=</span> Db.fetchUser req.UserId</span><br><span class="line"> <span class="keyword">if</span> user.Hash <span class="operator">=</span> bcrypt user.Salt req.OldPass <span class="keyword">then</span></span><br><span class="line"> <span class="keyword">let!</span> salt <span class="operator">=</span> Random.bytes <span class="number">32</span></span><br><span class="line"> <span class="keyword">do!</span> Db.<span class="keyword">updateUser</span> { user <span class="keyword">with</span> Salt <span class="operator">=</span> salt; Hash <span class="operator">=</span> bcrypt salt req.NewPass }</span><br><span class="line"> <span class="keyword">do!</span> Log.info <span class="string">"Changed password for user %i"</span> user.Id</span><br><span class="line"> <span class="keyword">return</span> <span class="literal">Ok</span> ()</span><br><span class="line"> <span class="keyword">else</span> </span><br><span class="line"> <span class="keyword">do!</span> Log.error <span class="string">"Password change unauthorized: user %i"</span> user.Id</span><br><span class="line"> <span class="keyword">return</span> <span class="literal">Error</span> <span class="string">"Old password is invalid"</span></span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>As you see, there’s no more <code>env</code> parameter being passed around. It’s now an implicit part of our effect expression. However at the moment we didn’t provide enough infrastructure in our code to make that thing work. What we’re going to need is a set of operators, we can use to make our computation expression happen.</p>
<p>First we’re going to need some <code>Effect<'env,'out></code> constructors:</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">module</span> Effect <span class="operator">=</span></span><br><span class="line"> <span class="comment">/// Create value with no dependency requirements.</span></span><br><span class="line"> <span class="keyword">let</span> <span class="keyword">inline</span> value (x<span class="operator">:</span> <span class="symbol">'out</span>)<span class="operator">:</span> Effect<span class="operator"><</span><span class="symbol">'env</span>,<span class="symbol">'out</span><span class="operator">></span> <span class="operator">=</span> Effect (<span class="keyword">fun</span> _ <span class="operator">-></span> x)</span><br><span class="line"> <span class="comment">/// Create value which uses depenendency.</span></span><br><span class="line"> <span class="keyword">let</span> <span class="keyword">inline</span> apply (fn<span class="operator">:</span> <span class="symbol">'env</span> <span class="operator">-></span> <span class="symbol">'out</span>)<span class="operator">:</span> Effect<span class="operator"><</span><span class="symbol">'env</span>,<span class="symbol">'out</span><span class="operator">></span> <span class="operator">=</span> Effect fn</span><br></pre></td></tr></table></figure>
<p>We also need some way to run our effect to be able to make it… well effectful:</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">module</span> Effect <span class="operator">=</span></span><br><span class="line"></span><br><span class="line"> <span class="comment">(* ...other functions... *)</span></span><br><span class="line"> </span><br><span class="line"> <span class="keyword">let</span> run (env<span class="operator">:</span> <span class="symbol">'env</span>) (Effect fn)<span class="operator">:</span> <span class="symbol">'out</span> <span class="operator">=</span> fn env</span><br></pre></td></tr></table></figure>
<p>And since we already mentioned <code>Effect</code> is monad, we also gonna need a <code>bind</code> function as well if we want to compose our effects together:</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">module</span> Effect <span class="operator">=</span></span><br><span class="line"></span><br><span class="line"> <span class="comment">(* ...other functions... *)</span></span><br><span class="line"> </span><br><span class="line"> <span class="keyword">let</span> <span class="keyword">inline</span> bind (fn<span class="operator">:</span> <span class="symbol">'a</span> <span class="operator">-></span> Effect<span class="operator"><</span><span class="symbol">'env</span>,<span class="symbol">'b</span><span class="operator">></span>) effect <span class="operator">=</span></span><br><span class="line"> Effect (<span class="keyword">fun</span> env <span class="operator">-></span></span><br><span class="line"> <span class="keyword">let</span> x <span class="operator">=</span> run env effect <span class="comment">// compute result of the first effect</span></span><br><span class="line"> run env (fn x) <span class="comment">// run second effect, based on result of first one</span></span><br><span class="line"> )</span><br></pre></td></tr></table></figure>
<p>This is pretty much it. We’re just going to add compose all of these into builder type to make it usable as F# computation expression:</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="meta">[<Struct>]</span></span><br><span class="line"><span class="keyword">type</span> <span class="title class_">EffectBuilder</span> <span class="operator">=</span></span><br><span class="line"> <span class="keyword">member</span> <span class="keyword">inline</span> __.Return value <span class="operator">=</span> Effect.value value</span><br><span class="line"> <span class="keyword">member</span> <span class="keyword">inline</span> __.Zero () <span class="operator">=</span> Effect.value (Unchecked.defaultof<span class="operator"><</span>_<span class="operator">></span>)</span><br><span class="line"> <span class="keyword">member</span> <span class="keyword">inline</span> __.ReturnFrom (effect<span class="operator">:</span> Effect<span class="operator"><</span><span class="symbol">'env</span>, <span class="symbol">'out</span><span class="operator">></span>) <span class="operator">=</span> effect</span><br><span class="line"> <span class="keyword">member</span> <span class="keyword">inline</span> __.Bind(effect, fn) <span class="operator">=</span> Effect.bind fn effect</span><br><span class="line"> </span><br><span class="line"><span class="keyword">let</span> effect <span class="operator">=</span> EffectBuilder()</span><br></pre></td></tr></table></figure>
<p>Of course this, we still need to adapt the modules we prepared earlier to now operate on effects rater than plain functions. We can make this easier by using our <code>Effect.apply</code> function, like:</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">module</span> Log <span class="operator">=</span> </span><br><span class="line"> <span class="keyword">let</span> debug fmt <span class="operator">=</span></span><br><span class="line"> <span class="keyword">let</span> ap s <span class="operator">=</span> Effect.apply (<span class="keyword">fun</span> (x<span class="operator">:</span> #ILog) <span class="operator">-></span> x.Logger.Debug s)</span><br><span class="line"> Printf.kprintf ap fmt</span><br></pre></td></tr></table></figure>
<p>So - as you may have noticed in final form of our effect-based <code>changePass</code> function - in result we almost fully erased all of the dependency-wiring code from our example. There are several downsides of this approach:</p>
<ul>
<li>We do a lot of more bindings (see <code>let!</code>/<code>do!</code> expressions), which means more lambda closures, indirection (wait to see call stacks) and more allocations.</li>
<li>Altogether we also erased <code>task { ... }</code> computation expression and with it an out-of-the-box ability to write asynchronous code. This is one of the downsides of using monads - cross-type composition is painful.</li>
</ul>
<p>Of course we could enrich our <code>Effect</code> type to be able to bind it with <code>Task</code>/<code>Async</code>. That however means, that our pattern grows in complexity and becomes more of a framework rather than something to be easily applied into existing code. Is that bad? Not necessarily, but for sure comes with a bigger commitment, as now you’re not only writing business logic but eventually maintain new effect library. Maybe in future this concept will grow into its own space in favor of the F# ecosystem.</p>
<h2 id="Summary"><a class="header-anchor" href="#Summary">¶</a>Summary</h2>
<p>We came from partial application as tool for dependency injection, over more structured approach promoting single environment type with help of powerful F# type inference, up to encapsulating it into a Reader Monad. That’s a long way. I hope you’ll give it a try and it will help you determine the approach that works for you.</p>
]]></content>
<tags>
<tag>Technique</tag>
<tag>Archive</tag>
</tags>
</entry>
<entry>
<title>F# 重载解析问题</title>
<url>/2025/03/30/F-%E9%87%8D%E8%BD%BD%E8%A7%A3%E6%9E%90%E9%97%AE%E9%A2%98/</url>
<content><![CDATA[<p>当在 F# 中使用一些 C# 类库的时候,可能会遇到类似如下的问题:</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">open</span> System</span><br><span class="line"><span class="keyword">open</span> System.Numerics</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> someFunction () <span class="operator">=</span></span><br><span class="line"> <span class="keyword">let</span> v<span class="operator">:</span> Vector<span class="operator"><</span><span class="type">byte</span><span class="operator">></span> <span class="operator">=</span> Vector(<span class="number">0</span>uy)</span><br><span class="line"> <span class="keyword">let</span> array<span class="operator">:</span> <span class="type">byte</span>[] <span class="operator">=</span> [<span class="operator">||</span>]</span><br><span class="line"> <span class="keyword">let</span> span<span class="operator">:</span> Span<span class="operator"><</span><span class="type">byte</span><span class="operator">></span> <span class="operator">=</span> Span(array)</span><br><span class="line"> v.CopyTo(span)</span><br><span class="line">Compiler error</span><br><span class="line"> v.CopyTo(span)</span><br><span class="line"> <span class="operator">----^^^^^^^^^^^^^^</span></span><br><span class="line"></span><br><span class="line"><span class="operator">/</span>home<span class="operator">/</span>muqiu<span class="operator">/</span>stdin(<span class="number">99</span>,<span class="number">5</span>)<span class="operator">:</span> error FS0041<span class="operator">:</span> A unique overload <span class="keyword">for</span> method <span class="symbol">'CopyTo</span>' could <span class="built_in">not</span> be determined based on <span class="keyword">type</span> information prior <span class="keyword">to</span> this program point. A <span class="keyword">type</span> annotation may be needed.</span><br><span class="line"></span><br><span class="line">Known <span class="keyword">type</span> <span class="title class_">of</span> argument<span class="operator">:</span> Span<span class="operator"><</span><span class="type">byte</span><span class="operator">></span></span><br><span class="line"></span><br><span class="line">Candidates<span class="operator">:</span></span><br><span class="line"> <span class="operator">-</span> Vector.CopyTo(destination<span class="operator">:</span> Span<span class="operator"><</span><span class="type">byte</span><span class="operator">></span>) <span class="operator">:</span> <span class="type">unit</span></span><br><span class="line"> <span class="operator">-</span> Vector.CopyTo(destination<span class="operator">:</span> Span<span class="operator"><</span><span class="type">byte</span><span class="operator">></span>) <span class="operator">:</span> <span class="type">unit</span></span><br></pre></td></tr></table></figure>
<p>编译器认为存在两个相同的 <code>CopyTo</code> 重载无法区分。但查阅文档发现,<code>Vector<T></code> 的 <code>CopyTo</code> 方法实际上只有一个匹配的重载(接受 <code>Span<T></code>),这似乎矛盾。</p>
<p>这是因为 F# 编译器处理泛型方法重载的方式:</p>
<ul>
<li>泛型接口继承:<code>Vector<T></code> 可能实现了多个接口,导致编译器看到两个签名相同的 <code>CopyTo</code> 方法(例如通过不同接口继承)。</li>
<li>语言设计差异:F# 缺乏 C# 的 “most concrete” 重载选择机制。当多个重载来自不同继承路径时,F# 不会自动选择最具体的实现,需要显式指引。</li>
</ul>
<p>仅我所知的一种解决方案是通过添加扩展方法显式指引编译器:</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">type</span> <span class="title class_">Vector</span><span class="operator"><</span><span class="symbol">'T</span><span class="operator">></span> <span class="keyword">with</span></span><br><span class="line"> <span class="keyword">member</span> <span class="keyword">inline</span> this.CopyToByteSpan (span <span class="operator">:</span> Span<span class="operator"><</span><span class="type">byte</span><span class="operator">></span>) <span class="operator">=</span> </span><br><span class="line"> this.CopyTo(span) <span class="comment">// 显式绑定具体重载</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 调用时使用新方法</span></span><br><span class="line">v.CopyToByteSpan(span)</span><br></pre></td></tr></table></figure>
<p>这种方法通过创建具体的类型路径,帮助编译器绕过复杂的重载解析逻辑。</p>
<p>学艺不精,不知道这是不是语言设计的差异,可能 F# 倾向于要求更明确的类型信息以避免意外行为?或许当泛型类型继承多个接口时,有没有可能出现在具体类型中不易察觉的隐式重载冲突?</p>
<h2 id="Refs"><a class="header-anchor" href="#Refs">¶</a>Refs.</h2>
<ul>
<li><a href="https://github.com/fsharp/fslang-suggestions/issues/905">“Most concrete” tiebreaker for generic overloads #905</a></li>
<li><a href="https://github.com/dotnet/fsharp/issues/1647">Fail to resolve to non generic overload. #1647</a></li>
</ul>
]]></content>
<tags>
<tag>Technique</tag>
</tags>
</entry>
<entry>
<title>Fever of Unknown Origin</title>
<url>/2023/11/16/Fever-of-Unknown-Origin/</url>
<content><![CDATA[<p>Fever of unknown origin (FUO) has been recognized as a disease state for over100 years.<br>
The first commonly accepted definition of FUO was published in 1961 and included fever greater than 101F (38.3C) on multiple occasions at least 3 week duration of fever without identifiable cause despite at least a 1 week inpatient evaluation.</p>
<p>Overtime, this definition was considered both too rigid and too ill defined.<br>
Given the impracticality of prolonged hospitalizations and advancing technology for diagnosis, the duration of work up was shortened to 3 days inpatient evaluation and/or 3 outpatient clinic visits.</p>
<ul>
<li><a href="/papers/Fever%20of%20Unknown%20Origin.pdf">Original Document</a></li>
</ul>
<p>这是FUO(不明原因发烧)的综合指南,第一个普遍接受的 FUO 定义是1961 年发布的,包括多次发烧超过三十八度三,发烧持续时间至少 3 周,并且就算住院至少 1 周但仍无明确原因。后面这个定义被认为过于僵化且过于模糊。又改成了三天住院评估和/或三次门。除了有关评估持续时间的所有时间要求,还应该将 FUO 定义为完成最低限度检查后缺乏已知病因的发烧。</p>
<p>常见原因就感染性,自身免疫和结缔组织疾病,其他炎症过程和恶性肿瘤。结缔组织疾病和自身免疫性疾病是青少年 FUO 比儿童更常见的原因。<br>
总体上有超过两百种病因可导致 FUO</p>
]]></content>
<tags>
<tag>Medicine</tag>
</tags>
</entry>
<entry>
<title>Functional Reactive Programming in F#</title>
<url>/2024/09/12/Functional-Reactive-Programming-in-F/</url>
<content><![CDATA[<blockquote>
<p><a href="https://fsharpforfunandprofit.com/posts/concurrency-reactive/">https://fsharpforfunandprofit.com/posts/concurrency-reactive/</a></p>
</blockquote>
<p>Events are everywhere. Almost every program has to handle events, whether it be button clicks in the user interface, listening to sockets in a server, or even a system shutdown notification.</p>
<p>And events are the basis of one of the most common OO design patterns: the “Observer” pattern.</p>
<p>But as we know, event handling, like concurrency in general, can be tricky to implement. Simple event logic is straightforward, but what about logic like “do something if two events happen in a row but do something different if only one event happens” or “do something if two events happen at roughly the same time”. And how easy is it to combine these requirements in other, more complex ways?</p>
<p>Even if you can successfully implement these requirements, the code tends to be spaghetti like and hard to understand, even with the best intentions.</p>
<p>Is there an approach that can make event handling easier?</p>
<p>We saw in the previous post on message queues that one of the advantages of that approach was that the requests were “serialized” making it conceptually easier to deal with.</p>
<p>There is a similar approach that can be used with events. The idea is to turn a series of events into an “event stream”. Event streams then become quite like IEnumerables, and so the obvious next step is to treat them in much the the same way that LINQ handles collections, so that they can be filtered, mapped, split and combined.</p>
<p>F# has built in support for this model, as well as for the more traditional approach.</p>
<h2 id="A-simple-event-stream"><a class="header-anchor" href="#A-simple-event-stream">¶</a>A simple event stream</h2>
<p>Let’s start with a simple example to compare the two approaches. We’ll implement the classic event handler approach first.</p>
<p>First, we define a utility function that will:</p>
<ul>
<li>create a timer</li>
<li>register a handler for the <code>Elapsed</code> event</li>
<li>run the timer for five seconds and then stop it</li>
</ul>
<p>Here’s the code:</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">open</span> System</span><br><span class="line"><span class="keyword">open</span> System.Threading</span><br><span class="line"></span><br><span class="line"><span class="comment">/// create a timer and register an event handler,</span></span><br><span class="line"><span class="comment">/// then run the timer for five seconds</span></span><br><span class="line"><span class="keyword">let</span> createTimer timerInterval eventHandler <span class="operator">=</span></span><br><span class="line"> <span class="comment">// setup a timer</span></span><br><span class="line"> <span class="keyword">let</span> timer <span class="operator">=</span> <span class="keyword">new</span> System.Timers.Timer(float timerInterval)</span><br><span class="line"> timer.AutoReset <span class="operator"><-</span> <span class="literal">true</span></span><br><span class="line"></span><br><span class="line"> <span class="comment">// add an event handler</span></span><br><span class="line"> timer.Elapsed.Add eventHandler</span><br><span class="line"></span><br><span class="line"> <span class="comment">// return an async task</span></span><br><span class="line"> <span class="keyword">async</span> {</span><br><span class="line"> <span class="comment">// start timer...</span></span><br><span class="line"> timer.Start()</span><br><span class="line"> <span class="comment">// ...run for five seconds...</span></span><br><span class="line"> <span class="keyword">do!</span> Async.Sleep <span class="number">5000</span></span><br><span class="line"> <span class="comment">// ... and stop</span></span><br><span class="line"> timer.Stop()</span><br><span class="line"> }</span><br></pre></td></tr></table></figure>
<p>Now test it interactively:</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="comment">// create a handler. The event args are ignored</span></span><br><span class="line"><span class="keyword">let</span> basicHandler _ <span class="operator">=</span> <span class="built_in">printfn</span> <span class="string">"tick %A"</span> DateTime.Now</span><br><span class="line"></span><br><span class="line"><span class="comment">// register the handler</span></span><br><span class="line"><span class="keyword">let</span> basicTimer1 <span class="operator">=</span> createTimer <span class="number">1000</span> basicHandler</span><br><span class="line"></span><br><span class="line"><span class="comment">// run the task now</span></span><br><span class="line">Async.RunSynchronously basicTimer1</span><br></pre></td></tr></table></figure>
<p>Now let’s create a similar utility method to create a timer, but this time it will return an “observable” as well, which is the stream of events.</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">let</span> createTimerAndObservable timerInterval <span class="operator">=</span></span><br><span class="line"> <span class="comment">// setup a timer</span></span><br><span class="line"> <span class="keyword">let</span> timer <span class="operator">=</span> <span class="keyword">new</span> System.Timers.Timer(float timerInterval)</span><br><span class="line"> timer.AutoReset <span class="operator"><-</span> <span class="literal">true</span></span><br><span class="line"></span><br><span class="line"> <span class="comment">// events are automatically IObservable</span></span><br><span class="line"> <span class="keyword">let</span> observable <span class="operator">=</span> timer.Elapsed</span><br><span class="line"></span><br><span class="line"> <span class="comment">// return an async task</span></span><br><span class="line"> <span class="keyword">let</span> task <span class="operator">=</span> <span class="keyword">async</span> {</span><br><span class="line"> timer.Start()</span><br><span class="line"> <span class="keyword">do!</span> Async.Sleep <span class="number">5000</span></span><br><span class="line"> timer.Stop()</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">// return a async task and the observable</span></span><br><span class="line"> (task,observable)</span><br></pre></td></tr></table></figure>
<p>And again test it interactively:</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="comment">// create the timer and the corresponding observable</span></span><br><span class="line"><span class="keyword">let</span> basicTimer2 , timerEventStream <span class="operator">=</span> createTimerAndObservable <span class="number">1000</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// register that every time something happens on the</span></span><br><span class="line"><span class="comment">// event stream, print the time.</span></span><br><span class="line">timerEventStream</span><br><span class="line"><span class="operator">|></span> Observable.subscribe (<span class="keyword">fun</span> _ <span class="operator">-></span> <span class="built_in">printfn</span> <span class="string">"tick %A"</span> DateTime.Now)</span><br><span class="line"></span><br><span class="line"><span class="comment">// run the task now</span></span><br><span class="line">Async.RunSynchronously basicTimer2</span><br></pre></td></tr></table></figure>
<p>The difference is that instead of registering a handler directly with an event, we are “subscribing” to an event stream. Subtly different, and important.</p>
<h2 id="Counting-events"><a class="header-anchor" href="#Counting-events">¶</a>Counting events</h2>
<p>In this next example, we’ll have a slightly more complex requirement:</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">Create a timer that ticks every 500ms.</span><br><span class="line">At each tick, print the number of ticks so far and the current time.</span><br></pre></td></tr></table></figure>
<p>To do this in a classic imperative way, we would probably create a class with a mutable counter, as below:</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">type</span> <span class="title class_">ImperativeTimerCount</span>() <span class="operator">=</span></span><br><span class="line"></span><br><span class="line"> <span class="keyword">let</span> <span class="keyword">mutable</span> count <span class="operator">=</span> <span class="number">0</span></span><br><span class="line"></span><br><span class="line"> <span class="comment">// the event handler. The event args are ignored</span></span><br><span class="line"> <span class="keyword">member</span> this.handleEvent _ <span class="operator">=</span></span><br><span class="line"> count <span class="operator"><-</span> count <span class="operator">+</span> <span class="number">1</span></span><br><span class="line"> <span class="built_in">printfn</span> <span class="string">"timer ticked with count %i"</span> count</span><br></pre></td></tr></table></figure>
<p>We can reuse the utility functions we created earlier to test it:</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="comment">// create a handler class</span></span><br><span class="line"><span class="keyword">let</span> handler <span class="operator">=</span> <span class="keyword">new</span> ImperativeTimerCount()</span><br><span class="line"></span><br><span class="line"><span class="comment">// register the handler method</span></span><br><span class="line"><span class="keyword">let</span> timerCount1 <span class="operator">=</span> createTimer <span class="number">500</span> handler.handleEvent</span><br><span class="line"></span><br><span class="line"><span class="comment">// run the task now</span></span><br><span class="line">Async.RunSynchronously timerCount1</span><br></pre></td></tr></table></figure>
<p>Let’s see how we would do this same thing in a functional way:</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="comment">// create the timer and the corresponding observable</span></span><br><span class="line"><span class="keyword">let</span> timerCount2, timerEventStream <span class="operator">=</span> createTimerAndObservable <span class="number">500</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// set up the transformations on the event stream</span></span><br><span class="line">timerEventStream</span><br><span class="line"><span class="operator">|></span> Observable.scan (<span class="keyword">fun</span> count _ <span class="operator">-></span> count <span class="operator">+</span> <span class="number">1</span>) <span class="number">0</span></span><br><span class="line"><span class="operator">|></span> Observable.subscribe (<span class="keyword">fun</span> count <span class="operator">-></span> <span class="built_in">printfn</span> <span class="string">"timer ticked with count %i"</span> count)</span><br><span class="line"></span><br><span class="line"><span class="comment">// run the task now</span></span><br><span class="line">Async.RunSynchronously timerCount2</span><br></pre></td></tr></table></figure>
<p>Here we see how you can build up layers of event transformations, just as you do with list transformations in LINQ.</p>
<p>The first transformation is <code>scan</code>, which accumulates state for each event. It is roughly equivalent to the <code>List.fold</code> function that we have seen used with lists. In this case, the accumulated state is just a counter.</p>
<p>And then, for each event, the count is printed out.</p>
<p>Note that in this functional approach, we didn’t have any mutable state, and we didn’t need to create any special classes.</p>
<h2 id="Merging-multiple-event-streams"><a class="header-anchor" href="#Merging-multiple-event-streams">¶</a>Merging multiple event streams</h2>
<p>For a final example, we’ll look at merging multiple event streams.</p>
<p>Let’s make a requirement based on the well-known “FizzBuzz” problem:</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">Create two timers, called '3' and '5'. The '3' timer ticks every 300ms and the '5' timer ticks</span><br><span class="line">every 500ms.</span><br><span class="line"></span><br><span class="line">Handle the events as follows:</span><br><span class="line">a) for all events, print the id of the time and the time</span><br><span class="line">b) when a tick is simultaneous with a previous tick, print 'FizzBuzz'</span><br><span class="line">otherwise:</span><br><span class="line">c) when the '3' timer ticks on its own, print 'Fizz'</span><br><span class="line">d) when the '5' timer ticks on its own, print 'Buzz'</span><br></pre></td></tr></table></figure>
<p>First let’s create some code that both implementations can use.</p>
<p>We’ll want a generic event type that captures the timer id and the time of the tick.</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">type</span> <span class="title class_">FizzBuzzEvent</span> <span class="operator">=</span> {label<span class="operator">:</span><span class="type">int</span>; time<span class="operator">:</span> DateTime}</span><br></pre></td></tr></table></figure>
<p>And then we need a utility function to see if two events are simultaneous. We’ll be generous and allow a time difference of up to 50ms.</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">let</span> areSimultaneous (earlierEvent,laterEvent) <span class="operator">=</span></span><br><span class="line"> <span class="keyword">let</span> {label<span class="operator">=</span>_;time<span class="operator">=</span>t1} <span class="operator">=</span> earlierEvent</span><br><span class="line"> <span class="keyword">let</span> {label<span class="operator">=</span>_;time<span class="operator">=</span>t2} <span class="operator">=</span> laterEvent</span><br><span class="line"> t2.Subtract(t1).Milliseconds <span class="operator"><</span> <span class="number">50</span></span><br></pre></td></tr></table></figure>
<p>In the imperative design, we’ll need to keep track of the previous event, so we can compare them. And we’ll need special case code for the first time, when the previous event doesn’t exist</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">type</span> <span class="title class_">ImperativeFizzBuzzHandler</span>() <span class="operator">=</span></span><br><span class="line"></span><br><span class="line"> <span class="keyword">let</span> <span class="keyword">mutable</span> previousEvent<span class="operator">:</span> FizzBuzzEvent <span class="type">option</span> <span class="operator">=</span> <span class="literal">None</span></span><br><span class="line"></span><br><span class="line"> <span class="keyword">let</span> printEvent thisEvent <span class="operator">=</span></span><br><span class="line"> <span class="keyword">let</span> {label<span class="operator">=</span><span class="built_in">id</span>; time<span class="operator">=</span>t} <span class="operator">=</span> thisEvent</span><br><span class="line"> <span class="built_in">printf</span> <span class="string">"[%i] %i.%03i "</span> <span class="built_in">id</span> t.Second t.Millisecond</span><br><span class="line"> <span class="keyword">let</span> simultaneous <span class="operator">=</span> previousEvent.IsSome <span class="operator">&&</span> areSimultaneous (previousEvent.Value,thisEvent)</span><br><span class="line"> <span class="keyword">if</span> simultaneous <span class="keyword">then</span> <span class="built_in">printfn</span> <span class="string">"FizzBuzz"</span></span><br><span class="line"> <span class="keyword">elif</span> <span class="built_in">id</span> <span class="operator">=</span> <span class="number">3</span> <span class="keyword">then</span> <span class="built_in">printfn</span> <span class="string">"Fizz"</span></span><br><span class="line"> <span class="keyword">elif</span> <span class="built_in">id</span> <span class="operator">=</span> <span class="number">5</span> <span class="keyword">then</span> <span class="built_in">printfn</span> <span class="string">"Buzz"</span></span><br><span class="line"></span><br><span class="line"> <span class="keyword">member</span> this.handleEvent3 eventArgs <span class="operator">=</span></span><br><span class="line"> <span class="keyword">let</span> event <span class="operator">=</span> {label<span class="operator">=</span><span class="number">3</span>; time<span class="operator">=</span>DateTime.Now}</span><br><span class="line"> printEvent event</span><br><span class="line"> previousEvent <span class="operator"><-</span> <span class="literal">Some</span> event</span><br><span class="line"></span><br><span class="line"> <span class="keyword">member</span> this.handleEvent5 eventArgs <span class="operator">=</span></span><br><span class="line"> <span class="keyword">let</span> event <span class="operator">=</span> {label<span class="operator">=</span><span class="number">5</span>; time<span class="operator">=</span>DateTime.Now}</span><br><span class="line"> printEvent event</span><br><span class="line"> previousEvent <span class="operator"><-</span> <span class="literal">Some</span> event</span><br></pre></td></tr></table></figure>
<p>Now the code is beginning to get ugly fast! Already we have mutable state, complex conditional logic, and special cases, just for such a simple requirement.</p>
<p>Let’s test it:</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="comment">// create the class</span></span><br><span class="line"><span class="keyword">let</span> handler <span class="operator">=</span> <span class="keyword">new</span> ImperativeFizzBuzzHandler()</span><br><span class="line"></span><br><span class="line"><span class="comment">// create the two timers and register the two handlers</span></span><br><span class="line"><span class="keyword">let</span> timer3 <span class="operator">=</span> createTimer <span class="number">300</span> handler.handleEvent3</span><br><span class="line"><span class="keyword">let</span> timer5 <span class="operator">=</span> createTimer <span class="number">500</span> handler.handleEvent5</span><br><span class="line"></span><br><span class="line"><span class="comment">// run the two timers at the same time</span></span><br><span class="line">[timer3;timer5]</span><br><span class="line"><span class="operator">|></span> Async.Parallel</span><br><span class="line"><span class="operator">|></span> Async.RunSynchronously</span><br></pre></td></tr></table></figure>
<p>It does work, but are you sure the code is not buggy? Are you likely to accidentally break something if you change it?</p>
<p>The problem with this imperative code is that it has a lot of noise that obscures the the requirements.</p>
<p>Can the functional version do better? Let’s see!</p>
<p>First, we create <em>two</em> event streams, one for each timer:</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">let</span> timer3, timerEventStream3 <span class="operator">=</span> createTimerAndObservable <span class="number">300</span></span><br><span class="line"><span class="keyword">let</span> timer5, timerEventStream5 <span class="operator">=</span> createTimerAndObservable <span class="number">500</span></span><br></pre></td></tr></table></figure>
<p>Next, we convert each event on the “raw” event streams into our FizzBuzz event type:</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="comment">// convert the time events into FizzBuzz events with the appropriate id</span></span><br><span class="line"><span class="keyword">let</span> eventStream3 <span class="operator">=</span></span><br><span class="line"> timerEventStream3</span><br><span class="line"> <span class="operator">|></span> Observable.map (<span class="keyword">fun</span> _ <span class="operator">-></span> {label<span class="operator">=</span><span class="number">3</span>; time<span class="operator">=</span>DateTime.Now})</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> eventStream5 <span class="operator">=</span></span><br><span class="line"> timerEventStream5</span><br><span class="line"> <span class="operator">|></span> Observable.map (<span class="keyword">fun</span> _ <span class="operator">-></span> {label<span class="operator">=</span><span class="number">5</span>; time<span class="operator">=</span>DateTime.Now})</span><br></pre></td></tr></table></figure>
<p>Now, to see if two events are simultaneous, we need to compare them from the two different streams somehow.</p>
<p>It’s actually easier than it sounds, because we can:</p>
<ul>
<li>combine the two streams into a single stream:</li>
<li>then create pairs of sequential events</li>
<li>then test the pairs to see if they are simultaneous</li>
<li>then split the input stream into two new output streams based on that test</li>
</ul>
<p>Here’s the actual code to do this:</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="comment">// combine the two streams</span></span><br><span class="line"><span class="keyword">let</span> combinedStream <span class="operator">=</span></span><br><span class="line"> Observable.merge eventStream3 eventStream5</span><br><span class="line"></span><br><span class="line"><span class="comment">// make pairs of events</span></span><br><span class="line"><span class="keyword">let</span> pairwiseStream <span class="operator">=</span></span><br><span class="line"> combinedStream <span class="operator">|></span> Observable.pairwise</span><br><span class="line"></span><br><span class="line"><span class="comment">// split the stream based on whether the pairs are simultaneous</span></span><br><span class="line"><span class="keyword">let</span> simultaneousStream, nonSimultaneousStream <span class="operator">=</span></span><br><span class="line"> pairwiseStream <span class="operator">|></span> Observable.partition areSimultaneous</span><br></pre></td></tr></table></figure>
<p>Finally, we can split the <code>nonSimultaneousStream</code> again, based on the event id:</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="comment">// split the non-simultaneous stream based on the id</span></span><br><span class="line"><span class="keyword">let</span> fizzStream, buzzStream <span class="operator">=</span></span><br><span class="line"> nonSimultaneousStream</span><br><span class="line"> <span class="comment">// convert pair of events to the first event</span></span><br><span class="line"> <span class="operator">|></span> Observable.map (<span class="keyword">fun</span> (ev1,_) <span class="operator">-></span> ev1)</span><br><span class="line"> <span class="comment">// split on whether the event id is three</span></span><br><span class="line"> <span class="operator">|></span> Observable.partition (<span class="keyword">fun</span> {label<span class="operator">=</span><span class="built_in">id</span>} <span class="operator">-></span> <span class="built_in">id</span><span class="operator">=</span><span class="number">3</span>)</span><br></pre></td></tr></table></figure>
<p>Let’s review so far. We have started with the two original event streams and from them created four new ones:</p>
<ul>
<li><code>combinedStream</code> contains all the events</li>
<li><code>simultaneousStream</code> contains only the simultaneous events</li>
<li><code>fizzStream</code> contains only the non-simultaneous events with id=3</li>
<li><code>buzzStream</code> contains only the non-simultaneous events with id=5</li>
</ul>
<p>Now all we need to do is attach behavior to each stream:</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="comment">//print events from the combinedStream</span></span><br><span class="line">combinedStream</span><br><span class="line"><span class="operator">|></span> Observable.subscribe (<span class="keyword">fun</span> {label<span class="operator">=</span><span class="built_in">id</span>;time<span class="operator">=</span>t} <span class="operator">-></span></span><br><span class="line"> <span class="built_in">printf</span> <span class="string">"[%i] %i.%03i "</span> <span class="built_in">id</span> t.Second t.Millisecond)</span><br><span class="line"></span><br><span class="line"><span class="comment">//print events from the simultaneous stream</span></span><br><span class="line">simultaneousStream</span><br><span class="line"><span class="operator">|></span> Observable.subscribe (<span class="keyword">fun</span> _ <span class="operator">-></span> <span class="built_in">printfn</span> <span class="string">"FizzBuzz"</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">//print events from the nonSimultaneous streams</span></span><br><span class="line">fizzStream</span><br><span class="line"><span class="operator">|></span> Observable.subscribe (<span class="keyword">fun</span> _ <span class="operator">-></span> <span class="built_in">printfn</span> <span class="string">"Fizz"</span>)</span><br><span class="line"></span><br><span class="line">buzzStream</span><br><span class="line"><span class="operator">|></span> Observable.subscribe (<span class="keyword">fun</span> _ <span class="operator">-></span> <span class="built_in">printfn</span> <span class="string">"Buzz"</span>)</span><br></pre></td></tr></table></figure>
<p>Let’s test it:</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="comment">// run the two timers at the same time</span></span><br><span class="line">[timer3;timer5]</span><br><span class="line"><span class="operator">|></span> Async.Parallel</span><br><span class="line"><span class="operator">|></span> Async.RunSynchronously</span><br></pre></td></tr></table></figure>
<p>Here’s all the code in one complete set:</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="comment">// create the event streams and raw observables</span></span><br><span class="line"><span class="keyword">let</span> timer3, timerEventStream3 <span class="operator">=</span> createTimerAndObservable <span class="number">300</span></span><br><span class="line"><span class="keyword">let</span> timer5, timerEventStream5 <span class="operator">=</span> createTimerAndObservable <span class="number">500</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// convert the time events into FizzBuzz events with the appropriate id</span></span><br><span class="line"><span class="keyword">let</span> eventStream3 <span class="operator">=</span> timerEventStream3</span><br><span class="line"> <span class="operator">|></span> Observable.map (<span class="keyword">fun</span> _ <span class="operator">-></span> {label<span class="operator">=</span><span class="number">3</span>; time<span class="operator">=</span>DateTime.Now})</span><br><span class="line"><span class="keyword">let</span> eventStream5 <span class="operator">=</span> timerEventStream5</span><br><span class="line"> <span class="operator">|></span> Observable.map (<span class="keyword">fun</span> _ <span class="operator">-></span> {label<span class="operator">=</span><span class="number">5</span>; time<span class="operator">=</span>DateTime.Now})</span><br><span class="line"></span><br><span class="line"><span class="comment">// combine the two streams</span></span><br><span class="line"><span class="keyword">let</span> combinedStream <span class="operator">=</span></span><br><span class="line"> Observable.merge eventStream3 eventStream5</span><br><span class="line"></span><br><span class="line"><span class="comment">// make pairs of events</span></span><br><span class="line"><span class="keyword">let</span> pairwiseStream <span class="operator">=</span></span><br><span class="line"> combinedStream <span class="operator">|></span> Observable.pairwise</span><br><span class="line"></span><br><span class="line"><span class="comment">// split the stream based on whether the pairs are simultaneous</span></span><br><span class="line"><span class="keyword">let</span> simultaneousStream, nonSimultaneousStream <span class="operator">=</span></span><br><span class="line"> pairwiseStream <span class="operator">|></span> Observable.partition areSimultaneous</span><br><span class="line"></span><br><span class="line"><span class="comment">// split the non-simultaneous stream based on the id</span></span><br><span class="line"><span class="keyword">let</span> fizzStream, buzzStream <span class="operator">=</span></span><br><span class="line"> nonSimultaneousStream</span><br><span class="line"> <span class="comment">// convert pair of events to the first event</span></span><br><span class="line"> <span class="operator">|></span> Observable.map (<span class="keyword">fun</span> (ev1,_) <span class="operator">-></span> ev1)</span><br><span class="line"> <span class="comment">// split on whether the event id is three</span></span><br><span class="line"> <span class="operator">|></span> Observable.partition (<span class="keyword">fun</span> {label<span class="operator">=</span><span class="built_in">id</span>} <span class="operator">-></span> <span class="built_in">id</span><span class="operator">=</span><span class="number">3</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">//print events from the combinedStream</span></span><br><span class="line">combinedStream</span><br><span class="line"><span class="operator">|></span> Observable.subscribe (<span class="keyword">fun</span> {label<span class="operator">=</span><span class="built_in">id</span>;time<span class="operator">=</span>t} <span class="operator">-></span></span><br><span class="line"> <span class="built_in">printf</span> <span class="string">"[%i] %i.%03i "</span> <span class="built_in">id</span> t.Second t.Millisecond)</span><br><span class="line"></span><br><span class="line"><span class="comment">//print events from the simultaneous stream</span></span><br><span class="line">simultaneousStream</span><br><span class="line"><span class="operator">|></span> Observable.subscribe (<span class="keyword">fun</span> _ <span class="operator">-></span> <span class="built_in">printfn</span> <span class="string">"FizzBuzz"</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">//print events from the nonSimultaneous streams</span></span><br><span class="line">fizzStream</span><br><span class="line"><span class="operator">|></span> Observable.subscribe (<span class="keyword">fun</span> _ <span class="operator">-></span> <span class="built_in">printfn</span> <span class="string">"Fizz"</span>)</span><br><span class="line"></span><br><span class="line">buzzStream</span><br><span class="line"><span class="operator">|></span> Observable.subscribe (<span class="keyword">fun</span> _ <span class="operator">-></span> <span class="built_in">printfn</span> <span class="string">"Buzz"</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">// run the two timers at the same time</span></span><br><span class="line">[timer3;timer5]</span><br><span class="line"><span class="operator">|></span> Async.Parallel</span><br><span class="line"><span class="operator">|></span> Async.RunSynchronously</span><br></pre></td></tr></table></figure>
<p>The code might seem a bit long winded, but this kind of incremental, step-wise approach is very clear and self-documenting.</p>
<p>Some of the benefits of this style are:</p>
<ul>
<li>I can see that it meets the requirements just by looking at it, without even running it. Not so with the imperative version.</li>
<li>From a design point of view, each final “output” stream follows the single responsibility principle – it only does one thing – so it is very easy to associate behavior with it.</li>
<li>This code has no conditionals, no mutable state, no edge cases. It would be easy to maintain or change, I hope.</li>
<li>It is easy to debug. For example, I could easily “tap” the output of the <code>simultaneousStream</code> to see if it contains what I think it contains:</li>
</ul>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="comment">// debugging code</span></span><br><span class="line"><span class="comment">//simultaneousStream |> Observable.subscribe (fun e -> printfn "sim %A" e)</span></span><br><span class="line"><span class="comment">//nonSimultaneousStream |> Observable.subscribe (fun e -> printfn "non-sim %A" e)</span></span><br></pre></td></tr></table></figure>
<p>This would be much harder in the imperative version.</p>
<h2 id="Summary"><a class="header-anchor" href="#Summary">¶</a>Summary</h2>
<p>Functional Reactive Programming (known as FRP) is a big topic, and we’ve only just touched on it here. I hope this introduction has given you a glimpse of the usefulness of this way of doing things.</p>
<p>If you want to learn more, see the documentation for the F# <a href="https://docs.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2010/ee370313%28v=vs.100%29?redirectedfrom=MSDN">Observable module</a>, which has the basic transformations used above. And there is also the <a href="https://docs.microsoft.com/en-us/previous-versions/dotnet/reactive-extensions/hh242985%28v=vs.103%29">Reactive Extensions (Rx)</a> library which shipped as part of .NET 4. That contains many other additional transformations.</p>
]]></content>
<tags>
<tag>Technique</tag>
<tag>Archive</tag>
</tags>
</entry>
<entry>
<title>G-Machine</title>
<url>/2024/01/23/G-Machine/</url>
<content><![CDATA[<ul>
<li><a href="https://link.springer.com/chapter/10.1007/3-540-15975-4_50">The G-machine: A fast, graph-reduction evaluator</a></li>
</ul>
<p>G-Machine 是一种通过图规约来对函数式语言程序求值的抽象架构。<br>
与组合子规约不同,组合子规约的control是从表达式图本身动态导出的,而G-Machine是由通过编译Application表达式导出的指令序列指定的。</p>
<hr>
<p>FP的程序基本上都可以用一个表达式的图表示,图计算机就是对这个图求值的机器,总的说来对图的求值是一个不停合并图上的节点生产新节点的过程。</p>
<p>例如:</p>
<figure class="highlight ocaml"><table><tr><td class="code"><pre><span class="line"><span class="keyword">let</span> x = <span class="number">2</span> + <span class="number">3</span> <span class="keyword">in</span> </span><br><span class="line"> x * x</span><br></pre></td></tr></table></figure>
<p>先计算出5,然后创建一个新的节点 <code>5 * 5</code>,然后再对这个节点求值,于是求值过程中就产生了很多临时的节点,这些中间节点也被叫做是 spine,求值过程是沿着 spine 进行的。</p>
<p>但是这样就产生了很多额外的开销,lambda lifting 里提到:可以把程序里,很多捕捉了外围绑定的闭包函数中的这些绑定,转换成函数的参数,从而消除闭包,得到的这个函数就可以自由脱离作用域,被静态的编译到机器码里。这些被 float out 的函数也叫 supercombinator.</p>
<p>在上面的代码中,如果不创建新的节点,顺序计算完了第一个 <code>x</code>,第二个 <code>x</code> 还会再被算一遍。</p>
<p>Spineless reduction 的概念:只有当面临需要重复计算的情况时,才去创建节点,不然就一路顺序算下去</p>
<ul>
<li><a href="https://en.wikipedia.org/wiki/Graph_reduction">Graph reduction</a></li>
<li><a href="https://en.wikipedia.org/wiki/Graph_reduction_machine">Graph reduction machine</a></li>
<li><a href="https://www.zhihu.com/question/54834531/answer/144213668">https://www.zhihu.com/question/54834531/answer/144213668</a></li>
</ul>
]]></content>
<tags>
<tag>Technique</tag>
</tags>
</entry>
<entry>
<title>什么是 G⁺</title>
<url>/2025/10/19/Gram-stain/</url>
<content><![CDATA[<p><strong>G⁺(Gram‑positive)</strong> 是细菌在 <strong>Gram 染色</strong>(Gram stain)技术下呈现的染色特征。Gram 染色是一种经典的显微镜染色方法,依据细菌细胞壁结构的差异,将细菌分为两大类:<strong>Gram‑positive(G⁺)</strong> 和 <strong>Gram‑negative(G⁻)</strong>。在染色程序中,细菌首先被结晶紫染料染色,随后加入碘液形成复合物,接着用酒精或丙酮脱色,最后用沙黄或复红复染。若细菌的细胞壁能够 <strong>保留结晶紫‑碘复合物</strong>,则在显微镜下呈现紫蓝色,这类细菌即被归类为 <strong>Gram‑positive(G⁺)</strong>;若细胞壁结构不保留复合物而被脱色,随后被沙黄染成红色,则为 <strong>Gram‑negative(G⁻)</strong>。</p>
<p>G⁺ 细菌的细胞壁外层主要由 <strong>厚度约 20–80 nm 的肽聚糖(peptidoglycan)</strong> 组成,这层结构密集且富含交联的糖肽链,能够在脱色步骤中形成物理屏障,阻止酒精/丙酮的渗透,从而保留结晶紫‑碘复合物。</p>
<p>与 G⁻ 细菌不同,G⁺ 细菌没有外膜(outer membrane)和脂多糖(LPS)层。外膜的缺失使得脱色剂难以进入细胞内部,进一步加强了紫色的保留。</p>
<p>某些 G⁺ 细菌在细胞壁上表达的酶(如酸性磷酸酶)可与染料形成更为稳定的复合物,进一步提升染色的持久性。</p>
<p>在呼吸道痰涂片中出现 <strong>“有荚膜的双球菌(capsulated diplococci)”</strong>,最典型的病原体是 <strong>肺炎链球菌(Streptococcus pneumoniae)</strong>。它是一种 <strong>革兰氏阳性(G⁺)</strong>、<strong>α‑hemolytic</strong>、<strong>嗜血清素</strong> 的双球菌,表面包裹有厚厚的多糖荚膜,正是该荚膜赋予它在显微镜下呈现明显的荧光或“胶状”外观。</p>
<p>在痰涂片或血培养的显微镜检查中,观察到 G⁺ 双球菌提示肺炎链球菌感染的可能性较大,帮助临床医生在培养结果出来前就能启动经验性抗生素治疗(如 β‑lactam 类药物)。</p>
<p>肺炎链球菌的荚膜多型性(> 90 种血清型)是疫苗设计的核心依据,同时也影响抗体介导的吞噬作用。了解其 G⁺ 特性有助于解释为何某些血清型(如 3 型)在免疫逃逸方面更具优势。</p>
]]></content>
<tags>
<tag>Medicine</tag>
</tags>
</entry>
<entry>
<title>多人协作系统中的实现策略 (CRDT,锁等实现方案)</title>
<url>/2025/05/08/Multiplayer-Collaborative-Systems-tips/</url>
<content><![CDATA[<p>最近碰到一块业务:在系统中可以存在多个用户同时对某个项目信息进行编辑,这种多人协作的场景挺有意思的,不过在我们的业务中,并不需要实时协作,只需要保证不会出错就行,话虽如此,但也可以探索一下实时协作的实现方案,防止老年痴呆。</p>
<p>先来看看第一个方案 —— CRDT(Conflict-free Replicated Data Type,无冲突可复制数据类型)是一类数据结构,它保证了在分布式节点(或多客户端)上进行离线/并发更新后,无需中心协调、也无需人工干预,通过“合并策略”就能得到一致的最终状态。</p>
<p>核心思想是:所有并发操作都是幂等(idempotent)、可交换(commutative)的。</p>
<p>常见类型有:<br>
一、G-Counter(只能增计数器)<br>
二、PN-Counter(可增可减计数器)<br>
三、LWW-Register(最后写入胜出)<br>
四、结合 JSON 的树型 CRDT(如 <a href="https://github.com/automerge/automerge">Automerge</a> / <a href="https://yjs.dev/">Yjs</a>)</p>
<p>更多原理可参考 Decipad 博客“Collaborative and Offline Editing Using CRDTs”<sup class="footnote-ref"><a href="#fn1" id="fnref1">[1]</a></sup>。</p>
<blockquote>
<p>有一个挺有趣的 Rust 项目 <a href="https://github.com/loro-dev/loro">Loro: Make your JSON data collaborative and version-controlled with CRDTs</a></p>
</blockquote>
<p>假设我的项目信息编辑页面允许多人实时/离线修改某个研究项目的“名称”、“描述”字段,前端用 SvelteKit + GraphQL 获取和提交变更:</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">┌── 用户 A 离线修改了 “description” 的若干段文本 </span><br><span class="line">└── 用户 B 同时在线修改了同一字段的其他段落 </span><br></pre></td></tr></table></figure>
<p>如果后端使用 CRDT(比如把 <code>description</code> 用 JSON-CRDT 存储),两次修改只要在任意顺序合并都能得到完整的内容:</p>
<p>首先,A 客户端本地 apply 操作并缓存,恢复网络后推给服务器;<br>
然后,服务器用 CRDT merge(A.delta, B.delta),得到一致文档<br>
最后,服务器广播新文档到所有客户端,A/B 均得到相同结果</p>
<hr>
<p>好了说点实际符合业务场景的方案,首先想到的是悲观锁(Pessimistic Locking) ,思路是:用户打开编辑界面时,向后端申请“锁” → 其它用户尝试编辑时被拒绝 → 编辑完成后释放锁/超时自动释放。</p>
<p>假如有一个这样的锁表:</p>
<pre><code class="language-sql">CREATE TABLE project_lock (
project_id UUID PRIMARY KEY,
locked_by UUID NOT NULL,
expires_at TIMESTAMPTZ NOT NULL
);
</code></pre>
<p>可以在事务内申请它:</p>
<pre><code class="language-ts">const now = new Date();
const expires = new Date(now.getTime() + 5*60*1000); // 5 分钟后过期
await prisma.$transaction(async tx => {
const existing = await tx.project_lock.findUnique({ where:{ project_id } });
if (existing && existing.expires_at > now) {
throw new Error('项目正被人编辑');
}
await tx.project_lock.upsert({
where: { project_id },
update: { locked_by: userId, expires_at: expires },
create: { project_id, locked_by: userId, expires_at: expires }
});
});
</code></pre>
<p>释放锁就直接从锁表里删掉对应的数据即可:</p>
<pre><code class="language-ts">await prisma.project_lock.delete({ where:{ project_id } });
</code></pre>
<p>前端的话,大概就是:</p>
<p>在进入编辑前请求一下 <code>/api/project/:id/lock</code> 之类的 API,失败则提示“被占用”;<br>
在 <code>onbeforeunload</code> 时执行 <code>/unlock</code>;<br>
超时后后端自动允许新锁。</p>
<hr>
<p>第二个方案是乐观并发控制(Optimistic Concurrency) :记录资源的版本号或时间戳;客户端提交更新时带上自己的版本号,后端检查版本是否一致,不一致则认为冲突,返回 409,由客户端告知用户“数据已过期,请刷新后合并”。</p>
<p>具体实现中,可以尝试在 <code>project</code> 表加上 <code>version INT NOT NULL DEFAULT 1, updated_at TIMESTAMPTZ</code> ,然后更新项目时:</p>
<pre><code class="language-ts">async updateProject(parent, { id, version, input }, ctx) {
const result = await prisma.$executeRaw`
UPDATE project
SET name = ${input.name},
description = ${input.description},
version = version + 1,
updated_at = now()
WHERE id = ${id} AND version = ${version}
`;
if (result === 0) {
throw new ConflictException('版本冲突,请刷新后重试');
}
return prisma.project.findUnique({ where:{ id } });
}
</code></pre>
<p>前端捕获到冲突错误可以用一个弹窗提示“另有用户已更新此项目,是否合并/重新加载?” 之类的玩意儿。</p>
<hr>
<p>第三个方案是:操作转化(Operational Transformation,OT)</p>
<p>也就是记录用户每次的“操作”(insert/delete at position),服务器根据历史操作序列对并发操作做转化(transform),确保先到达的操作调整后再应用后到达的。</p>
<p>有一些实现案例:<br>
一、ShareDB(Node.js)<br>
二、Google Docs 中的同步算法</p>
<p>具体实现的话,可能要现在前端逐字符/块地包装成操作并 WebSocket 推送,服务器再维护一个“操作历史队列”,每来一个 op 就 transform 并 broadcast,而客户端收到广播后,按顺序 replay 保证视图一致。</p>
<hr>
<p>最后可能还可以用事件溯源(Event Sourcing)+ 场景命令模式来实现:</p>
<p>不直接存状态,而是存所有“命令 / 事件”(Event),回放事件得到当前状态。冲突通过合并策略或补偿事件(Compensating Events)解决。</p>
<p>例如:<br>
在每次更新时推送 <code>ProjectUpdated { projectId, fieldsChanged, userId, timestamp }</code> ,<br>
然后写入事件存储(如 Kafka / EventStoreDB),<br>
读端 Consumer 按顺序重建最新状态或按领域聚合 ,<br>
最后在并发时如果两个事件都修改了同一字段,可在写端做校验/补偿,或在读端做最后写入胜出等策略 。</p>
<hr>
<p>总结来说,</p>
<ul>
<li>CRDT 最擅长 去中心化、离线编辑、自动合并;</li>
<li>若不引入 CRDT,可根据业务侧重点选用:
<ol>
<li>悲观锁 → 强制串行编辑,简单粗暴;</li>
<li>乐观并发 → 适合大多数业务场景,成本低;</li>
<li>OT → 适合富文本或实时协同场景,复杂度中等;</li>
<li>事件溯源 → 适合需要全历史审计、可回放的场景。</li>
</ol>
</li>
</ul>
<hr>
<hr class="footnotes-sep">
<section class="footnotes">
<ol class="footnotes-list">
<li id="fn1" class="footnote-item"><p>Decipad 博客 “Collaborative and Offline Editing Using CRDTs”<br>
<a href="https://www.decipad.com/blog/decipads-innovative-method-collaborative-and-offline-editing-using-crdts">https://www.decipad.com/blog/decipads-innovative-method-collaborative-and-offline-editing-using-crdts</a> <a href="#fnref1" class="footnote-backref">↩︎</a></p>
</li>
</ol>
</section>
]]></content>
<tags>
<tag>Technique</tag>
</tags>
</entry>
<entry>
<title>N+1 selects problem 与 Prisma ORM</title>
<url>/2025/04/02/N-1-selects-problem-%E4%B8%8E-Prisma-ORM/</url>
<content><![CDATA[<p>N+1 查询问题是指在通过 ORM 查询数据时,执行了一次初始查询来获取父对象列表(这 1 次查询),然后为列表中的每一个父对象都单独执行了一次额外的查询来获取其关联的子对象(这 N 次查询)。最终导致总共执行了 1 + N 次数据库查询,其中 N 是初始查询返回的父对象的数量。</p>
<p>举个例子:</p>
<p>假设有两个数据库模型:<code>User</code>(用户)和 <code>Post</code>(帖子),一个用户可以有多篇帖子(一对多关系)。</p>
<p>现在,需要获取前 10 个用户以及他们各自的所有帖子。</p>
<p>一种有问题的 ORM 实现(或不当的使用方式)可能会这样执行:</p>
<ol>
<li>第一次查询 (The “1”): 获取前 10 个用户。<figure class="highlight sql"><table><tr><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span> <span class="keyword">FROM</span> <span class="keyword">User</span> LIMIT <span class="number">10</span>;</span><br></pre></td></tr></table></figure>
</li>
<li>接下来的 N (=10) 次查询 (The “N”): 对于上一步获取到的每一个用户,单独执行一次查询来获取该用户的帖子。<figure class="highlight sql"><table><tr><td class="code"><pre><span class="line"><span class="comment">-- 用户 1</span></span><br><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span> <span class="keyword">FROM</span> Post <span class="keyword">WHERE</span> authorId <span class="operator">=</span> <span class="number">1</span>;</span><br><span class="line"><span class="comment">-- 用户 2</span></span><br><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span> <span class="keyword">FROM</span> Post <span class="keyword">WHERE</span> authorId <span class="operator">=</span> <span class="number">2</span>;</span><br><span class="line"><span class="comment">-- 用户 3</span></span><br><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span> <span class="keyword">FROM</span> Post <span class="keyword">WHERE</span> authorId <span class="operator">=</span> <span class="number">3</span>;</span><br><span class="line"><span class="comment">-- ... 直到 用户 10</span></span><br><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span> <span class="keyword">FROM</span> Post <span class="keyword">WHERE</span> authorId <span class="operator">=</span> <span class="number">10</span>;</span><br></pre></td></tr></table></figure>
</li>
</ol>
<p>在这个场景下,总共执行了 1 + 10 = 11 次数据库查询。如果 N 的值很大(比如获取 1000 个用户),就会产生 1001 次查询,这对数据库造成巨大的、不必要的压力,并显著增加应用程序的响应时间。每一次数据库交互都有网络延迟和数据库处理的开销,N+1 次查询会将这些开销放大 N 倍。</p>
<p>N+1 问题通常源于 ORM 处理关联数据的方式,特别是与“懒加载”(Lazy Loading)相关的策略。懒加载是指只有在显式访问关联属性时,ORM 才会去数据库加载这些数据。虽然这在某些情况下可以避免加载不需要的数据,但如果在循环中访问关联属性,就很容易触发 N+1 问题。</p>
<p>然而,问题的根源在于没有有效地预先加载(或批量加载)所需的关联数据。即使不使用严格意义上的懒加载,如果 ORM 在处理关联查询时不够智能,采用了逐个获取关联对象的策略,同样会产生 N+1 查询。</p>
<p>在 Prisma 出现之前或在其他 ORM 中,解决 N+1 问题常见的方法包括:</p>
<ol>
<li>预先加载(Eager Loading): 在执行初始查询时,就明确指示 ORM 同时将关联数据也查询出来。这通常通过 SQL 的 <code>JOIN</code> 操作实现。例如,一次性查询出用户和他们的帖子。虽然这减少了查询次数,但复杂的 <code>JOIN</code> 可能会导致查询本身变得庞大和低效,并可能返回冗余数据。</li>
<li>批量加载(Batch Loading): 先执行初始查询获取父对象列表,然后收集所有父对象的 ID,在第二次查询中使用 <code>WHERE IN (...)</code> 子句一次性加载所有相关的子对象。这种方式通常需要两次查询,但避免了 N 次单独的查询。</li>
</ol>
<p>Prisma ORM 在设计上就考虑了 N+1 问题,并提供了一种既方便开发者又高效的解决方案。当使用 Prisma Client 查询数据并需要包含关联模型时,Prisma 会自动优化查询,避免产生 N+1 查询,主要通过关系查询(Relation Queries)中的 <code>include</code> 选项或嵌套读取(nested reads)来实现这一点:</p>
<p>假设想获取所有用户及其发布的帖子,使用 Prisma Client,可以这样写:</p>
<figure class="highlight typescript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> { <span class="title class_">PrismaClient</span> } <span class="keyword">from</span> <span class="string">'@prisma/client'</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> prisma = <span class="keyword">new</span> <span class="title class_">PrismaClient</span>()</span><br><span class="line"></span><br><span class="line"><span class="keyword">async</span> <span class="keyword">function</span> <span class="title function_">getUsersWithPosts</span>(<span class="params"></span>) {</span><br><span class="line"> <span class="keyword">const</span> usersWithPosts = <span class="keyword">await</span> prisma.<span class="property">user</span>.<span class="title function_">findMany</span>({</span><br><span class="line"> <span class="attr">include</span>: {</span><br><span class="line"> <span class="attr">posts</span>: <span class="literal">true</span>, <span class="comment">// 指示 Prisma 加载关联的 posts</span></span><br><span class="line"> },</span><br><span class="line"> })</span><br><span class="line"> <span class="comment">// usersWithPosts 包含了用户列表,每个用户对象中都有一个 posts 数组</span></span><br><span class="line"> <span class="variable language_">console</span>.<span class="title function_">log</span>(usersWithPosts)</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="title function_">getUsersWithPosts</span>()</span><br><span class="line"> .<span class="title function_">catch</span>(<span class="function">(<span class="params">e</span>) =></span> {</span><br><span class="line"> <span class="keyword">throw</span> e</span><br><span class="line"> })</span><br><span class="line"> .<span class="title function_">finally</span>(<span class="title function_">async</span> () => {</span><br><span class="line"> <span class="keyword">await</span> prisma.$disconnect()</span><br><span class="line"> })</span><br></pre></td></tr></table></figure>
<p>当执行上述查询时,Prisma 不会 生成 N+1 个 SQL 查询。而是首先会分析请求,并将其转化为数量非常有限的高效 SQL 查询。对于上面这个一对多关系的 <code>include</code> 查询,Prisma 通常会执行以下两步(类似于批量加载策略):</p>
<ol>
<li>查询父模型: 获取所有 <code>User</code> 记录。<figure class="highlight sql"><table><tr><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> "public"."User"."id", "public"."User"."name", <span class="comment">/* ... other user fields */</span> <span class="keyword">FROM</span> "public"."User" <span class="keyword">WHERE</span> <span class="number">1</span><span class="operator">=</span><span class="number">1</span></span><br></pre></td></tr></table></figure>
</li>
<li>查询关联的子模型: 使用上一步获取到的所有用户 <code>id</code>,通过 <code>WHERE IN (...)</code> 子句一次性查询所有相关的 <code>Post</code> 记录。<figure class="highlight sql"><table><tr><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> "public"."Post"."id", "public"."Post"."title", "public"."Post"."authorId", <span class="comment">/* ... other post fields */</span> <span class="keyword">FROM</span> "public"."Post" <span class="keyword">WHERE</span> "public"."Post"."authorId" <span class="keyword">IN</span> ($<span class="number">1</span>, $<span class="number">2</span>, $<span class="number">3</span>, ...) <span class="comment">/* 这里的 $1, $2, ... 是第一步查到的用户 ID 列表 */</span></span><br></pre></td></tr></table></figure>
</li>
</ol>
<p>Prisma Client 在内存中将这两次查询的结果高效地组合起来,最终返回嵌套的、符合 TypeScript 类型的数据。</p>
<h2 id="Refs"><a class="header-anchor" href="#Refs">¶</a>Refs.</h2>
<ul>
<li><a href="https://stackoverflow.com/questions/97197/what-is-the-n1-selects-problem-in-orm-object-relational-mapping">Stack Overflow: What is the N+1 selects problem in ORM?</a></li>
<li><a href="https://www.prisma.io/docs/orm/prisma-client/queries/query-optimization-performance#solving-the-n1-problem">Prisma Docs: Solving the N+1 problem</a></li>
</ul>
]]></content>
<tags>
<tag>Technique</tag>
</tags>
</entry>
<entry>
<title>.NET AOT 下的 F# 命令行参数解析库选择</title>
<url>/2024/06/06/NET-AOT-%E4%B8%8B%E7%9A%84-F-%E5%91%BD%E4%BB%A4%E8%A1%8C%E5%8F%82%E6%95%B0%E8%A7%A3%E6%9E%90%E5%BA%93%E9%80%89%E6%8B%A9/</url>
<content><![CDATA[<p>Argu 不支持 AOT,不过用 F# 的话可以看整个 .NET 的生态,我看了一下 C# 的 CommandLineParser:<br>
<a href="https://github.com/commandlineparser">https://github.com/commandlineparser</a></p>
<p>在这个 PR 中支持了 Native AOT<br>
<a href="https://github.com/commandlineparser/commandline/pull/913">https://github.com/commandlineparser/commandline/pull/913</a></p>
<p>除此之外,还有一个更加精巧的 F# 库可以用,只有两百多行:<br>
<a href="https://github.com/B2R2-org/FsOptParse/">https://github.com/B2R2-org/FsOptParse/</a></p>
<p>AOT 后的大小很可观,并且支持 full trim.</p>
<p>用例:</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="comment">(** defines a state to pass to the option parser *)</span></span><br><span class="line"><span class="keyword">type</span> <span class="title class_">opts</span> <span class="operator">=</span></span><br><span class="line"> {</span><br><span class="line"> optX <span class="operator">:</span> <span class="type">int</span>;</span><br><span class="line"> optY <span class="operator">:</span> <span class="type">bool</span>;</span><br><span class="line"> optZ <span class="operator">:</span> <span class="type">string</span>;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"><span class="comment">(** default option state *)</span></span><br><span class="line"><span class="keyword">let</span> defaultOpts <span class="operator">=</span></span><br><span class="line"> {</span><br><span class="line"> optX <span class="operator">=</span> <span class="number">0</span>;</span><br><span class="line"> optY <span class="operator">=</span> <span class="literal">false</span>;</span><br><span class="line"> optZ <span class="operator">=</span> <span class="string">""</span>;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"><span class="comment">(*</span></span><br><span class="line"><span class="comment"> An example command line specification, which is a list of Options.</span></span><br><span class="line"><span class="comment"> Each Option describes a command line option (switch) that is specified with</span></span><br><span class="line"><span class="comment"> either a short (a single-dash option) or long option (a double-dash option).</span></span><br><span class="line"><span class="comment">*)</span></span><br><span class="line"><span class="keyword">let</span> spec <span class="operator">=</span></span><br><span class="line"> [</span><br><span class="line"> <span class="comment">(* This option can be specified with -x <NUM>. There is an extra argument to</span></span><br><span class="line"><span class="comment"> specify a value in integer. *)</span></span><br><span class="line"> Option (<span class="comment">(* description of the option *)</span></span><br><span class="line"> descr<span class="operator">=</span><span class="string">"this is a testing param X"</span>,</span><br><span class="line"> <span class="comment">(* how many extra argument must be provided by a user? *)</span></span><br><span class="line"> extra<span class="operator">=</span><span class="number">1</span>,</span><br><span class="line"> <span class="comment">(* callback sets up the option and returns it *)</span></span><br><span class="line"> callback<span class="operator">=</span>(<span class="keyword">fun</span> opts arg <span class="operator">-></span> {opts <span class="keyword">with</span> optX<span class="operator">=</span>(int) arg.[<span class="number">0</span>]}),</span><br><span class="line"> <span class="comment">(* use a short option style -x *)</span></span><br><span class="line"> short<span class="operator">=</span><span class="string">"-x"</span></span><br><span class="line"> );</span><br><span class="line"></span><br><span class="line"> <span class="comment">(* This option can be specified with -y. There is no extra argument. This</span></span><br><span class="line"><span class="comment"> option just sets a flag, optY. *)</span></span><br><span class="line"> Option (<span class="comment">(* description of the option *)</span></span><br><span class="line"> descr<span class="operator">=</span><span class="string">"this is a testing param Y"</span>,</span><br><span class="line"> <span class="comment">(* set the option to be true *)</span></span><br><span class="line"> callback<span class="operator">=</span>(<span class="keyword">fun</span> opts _ <span class="operator">-></span> {opts <span class="keyword">with</span> optY<span class="operator">=</span><span class="literal">true</span>}),</span><br><span class="line"> <span class="comment">(* use a short option style (-y) *)</span></span><br><span class="line"> short<span class="operator">=</span><span class="string">"-y"</span>,</span><br><span class="line"> <span class="comment">(* also use a long option style (--yoohoo) *)</span></span><br><span class="line"> long<span class="operator">=</span><span class="string">"--yoohoo"</span></span><br><span class="line"> );</span><br><span class="line"></span><br><span class="line"> <span class="comment">(* A dummy option to pretty-print the usage *)</span></span><br><span class="line"> Option (<span class="comment">(* description of the option *)</span></span><br><span class="line"> descr<span class="operator">=</span><span class="string">""</span>,</span><br><span class="line"> dummy<span class="operator">=</span><span class="literal">true</span></span><br><span class="line"> );</span><br><span class="line"> Option (<span class="comment">(* description of the option *)</span></span><br><span class="line"> descr<span class="operator">=</span><span class="string">"[Required Options]"</span>,</span><br><span class="line"> descrColor<span class="operator">=</span>System.ConsoleColor.DarkCyan,</span><br><span class="line"> dummy<span class="operator">=</span><span class="literal">true</span></span><br><span class="line"> );</span><br><span class="line"></span><br><span class="line"> <span class="comment">(* The third option is a required option. In other words, option parsing</span></span><br><span class="line"><span class="comment"> will raise an exception if this option is not given by a user. This</span></span><br><span class="line"><span class="comment"> option takes in an additional integer argument, and set it to the global</span></span><br><span class="line"><span class="comment"> variable z. *)</span></span><br><span class="line"> Option (<span class="comment">(* description of the option *)</span></span><br><span class="line"> descr<span class="operator">=</span><span class="string">"required parameter <STRING> with an integer option"</span>,</span><br><span class="line"> <span class="comment">(* callback to set the optZ value *)</span></span><br><span class="line"> callback<span class="operator">=</span>(<span class="keyword">fun</span> opts arg <span class="operator">-></span> {opts <span class="keyword">with</span> optZ<span class="operator">=</span>arg.[<span class="number">0</span>]}),</span><br><span class="line"> <span class="comment">(* specifying this is a required option *)</span></span><br><span class="line"> required<span class="operator">=</span><span class="literal">true</span>,</span><br><span class="line"> <span class="comment">(* one additional argument to specify an integer value *)</span></span><br><span class="line"> extra<span class="operator">=</span><span class="number">1</span>,</span><br><span class="line"> <span class="comment">(* use only a long option style *)</span></span><br><span class="line"> long<span class="operator">=</span><span class="string">"--req"</span></span><br><span class="line"> );</span><br><span class="line"> ]</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> _ <span class="operator">=</span></span><br><span class="line"> <span class="keyword">let</span> prog <span class="operator">=</span> <span class="string">"opttest.fsx"</span></span><br><span class="line"> <span class="keyword">let</span> args <span class="operator">=</span> System.Environment.GetCommandLineArgs ()</span><br><span class="line"> <span class="keyword">let</span> usageGetter () <span class="operator">=</span> <span class="string">"[Usage]\n %p %o"</span></span><br><span class="line"> <span class="keyword">try</span></span><br><span class="line"> <span class="keyword">let</span> left, opts <span class="operator">=</span> optParse spec usageGetter prog args defaultOpts</span><br><span class="line"> <span class="built_in">printfn</span> <span class="string">"Rest args: %A, x: %d, y: %b, z: %s"</span></span><br><span class="line"> left opts.optX opts.optY opts.optZ</span><br><span class="line"> <span class="number">0</span></span><br><span class="line"> <span class="keyword">with</span></span><br><span class="line"> <span class="operator">|</span> SpecErr msg <span class="operator">-></span></span><br><span class="line"> <span class="built_in">eprintfn</span> <span class="string">"Invalid spec: %s"</span> msg</span><br><span class="line"> exit <span class="number">1</span></span><br><span class="line"> <span class="operator">|</span> RuntimeErr msg <span class="operator">-></span></span><br><span class="line"> <span class="built_in">eprintfn</span> <span class="string">"Invalid args given by user: %s"</span> msg</span><br><span class="line"> usagePrint spec prog usageGetter (<span class="keyword">fun</span> () <span class="operator">-></span> exit <span class="number">1</span>)</span><br></pre></td></tr></table></figure>
]]></content>
<tags>
<tag>Technique</tag>
</tags>
</entry>
<entry>
<title>OCaml Core.Int.pow 的实现</title>
<url>/2023/10/21/OCaml-Core-Int-pow-%E7%9A%84%E5%AE%9E%E7%8E%B0/</url>
<content><![CDATA[<p>Core内部直接引用的Base的实现, Base的实现在 <a href="https://github.com/janestreet/base/blob/494a0876168d24cda695cbb9a3d86ad8d1eb97d8/src/int_math.ml#L11-L18">src/int_math.ml</a> 中:</p>
<figure class="highlight ocaml"><table><tr><td class="code"><pre><span class="line"><span class="keyword">let</span> int_pow base exponent =</span><br><span class="line"> <span class="keyword">if</span> exponent < <span class="number">0</span> <span class="keyword">then</span> negative_exponent <span class="literal">()</span>;</span><br><span class="line"> <span class="keyword">if</span> abs base > <span class="number">1</span></span><br><span class="line"> && (exponent > <span class="number">63</span></span><br><span class="line"> || abs base > <span class="type">Pow_overflow_bounds</span>.int_positive_overflow_bounds.(exponent))</span><br><span class="line"> <span class="keyword">then</span> overflow <span class="literal">()</span>;</span><br><span class="line"> int_math_int_pow base exponent</span><br><span class="line">;;</span><br></pre></td></tr></table></figure>
<p>其中 <code>int_math_int_pow()</code> 由 C 实现:</p>
<figure class="highlight ocaml"><table><tr><td class="code"><pre><span class="line"><span class="keyword">external</span> int_math_int_pow : <span class="built_in">int</span> -> <span class="built_in">int</span> -> <span class="built_in">int</span> = <span class="string">"Base_int_math_int_pow_stub"</span> [@@noalloc]</span><br></pre></td></tr></table></figure>
<p>其实现在 <a href="https://github.com/janestreet/base/blob/494a0876168d24cda695cbb9a3d86ad8d1eb97d8/src/int_math_stubs.c#L56-L92">src/int_math_stubs.c</a> 中:</p>
<figure class="highlight c"><table><tr><td class="code"><pre><span class="line"><span class="type">static</span> <span class="type">int64_t</span> <span class="title function_">int_pow</span><span class="params">(<span class="type">int64_t</span> base, <span class="type">int64_t</span> exponent)</span> {</span><br><span class="line"> <span class="type">int64_t</span> ret = <span class="number">1</span>;</span><br><span class="line"> <span class="type">int64_t</span> mul[<span class="number">4</span>];</span><br><span class="line"> mul[<span class="number">0</span>] = <span class="number">1</span>;</span><br><span class="line"> mul[<span class="number">1</span>] = base;</span><br><span class="line"> mul[<span class="number">3</span>] = <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">while</span> (exponent != <span class="number">0</span>) {</span><br><span class="line"> mul[<span class="number">1</span>] *= mul[<span class="number">3</span>];</span><br><span class="line"> mul[<span class="number">2</span>] = mul[<span class="number">1</span>] * mul[<span class="number">1</span>];</span><br><span class="line"> mul[<span class="number">3</span>] = mul[<span class="number">2</span>] * mul[<span class="number">1</span>];</span><br><span class="line"> ret *= mul[exponent & <span class="number">3</span>];</span><br><span class="line"> exponent >>= <span class="number">2</span>;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> ret;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>这是一个四分快速幂的实现,它是二分快速幂的一种变种。二分快速幂将指数分为两部分,然后递归地计算每一部分的结果。<br>
而四分快速幂将指数分为四部分,然后递归地计算每一部分的结果。<br>
这里通过将指数右移2位(相当于除以4)和使用位与操作来实现,进一步减少了乘法次数。</p>
<p>主要步骤:</p>
<ul>
<li>初始化返回值ret为1,和一个包含4个元素的数组mul, mul[0]和mul[3]被初始化为1, mul[1]被初始化为基数</li>
<li>当指数不为0时,执行循环, 在每次循环中,首先更新mul数组的值
<blockquote>
<p>mul[1]是基数和mul[3]的乘积,mul[2]是mul[1]的平方,mul[3]是mul[2]和基数的乘积</p>
</blockquote>
</li>
<li>然后,将ret乘以mul数组中的一个元素, 这个元素的索引是指数和3的位与运算的结果
<blockquote>
<p>这样做的目的是为了选择正确的乘数,因为指数被分解为4的倍数</p>
</blockquote>
</li>
<li>最后,将指数右移2位,相当于将指数除以4</li>
<li>当指数变为0时,循环结束,返回ret</li>
</ul>
<p>这个实现的优点是它可以在对数时间内计算出幂运算,而且每次循环只需要4次乘法。这比标准的二分快速幂算法需要的乘法次数更少。</p>
]]></content>
<tags>
<tag>Technique</tag>
</tags>
</entry>
<entry>
<title>OCaml News 2024-1</title>
<url>/2024/01/08/OCaml-News-2024-1/</url>
<content><![CDATA[<h2 id="News"><a class="header-anchor" href="#News">¶</a>News</h2>
<ul>
<li><a href="https://tarides.com/blog/2023-12-20-ocaml-survey-developers-perception-interest-and-perceived-barriers/">OCaml Survey: Developers’ Perception, Interest, and Perceived Barriers</a></li>
<li><a href="https://tarides.com/blog/2023-12-29-announcing-the-orchide-project-powering-satellite-innovation/">Announcing the ORCHIDE Project: Powering Satellite Innovation</a></li>
<li><a href="https://github.com/ocaml/ocaml/pull/12885">Dynarrays, unboxed (with local dummies) #12885</a></li>
<li><a href="https://github.com/ocaml/ocaml/pull/12871">Stdlib priority queues #12871</a></li>
<li><a href="https://github.com/ocaml/ocaml/pull/12596">Compile recursive bindings in Lambda #12596</a></li>
<li><a href="https://github.com/ocaml/ocaml/pull/12828">Add short syntax for dependent functor types #12828</a></li>
<li><a href="https://github.com/ocaml/ocaml/pull/12508">[shapes] Add support for project-wide occurrences #12508</a></li>
<li><a href="https://github.com/ocaml/ocaml/pull/1802">Make the character set for OCaml source code officially UTF-8. #1802</a></li>
<li><a href="https://github.com/ocaml/ocaml/pull/12719">Add thread local storage #12719</a></li>
</ul>
<h2 id="Videos"><a class="header-anchor" href="#Videos">¶</a>Videos</h2>
<ul>
<li><a href="https://www.youtube.com/watch?v=zG7JejHlQoM">“Melange: The next frontier in type-safe web development” by Dillon Mulroy - RVAJS 2023</a></li>
<li><a href="https://www.youtube.com/watch?v=R-XJzUrP7bQ">Trying out OCaml TUI framework Mint Tea!</a></li>
<li><a href="https://www.youtube.com/watch?v=XyDbG9FGR1o">A TUI chat in OCaml 🐫</a></li>
<li>[Building a Game Engine… with OCaml ?! [Part 1]](<a href="https://www.youtube.com/watch?v=1XpUaTnssQE">https://www.youtube.com/watch?v=1XpUaTnssQE</a></li>
</ul>
<h2 id="Blogs"><a class="header-anchor" href="#Blogs">¶</a>Blogs</h2>
<ul>
<li><a href="https://sancho.dev/blog/whats-possible-with-melange">What’s possible with Melange</a></li>
<li><a href="https://discuss.ocaml.org/t/access-inferred-types/13805">Access inferred types</a></li>
<li><a href="https://discuss.ocaml.org/t/using-menhir-to-parse-into-idiomatic-js-typescript-structures/13809">Using Menhir to parse into idiomatic JS (TypeScript) structures</a></li>
<li><a href="https://discuss.ocaml.org/t/why-constructors-are-not-curried/13792">Why constructors are not curried?</a></li>
<li><a href="https://discuss.ocaml.org/t/practical-example-of-applicative-vs-generative-functors/13777">Practical example of applicative vs generative functors?</a></li>
<li><a href="https://discuss.ocaml.org/t/compiler-optimization-on-flattening-adt-for-less-boxing/13764">Compiler optimization on flattening ADT for less boxing?</a></li>
<li><a href="https://discuss.ocaml.org/t/how-to-express-koka-home-page-example/13748">How to express Koka home page example?</a></li>
<li><a href="https://practicalocaml.com/parsing-with-binary-string-pattern-matching/">Parsing with Binary String Pattern Matching</a></li>
</ul>
<h2 id="Highlighted-Projects"><a class="header-anchor" href="#Highlighted-Projects">¶</a>Highlighted Projects</h2>
<ul>
<li><a href="https://github.com/backtracking/grid">grid: A tiny library for two-dimensional arrays</a></li>
<li><a href="https://github.com/leostera/minttea">A fun little TUI framework for OCaml</a></li>
<li><a href="https://github.com/terrateamio/ocaml-ts-mode">ocaml-ts-mode: Ocaml mode for emacs using treesitter</a></li>
<li><a href="https://github.com/dmmulroy/create-melange-app">create-melange-app: An example app created by create-melange-app</a></li>
<li><a href="https://github.com/andersfugmann/ppx_protocol_conv">ppx_protocol_conv: Pluggable serialization and deserialization of ocaml data strucures based on type_conv</a></li>
<li><a href="https://spatial-shell.app/">spatial-shell: Spatial Shell is a daemon implementing a spatial model inspired by Material Shell, for i3 and sway. More precisely, it organizes your windows within a grid whose rows are the workspaces of your favorite WM.</a></li>
<li><a href="https://codeberg.org/marcc/fixgen">Fixgen: A language agnostic fixture generator</a></li>
<li><a href="https://github.com/tweag/opam-nix">opam-nix: Turn opam-based OCaml projects into Nix derivations</a></li>
<li><a href="https://github.com/leostera/blink">Blink: A pure OCaml HTTP client for Riot</a></li>
<li><a href="https://github.com/leostera/colors">colors: A pure OCaml library for manipulating colors in different color spaces.</a></li>
<li><a href="https://github.com/nationalarchives/miiify">miilfy: A web annotation server built with the same principles as Git</a></li>
</ul>
<h2 id="New-Releases"><a class="header-anchor" href="#New-Releases">¶</a>New Releases</h2>
<ul>
<li><a href="https://github.com/leostera/riot">Roit v0.0.7: An actor-model multi-core scheduler for OCaml 5 🐫</a></li>
<li>[CAStore: A portable pure OCaml CA Store, with no dependencies, inspired by Elixir’s <a href="https://github.com/elixir-mint/castore">:castore</a>.](<a href="https://github.com/leostera/castore">https://github.com/leostera/castore</a>)</li>
<li><a href="https://discuss.ocaml.org/t/learn-ocaml-1-0-approaching-call-for-testers/13621">Lean-OCaml 1.0: A Web Application for Learning OCaml</a></li>
<li><a href="https://erratique.ch/software/cmarkit">cmarkit 0.3.0" CommonMark parser and renderer for OCaml</a></li>
<li><a href="https://ocaml.org/p/dream-html/latest">dream-html 2.0.0: A library for generating HTML</a></li>
<li><a href="https://git.frama-c.com/pub/caisar/">Caisar: A platform under active development at CEA LIST, aiming to provide a wide range of features to characterize the safety and robustness of artificial intelligence based software.</a></li>
<li><a href="https://github.com/jserot/rfsm">RFSM 2.0: A toolset for describing and simulating StateChart-like state diagrams.</a></li>
<li><a href="https://github.com/chshersh/zbg/tree/main">Zbg 2.0: <code>zbg</code> (short for <strong>Z</strong>ero <strong>B</strong>ullshit <strong>G</strong>it) is a CLI tool for using <code>git</code> efficiently.</a></li>
</ul>
]]></content>
<tags>
<tag>Technique</tag>
</tags>
</entry>
<entry>
<title>OCaml News 2024-2</title>
<url>/2024/01/18/OCaml-News-2024-2/</url>
<content><![CDATA[<h2 id="News"><a class="header-anchor" href="#News">¶</a>News</h2>
<ul>
<li><a href="https://discuss.ocaml.org/t/ann-preview-play-with-project-wide-occurrences-for-ocaml/13814">[ANN][PREVIEW] Play with project-wide occurrences for OCaml!</a></li>
<li><a href="https://github.com/ocaml/dune/pull/8784">[Dune]: Add link flags ocamlmklib when using ctypes stubs. #8784</a></li>
<li><a href="https://discuss.ocaml.org/t/ocaml-software-foundation-january-2024-update/13828">OCaml Software Foundation: January 2024 update</a></li>
<li>[Apprendre à programmer avec OCaml](Apprendre à programmer avec OCaml)</li>
<li>[<a href="https://discuss.ocaml.org/t/call-for-speakers-for-the-2024-carolina-code-conference-is-open-until-april-15th/13827">Call for Speakers for the 2024 Carolina Code Conference is open until April 15th</a>](<a href="https://discuss.ocaml.org/t/call-for-speakers-for-the-2024-carolina-code-conference-is-open-until-april-15th/13827">https://discuss.ocaml.org/t/call-for-speakers-for-the-2024-carolina-code-conference-is-open-until-april-15th/13827</a>)</li>
<li><a href="https://github.com/ocaml/ocaml/discussions/11924">[OCaml]: feature request: better errors #11924</a></li>
<li><a href="https://discuss.ocaml.org/t/ocsigen-summary-of-recent-releases/13817">Ocsigen: summary of recent releases</a></li>
</ul>
<h2 id="Discussion"><a class="header-anchor" href="#Discussion">¶</a>Discussion</h2>
<ul>
<li><a href="https://discuss.ocaml.org/t/benchmark-between-open-addressing-and-closed-addressing-hashtbl/13882">Benchmark between open-addressing and closed-addressing hashtbl</a></li>
<li><a href="https://discuss.ocaml.org/t/examples-of-caqti-infix/13878">Examples of Caqti infix?</a></li>
<li><a href="https://discuss.ocaml.org/t/why-can-t-i-create-a-project-with-non-ascii-characters/13865">Why can’t I create a project with non-ASCII characters? </a></li>
<li><a href="https://discuss.ocaml.org/t/old-self-taught-vs-uni-debate-landscape-for-former-jane-street/13851">Old self-taught vs. uni debate; Landscape for former; Jane Street</a></li>
<li><a href="https://discuss.ocaml.org/t/toml-file-parser/13854">Is there an easy way to read the values from a toml file?</a></li>
<li><a href="https://discuss.ocaml.org/t/generate-typed-ast-fragments/13824">Generate typed AST fragments</a></li>
<li><a href="https://discuss.ocaml.org/t/partially-apply-function-accepting-multiple-modules-and-keep-polymorphic-types/13823">Partially apply function accepting multiple modules and keep polymorphic types</a></li>
<li><a href="https://discuss.ocaml.org/t/printing-unicode-characters-on-different-platforms/13813">Printing Unicode Characters on Different Platforms</a></li>
</ul>
<h2 id="Videos"><a class="header-anchor" href="#Videos">¶</a>Videos</h2>
<ul>
<li><a href="https://watch.ocaml.org/w/iQNqZzA8gVmd4RQaycAwx4">Verifying an Effect-Based Cooperative Concurrency Scheduler in Iris by Adrian Dapprich</a></li>
</ul>
<h2 id="Blogs"><a class="header-anchor" href="#Blogs">¶</a>Blogs</h2>
<ul>
<li><a href="https://discuss.ocaml.org/t/advice-for-combining-multiple-monads/10409">Advice for combining multiple monads</a></li>
<li><a href="https://discuss.ocaml.org/t/writing-ctypes-bindings-to-system-shared-libraries-for-bytecode-targets-via-the-dune-ctypes-stanza/13844">Writing ctypes bindings to system shared libraries for bytecode targets via the dune ctypes stanza</a></li>
</ul>
<h2 id="Highlighted-Projects"><a class="header-anchor" href="#Highlighted-Projects">¶</a>Highlighted Projects</h2>
<ul>
<li><a href="https://github.com/johnyob/grace">Grace: A fancy diagnostics library that allows your compilers to exit with grace</a></li>
<li><a href="https://github.com/thierry-martinez/metapp">meta-pp: Meta-preprocessor for OCaml</a></li>
<li><a href="https://github.com/stedolan/ppx_stage">ppx_stage: Staged metaprogramming in stock OCaml</a></li>
<li>[Cmarkit is an <a href="http://ocaml.org">OCaml</a> libary for parsing the <a href="https://spec.commonmark.org/">CommonMark</a> specification.](<a href="https://erratique.ch/software/cmarkit">https://erratique.ch/software/cmarkit</a>)</li>
<li>[Brr is a toolkit for programming browsers in <a href="http://ocaml.org">OCaml</a> with the <a href="http://ocsigen.org/js_of_ocaml/">js_of_ocaml</a> compiler. It provides:](<a href="https://erratique.ch/software/brr">https://erratique.ch/software/brr</a>)</li>
<li>[Zipc is an in-memory <a href="https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT">ZIP</a> archive and <a href="https://www.rfc-editor.org/rfc/rfc1951">deflate</a> compression codec for <a href="http://ocaml.org">OCaml</a>.](<a href="https://erratique.ch/software/zipc">https://erratique.ch/software/zipc</a>)</li>
<li><a href="https://github.com/lukstafi/ocaml-gccjit">ocaml-gccjit: OCaml bindings for libgccjit</a></li>
<li><a href="https://github.com/whitequark/ocaml-m17n">ocaml-m17n: Multilingualization for the OCaml source code</a></li>
<li><a href="https://github.com/hackwaly/ocamlearlybird">ocamlearlybird: OCaml debug adapter</a></li>
<li><a href="https://github.com/dmbaturin/otoml">otoml: TOML parsing, manipulation, and pretty-printing library for OCaml (fully 1.0.0-compliant)</a></li>
<li><a href="https://github.com/mattjbray/ocaml-decoders">ocaml-decoders: Elm-inspired decoders for Ocaml</a></li>
<li><a href="https://github.com/OCamlPro/ez_toml">ez_toml: A library to parse and print TOML files</a></li>
</ul>
<h2 id="New-Releases"><a class="header-anchor" href="#New-Releases">¶</a>New Releases</h2>
<ul>
<li><a href="http://cambium.inria.fr/~fpottier/oma/doc/oma/Oma/index.html">Oma: OCaml implementation of ‘Two Simplified Algorithms for Maintaining Order in a List’</a>
<ul>
<li><a href="https://discuss.ocaml.org/t/ann-first-release-of-oma/13845">[ANN] First release of oma</a></li>
<li><a href="https://erikdemaine.org/papers/DietzSleator_ESA2002/paper.pdf">Two Simplified Algorithms for Maintaining Order in a List</a></li>
</ul>
</li>
<li><a href="https://github.com/hackwaly/ocamlearlybird">ocamlearlybird: v1.3.0</a>
<ul>
<li><a href="https://discuss.ocaml.org/t/ann-ocamlearlybird-just-got-ability-to-inspect-opaque-abstract-values/13852">[ANN] Ocamlearlybird just got ability to inspect opaque/abstract values</a></li>
</ul>
</li>
<li><a href="https://github.com/Axot017/validate">validate: OCaml Data Validation library</a>
<ul>
<li><a href="https://discuss.ocaml.org/t/ann-validate-a-new-library-for-data-validation/13861">[ANN] Validate - A New Library for Data Validation</a></li>
</ul>
</li>
<li><a href="https://github.com/gborough/sarif">sarif v2.1.0: Static Analysis Results Interchange Format (SARIF) For OCaml</a>
<ul>
<li><a href="https://discuss.ocaml.org/t/ann-sarif-0-1-0-static-analysis-results-interchange-format-sarif-for-ocaml/13821">[ANN] sarif 0.1.0 - Static Analysis Results Interchange Format (SARIF) For OCaml</a></li>
</ul>
</li>
<li><a href="https://discuss.ocaml.org/t/ann-new-release-of-menhir-20231231/13816">[ANN] New release of Menhir (20231231)</a></li>
</ul>
]]></content>
<tags>
<tag>Technique</tag>
</tags>
</entry>
<entry>
<title>OCaml News 2024-3</title>
<url>/2024/02/26/OCaml-News-2024-3/</url>
<content><![CDATA[<h2 id=""><a class="header-anchor" href="#">¶</a></h2>
<p>o-新鲜事儿</p>
<ul>
<li>
<p><a href="https://discuss.ocaml.org/t/ocaml-5-1-0-released/13021">OCaml 5.1.0 released</a></p>
</li>
<li>
<p><a href="https://discuss.ocaml.org/t/ann-dune-3-14/14096">[ANN] dune 3.14</a></p>
</li>
<li>
<p><a href="https://ocaml.codes/search/">code search</a></p>
</li>
<li>
<p><a href="https://discuss.ocaml.org/t/ann-ocaml-codes-code-search-for-opam-packages/14092">[ANN] ocaml.codes, code search for OPAM packages</a></p>
</li>
<li>
<p>用 livegrep 基于 opam 包的源码做的代码搜索,还挺方便的。</p>
</li>
<li>
<p><a href="https://melange.re/blog/posts/announcing-melange-3">Announcing Melange 3</a></p>
</li>
<li>
<p><a href="https://discuss.ocaml.org/t/ann-melange-3-0/14102">[ANN] Melange 3.0</a></p>
</li>
<li>
<p><a href="https://discuss.ocaml.org/t/learn-ocaml-1-0-is-out/14100">Learn-OCaml 1.0 is out!</a></p>
</li>
<li>
<p><a href="https://ocaml-sf.org/learn-ocaml-public/">https://ocaml-sf.org/learn-ocaml-public/</a></p>
</li>
<li>
<p><a href="https://github.com/owlbarn/owl">GitHub - owlbarn/owl: Owl - OCaml Scientific Computing @ http://ocaml.xyz</a></p>
</li>
<li>
<p><a href="https://discuss.ocaml.org/t/owl-project-concluding/14117">Owl project concluding</a></p>
</li>
<li>
<p>经过八年的维护,Owl项目即将终止</p>
</li>
</ul>
<h2 id="-v2"><a class="header-anchor" href="#-v2">¶</a></h2>
<p>o-视频</p>
<ul>
<li>
<p><a href="https://www.youtube.com/watch?v=jvQ7fj9LlVA">Inferring Locality in OCaml | OCaml Unboxed</a></p>
</li>
<li>
<p><a href="https://www.youtube.com/watch?v=AGu4AO5zO8o">OCaml Locals Save Allocations | OCaml Unboxed</a></p>
</li>
<li>
<p><a href="https://watch.ocaml.org/w/qQzb94X9WM7zLif7FynPyN">Ocsigen: Developing Web and mobile applications in OCaml – Jérôme Vouillon & Vincent Balat</a></p>
</li>
<li>
<p><a href="https://watch.ocaml.org/w/iQNqZzA8gVmd4RQaycAwx4">Verifying an Effect-Based Cooperative Concurrency Scheduler in Iris by Adrian Dapprich</a></p>
</li>
</ul>
<h2 id="-v3"><a class="header-anchor" href="#-v3">¶</a></h2>
<p>o-博客 / 文章 / 帖子</p>
<ul>
<li>
<p><a href="https://priver.dev/blog/dbcaml/dbcaml/">Introducing DBCaml, Database toolkit for OCaml</a></p>
</li>
<li>
<p><a href="https://priver.dev/blog/dbcaml/building-a-connnection-pool/">Building a Connnection Pool for DBCaml on top of riot</a></p>
</li>
<li>
<p><a href="https://ocamlpro.com/blog/2021_09_02_generating_static_and_portable_executables_with_ocaml/">Generating static and portable executables with OCaml</a></p>
</li>
<li>
<p>OCaml编译器没有内置生成静态可移植可执行文件的特性,这里提到了一些技巧</p>
</li>
<li>
<p><a href="https://discuss.ocaml.org/t/analog-of-promise-any-for-multicore-ocaml/14145">Analog of Promise.any() for Multicore OCaml</a></p>
</li>
<li>
<p><a href="https://discuss.ocaml.org/t/printf-vs-format/14130">Printf vs. Format?</a></p>
</li>
<li>
<p><a href="https://discuss.ocaml.org/t/how-to-represent-tuples-in-ast/14095">How to represent tuples in AST?</a></p>
</li>
<li>
<p><a href="https://discuss.ocaml.org/t/how-do-i-pass-an-unsigned-char-an-array-of-bytes-representing-binary-data-from-c-to-ocaml/14074">How do I pass an unsigned char * (an array of bytes representing binary data) from C to OCaml?</a></p>
</li>
</ul>
<h2 id="-v4"><a class="header-anchor" href="#-v4">¶</a></h2>
<p>o-未来</p>
<ul>
<li>
<p><a href="https://docs.google.com/forms/d/e/1FAIpQLSe1U_5KanTeKt1h9t5vjYohYXepXDhPCru4tsms4OcI5k0Fkw/viewform?pli=1">How do we want to present OCaml to the World on OCaml.org?</a></p>
</li>
<li>
<p>一个问卷,用于更好的改进 <a href="http://ocaml.org/">ocaml.org</a> 有关学术和工业应用板块的内容。</p>
</li>
<li>
<p><a href="https://discuss.ocaml.org/t/feedback-help-wanted-upcoming-ocaml-org-cookbook-feature/14127">Feedback / Help Wanted: Upcoming OCaml.org Cookbook Feature</a></p>
</li>
<li>
<p><a href="http://ocaml.org/">ocaml.org</a> 准备上线一个cookbook页面,放一些如何用OCaml的生态解决常见需求的资源</p>
</li>
<li>
<p><a href="https://discuss.ocaml.org/t/state-of-compaction-in-ocaml-5/14121/1">State of compaction in OCaml 5?</a></p>
</li>
<li>
<p>OCaml 5.2 的 compact heap 会将未使用的内存返回给操作系统。在 OCaml 5 的 GC 中,小于 128byte 的块用大小隔离池进行管理,比如有一个池,处理大小为 3byte 的分配,另一个池处理大小为 4byte 的分配等等,这样的池在每个Domain里都有。用这个方法分配速度很快,因为不用找合适的内存间隙了,只要找正确的池大小就行。</p>
</li>
</ul>
<h2 id="-v5"><a class="header-anchor" href="#-v5">¶</a></h2>
<p>o-值得被注意的项目</p>
<ul>
<li>
<p><a href="https://github.com/mbarbin/vcs">GitHub - mbarbin/vcs: A versatile OCaml library for Git interaction</a></p>
</li>
<li>
<p><a href="https://discuss.ocaml.org/t/a-versatile-ocaml-library-for-git-interaction-seeking-community-feedback/14155">A Versatile OCaml Library for Git Interaction - Seeking Community Feedback</a></p>
</li>
<li>
<p><a href="https://github.com/dbcaml/dbcaml">GitHub - dbcaml/dbcaml: DBCaml is a database library for OCaml</a></p>
</li>
<li>
<p><a href="https://discuss.ocaml.org/t/dbcaml-a-new-database-toolkit-built-on-riot/14150">DBcaml, a new database toolkit built on Riot</a></p>
</li>
<li>
<p><a href="https://github.com/c-cube/fuseau">GitHub - c-cube/fuseau: [alpha] lightweight fiber library for OCaml 5</a></p>
</li>
<li>
<p><a href="https://discuss.ocaml.org/t/ann-fuseau-0-1/14157">[ANN] fuseau 0.1</a></p>
</li>
<li>
<p><a href="https://github.com/issuu/ocaml-protoc-plugin">GitHub - issuu/ocaml-protoc-plugin: ocaml-protoc-plugin</a></p>
</li>
<li>
<p><a href="https://discuss.ocaml.org/t/taking-over-maintanence-of-a-stale-project/14156">Taking over maintanence of a stale project</a></p>
</li>
<li>
<p><a href="https://github.com/darrenldl/docfd">GitHub - darrenldl/docfd: TUI multiline fuzzy document finder</a></p>
</li>
<li>
<p><a href="https://discuss.ocaml.org/t/ann-docfd-tui-multiline-fuzzy-document-finder-2-2-0/14109/1">[ANN] Docfd: TUI multiline fuzzy document finder 2.2.0</a></p>
</li>
</ul>
]]></content>
<tags>
<tag>Technique</tag>
</tags>
</entry>
<entry>
<title>OCaml News 2024-4</title>
<url>/2024/07/01/OCaml-News-2024-4/</url>
<content><![CDATA[<h2 id="那么接下来"><a class="header-anchor" href="#那么接下来">¶</a>那么接下来</h2>
<ul>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/first-beta-release-for-ocaml-5-2-0/14356">First beta release for OCaml 5.2.0</a>.</p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//github.com/ocaml/ocaml/issues/12876">#12876</a> 和 <a href="https://link.zhihu.com/?target=https%3A//github.com/ocaml/ocaml/issues/12915">#12915</a> 提供了 ThreadSanitizer 对 Power 和 s390x 上的 Linux 支持。</p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//github.com/ocaml/ocaml/issues/12677">#12677</a> 和 <a href="https://link.zhihu.com/?target=https%3A//github.com/ocaml/ocaml/issues/12889">#12889</a> 实现了 <code>Domain.DLS</code> 的线程安全。</p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//github.com/ocaml/ocaml/issues/12924">#12924</a> 和 <a href="https://link.zhihu.com/?target=https%3A//github.com/ocaml/ocaml/issues/12930">#12930</a> 提到了一个类型系统的错误和其修复方式(merged):</p>
</li>
</ul>
<figure class="highlight ocaml"><table><tr><td class="code"><pre><span class="line"><span class="keyword">module</span> <span class="keyword">type</span> <span class="type">S</span> = <span class="keyword">sig</span></span><br><span class="line"> <span class="keyword">type</span> t [@@immediate]</span><br><span class="line"><span class="keyword">end</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">type</span> t = (<span class="keyword">module</span> <span class="type">S</span> <span class="keyword">with</span> <span class="keyword">type</span> t = <span class="built_in">int</span>)</span><br><span class="line"> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^</span><br><span class="line"><span class="comment">(*</span></span><br><span class="line"><span class="comment">Error: In this with constraint, the new definition of t</span></span><br><span class="line"><span class="comment"> does not match its original definition in the constrained signature:</span></span><br><span class="line"><span class="comment"> Type declarations do not match:</span></span><br><span class="line"><span class="comment"> type t</span></span><br><span class="line"><span class="comment"> is not included in</span></span><br><span class="line"><span class="comment"> type t [@@immediate]</span></span><br><span class="line"><span class="comment"> The first is not an immediate type.</span></span><br><span class="line"><span class="comment">*)</span></span><br></pre></td></tr></table></figure>
<ul>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/ocaml-4-14-2-released/14308">OCaml 4.14.2 released</a>.</p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/owl-project-restructured/14226">Owl project restructured</a>.</p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/your-feedback-needed-on-ocaml-home-page-wireframe/14366">Your Feedback Needed on OCaml Home Page Wireframe!</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/shape-with-us-the-new-ocaml-org-community-area/14322">Shape with us the New OCaml.org Community Area!</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/ocaml-platform-newsletter-february-2024/14361">OCaml Platform Newsletter: February 2024</a>.</p>
</li>
<li>
<p>其中值得注意的是:<a href="https://link.zhihu.com/?target=https%3A//ocaml.org/docs/platform-roadmap%23w4-build-a-project">探索 dune 和包管理的集成</a> 和 <a href="https://link.zhihu.com/?target=https%3A//ocaml.org/docs/platform-roadmap%23w5-manage-dependencies">opam 2.2 的 Windows Native support</a>.</p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/ocaml-workshop-2024-at-icfp-announcement-and-call-for-proposals/14371">OCaml Workshop 2024 at ICFP – announcement and call for proposals</a>.</p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/volunteers-for-icfp-2024-artifact-evaluation-committee-aec/14355">Volunteers for ICFP 2024 Artifact Evaluation Committee (AEC)</a>.</p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/odoc-3-0-planning/14360">Odoc 3.0 planning</a>.</p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/opam-repository-updated-documentation-retirement-and-call-for-maintainers/14325">Opam-repository: Updated documentation, retirement and call for maintainers</a>.</p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/u/kit-ty-kate">kit-ty-kate</a> 退休啦。</p>
</li>
</ul>
<h2 id="论文"><a class="header-anchor" href="#论文">¶</a>论文</h2>
<ul>
<li><a href="https://link.zhihu.com/?target=https%3A//jcheminf.biomedcentral.com/articles/10.1186/s13321-019-0332-0">Chemoinformatics and structural bioinformatics in OCaml</a>.</li>
</ul>
<h2 id="帖子-文章"><a class="header-anchor" href="#帖子-文章">¶</a>帖子/文章</h2>
<ul>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/ocaml-for-building-shared-libraries-how-are-the-ergonomics-and-performance/14352">OCaml for building shared libraries: how are the ergonomics and performance?</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//mccd.space/posts/awk-with-kakoune/">Using Awk in Kakoune to Generate SQL Types for OCaml</a>.</p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//tarides.com/blog/2024-03-20-eio-1-0-release-introducing-a-new-effects-based-i-o-library-for-ocaml/">Eio 1.0 Release: Introducing a new Effects-Based I/O Library for OCaml</a>.</p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/blog-the-flambda2-snippets-by-ocamlpro/14331">[BLOG] The Flambda2 Snippets, by OCamlPro</a>.</p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//ocamlpro.com/blog/2024_03_18_the_flambda2_snippets_0/">The Flambda2 Snippets, Episode 0</a>.</p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//ocamlpro.com/blog/2024_03_19_the_flambda2_snippets_1/">CPS Representation and Foundational Design Decisions in Flambda2</a>.</p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/monadic-library-for-eio-capabilities/14202">Monadic Library for Eio Capabilities?</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/is-there-any-consensus-on-which-type-to-unify-on-for-errors-in-result-types-whats-your-preference/14304">Is there any consensus on which type to unify on for errors in result types? What’s your preference?</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/create-opam-switch-specifying-custom-c-compiler/14276">Create opam switch specifying custom C compiler</a>.</p>
</li>
</ul>
<h2 id="视频"><a class="header-anchor" href="#视频">¶</a>视频</h2>
<ul>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//watch.ocaml.org/w/b7sv1LQSVZQH6trf4xpwFX">Outreachy Presentations for the December 2023 Round</a>.</p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//www.youtube.com/watch%3Fv%3DIxQ586TS8Gw">Taking Erlang to OCaml 5 (with Leandro Ostera)</a>.</p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//www.youtube.com/watch%3Fv%3DLwD3GxsY-pc">OCaml Unboxed: An Exploration of Jane Street’s Experiments with OCaml</a>.</p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//www.youtube.com/watch%3Fv%3DTxuLrsQZprE">OCaml for Fun & Profit: An Experience Report • Tim McGilchrist • YOW! 2023</a>.</p>
</li>
</ul>
<h2 id="值得注意的项目"><a class="header-anchor" href="#值得注意的项目">¶</a>值得注意的项目</h2>
<ul>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//github.com/ocaml-multicore/eio/releases/tag/v1.0">Eio 1.0: Effects-based direct-style IO for multicore OCaml</a>.</p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//github.com/ocaml-multicore/icfp-2023-eio-tutorial">icfp-2023-eio-tutorial: This tutorial covers how to convert an OCaml 4 program using Lwt for concurrency to use OCaml 5 and Eio.</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//erratique.ch/software/down">Down: An unintrusive user experience upgrade for the OCaml toplevel system (REPL).</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//github.com/zshipko/ocaml-rs">ocaml-rs: OCaml extensions in Rust</a>.</p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//github.com/ocaml-multicore/eio-trace">eio-trace: Trace visualisation tool for Eio programs</a>.</p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/ann-dkcoder-0-1-0/14327">[ANN] DkCoder 0.1.0: A transparently installed OCaml 4.14 environment with one API: run a script.</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//github.com/darrenldl/docfd/releases/tag/3.0.0">[ANN] Docfd 3.0.0: TUI multiline fuzzy document finder</a>.</p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/ann-ocaml-windows-5-1-1/14268">[ANN] Ocaml-windows 5.1.1</a>.</p>
</li>
<li>
<p>这里指的是 <a href="https://link.zhihu.com/?target=https%3A//github.com/ocaml-cross/opam-cross-windows">opam-cross-windows</a> 支持 5.1.1 了,不是 Windows 支持。</p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//github.com/ml-in-barcelona/quickjs.ml">[ANN] Bindings to QuickJS</a>.</p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//github.com/lukstafi/ppx_minidebug">[ANN] ppx_minidebug 1.3.0: toward a logging framework</a>.</p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//github.com/c-cube/ocaml-iostream/releases/tag/v0.2">[ANN] iostream 0.2</a>.</p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//github.com/ocsigen/js_of_ocaml/releases/tag/5.7.1">[ANN] Js_of_ocaml 5.7</a>.</p>
</li>
</ul>
<h2 id="有趣的项目"><a class="header-anchor" href="#有趣的项目">¶</a>有趣的项目</h2>
<ul>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//github.com/pdonadeo/ocaml-lua">ocaml-lua: Lua bindings for OCaml</a>.</p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//github.com/LexiFi/csml">csml: High-level bindings between .Net and OCaml</a>.</p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//github.com/LexiFi/landmarks">landmarks: A Simple Profiling Library for OCaml</a>.</p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//erratique.ch/software/ttweetnacl">Ttweetnacl: An OCaml module providing thin bindings to the TweetNaCl cryptographic library.</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//github.com/xavierleroy/cryptokit">cryptokit: A library of cryptographic primitives (ciphers, hashes, etc) for OCaml</a>.</p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//github.com/gridbugs/llama">llama: A library for building software-defined modular synthesizers in a declarative style.</a></p>
</li>
</ul>
]]></content>
<tags>
<tag>Technique</tag>
</tags>
</entry>
<entry>
<title>OCaml News 2024-5</title>
<url>/2024/06/27/OCaml-News-2024-5/</url>
<content><![CDATA[<h2 id="语言的发展"><a class="header-anchor" href="#语言的发展">¶</a>语言的发展</h2>
<ul>
<li><a href="https://link.zhihu.com/?target=https%3A//github.com/ocaml/ocaml/pull/13097">Immutable arrays #13097: This PR proposes to add immutable arrays to OCaml.</a></li>
<li><a href="https://link.zhihu.com/?target=https%3A//github.com/ocaml/ocaml/pull/13199">Support running native debuggers in ocamltest #13199</a>.</li>
<li><a href="https://link.zhihu.com/?target=https%3A//github.com/ocaml/ocaml/pull/13152">The Pattern-Matching Bug: fix totality information by gasche · Pull Request #13152 · ocaml/ocaml</a>.</li>
<li><a href="https://link.zhihu.com/?target=https%3A//github.com/ocaml/ocaml/pull/11974%23discussion_r1621832545">Add loongarch architecture support #11974</a>.</li>
<li><a href="https://link.zhihu.com/?target=https%3A//github.com/ocaml/ocaml/pull/13101">Add Stdlib.todo #13101</a>.</li>
<li><a href="https://link.zhihu.com/?target=https%3A//tarides.com/blog/2024-06-26-enhancing-the-ocaml-org-community-page-boosting-ux-and-ui-based-on-user-research/">Enhancing the OCaml.org Community Page: Boosting UX and UI Based on User Research.</a></li>
<li><a href="https://link.zhihu.com/?target=https%3A//tarides.com/blog/2024-05-22-launching-the-first-class-windows-project/">Launching the First-Class Windows Project</a>.</li>
</ul>
<h2 id="新消息"><a class="header-anchor" href="#新消息">¶</a>新消息</h2>
<ul>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/ann-project-wide-occurrences-in-merlin-and-lsp/14847">[ANN] Project wide occurrences in Merlin and LSP</a>.</p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/ann-opam-2-2-0-rc1-release/14842">[ANN] opam 2.2.0 rc1 release</a>.</p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/ann-first-release-of-baby/14840">[ANN] First release of baby</a>.</p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//github.com/fpottier/baby">baby</a> is an OCaml library that offers several implementations of balanced binary search trees.</p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/ann-preview-of-stripe-client-and-mock-server-dkstdrestapis/14841">[ANN] Preview of Stripe client and mock server - DkStdRestApis</a>.</p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/ann-caisar-release-2-0-a-platform-for-characterizing-ai-safety-and-robustness/14831">[ANN] CAISAR release 2.0, a platform for characterizing AI safety and robustness</a>.</p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/ann-forester-4-1/14800">[ANN] Forester 4.1</a>.</p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/ann-dream-html-pure-html-3-5-2/14808">[ANN] dream-html & pure-html 3.5.2</a></p>
</li>
</ul>
<h2 id="有价值的文章"><a class="header-anchor" href="#有价值的文章">¶</a>有价值的文章</h2>
<ul>
<li><a href="https://link.zhihu.com/?target=https%3A//tarides.com/blog/2024-06-19-keeping-up-with-the-compiler-how-we-help-maintain-the-ocaml-language/">Keeping Up With the Compiler: How we Help Maintain the OCaml Language</a></li>
<li><a href="https://link.zhihu.com/?target=https%3A//github.com/ocaml/ocaml/pull/10995">Explain mapping between OCaml memory model and C.</a></li>
<li><a href="https://link.zhihu.com/?target=https%3A//xavierleroy.org/CdF/2023-2024/index.html">Control structures – Xavier Leroy</a>.</li>
<li><a href="https://link.zhihu.com/?target=https%3A//dev.to/chshersh/8-months-of-ocaml-after-8-years-of-haskell-in-production-h96">8 months of OCaml after 8 years of Haskell in production</a>.</li>
<li><a href="https://link.zhihu.com/?target=https%3A//hypirion.com/musings/getting-ocaml-through-the-eye-of-a-needle">Getting OCaml Through the Eye of a Needle</a>.</li>
<li><a href="https://link.zhihu.com/?target=https%3A//tarides.com/blog/2024-06-05-secure-from-the-ground-up-introducing-the-fides-project-combining-risc-v-and-mirageos/">Secure From the Ground Up: Introducing the FIDES Project Combining RISC-V and MirageOS.</a></li>
<li><a href="https://link.zhihu.com/?target=https%3A//tarides.com/blog/2024-05-29-effective-ml-through-merlin-s-destruct-command/">Effective ML Through Merlin’s Destruct Command.</a></li>
<li><a href="https://link.zhihu.com/?target=https%3A//blog.osau.re/articles/reproducible.html">Dinosaure’s website - Reproducibility!</a></li>
<li><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/experiments-in-jitting/14853">Experiments in JITting</a>.</li>
<li><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/how-to-write-an-efficient-cp-r-clone-with-eio/14848">How to write an efficient ‘cp -R’ clone with Eio?</a></li>
<li><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/generating-infix-extension-nodes-with-ppxlib/14833">Generating infix extension nodes with Ppxlib</a>.</li>
<li><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/this-made-my-day-float-to-string-and-string-of-float-without-loss-of-precision/14825">This made my day (float to string and string of float without loss of precision)</a>.</li>
<li><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/help-with-cryptic-dependency-cycle-compile-errors-using-dune-build/14864">Help with cryptic dependency cycle compile errors using <code>dune build</code></a>.</li>
<li><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/why-does-relaxed-value-restriction-treat-function-types-differently/14802">Why does relaxed value restriction treat function types differently?</a></li>
<li><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/binding-existentials-in-a-non-uniform-gadt/14809">Binding existentials in a non-uniform GADT</a>.</li>
</ul>
<h2 id="有趣的项目"><a class="header-anchor" href="#有趣的项目">¶</a>有趣的项目</h2>
<ul>
<li><a href="https://link.zhihu.com/?target=https%3A//ocaml.org/p/ocaml-index/latest">ocaml-index 1.0 (latest) · OCaml Package: A tool that indexes value usages from cmt files</a>.</li>
<li><a href="https://link.zhihu.com/?target=https%3A//ocaml.org/p/fileutils/latest">fileutils 0.6.4 (latest) · OCaml Package: API to manipulate files (POSIX like) and filenames.</a></li>
<li><a href="https://link.zhihu.com/?target=https%3A//ocaml.org/p/pure-html/latest">pure-html 3.5.2 (latest) · OCaml Package</a></li>
<li><a href="https://link.zhihu.com/?target=https%3A//github.com/fpottier/baby">GitHub - fpottier/baby: A balanced binary search tree library for OCaml</a>.</li>
<li><a href="https://link.zhihu.com/?target=https%3A//github.com/akabe/ocaml-jupyter">GitHub - akabe/ocaml-jupyter: An OCaml kernel for Jupyter (IPython) notebook</a>.</li>
<li><a href="https://link.zhihu.com/?target=https%3A//github.com/robur-coop/orb">Github - robur-coop/orb: check opam package reproductibility.</a></li>
<li><a href="https://link.zhihu.com/?target=https%3A//github.com/ocaml-multicore/dscheck%23motivation">Github - ocaml-multicore/dscheck: Experimental model checker for testing concurrent algorithms.</a></li>
</ul>
]]></content>
<tags>
<tag>Technique</tag>
</tags>
</entry>
<entry>
<title>OCaml News 2024-6</title>
<url>/2024/07/23/OCaml-News-2024-6/</url>
<content><![CDATA[<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line"> ^o3</span><br><span class="line">~/\_/\_|)</span><br><span class="line">|/=_=\|</span><br><span class="line">" "</span><br></pre></td></tr></table></figure>
<h2 id="语言的发展"><a class="header-anchor" href="#语言的发展">¶</a>语言的发展</h2>
<ul>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//github.com/ocaml/ocaml/pull/13275">Modular explicits #13275</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//github.com/ocaml/ocaml/pull/12828">Add short syntax for dependent functor types #12828</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//github.com/ocaml/ocaml/pull/13310">Add Pair module to standard library #13310</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//github.com/ocaml/ocaml/pull/13272">Allow maximum number of domains to be specified as a OCAMLRUNPARAM parameter #13272</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//github.com/ocaml/ocaml/pull/13097">Immutable arrays #13097</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//github.com/ocaml/ocaml/pull/13161">Restore native armv7 support for NetBSD 10.0 #13161</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//github.com/ocaml/ocaml/pull/12309">Add effect syntax #12309</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//github.com/ocaml/ocaml/pull/12114">Add ThreadSanitizer support #12114</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//github.com/ocaml/ocaml/pull/13195">A new abstract data type of enumerations in Set.Make(Ord).Enum #13195</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//github.com/ocaml/ocaml/pull/12871">Stdlib priority queues #12871</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//github.com/ocaml/ocaml/pull/9080">Aliasing == and != with explicit names #9080</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//github.com/ocaml/ocaml/pull/12964">Memory cleanup at exit #12964</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//github.com/ocaml/ocaml/pull/13169">A document type for error messages #13169</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//github.com/ocaml/ocaml/pull/13318">Fix GC alarm regression #13318</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//github.com/ocaml/ocaml/pull/13326">Implement O_APPEND on windows #13326</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//github.com/ocaml/ocaml/pull/13296">Add missing functions from Array to Dynarray #13296</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//github.com/ocaml/ocaml/pull/12182%23discussion_r1678134261">Improve the type clash error message #12182</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//github.com/ocaml/ocaml/pull/12298">Emphasize that Bigarray.int refers to the OCaml int type, and not the C int type #12298</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/effects-with-lwt-a-dead-end-for-now/15002">Effects with Lwt, a dead end for now?</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/changes-in-handling-of-gc-parameters-and-alarms-in-5-2-0/14986">Changes in handling of Gc parameters and alarms in 5.2.0</a></p>
</li>
</ul>
<h2 id="新消息"><a class="header-anchor" href="#新消息">¶</a>新消息</h2>
<ul>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/ann-mopsa-1-0-modular-open-platform-for-static-analysis/15013">[ANN] Mopsa 1.0 – Modular Open Platform for Static Analysis</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/ann-a-small-extension-of-bigarray-genarray-adding-iteration-mapping-and-folding/15005">[ANN] A small extension of Bigarray.Genarray adding iteration, mapping and folding</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/ann-cudajit-bindings-to-the-cuda-and-nvrtc-libraries/15010">[ANN] cudajit: Bindings to the <code>cuda</code> and <code>nvrtc</code> libraries</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/ann-ocaml-lsp-1-18-0/14952">[ANN] OCaml LSP 1.18.0</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/ann-ortac-0-3-0-dynamic-formal-verification-made-easy/14936">[ANN] Ortac 0.3.0 Dynamic formal verification made easy</a></p>
</li>
</ul>
<h2 id="有价值的文章"><a class="header-anchor" href="#有价值的文章">¶</a>有价值的文章</h2>
<ul>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//hal.sorbonne-universite.fr/hal-02890500v1/document">Combinations of Reusable Abstract Domains for a Multilingual Static Analyzer</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//fizzixnerd.com/blog/2024-07-21-fixing-living/">Fighting Mutation with Mutation in Living</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/type-system-and-polymorphic-lets/14990">Type system and polymorphic let’s</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/using-docusaurus-to-document-an-ocaml-project/13359">Using Docusaurus to document an OCaml project</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/exploring-the-docusaurus-odoc-combo/15012">Exploring the Docusaurus+Odoc combo</a><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/type-system-and-polymorphic-lets/14990">Type system and polymorphic let’s</a><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/exploring-the-docusaurus-odoc-combo/15012">Exploring the Docusaurus+Odoc combo</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//fizzixnerd.com/blog/2024-07-11-a-possibly-safer-interface-to-the-ctypes-ffi/">A (Possibly) Safer Interface to the Ctypes FFI</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//fizzixnerd.com/blog/2024-07-09-ocaml-ffi-sharp-edges-and-how-to-avoid-them/">OCaml FFI Sharp Edges – and How to Avoid them!</a></p>
</li>
</ul>
<h2 id="有趣的项目"><a class="header-anchor" href="#有趣的项目">¶</a>有趣的项目</h2>
<ul>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//ocaml.libvirt.org/">ocaml-libvirt OCaml bindings for libvirt</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//gitlab.com/mopsa/mopsa-analyzer/">Gitlab - MOPSA/MOPSA analyzer: stands for Modular and Open Platform for Static Analysis.</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//github.com/Heyji2/GenArrayIter">GitHub - Heyji2/GenArrayIter: Adding iteration, mapping and folding to the ocaml BigArray.Genarrays module which provides arrays of arbitrary dimensions</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//github.com/mbarbin/bopkit">GitHub - mbarbin/bopkit: An educational project for digital circuits programming</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//github.com/dx3mod/rpmfile">GitHub - dx3mod/rpmfile: A library for reading metadata from RPM packages.</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//github.com/gildor478/ocaml-fileutils">Github - gildor478/ocaml-fileutils: OCaml API to manipulate real files (POSIX like) and filenames</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//github.com/NathanReb/ocaml-api-watch">Github - NathanReb/ocaml-api-watch: Libraries and tools to keep watch on you OCaml lib’s API changes</a></p>
</li>
</ul>
]]></content>
<tags>
<tag>Technique</tag>
</tags>
</entry>
<entry>
<title>OCaml News 2024-7</title>
<url>/2024/12/15/OCaml-News-2024-7/</url>
<content><![CDATA[<figure class="highlight text"><table><tr><td class="code"><pre><span class="line"> ^o3</span><br><span class="line">~/\_/\_|)</span><br><span class="line">|/=_=\|</span><br><span class="line">" "</span><br><span class="line"></span><br><span class="line">^~~~~~~~~~~~这其实是用 ASCII 画的一个骆驼。</span><br></pre></td></tr></table></figure>
<h3 id="语言的发展"><a class="header-anchor" href="#语言的发展">¶</a>语言的发展</h3>
<ul>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/ocaml-5-2-1-released/15634">OCaml 5.2.1 released</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/feedback-help-wanted-upcoming-ocaml-org-cookbook-feature/14127">Feedback / Help Wanted: Upcoming OCaml.org Cookbook Feature</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/second-beta-release-of-ocaml-5-3-0/15700">Second beta release of OCaml 5.3.0</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/terrateams-open-source-ocaml-repository/15645">Terrateam’s open source Ocaml repository</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/ann-mlx-syntax-dialect/15035">[ANN] .mlx syntax dialect</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/ann-opam-2-3-0-is-out/15609">[ANN] opam 2.3.0 is out!</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//fun-ocaml.com/">Fun OCaml 2024 - Berlin - September 16+17, 2024</a></p>
</li>
<li>
<p><strong><a href="https://link.zhihu.com/?target=https%3A//www.youtube.com/%40FUNOCaml">Fun OCaml is an open source hacking event dedicated to OCaml enthusiasts and professionals around the globe!</a></strong></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//github.com/ocaml/ocaml/pull/13526">Simplify the build of cross compilers by shym · Pull Request #13526 · ocaml/ocaml</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//github.com/ocaml/ocaml/pull/13580">“Mark-delay” performance improvement to major GC by NickBarnes · Pull Request #13580 · ocaml/ocaml</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//github.com/ocaml/ocaml/pull/13416">Use WinAPI concurrency primitives on Windows ports (remove winpthreads) by MisterDA · Pull Request #13416 · ocaml/ocaml</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//github.com/ocaml/ocaml/pull/13404">Atomic record fields by clef-men · Pull Request #13404 · ocaml/ocaml</a></p>
</li>
</ul>
<h3 id="视频"><a class="header-anchor" href="#视频">¶</a>视频</h3>
<ul>
<li><a href="https://link.zhihu.com/?target=https%3A//www.youtube.com/playlist%3Flist%3DPLP3MfTGqcNVJMFcWWDF6VSPJB7zuKN5yQ">All the talk recordings from FUN OCaml 2024 in Berlin, September 16 + 17, 2024.</a></li>
<li><a href="https://link.zhihu.com/?target=https%3A//www.youtube.com/watch%3Fv%3DwbrELQrzwQk">[ICFP24] Oxidizing OCaml with Modal Memory Management</a></li>
<li><a href="https://link.zhihu.com/?target=https%3A//www.youtube.com/watch%3Fv%3D7yYC6EGYg10">[OCaml24] Priodomainslib: Prioritized Fine-grained Parallelism for Multicore OCaml</a></li>
<li><a href="https://link.zhihu.com/?target=https%3A//www.youtube.com/watch%3Fv%3DKEkmcXVtFi0">[OCaml24] ChorCaml: Functional Choreographic Programming in OCaml</a></li>
<li><a href="https://link.zhihu.com/?target=https%3A//www.youtube.com/watch%3Fv%3Dvanyv3ZEto8">[OCaml24] Saturn: a library of verified concurrent data structures for OCaml 5</a></li>
<li><a href="https://link.zhihu.com/?target=https%3A//www.youtube.com/watch%3Fv%3DmUzOBC3V0ds">Beyond the Basics of LSP: Advanced IDE services for OCaml</a></li>
<li><a href="https://link.zhihu.com/?target=https%3A//www.youtube.com/watch%3Fv%3DBUpRPSzkrH0">Why Terrateam chose OCaml</a></li>
<li><a href="https://link.zhihu.com/?target=https%3A//www.youtube.com/watch%3Fv%3D8zTN3rtcED4">A Crash Course in OCaml Modules • Tim McGilchrist • YOW! 2015</a></li>
<li><a href="https://link.zhihu.com/?target=https%3A//watch.ocaml.org/w/peT3MdWjS1BYYMbowEJ1gv">Outreachy May 2024 Demo</a></li>
<li><a href="https://link.zhihu.com/?target=https%3A//www.youtube.com/watch%3Fv%3DJFZvnGD_hV8">Octane: A Query Builder for OCaml</a></li>
</ul>
<h3 id="讨论"><a class="header-anchor" href="#讨论">¶</a>讨论</h3>
<ul>
<li><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/what-are-the-biggest-reasons-newcomers-give-up-on-ocaml/10958">What are the biggest reasons newcomers give up on OCaml?</a></li>
<li><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/do-you-use-return-to-describe-a-function/15615">Do you use “return” to describe a function?</a></li>
<li><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/ocaml-cycle-detection-and-garbage-collector/15757">OCaml Cycle detection and Garbage Collector</a></li>
<li><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/modeling-rust-trait-objects-subtyping-relation-via-ocaml-polymorphic-variants/15644">Modeling Rust trait objects subtyping relation via OCaml polymorphic variants</a></li>
<li><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/ocamldebug-using-dune/15595">Ocamldebug using dune</a></li>
<li><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/typesafe-sql-with-octane-ml/15582">Typesafe SQL with Octane.ml</a></li>
</ul>
<h3 id="文章"><a class="header-anchor" href="#文章">¶</a>文章</h3>
<ul>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/using-map-and-ppx-yojson/12524">Using Map and ppx yojson</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/correct-syntax-to-add-compare-ppx/15764">Correct syntax to add compare ppx</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/tiny-educational-concurrent-i-o-and-promises-library/15703">Tiny educational concurrent I/O and promises library</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/good-example-of-handwritten-lexer-recursive-descent-parser/15672">Good example of handwritten Lexer + Recursive Descent Parser?</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/new-part-pragmatic-category-theory-part-2-published/15056">[NEW PART] Pragmatic Category Theory: Part 2 published!</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//dev.to/chshersh/pragmatic-category-theory-part-1-semigroup-intro-1ign">Pragmatic Category Theory | Part 1: Semigroup Intro</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//dev.to/chshersh/pragmatic-category-theory-part-2-composing-semigroups-87">Pragmatic Category Theory | Part 2: Composing Semigroups</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/installing-ocaml-on-a-mac-m1-mini/15642">Installing OCaml on a mac m1 mini</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/a-case-for-feminism-in-programming-language-design/15478">« A Case for Feminism in Programming Language Design »</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//dl.acm.org/doi/pdf/10.1145/3689492.3689809">A Case for Feminism in Programming Language Design.pdf</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//www.lexifi.com/blog/ocaml/decoders/">Vdom 0.3 brings Elm-style decoders to OCaml</a></p>
</li>
</ul>
<h3 id="生态"><a class="header-anchor" href="#生态">¶</a>生态</h3>
<ul>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/re2ocaml-regexp-compiler/15669">Re2ocaml regexp compiler</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/eliom-11-1-towards-web-assembly-support/15704">Eliom 11.1: Towards Web Assembly support</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/ann-new-releases-of-merlin-and-ocaml-lsp/15752">[ANN] New releases of Merlin and OCaml-LSP</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/new-release-of-baby/15754">New release of baby</a></p>
</li>
<li>
<p><code>baby</code>is an OCaml library that offers persistent sets and maps based on balanced binary search trees. It offers replacements for OCaml’s<code>Set</code>and<code>Map</code>modules.</p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/ann-dune-3-17/15770">[ANN] Dune 3.17</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/ann-jsont-0-1-0-declarative-json-data-manipulation-for-ocaml/15702">[ANN] Jsont 0.1.0 – Declarative JSON data manipulation for OCaml</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/ann-release-of-saturn-1-0/15763">[ANN] Release of Saturn 1.0</a></p>
<ul>
<li>Saturn is a collection of concurrent-safe data structures designed for OCaml 5.</li>
</ul>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/ann-areas-and-adversaries/15706/3">[ANN] Areas and Adversaries</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/ann-release-of-cppo-1-8-0/15749">[ANN] Release of cppo 1.8.0</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/ann-capnp-rpc-2-0/15739">[ANN] capnp-rpc 2.0</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/ann-advent-of-code-project-template/13539">[ANN] Advent of Code project template</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/ann-testo-0-1-0-a-new-testing-framework-for-ocaml/15624">[ANN] Testo 0.1.0 - a new testing framework for OCaml</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/ann-vdom-0-3-functional-ui-applications-now-with-custom-event-handlers/13298">[ANN] vdom 0.3: functional UI applications now with custom event handlers</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/visualizing-dependencies-between-ocaml-modules/15254">Visualizing dependencies between Ocaml modules</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/ann-mariadb-1-2-0/15709">[ANN] mariadb 1.2.0</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/ann-new-release-of-monolith/15701">[ANN] New release of Monolith</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/ann-first-release-of-hachis/15309">[ANN] First release of hachis</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/ann-liquidsoap-2-3-0/15677">[ANN] Liquidsoap 2.3.0</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/ann-dream-html-pure-html-3-5-2/14808/3">[ANN] dream-html & pure-html 3.5.2</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/ann-js-of-ocaml-5-9-0/15674">[ANN] Js_of_ocaml 5.9.0</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/ann-ppx-deriving-ezjsonm/15637">[ANN] ppx_deriving_ezjsonm</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/ann-smaws-preview-release-an-aws-sdk-for-ocaml-using-eio/15635">[ANN] smaws preview release, an AWS SDK for OCaml using eio</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/ann-aws-s3-4-0-1/2451">[ANN] aws-s3 4.0.1</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/first-release-of-cmdlang/15616">First release of cmdlang</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/ann-eliom-11-and-ocsigen-start-7/15487">[ANN] Eliom 11 and Ocsigen Start 7</a></p>
</li>
<li>
<p><a href="https://link.zhihu.com/?target=https%3A//discuss.ocaml.org/t/ann-jane-street-ocaml-extensions-now-with-developer-tooling/15597">[ANN] Jane Street OCaml extensions – now with developer tooling!</a></p>
</li>
</ul>
<h3 id="亮点项目"><a class="header-anchor" href="#亮点项目">¶</a>亮点项目</h3>
<ul>
<li><a href="https://link.zhihu.com/?target=https%3A//github.com/tjdevries/octane.ml">GitHub - tjdevries/octane.ml: The fastest, hottest</a></li>
<li><a href="https://link.zhihu.com/?target=https%3A//github.com/semgrep/testo">Github - semgrep/testo: Test framework for OCaml</a></li>
<li><a href="https://link.zhihu.com/?target=https%3A//github.com/sim642/odep">GitHub - sim642/odep: Dependency graphs for OCaml modules, libraries and packages</a></li>
<li><a href="https://link.zhihu.com/?target=https%3A//github.com/LexiFi/ocaml-vdom">GitHub - LexiFi/ocaml-vdom: Elm architecture and (V)DOM for OCaml</a></li>
<li><a href="https://link.zhihu.com/?target=https%3A//github.com/ocaml-community/ocaml-mariadb">GitHub - ocaml-community/ocaml-mariadb: OCaml bindings to MariaDB, supporting the nonblocking API</a></li>
<li><a href="https://link.zhihu.com/?target=https%3A//cambium.inria.fr/~fpottier/monolith/doc/monolith/Monolith/">Monolith (monolith.Monolith)</a>: Monolith offers facilities for testing an OCaml library by comparing its implementation (known as the candidate implementation) against a reference implementation.</li>
</ul>
]]></content>
<tags>
<tag>Technique</tag>
</tags>
</entry>
<entry>
<title>Obsidian Vault 的 .obsidian 目录中的各文件作用</title>
<url>/2024/10/06/Obsidian-Vault-%E7%9A%84-obsidian-%E7%9B%AE%E5%BD%95%E4%B8%AD%E7%9A%84%E5%90%84%E6%96%87%E4%BB%B6%E4%BD%9C%E7%94%A8/</url>
<content><![CDATA[<p>一个简单的例子是:</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">.obsidian</span><br><span class="line">├── appearance.json</span><br><span class="line">├── app.json</span><br><span class="line">├── community-plugins.json</span><br><span class="line">├── core-plugins.json</span><br><span class="line">├── core-plugins-migration.json</span><br><span class="line">├── plugins</span><br><span class="line">│ └── obsidian-git</span><br><span class="line">│ ├── data.json</span><br><span class="line">│ ├── main.js</span><br><span class="line">│ ├── manifest.json</span><br><span class="line">│ └── styles.css</span><br><span class="line">└── workspace.json</span><br></pre></td></tr></table></figure>
<p>其中:</p>
<ul>
<li><code>appearance.json</code> 包含当前 vault 的外观设置,例如主题、字体大小和行高,例如:</li>
</ul>
<figure class="highlight json"><table><tr><td class="code"><pre><span class="line"><span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"interfaceFontFamily"</span><span class="punctuation">:</span> <span class="string">"HarmonyOS Sans SC"</span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">"textFontFamily"</span><span class="punctuation">:</span> <span class="string">"HarmonyOS Sans SC"</span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">"monospaceFontFamily"</span><span class="punctuation">:</span> <span class="string">"FrankMono"</span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">"accentColor"</span><span class="punctuation">:</span> <span class="string">""</span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">"cssTheme"</span><span class="punctuation">:</span> <span class="string">"Listive"</span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">"nativeMenus"</span><span class="punctuation">:</span> <span class="literal"><span class="keyword">false</span></span></span><br><span class="line"><span class="punctuation">}</span></span><br></pre></td></tr></table></figure>
<ul>
<li><code>app.json</code> 包含了编辑器的一些设置,例如 vim mode 和断行设置:</li>
</ul>
<figure class="highlight json"><table><tr><td class="code"><pre><span class="line"><span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"vimMode"</span><span class="punctuation">:</span> <span class="literal"><span class="keyword">true</span></span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">"strictLineBreaks"</span><span class="punctuation">:</span> <span class="literal"><span class="keyword">false</span></span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">"promptDelete"</span><span class="punctuation">:</span> <span class="literal"><span class="keyword">false</span></span></span><br><span class="line"><span class="punctuation">}</span></span><br></pre></td></tr></table></figure>
<ul>
<li><code>community-plugins.json</code>:包含有关当前 vault 中安装的社区插件的数据,例如:</li>
</ul>
<figure class="highlight json"><table><tr><td class="code"><pre><span class="line"><span class="punctuation">[</span></span><br><span class="line"> <span class="string">"dataview"</span><span class="punctuation">,</span></span><br><span class="line"> <span class="string">"novel-word-count"</span><span class="punctuation">,</span></span><br><span class="line"> <span class="string">"remotely-save"</span><span class="punctuation">,</span></span><br><span class="line"> <span class="string">"obsidian-tasks-plugin"</span><span class="punctuation">,</span></span><br><span class="line"> <span class="string">"obsidian-style-settings"</span><span class="punctuation">,</span></span><br><span class="line"> <span class="string">"obsidian-advanced-slides"</span><span class="punctuation">,</span></span><br><span class="line"> <span class="string">"calendar"</span><span class="punctuation">,</span></span><br><span class="line"> <span class="string">"3d-graph-new"</span><span class="punctuation">,</span></span><br><span class="line"> <span class="string">"webpage-html-export"</span></span><br><span class="line"><span class="punctuation">]</span></span><br></pre></td></tr></table></figure>
<ul>
<li><code>core-plugins.json</code>: 就是当前 valut 中的所有内置插件,例如:</li>
</ul>
<figure class="highlight json"><table><tr><td class="code"><pre><span class="line"> <span class="string">"file-explorer"</span><span class="punctuation">,</span></span><br><span class="line"> <span class="string">"global-search"</span><span class="punctuation">,</span></span><br><span class="line"> <span class="string">"switcher"</span><span class="punctuation">,</span></span><br><span class="line"> <span class="string">"graph"</span><span class="punctuation">,</span></span><br><span class="line"> <span class="string">"backlink"</span><span class="punctuation">,</span></span><br><span class="line"> <span class="string">"canvas"</span><span class="punctuation">,</span></span><br><span class="line"> <span class="string">"outgoing-link"</span><span class="punctuation">,</span></span><br><span class="line"> <span class="string">"tag-pane"</span><span class="punctuation">,</span></span><br><span class="line"> <span class="string">"page-preview"</span><span class="punctuation">,</span></span><br><span class="line"> <span class="string">"daily-notes"</span><span class="punctuation">,</span></span><br><span class="line"> <span class="string">"templates"</span><span class="punctuation">,</span></span><br><span class="line"> <span class="string">"note-composer"</span><span class="punctuation">,</span></span><br><span class="line"> <span class="string">"command-palette"</span><span class="punctuation">,</span></span><br><span class="line"> <span class="string">"editor-status"</span><span class="punctuation">,</span></span><br><span class="line"> <span class="string">"bookmarks"</span><span class="punctuation">,</span></span><br><span class="line"> <span class="string">"outline"</span><span class="punctuation">,</span></span><br><span class="line"> <span class="string">"word-count"</span><span class="punctuation">,</span></span><br><span class="line"> <span class="string">"file-recovery"</span></span><br><span class="line"><span class="punctuation">]</span></span><br></pre></td></tr></table></figure>
<pre><code> - `core-plugins-migration.json`: 就是 `core-plugins.json` 中的内置插件的开关状态,例如:
</code></pre>
<figure class="highlight json"><table><tr><td class="code"><pre><span class="line"><span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"file-explorer"</span><span class="punctuation">:</span> <span class="literal"><span class="keyword">true</span></span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">"global-search"</span><span class="punctuation">:</span> <span class="literal"><span class="keyword">true</span></span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">"switcher"</span><span class="punctuation">:</span> <span class="literal"><span class="keyword">true</span></span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">"graph"</span><span class="punctuation">:</span> <span class="literal"><span class="keyword">true</span></span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">"backlink"</span><span class="punctuation">:</span> <span class="literal"><span class="keyword">true</span></span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">"canvas"</span><span class="punctuation">:</span> <span class="literal"><span class="keyword">true</span></span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">"outgoing-link"</span><span class="punctuation">:</span> <span class="literal"><span class="keyword">true</span></span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">"tag-pane"</span><span class="punctuation">:</span> <span class="literal"><span class="keyword">true</span></span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">"properties"</span><span class="punctuation">:</span> <span class="literal"><span class="keyword">false</span></span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">"page-preview"</span><span class="punctuation">:</span> <span class="literal"><span class="keyword">true</span></span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">"daily-notes"</span><span class="punctuation">:</span> <span class="literal"><span class="keyword">true</span></span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">"templates"</span><span class="punctuation">:</span> <span class="literal"><span class="keyword">true</span></span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">"note-composer"</span><span class="punctuation">:</span> <span class="literal"><span class="keyword">true</span></span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">"command-palette"</span><span class="punctuation">:</span> <span class="literal"><span class="keyword">true</span></span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">"slash-command"</span><span class="punctuation">:</span> <span class="literal"><span class="keyword">false</span></span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">"editor-status"</span><span class="punctuation">:</span> <span class="literal"><span class="keyword">true</span></span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">"bookmarks"</span><span class="punctuation">:</span> <span class="literal"><span class="keyword">true</span></span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">"markdown-importer"</span><span class="punctuation">:</span> <span class="literal"><span class="keyword">false</span></span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">"zk-prefixer"</span><span class="punctuation">:</span> <span class="literal"><span class="keyword">false</span></span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">"random-note"</span><span class="punctuation">:</span> <span class="literal"><span class="keyword">false</span></span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">"outline"</span><span class="punctuation">:</span> <span class="literal"><span class="keyword">true</span></span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">"word-count"</span><span class="punctuation">:</span> <span class="literal"><span class="keyword">true</span></span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">"slides"</span><span class="punctuation">:</span> <span class="literal"><span class="keyword">false</span></span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">"audio-recorder"</span><span class="punctuation">:</span> <span class="literal"><span class="keyword">false</span></span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">"workspaces"</span><span class="punctuation">:</span> <span class="literal"><span class="keyword">false</span></span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">"file-recovery"</span><span class="punctuation">:</span> <span class="literal"><span class="keyword">true</span></span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">"publish"</span><span class="punctuation">:</span> <span class="literal"><span class="keyword">false</span></span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">"sync"</span><span class="punctuation">:</span> <span class="literal"><span class="keyword">false</span></span></span><br><span class="line"><span class="punctuation">}</span></span><br></pre></td></tr></table></figure>
<ul>
<li><code>workspace.json</code>:包含有关当前 Vault 工作区的数据,包括打开的文件、窗口和布局的设置,例如:</li>
</ul>
<figure class="highlight json"><table><tr><td class="code"><pre><span class="line"><span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"main"</span><span class="punctuation">:</span> <span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"id"</span><span class="punctuation">:</span> <span class="string">"7bc59326f5497d4a"</span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">"type"</span><span class="punctuation">:</span> <span class="string">"split"</span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">"children"</span><span class="punctuation">:</span> <span class="punctuation">[</span></span><br><span class="line"> <span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"id"</span><span class="punctuation">:</span> <span class="string">"06fc674d29eb4c3a"</span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">"type"</span><span class="punctuation">:</span> <span class="string">"tabs"</span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">"children"</span><span class="punctuation">:</span> <span class="punctuation">[</span></span><br><span class="line"> <span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"id"</span><span class="punctuation">:</span> <span class="string">"97d62f14e2fad2f0"</span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">"type"</span><span class="punctuation">:</span> <span class="string">"leaf"</span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">"state"</span><span class="punctuation">:</span> <span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"type"</span><span class="punctuation">:</span> <span class="string">"markdown"</span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">"state"</span><span class="punctuation">:</span> <span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"file"</span><span class="punctuation">:</span> <span class="string">"README.md"</span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">"mode"</span><span class="punctuation">:</span> <span class="string">"source"</span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">"source"</span><span class="punctuation">:</span> <span class="literal"><span class="keyword">false</span></span></span><br><span class="line"> <span class="punctuation">}</span></span><br><span class="line"> <span class="punctuation">}</span></span><br><span class="line"> <span class="punctuation">}</span><span class="punctuation">,</span></span><br><span class="line"> <span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"id"</span><span class="punctuation">:</span> <span class="string">"c94063b5e5f6f995"</span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">"type"</span><span class="punctuation">:</span> <span class="string">"leaf"</span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">"state"</span><span class="punctuation">:</span> <span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"type"</span><span class="punctuation">:</span> <span class="string">"markdown"</span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">"state"</span><span class="punctuation">:</span> <span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"file"</span><span class="punctuation">:</span> <span class="string">"README.md"</span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">"mode"</span><span class="punctuation">:</span> <span class="string">"source"</span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">"source"</span><span class="punctuation">:</span> <span class="literal"><span class="keyword">false</span></span></span><br><span class="line"> <span class="punctuation">}</span></span><br><span class="line"> <span class="punctuation">}</span></span><br><span class="line"> <span class="punctuation">}</span><span class="punctuation">,</span></span><br><span class="line"> <span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"id"</span><span class="punctuation">:</span> <span class="string">"143d5ea7a56d7e1d"</span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">"type"</span><span class="punctuation">:</span> <span class="string">"leaf"</span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">"state"</span><span class="punctuation">:</span> <span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"type"</span><span class="punctuation">:</span> <span class="string">"markdown"</span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">"state"</span><span class="punctuation">:</span> <span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"file"</span><span class="punctuation">:</span> <span class="string">"测试.md"</span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">"mode"</span><span class="punctuation">:</span> <span class="string">"source"</span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">"source"</span><span class="punctuation">:</span> <span class="literal"><span class="keyword">false</span></span></span><br><span class="line"> <span class="punctuation">}</span></span><br><span class="line"> <span class="punctuation">}</span></span><br><span class="line"> <span class="punctuation">}</span></span><br><span class="line"> <span class="punctuation">]</span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">"currentTab"</span><span class="punctuation">:</span> <span class="number">2</span></span><br><span class="line"> <span class="punctuation">}</span></span><br><span class="line"> ...</span><br></pre></td></tr></table></figure>
]]></content>
<tags>
<tag>Technique</tag>
</tags>
</entry>
<entry>
<title>Prisma 关系型数据库的 Self-relations</title>
<url>/2025/02/18/Prisma-%E5%85%B3%E7%B3%BB%E5%9E%8B%E6%95%B0%E6%8D%AE%E5%BA%93%E7%9A%84-Self-relations/</url>
<content><![CDATA[<blockquote>
<p>A relation field can also reference its own model, in this case the relation is called a <em>self-relation</em>. Self-relations can be of any cardinality, 1-1, 1-n and m-n.</p>
</blockquote>
<h2 id="一对一"><a class="header-anchor" href="#一对一">¶</a>一对一</h2>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">model User {</span><br><span class="line"> id Int @id @default(autoincrement())</span><br><span class="line"> name String?</span><br><span class="line"> successorId Int? @unique</span><br><span class="line"> successor User? @relation("BlogOwnerHistory", fields: [successorId], references: [id])</span><br><span class="line"> predecessor User? @relation("BlogOwnerHistory")</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>User 展现了这样一个模型:</p>
<ul>
<li>User 可以有一个或零个前驱(predecessor)</li>
<li>User 可以有一个或零个后继(successor)</li>
</ul>
<p>注意:不能要求前驱和后继都必须存在,这两个必须有一个是可选的,否则没办法创建第一个 User。</p>
<p>要创建一对一的 self-relation:</p>
<ul>
<li>关系的两端都必须定义一个共享相同名称的 <code>@relation</code> 属性(<code>BlogOwnerHistory</code>)</li>
<li>关系字段必须是<a href="https://www.prisma.io/docs/orm/prisma-schema/data-model/relations#relation-fields">完全注释</a>的。例如 <code>successor</code> 字段需要定义 <code>field</code> 和 <code>references</code> 参数。</li>
<li>关系字段必须由外键支持。<code>successor</code> 字段由 <code>successorId</code> 外键提供支持,该外键引用 <code>id</code> 字段中的值。<code>successorId</code> 还需要 <code>@unique</code> 属性来保证一对一的关系。</li>
</ul>
<blockquote>
<p>一对一的 self-relation 需要两个端点,即使这两个端点是同一条数据。</p>
</blockquote>
<p>而在关系型数据库中,一对一的 self-relation 可以用如下 SQL 描述:</p>
<figure class="highlight sql"><table><tr><td class="code"><pre><span class="line"><span class="keyword">CREATE TABLE</span> "User" (</span><br><span class="line"> id SERIAL <span class="keyword">PRIMARY KEY</span>,</span><br><span class="line"> "name" TEXT,</span><br><span class="line"> "successorId" <span class="type">INTEGER</span></span><br><span class="line">);</span><br><span class="line"></span><br><span class="line"><span class="keyword">ALTER TABLE</span> "User" <span class="keyword">ADD CONSTRAINT</span> fk_successor_user <span class="keyword">FOREIGN KEY</span> ("successorId") <span class="keyword">REFERENCES</span> "User" (id);</span><br><span class="line"></span><br><span class="line"><span class="keyword">ALTER TABLE</span> "User" <span class="keyword">ADD CONSTRAINT</span> successor_unique <span class="keyword">UNIQUE</span> ("successorId");</span><br></pre></td></tr></table></figure>
<h2 id="一对多"><a class="header-anchor" href="#一对多">¶</a>一对多</h2>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">model User {</span><br><span class="line"> id Int @id @default(autoincrement())</span><br><span class="line"> name String?</span><br><span class="line"> teacherId Int?</span><br><span class="line"> teacher User? @relation("TeacherStudents", fields: [teacherId], references: [id])</span><br><span class="line"> students User[] @relation("TeacherStudents")</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>User 展现了这样一个模型:</p>
<ul>
<li>一个 User 只能有零个或一个 teacher</li>
<li>一个 User 可以有零个或多个 students</li>
</ul>
<blockquote>
<p>可以通过将 <code>teacher</code> 字段设<a href="https://www.prisma.io/docs/orm/prisma-schema/data-model/models#optional-and-mandatory-fields">为 required</a> 来要求每个 User 都有一名 teacher。</p>
</blockquote>
<p>用 SQL 描述 User model:</p>
<figure class="highlight sql"><table><tr><td class="code"><pre><span class="line"><span class="keyword">CREATE TABLE</span> "User" (</span><br><span class="line"> id SERIAL <span class="keyword">PRIMARY KEY</span>,</span><br><span class="line"> "name" TEXT,</span><br><span class="line"> "teacherId" <span class="type">INTEGER</span></span><br><span class="line">);</span><br><span class="line"></span><br><span class="line"><span class="keyword">ALTER TABLE</span> "User" <span class="keyword">ADD CONSTRAINT</span> fk_teacherid_user <span class="keyword">FOREIGN KEY</span> ("teacherId") <span class="keyword">REFERENCES</span> "User" (id);</span><br></pre></td></tr></table></figure>
<p><code>teacherId</code> 没有使用 <code>UNIQUE</code> 约束,这代表着多个 students 可以有同一个 teacher</p>
<h2 id="多对多"><a class="header-anchor" href="#多对多">¶</a>多对多</h2>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">model User {</span><br><span class="line"> id Int @id @default(autoincrement())</span><br><span class="line"> name String?</span><br><span class="line"> followedBy User[] @relation("UserFollows")</span><br><span class="line"> following User[] @relation("UserFollows")</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<ul>
<li>一个 User 可以被零个或多个 Users 关注</li>
<li>一个 User 可以关注零个或多个 Users</li>
</ul>
<blockquote>
<p>对于关系型数据库,多对多的关系是隐式的,这意味着 Prisma ORM 会在底层数据库中维护一个 <a href="https://www.prisma.io/docs/orm/prisma-schema/data-model/relations/many-to-many-relations#relation-tables">relation table</a>:<br>
A relation table (also sometimes called a <em>JOIN</em>, <em>link</em> or <em>pivot</em> table) connects two or more other tables and therefore creates a <em>relation</em> between them. Creating relation tables is a common data modelling practice in SQL to represent relationships between different entities. In essence it means that “one m-n relation is modeled as two 1-n relations in the database”.</p>
<p>We recommend using <a href="https://www.prisma.io/docs/orm/prisma-schema/data-model/relations/many-to-many-relations#implicit-many-to-many-relations">implicit</a> m-n-relations, where Prisma ORM automatically generates the relation table in the underlying database. <a href="https://www.prisma.io/docs/orm/prisma-schema/data-model/relations/many-to-many-relations#explicit-many-to-many-relations">Explicit</a> m-n-relations should be used when you need to store additional data in the relations, such as the date the relation was created.</p>
</blockquote>
<p>如果需要需要通过多对多的关系来保存其他字段,也可以创建<a href="https://www.prisma.io/docs/orm/prisma-schema/data-model/relations/many-to-many-relations#explicit-many-to-many-relations">显式</a>的多对多 self 关系:</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">model User {</span><br><span class="line"> id Int @id @default(autoincrement())</span><br><span class="line"> name String?</span><br><span class="line"> followedBy Follows[] @relation("followedBy")</span><br><span class="line"> following Follows[] @relation("following")</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">model Follows {</span><br><span class="line"> followedBy User @relation("followedBy", fields: [followedById], references: [id])</span><br><span class="line"> followedById Int</span><br><span class="line"> following User @relation("following", fields: [followingId], references: [id])</span><br><span class="line"> followingId Int</span><br><span class="line"></span><br><span class="line"> @@id([followingId, followedById])</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>在关系型数据库中,可以用如下 SQL 描述:</p>
<figure class="highlight sql"><table><tr><td class="code"><pre><span class="line"><span class="keyword">CREATE TABLE</span> "User" (</span><br><span class="line"> id <span class="type">integer</span> <span class="keyword">DEFAULT</span> nextval(<span class="string">'"User_id_seq"'</span>::regclass) <span class="keyword">PRIMARY KEY</span>,</span><br><span class="line"> name text</span><br><span class="line">);</span><br><span class="line"><span class="keyword">CREATE TABLE</span> "_UserFollows" (</span><br><span class="line"> "A" <span class="type">integer</span> <span class="keyword">NOT NULL</span> <span class="keyword">REFERENCES</span> "User"(id) <span class="keyword">ON</span> <span class="keyword">DELETE</span> CASCADE <span class="keyword">ON</span> <span class="keyword">UPDATE</span> CASCADE,</span><br><span class="line"> "B" <span class="type">integer</span> <span class="keyword">NOT NULL</span> <span class="keyword">REFERENCES</span> "User"(id) <span class="keyword">ON</span> <span class="keyword">DELETE</span> CASCADE <span class="keyword">ON</span> <span class="keyword">UPDATE</span> CASCADE</span><br><span class="line">);</span><br></pre></td></tr></table></figure>
<h2 id="在同一模型上建立多个-self-relations"><a class="header-anchor" href="#在同一模型上建立多个-self-relations">¶</a>在同一模型上建立多个 self-relations</h2>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">model User {</span><br><span class="line"> id Int @id @default(autoincrement())</span><br><span class="line"> name String?</span><br><span class="line"> teacherId Int?</span><br><span class="line"> teacher User? @relation("TeacherStudents", fields: [teacherId], references: [id])</span><br><span class="line"> students User[] @relation("TeacherStudents")</span><br><span class="line"> followedBy User[] @relation("UserFollows")</span><br><span class="line"> following User[] @relation("UserFollows")</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h2 id="REFS"><a class="header-anchor" href="#REFS">¶</a>REFS.</h2>
<ul>
<li><a href="https://www.prisma.io/docs/orm/prisma-schema/data-model/relations#relation-fields">fully annotated</a></li>
<li><a href="https://www.prisma.io/docs/orm/prisma-schema/data-model/models#optional-and-mandatory-fields">required</a></li>
<li><a href="https://www.prisma.io/docs/orm/prisma-schema/data-model/relations/many-to-many-relations#implicit-many-to-many-relations">implicit</a></li>
<li><a href="https://www.prisma.io/docs/orm/prisma-schema/data-model/relations/many-to-many-relations#relation-tables">relation table</a></li>
<li><a href="https://www.prisma.io/docs/orm/prisma-schema/data-model/relations/many-to-many-relations#explicit-many-to-many-relations">explicit</a></li>
</ul>
]]></content>
<tags>
<tag>Technique</tag>
</tags>
</entry>
<entry>
<title>Radiographic Absence of the Left Humeral Head</title>
<url>/2023/10/25/Radiographic-Absence-of-the-Left-Humeral-Head/</url>
<content><![CDATA[<p>A 67-year-old man with thoracolumbar scoliosis, poor mobility, and history of frequent falls presented to the emergency department with 2 months of left shoulder pain, stiffness and reduced range of motion, and numbness and paresthesias in his left upper extremity.</p>
<p>Ten years prior to presentation, he underwent surgical decompression for syringomyelia.</p>
<p>A magnetic resonance imaging (MRI) scan of his cervical and thoracic spine performed 2 years prior to presentation revealed a recurrent syrinx extending from Cl C1 to T11, which was not resected because it did not cause symptoms at that time.</p>
<p>On physical examination, he had mild tenderness to palpation and reduced range of motion of the left shoulder with abduction and flexion limited to 120° (normal range of motion, 180°).</p>
<p>The left scapular muscles were atrophic, and pain and temperature sensation were reduced in his proximal left arm, and dorsal aspect of his left shoulder.</p>
<p>His complete blood cell count, serum glucose levels, C-reactive protein levels, and erythrocyte sedimentation rate were normal.</p>
<p>Results of tests for rheumatoid factor and antinuclear antibody were negative.</p>
<p>Left shoulder radiograph showed complete absence of the left humeral head and a well-demarcated smooth osseous margin of the proximal humerus with associated soft tissue swelling and periarticular calcification.</p>
<p>A chest radiograph taken 2 years prior revealed a normal left shoulder joint. The patient was hospitalized for further evaluation and treatment.</p>
<ul>
<li><a href="/papers/Radiographic%20Absence%20of%20the%20Left%20Humeral%20Head.pdf">报告原档</a></li>
</ul>
<p>这里提到了一种罕见的进展性疾病, 叫Charcot肩, 是一种神经性关节病变,特点是关节迅速被破坏和相关软组织肿胀。患者通常表现为肩部逐渐增大的肿胀,可能伴有疼痛或无痛,肩部无力和活动范围减少。<br>
大约80%的Charcot肩患者有脊髓空洞症, 表现为脊髓中的液体充满腔体</p>
]]></content>
<tags>
<tag>Medicine</tag>
</tags>
</entry>
<entry>
<title>Repository pattern in Typescript</title>
<url>/2025/02/19/Repository-pattern-in-Typescript/</url>
<content><![CDATA[<blockquote>
<p>The Repository design pattern is a <a href="https://www.geeksforgeeks.org/structural-design-patterns/">structural pattern</a> that abstracts data access, providing a centralized way to manage data operations. By separating the data layer from business logic, it enhances code maintainability, testability, and flexibility, making it easier to work with various data sources in an application.</p>
</blockquote>
<p>抽象化数据访问层的主要作用是将应用的业务逻辑与对数据库的数据访问等实现细节进行解耦。DB 框架的更改不应该影响到应用程序的核心服务,并且它们应该对业务逻辑代码透明。</p>
<p>业务服务应该只依赖于抽象,而不是实现,数据服务同理。</p>
<hr>
<h1>Intro.</h1>
<p>例如,此时有一个书籍存储的微服务,并且有一个“添加新书籍”的操作用例,在此用例中可以通过使用 <code>Book</code> 这个 <em>Repository</em> 来添加新书籍,而 <code>Book</code> 具体使用什么数据库,怎么存并不是业务需要关心的事情。</p>
<h2 id="具体实现方式"><a class="header-anchor" href="#具体实现方式">¶</a>具体实现方式</h2>
<p>假设有以下实体类型定义:</p>
<figure class="highlight typescript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">class</span> <span class="title class_">Author</span> {</span><br><span class="line"> <span class="attr">firstName</span>: <span class="built_in">string</span>;</span><br><span class="line"> <span class="attr">lastName</span>: <span class="built_in">string</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">class</span> <span class="title class_">Genre</span> {</span><br><span class="line"> <span class="attr">name</span>: <span class="built_in">string</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">class</span> <span class="title class_">Book</span> {</span><br><span class="line"> <span class="attr">title</span>: <span class="built_in">string</span>;</span><br><span class="line"> <span class="attr">author</span>: <span class="title class_">Author</span>;</span><br><span class="line"> <span class="attr">genre</span>: <span class="title class_">Genre</span>;</span><br><span class="line"> <span class="attr">publishDate</span>: <span class="title class_">Date</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h3 id="抽象化"><a class="header-anchor" href="#抽象化">¶</a>抽象化</h3>
<p>第一步是抽象化出一个通用的 <em>Repository</em> ,其中包含通用的操作:</p>
<figure class="highlight typescript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">abstract</span> <span class="keyword">class</span> <span class="title class_">IGenericRepository</span><T> {</span><br><span class="line"> <span class="keyword">abstract</span> <span class="title function_">getAll</span>(): <span class="title class_">Promise</span><T[]>;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">abstract</span> <span class="title function_">get</span>(<span class="attr">id</span>: <span class="built_in">string</span>): <span class="title class_">Promise</span><T>;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">abstract</span> <span class="title function_">create</span>(<span class="attr">item</span>: T): <span class="title class_">Promise</span><T>;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">abstract</span> <span class="title function_">update</span>(<span class="attr">id</span>: <span class="built_in">string</span>, <span class="attr">item</span>: T);</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<ul>
<li>这里具体有那些函数可以根据需要(业务)自己定义。</li>
<li>类型参数 <code>T</code> 表示每个实体。</li>
</ul>
<p>接着,定义一个数据服务,其中包含使用 <code>IGenericRepositoiry</code> 定义的具体实例对应的 <em>Repository</em>:</p>
<figure class="highlight typescript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> { <span class="title class_">Author</span>, <span class="title class_">Book</span>, <span class="title class_">Genre</span> } <span class="keyword">from</span> <span class="string">'../entities'</span>;</span><br><span class="line"><span class="keyword">import</span> { <span class="title class_">IGenericRepository</span> } <span class="keyword">from</span> <span class="string">'./generic-repository.abstract'</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">abstract</span> <span class="keyword">class</span> <span class="title class_">IDataServices</span> {</span><br><span class="line"> <span class="keyword">abstract</span> <span class="attr">authors</span>: <span class="title class_">IGenericRepository</span><<span class="title class_">Author</span>>;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">abstract</span> <span class="attr">books</span>: <span class="title class_">IGenericRepository</span><<span class="title class_">Book</span>>;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">abstract</span> <span class="attr">genres</span>: <span class="title class_">IGenericRepository</span><<span class="title class_">Genre</span>>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<ul>
<li>这里有三个 <em>Repository</em> 分别对应不同的实体。</li>
<li><code>IGenericRepository</code> 中定义的函数是每个 <em>Repository</em> 公开的通用存储函数。</li>
</ul>
<p>以上这些就是对业务中的实体的 <em>Repository</em> 抽象,这样就隔离开了业务和存储逻辑,例如使用 MongoDB,可以实现一个 <code>MongoGenericRepository</code>:</p>
<figure class="highlight typescript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> { <span class="title class_">Model</span> } <span class="keyword">from</span> <span class="string">'mongoose'</span>;</span><br><span class="line"><span class="keyword">import</span> { <span class="title class_">IGenericRepository</span> } <span class="keyword">from</span> <span class="string">'../../../core'</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">class</span> <span class="title class_">MongoGenericRepository</span><T> <span class="keyword">implements</span> <span class="title class_">IGenericRepository</span><T> {</span><br><span class="line"> <span class="keyword">private</span> <span class="attr">_repository</span>: <span class="title class_">Model</span><T>;</span><br><span class="line"> <span class="keyword">private</span> <span class="attr">_populateOnFind</span>: <span class="built_in">string</span>[];</span><br><span class="line"></span><br><span class="line"> <span class="title function_">constructor</span>(<span class="params"><span class="attr">repository</span>: <span class="title class_">Model</span><T>, <span class="attr">populateOnFind</span>: <span class="built_in">string</span>[] = []</span>) {</span><br><span class="line"> <span class="variable language_">this</span>.<span class="property">_repository</span> = repository;</span><br><span class="line"> <span class="variable language_">this</span>.<span class="property">_populateOnFind</span> = populateOnFind;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="title function_">getAll</span>(): <span class="title class_">Promise</span><T[]> {</span><br><span class="line"> <span class="keyword">return</span> <span class="variable language_">this</span>.<span class="property">_repository</span>.<span class="title function_">find</span>().<span class="title function_">populate</span>(<span class="variable language_">this</span>.<span class="property">_populateOnFind</span>).<span class="title function_">exec</span>();</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="title function_">get</span>(<span class="attr">id</span>: <span class="built_in">any</span>): <span class="title class_">Promise</span><T> {</span><br><span class="line"> <span class="keyword">return</span> <span class="variable language_">this</span>.<span class="property">_repository</span>.<span class="title function_">findById</span>(id).<span class="title function_">populate</span>(<span class="variable language_">this</span>.<span class="property">_populateOnFind</span>).<span class="title function_">exec</span>();</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="title function_">create</span>(<span class="attr">item</span>: T): <span class="title class_">Promise</span><T> {</span><br><span class="line"> <span class="keyword">return</span> <span class="variable language_">this</span>.<span class="property">_repository</span>.<span class="title function_">create</span>(item);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="title function_">update</span>(<span class="params"><span class="attr">id</span>: <span class="built_in">string</span>, <span class="attr">item</span>: T</span>) {</span><br><span class="line"> <span class="keyword">return</span> <span class="variable language_">this</span>.<span class="property">_repository</span>.<span class="title function_">findByIdAndUpdate</span>(id, item);</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>然后实现一个 <code>MongoDataServices</code>:</p>
<figure class="highlight typescript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> { <span class="title class_">Injectable</span>, <span class="title class_">OnApplicationBootstrap</span> } <span class="keyword">from</span> <span class="string">'@nestjs/common'</span>;</span><br><span class="line"><span class="keyword">import</span> { <span class="title class_">InjectModel</span> } <span class="keyword">from</span> <span class="string">'@nestjs/mongoose'</span>;</span><br><span class="line"><span class="keyword">import</span> { <span class="title class_">Model</span> } <span class="keyword">from</span> <span class="string">'mongoose'</span>;</span><br><span class="line"><span class="keyword">import</span> { <span class="title class_">IDataServices</span> } <span class="keyword">from</span> <span class="string">'../../../core'</span>;</span><br><span class="line"><span class="keyword">import</span> { <span class="title class_">MongoGenericRepository</span> } <span class="keyword">from</span> <span class="string">'./mongo-generic-repository'</span>;</span><br><span class="line"><span class="keyword">import</span> {</span><br><span class="line"> <span class="title class_">Author</span>,</span><br><span class="line"> <span class="title class_">AuthorDocument</span>,</span><br><span class="line"> <span class="title class_">Book</span>,</span><br><span class="line"> <span class="title class_">BookDocument</span>,</span><br><span class="line"> <span class="title class_">Genre</span>,</span><br><span class="line"> <span class="title class_">GenreDocument</span>,</span><br><span class="line">} <span class="keyword">from</span> <span class="string">'./model'</span>;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Injectable</span>()</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">class</span> <span class="title class_">MongoDataServices</span></span><br><span class="line"> <span class="keyword">implements</span> <span class="title class_">IDataServices</span>, <span class="title class_">OnApplicationBootstrap</span></span><br><span class="line">{</span><br><span class="line"> <span class="attr">authors</span>: <span class="title class_">MongoGenericRepository</span><<span class="title class_">Author</span>>;</span><br><span class="line"> <span class="attr">books</span>: <span class="title class_">MongoGenericRepository</span><<span class="title class_">Book</span>>;</span><br><span class="line"> <span class="attr">genres</span>: <span class="title class_">MongoGenericRepository</span><<span class="title class_">Genre</span>>;</span><br><span class="line"></span><br><span class="line"> <span class="title function_">constructor</span>(<span class="params"></span></span><br><span class="line"><span class="params"> <span class="meta">@InjectModel</span>(Author.name)</span></span><br><span class="line"><span class="params"> <span class="keyword">private</span> <span class="title class_">AuthorRepository</span>: <span class="title class_">Model</span><<span class="title class_">AuthorDocument</span>>,</span></span><br><span class="line"><span class="params"> <span class="meta">@InjectModel</span>(Book.name)</span></span><br><span class="line"><span class="params"> <span class="keyword">private</span> <span class="title class_">BookRepository</span>: <span class="title class_">Model</span><<span class="title class_">BookDocument</span>>,</span></span><br><span class="line"><span class="params"> <span class="meta">@InjectModel</span>(Genre.name)</span></span><br><span class="line"><span class="params"> <span class="keyword">private</span> <span class="title class_">GenreRepository</span>: <span class="title class_">Model</span><<span class="title class_">GenreDocument</span>>,</span></span><br><span class="line"><span class="params"> </span>) {}</span><br><span class="line"></span><br><span class="line"> <span class="title function_">onApplicationBootstrap</span>(<span class="params"></span>) {</span><br><span class="line"> <span class="variable language_">this</span>.<span class="property">authors</span> = <span class="keyword">new</span> <span class="title class_">MongoGenericRepository</span><<span class="title class_">Author</span>>(<span class="variable language_">this</span>.<span class="property">AuthorRepository</span>);</span><br><span class="line"> <span class="variable language_">this</span>.<span class="property">books</span> = <span class="keyword">new</span> <span class="title class_">MongoGenericRepository</span><<span class="title class_">Book</span>>(<span class="variable language_">this</span>.<span class="property">BookRepository</span>, [</span><br><span class="line"> <span class="string">'author'</span>,</span><br><span class="line"> <span class="string">'genre'</span>,</span><br><span class="line"> ]);</span><br><span class="line"> <span class="variable language_">this</span>.<span class="property">genres</span> = <span class="keyword">new</span> <span class="title class_">MongoGenericRepository</span><<span class="title class_">Genre</span>>(<span class="variable language_">this</span>.<span class="property">GenreRepository</span>);</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<hr>
<p>Sunday, March 30, 2025 7:58 PM:</p>
<p>感觉以上全错,不应该过度抽象 Repository 的,就让它分散在各个模块中,还可以参考 <a href="https://github.com/Papooch/nestjs-cls">https://github.com/Papooch/nestjs-cls</a> 实现不会抽象泄漏的事务性 Repository 方法。</p>
]]></content>
<tags>
<tag>Technique</tag>
</tags>
</entry>
<entry>
<title>Rescript and React Native</title>
<url>/2024/01/22/Rescript-React-Native/</url>
<content><![CDATA[<p>Rescript 对 React Native 的支持还算可以,但如今的 RN 已经是 Expo 的 RN 了,所以仍然有些落后,在这个 template 中,我测试了 rescript + rescript-react-native + expo + nativewind 的组合,可以跑起来:</p>
<ul>
<li><a href="https://github.com/muqiuhan/rescript-react-native-expo-nativewind-template">muqiuhan/rescript-react-native-expo-nativewind-template</a></li>
</ul>
<p>其中有两个问题,一是 <a href="https://github.com/rescript-bindings/expo">rescript-expo</a> 对 rescript v11 的兼容性,我通过简单的注释让其通过编译:</p>
<ul>
<li><a href="https://github.com/muqiuhan/expo/commit/918e9578f2a991450821760352c2445c4866f0d8">[Fix]: DocumentPicker type options</a></li>
</ul>
<p>二是对于 nativewind 的支持,编写的 binding 非常丑陋:</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">type default_style = {</span><br><span class="line"> className?: string,</span><br><span class="line"> children?: React.element,</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">@module("nativewind")</span><br><span class="line">external styled: React.component<'a> => React.component<default_style> = "styled"</span><br><span class="line"></span><br><span class="line">module StyledText = {</span><br><span class="line"> let make = styled(ReactNative.Text.make)</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">module StyledView = {</span><br><span class="line"> let make = styled(ReactNative.View.make)</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">module StyledImage = {</span><br><span class="line"> type props = {</span><br><span class="line"> source?: string,</span><br><span class="line"> ...default_style,</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> @module("nativewind")</span><br><span class="line"> external styled: React.component<'a> => React.component<props> = "styled"</span><br><span class="line"></span><br><span class="line"> let make: React.component<props> = styled(ReactNative.Image.make)</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>而这似乎并没有好的解决方案,理想中的实现应该是:</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">module Styled = (Component: {</span><br><span class="line"> type params // or type params = 'a</span><br><span class="line"> let make: React.component<props></span><br><span class="line">}) => {</span><br><span class="line"> type props = {</span><br><span class="line"> ...Component.props, // This can't work as it is not defined as record</span><br><span class="line"> className?: string</span><br><span class="line"> }</span><br><span class="line"> @module("nativewind")</span><br><span class="line"> external styled: React.component<Component.props> => React.component<props> = "styled"</span><br><span class="line"></span><br><span class="line"> let make: React.component<props> = styled(Component.make)</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>或者退一步:</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">type styledProps = {</span><br><span class="line"> className?: string,</span><br><span class="line"> // ...</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">@module("nativewind")</span><br><span class="line"> external styled: React.component<'a> => React.component<'b> = "styled"</span><br><span class="line"></span><br><span class="line">module StyledText = {</span><br><span class="line"> type props = {</span><br><span class="line"> ...ReactNative.Text.props,</span><br><span class="line"> ...styledProps</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> let make: React.component<props> = styled(ReactNative.Text.make)</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>但这在当前的 rescript 中,根本无法实现。</p>
<p>详细的讨论看这个帖子:</p>
<ul>
<li><a href="https://forum.rescript-lang.org/t/how-to-use-nativewind-in-rescript-react-native/5005">How to use Nativewind in Rescript React Native</a></li>
</ul>
]]></content>
<tags>
<tag>Technique</tag>
</tags>
</entry>
<entry>
<title>Rescript @genType import 问题</title>
<url>/2024/05/20/Rescript-genType-import-%E9%97%AE%E9%A2%98/</url>
<content><![CDATA[<p>Rescript 11 之后,<code>@genType</code> 被合并进编译器,无需任何依赖就能使用,当在 Rescript 中 <code>@genType</code> 了使用某些 Rescript built-in 的基本类型时,可能会生成有问题的 <code>import</code> 相关代码,例如:</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">@genType</span><br><span class="line">module LoginResponse = {</span><br><span class="line"> let status = response => {</span><br><span class="line"> response</span><br><span class="line"> ->Js.Json.decodeObject</span><br><span class="line"> ->Option.flatMap(response => {</span><br><span class="line"> response->Js_dict.get("status")</span><br><span class="line"> })</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p><code>status</code> 函数具有 <code>Js.Json.t => option<Js.Json.t></code> 类型,那么在生成的 TypeScript 文件中,会出现这样的 import:</p>
<figure class="highlight typescript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> <span class="keyword">type</span> { <span class="title class_">Json</span>_t <span class="keyword">as</span> <span class="title class_">Js</span>_Json_t } <span class="keyword">from</span> <span class="string">"./Js.gen.tsx"</span></span><br></pre></td></tr></table></figure>
<p>而 <code>Js.gen.tsx</code> 这个文件是不存在的,解决方案是使用 <code>@genType</code> 的 <code>shim</code>:</p>
<ul>
<li>在 <code>rescript.json</code> 中的 <code>gentypeconfig</code> 中添加 <code>shim</code> 配置:</li>
</ul>
<figure class="highlight json"><table><tr><td class="code"><pre><span class="line">...</span><br><span class="line"><span class="attr">"gentypeconfig"</span><span class="punctuation">:</span> <span class="punctuation">{</span></span><br><span class="line">...</span><br><span class="line"> <span class="attr">"shims"</span><span class="punctuation">:</span> <span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"Js"</span><span class="punctuation">:</span> <span class="string">"Js"</span></span><br><span class="line"> <span class="punctuation">}</span><span class="punctuation">,</span></span><br><span class="line">...</span><br><span class="line"><span class="punctuation">}</span></span><br><span class="line">...</span><br></pre></td></tr></table></figure>
<ul>
<li>然后新建 <code>Js.shim.ts</code>:</li>
</ul>
<figure class="highlight typescript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">type</span> <span class="title class_">Json</span>_t = <span class="built_in">unknown</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">type</span> t = <span class="built_in">unknown</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">type</span> <span class="title class_">Exn</span>_t = <span class="title class_">Error</span>;</span><br></pre></td></tr></table></figure>
<ul>
<li>删除原来由 <code>@genType</code> 生成的 TypeScript 文件并重新生成</li>
</ul>
<p>现在生成的 TypeScript 文件将会从 <code>Js.shim.ts</code> import 类型:</p>
<figure class="highlight typescript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> <span class="keyword">type</span> {<span class="title class_">Json</span>_t <span class="keyword">as</span> <span class="title class_">Js</span>_Json_t} <span class="keyword">from</span> <span class="string">'../../src/model/Js.shim.ts'</span>;</span><br></pre></td></tr></table></figure>
]]></content>
<tags>
<tag>Technique</tag>
</tags>
</entry>
<entry>
<title>Rust NewType 模式</title>
<url>/2023/05/03/Rust-NewType-%E6%A8%A1%E5%BC%8F/</url>
<content><![CDATA[<p>New Type模式是一种软件设计模式,用于在已有类型的基础上创建一个新的类型。在Rust中,这通常是通过定义一个结构体,其中只包含一个单一成员。这个结构体(New Type)对外提供了一个新的、独立的类型,用于对原始类型增加额外的语义或限制。</p>
<h2 id="加强类型安全"><a class="header-anchor" href="#加强类型安全">¶</a>加强类型安全</h2>
<figure class="highlight rust"><table><tr><td class="code"><pre><span class="line"><span class="keyword">struct</span> <span class="title class_">Meters</span>(<span class="type">f64</span>);</span><br><span class="line"><span class="keyword">struct</span> <span class="title class_">Feet</span>(<span class="type">f64</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> <span class="variable">length_in_meters</span> = <span class="title function_ invoke__">Meters</span>(<span class="number">100.0</span>);</span><br><span class="line"><span class="keyword">let</span> <span class="variable">length_in_feet</span> = <span class="title function_ invoke__">Feet</span>(<span class="number">328.084</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 编译器会防止以下代码执行,因为类型不匹配</span></span><br><span class="line"><span class="comment">// let wrong_length = Meters(length_in_feet); // 编译错误</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 正确的构造</span></span><br><span class="line"><span class="keyword">fn</span> <span class="title function_">add_lengths</span>(length1: Meters, length2: Meters) <span class="punctuation">-></span> Meters {</span><br><span class="line"> <span class="title function_ invoke__">Meters</span>(length1.<span class="number">0</span> + length2.<span class="number">0</span>)</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>这个例子使用 newtype 模式避免将原始类型f64用于不同的量度,从而增强了类型的安全性。</p>
<h2 id="实现特定-trait"><a class="header-anchor" href="#实现特定-trait">¶</a>实现特定 trait</h2>
<figure class="highlight rust"><table><tr><td class="code"><pre><span class="line"><span class="keyword">struct</span> <span class="title class_">Kilometers</span>(<span class="type">f64</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">impl</span> <span class="title class_">Kilometers</span> {</span><br><span class="line"> <span class="keyword">fn</span> <span class="title function_">to_miles</span>(&<span class="keyword">self</span>) <span class="punctuation">-></span> <span class="type">f64</span> {</span><br><span class="line"> <span class="keyword">self</span>.<span class="number">0</span> * <span class="number">0.621371</span></span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> <span class="variable">distance</span> = <span class="title function_ invoke__">Kilometers</span>(<span class="number">10.0</span>);</span><br><span class="line"><span class="built_in">println!</span>(<span class="string">"The distance in miles is {}"</span>, distance.<span class="title function_ invoke__">to_miles</span>());</span><br></pre></td></tr></table></figure>
<p>这里,Kilometers有一个方法to_miles,该方法是不会影响其他f64数据的。如果我们有另一个表示温度的f64类型,就不会意外调用到与距离相关的方法。</p>
<p>New Type模式同样适用于对<code>Box<dyn SomeTrait></code>类型的包装,这可以在需要动态分派(动态调用实现了某个接口的不同类型的对象的方法)的时候提供便利。通过创建一个New Type来包装这样的<code>Box<dyn SomeTrait></code>类型,可以提供自定义的方法或实现更多的trait,同时也可以让API更加清晰和易于使用。</p>
<h2 id="零成本抽象"><a class="header-anchor" href="#零成本抽象">¶</a>零成本抽象</h2>
<p>在Rust中,New Type模式不仅是类型安全的,还是一种零成本抽象。这是因为Rust编译器在编译时期会进行足够的优化,以确保New Type的使用没有运行时开销。 Rust的零成本抽象原则确保了抽象不会引入额外的运行时成本。例如,当你使用Meters这样的New Type时,Rust确保:</p>
<ol>
<li>无额外内存开销:Meters只包含一个f64,在内存中的表现和单独的f64是一样的。</li>
<li>无额外运行时开销:使用Meters时,性能和直接使用f64完全相同。编译器会移除任何关于New Type的包装和解包的代码。</li>
</ol>
]]></content>
<tags>
<tag>Technique</tag>
</tags>
</entry>
<entry>
<title>Rust Partial 语义</title>
<url>/2023/05/02/Rust-Partial-%E8%AF%AD%E4%B9%89/</url>
<content><![CDATA[<blockquote>
<p>在Rust中,PartialEq和PartialOrd trait处理了不是所有值都可以相互比较的情况。</p>
</blockquote>
<h2 id="PartialEq-Trait"><a class="header-anchor" href="#PartialEq-Trait">¶</a>PartialEq Trait</h2>
<p>PartialEq trait用于定义值相等性的比较。它的设计允许类型的值之间进行相等(<code>==</code>)和不等(<code>!=</code>)的比较。与其对应的 Eq trait 确保一个类型的所有值都是可以可靠比较的,即满足等价关系的特性,如自反性、对称性和传递性。</p>
<figure class="highlight rust"><table><tr><td class="code"><pre><span class="line"><span class="keyword">fn</span> <span class="title function_">eq</span>(&<span class="keyword">self</span>, other: &<span class="keyword">Self</span>) <span class="punctuation">-></span> <span class="type">bool</span>;</span><br><span class="line"><span class="keyword">fn</span> <span class="title function_">ne</span>(&<span class="keyword">self</span>, other: &<span class="keyword">Self</span>) <span class="punctuation">-></span> <span class="type">bool</span>;</span><br></pre></td></tr></table></figure>
<p>在大多数情况下,类型的值都能够完全比较相等性,这时可以实现Eq。然而,对于一些特殊类型的值,如浮点数,由于存在无穷大的正负值和NaN值,导致它们的比较更加复杂。例如,根据IEEE浮点数的标准,NaN与任何值(包括它自己)比较都不相等。</p>
<h2 id="PartialOrd-Trait"><a class="header-anchor" href="#PartialOrd-Trait">¶</a>PartialOrd Trait</h2>
<p>PartialOrd trait用于定义值之间的大小比较。类似于PartialEq,它允许部分比较大小,返回一个Option,表示比较结果可能存在,也可能不存在(即比较无法进行时返回None):</p>
<figure class="highlight rust"><table><tr><td class="code"><pre><span class="line"><span class="keyword">fn</span> <span class="title function_">partial_cmp</span>(&<span class="keyword">self</span>, other: &<span class="keyword">Self</span>) <span class="punctuation">-></span> <span class="type">Option</span><Ordering>;</span><br></pre></td></tr></table></figure>
<p>在全部比较可能的场景,我们会使用Ord trait,它要求实现cmp方法,总是返回一个Ordering,表示两个值之间的确切比较关系。Ord是在所有值都能够比较时使用的,例如整数和字符串。</p>
<h2 id="设计用意和解决的问题"><a class="header-anchor" href="#设计用意和解决的问题">¶</a>设计用意和解决的问题</h2>
<p>Rust 设计 PartialEq 和 PartialOrd trait 主要出于以下几个理由:</p>
<ul>
<li>非总序理念:并不是所有类型都有一个全局的排序方法。例如,复数之间就没有一个自然的大小顺序。为了避免为这些类型人为地赋予一个排序方法,Rust 提供了一个只需部分实现序列操作的选择。</li>
<li>IEEE 浮点数标准:由于浮点数标准定义了特殊值(NaN, 正负无穷),以及NaN不等于自身的规则,浮点数在一些情况下不能进行相等性或大小比较。</li>
<li>提升错误处理能力和安全性:通过返回 <code>Option<Ordering></code>,<code>partial_cmp</code> 方法明确指出了失败的可能性,从而迫使程序员在使用时考虑并处理这种情况,增加了代码的正确性和稳健性。</li>
<li>表达性和灵活性:这些 trait 允许开发者为自定义类型定义适当的相等性和排序行为,从而加强了 Rust 类型系统的表达性和灵活性。</li>
</ul>
<p>PartialEq 和 PartialOrd trait 的设计允许程序员选择精准的相等性和排序语义,同时明确了对于某些类型相等性比较和大小排序并不总是可能的事实。通过引入适度的复杂性,让 Rust 的类型系统更加安全。</p>
]]></content>
<tags>
<tag>Technique</tag>
</tags>
</entry>
<entry>
<title>Rust 虚表布局规则介绍</title>
<url>/2023/05/01/Rust-%E8%99%9A%E8%A1%A8%E5%B8%83%E5%B1%80%E8%A7%84%E5%88%99%E4%BB%8B%E7%BB%8D/</url>
<content><![CDATA[<p>在 Rust 中,一个指向未知大小对象(!Sized)的引用或指针被实现为一个由两个 usize 大小的域构成的胖指针。这两个域中,其中一个域保存了被引用或被指向的对象的地址,另一个域保存了一个名为 metadata 的数据。对于 slice 的引用或指针来说,其 metadata 为 slice 的长度。对于 trait object 的引用或指针来说,其 metadata 为虚表(vtable)地址。与 C++ 虚表类似,Rust 虚表的存在使得诸多动态语言特性得以实现,例如动态派发(dynamic dispatch)、向上转换(upcasting)、向下转换(downcast)等。本文将对 Rust 中虚表的布局规则进行简要介绍,并在此过程中对 Rust 中若干动态特性的实现方法进行简要介绍。</p>
<blockquote>
<p>注意:Rust 虚表及其结构属于 Rust 语言的内部实现细节,不保证稳定性。本文所介绍的虚表布局仅反映本文创作时最新的 Rust 虚表结构[1],在将来 Rust 虚表结构可能会发生变化。一个 Rust 程序的正确性不应该以任何方式依赖于 Rust 虚表的结构。</p>
</blockquote>
<h2 id="基本结构"><a class="header-anchor" href="#基本结构">¶</a>基本结构</h2>
<p>Rust 程序中的所有虚表均以一个固定结构的 header 开头。Header 中按顺序包含三个usize 大小的字段:drop_in_place ,size 和 align 。在 header 之后是一系列的 usize 大小字段,其数量以及含义在每个虚表中可能都不同。</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">+---------------+</span><br><span class="line">| drop_in_place |</span><br><span class="line">+---------------+</span><br><span class="line">| size |</span><br><span class="line">+---------------+</span><br><span class="line">| align |</span><br><span class="line">+---------------+</span><br><span class="line">| entry1 |</span><br><span class="line">+---------------+</span><br><span class="line">| entry2 |</span><br><span class="line">+---------------+</span><br><span class="line">| entry3 |</span><br><span class="line">+---------------+</span><br></pre></td></tr></table></figure>
<p>虚表 header 中的drop_in_place 是一个函数指针,其指向的函数能够原地 drop 当前胖指针所引用的对象。size 和 align 两个域分别给出对象的大小和内存对齐,这两个域共同构成一个 std::alloc::Layout 结构,可用于释放当前胖指针所引用的对象所占据的内存。虚表 header 的存在使得 trait object 总是能被销毁和释放。例如当销毁一个 <code>Box<dyn Trait></code> 时,<code>Box::<dyn Trait>::drop</code> 会首先调用虚表中的 drop_in_place 函数原地销毁 Box 所引用的对象,然后再调用 dealloc 函数并传递虚表中的 size 和 align 释放堆空间。</p>
<p>在虚表 header 之后是一系列的字段。在最普遍的情况下,每个字段代表一个指向 trait 所定义的函数的指针。例如,对于下列 object safe 的 trait:</p>
<figure class="highlight rust"><table><tr><td class="code"><pre><span class="line"><span class="keyword">pub</span> <span class="keyword">trait</span> <span class="title class_">Trait</span> {</span><br><span class="line"> <span class="keyword">fn</span> <span class="title function_">fun1</span>(&<span class="keyword">self</span>);</span><br><span class="line"> <span class="keyword">fn</span> <span class="title function_">fun2</span>(&<span class="keyword">self</span>);</span><br><span class="line"> <span class="keyword">fn</span> <span class="title function_">fun3</span>(&<span class="keyword">self</span>);</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>如果类型T 实现了 Trait,那么为 T 生成的 Trait 虚表的结构为:</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">+--------------------------+</span><br><span class="line">| fn drop_in_place(*mut T) |</span><br><span class="line">+--------------------------+</span><br><span class="line">| size of T |</span><br><span class="line">+--------------------------+</span><br><span class="line">| align of T |</span><br><span class="line">+--------------------------+</span><br><span class="line">| fn <T as Trait>::fun1 |</span><br><span class="line">+--------------------------+</span><br><span class="line">| fn <T as Trait>::fun2 |</span><br><span class="line">+--------------------------+</span><br><span class="line">| fn <T as Trait>::fun3 |</span><br><span class="line">+--------------------------+</span><br></pre></td></tr></table></figure>
<p>Trait 中的函数按照声明顺序依次排列在虚表 header 之后。当通过一个指向 T 对象的 &dyn Trait 调用 fun2 函数时,程序会先从虚表的第 5 个域中得到为 T 实现的 Trait::fun2 函数的地址,然后再调用之。</p>
<h2 id="Super-Trait"><a class="header-anchor" href="#Super-Trait">¶</a>Super Trait</h2>
<p>Object safe 的 trait 可以有 super trait。例如:</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">pub trait Grand {</span><br><span class="line"> fn grand_fun1(&self);</span><br><span class="line"> fn grand_fun2(&self);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">pub trait Parent : Grand {</span><br><span class="line"> fn parent_fun1(&self);</span><br><span class="line"> fn parent_fun2(&self);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">pub trait Trait : Parent {</span><br><span class="line"> fn fun(&self);</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>如果类型T 实现了 Trait,那么此时为 T 生成的 Trait 虚表的结构为:</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">+-------------------------------+</span><br><span class="line">| fn drop_in_place(*mut T) |</span><br><span class="line">+-------------------------------+</span><br><span class="line">| size of T |</span><br><span class="line">+-------------------------------+</span><br><span class="line">| align of T |</span><br><span class="line">+-------------------------------+</span><br><span class="line">| fn <T as Grand>::grand_fun1 |</span><br><span class="line">+-------------------------------+</span><br><span class="line">| fn <T as Grand>::grand_fun2 |</span><br><span class="line">+-------------------------------+</span><br><span class="line">| fn <T as Parent>::parent_fun1 |</span><br><span class="line">+-------------------------------+</span><br><span class="line">| fn <T as Parent>::parent_fun2 |</span><br><span class="line">+-------------------------------+</span><br><span class="line">| fn <T as Trait>::fun |</span><br><span class="line">+-------------------------------+</span><br></pre></td></tr></table></figure>
<p>可以看到,此时Trait 以及 Trait 的所有直接或间接父 trait 所定义的所有函数均包含在虚表 header 之后,且顺序为后序(即先排布 Trait 的父 trait 所定义的所有函数,最后再排布 Trait 所定义的所有函数)。这样的排布方式使得在得到 T 类型的 Trait 虚表的同时也同时得到了 T 类型的 Parent 虚表和 Grand 虚表。T 类型的 Grand 虚表恰好由 Trait 虚表的前五个域构成,T 类型的 Parent 虚表恰好由 Trait 虚表的前七项构成。这使得向上转换变得非常简单。</p>
<p>所谓向上转换,即 Rust 允许将&dyn Trait 转换为 &dyn Parent 或 &dyn Grand 。在向上转换的过程中,胖指针的对象地址域保持不变,但 metadata 域可能需要进行调整,因为不同的 trait 可能具有不同的虚表地址。但在当前示例中,向上转换不需要调整 metadata 域,因为一个指向 Trait 虚表的指针同时也指向 Parent 虚表和 Grand 虚表。在后文中我们会进一步介绍需要调整 metadata 域的向上转换的情况。</p>
<blockquote>
<p>注意:目前 stable Rust 暂不支持向上转换。要使用向上转换特性,必须使用 nightly 工具链,并向源文件中添加 #![feature(trait_upcasting)] 特性开关。</p>
</blockquote>
<h2 id="多重继承"><a class="header-anchor" href="#多重继承">¶</a>多重继承</h2>
<p>Trait 可以有多个 super trait。例如:</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">pub trait Base {</span><br><span class="line"> fn base_fun1(&self);</span><br><span class="line"> fn base_fun2(&self);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">pub trait Left : Base {</span><br><span class="line"> fn left_fun1(&self);</span><br><span class="line"> fn left_fun2(&self);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">pub trait Right : Base {</span><br><span class="line"> fn right_fun1(&self);</span><br><span class="line"> fn right_fun2(&self);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">pub trait Trait : Left + Right {</span><br><span class="line"> fn fun(&self);</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>如果类型T 实现了 Trait,那么此时为 T 生成的 Trait 虚表的结构为:</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">+-----------------------------+</span><br><span class="line">| fn drop_in_place(*mut T) |</span><br><span class="line">+-----------------------------+</span><br><span class="line">| size of T |</span><br><span class="line">+-----------------------------+</span><br><span class="line">| align of T |</span><br><span class="line">+-----------------------------+</span><br><span class="line">| fn <T as Base>::base_fun1 |</span><br><span class="line">+-----------------------------+</span><br><span class="line">| fn <T as Base>::base_fun2 |</span><br><span class="line">+-----------------------------+</span><br><span class="line">| fn <T as Left>::left_fun1 |</span><br><span class="line">+-----------------------------+</span><br><span class="line">| fn <T as Left>::left_fun2 |</span><br><span class="line">+-----------------------------+</span><br><span class="line">| fn <T as Right>::right_fun1 |</span><br><span class="line">+-----------------------------+</span><br><span class="line">| fn <T as Right>::right_fun2 |</span><br><span class="line">+-----------------------------+</span><br><span class="line">| ptr to <T as Right>::vtable |</span><br><span class="line">+-----------------------------+</span><br><span class="line">| fn <T as Trait>::fun |</span><br><span class="line">+-----------------------------+</span><br></pre></td></tr></table></figure>
<p>可以看到,此时Trait 及其所有直接或间接父 trait 所定义的所有函数仍然包含在虚表内,因此通过 &dyn Trait 调用的函数仍然可以直接从虚表内得到其实际目标函数的地址。另外,Trait 虚表内仍然包含有效的 Base 虚表和 Left 虚表。因此,将 &dyn Trait 向上转换为 &dyn Left 或 &dyn Base 仍然是极其简单的,不需要调整胖指针的 metadata 域。但是,将 &dyn Trait 向上转换为 &dyn Right 就需要调整 metadata 域了,因为 Trait 虚表内并不包含一个有效的 Right 虚表。这也是 Trait 虚表中 ptr to <code><T as Right>::vtable</code> 域的作用:在执行向上转换时,程序会读取 Trait 虚表的这个域作为得到的 &dyn Right 胖指针的 metadata 。这也是 <em>Rust 向上转换与 C++ 向上转换的一个很大不同:在 C++ 中的向上转换通常并不需要访问虚表(除非需要执行跨虚继承边界的转换),但在 Rust 中向上转换可能需要访问虚表。</em></p>
<p>更加一般地,对于一个 object safe 的 traitTr,将其第一个父 trait、第一个父 trait 的第一个父 trait、…… 这一系列直接或间接父 trait 记为这个 trait 的 PrefixTrait 集合。在将 &dyn Tr 向上转换时,如果转换到的目标 trait 包含在 PrefixTrait 集合内,那么这个向上转换是平凡的:不需要调整胖指针的 metadata 域。否则,这个向上转换需要在 Tr 的虚表内读取目标 trait 的虚表指针作为转换结果的 metadata 。在 Tr 的虚表结构中,位于 PrefixTrait 集合中的父 trait 只需要排布他们所定义的函数即可;对于其他父 trait 还需要额外在虚表内排布一个指向其虚表的指针用于向上转换。</p>
<h2 id="向下转换"><a class="header-anchor" href="#向下转换">¶</a>向下转换</h2>
<p>Rust 提供了一个特殊的 trait:std::any::Any 。该 trait 支持向下转换,即可以将 &dyn Any 转换为 T 。转换过程中会对胖指针所指向的对象的实际类型进行检查,确认其确实是一个 T 类型的对象。Any trait 的虚表结构有一些特殊;在虚表 header 之后,Any 虚表仅包含一个域,这个域直接给出胖指针指向的对象的类型标识(由一个 std::any::TypeId 类型的值表示)。例如,对于任意的 T: 'static,编译器为其生成的 Any 虚表为:</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">+--------------------------+</span><br><span class="line">| fn drop_in_place(*mut T) |</span><br><span class="line">+--------------------------+</span><br><span class="line">| size of T |</span><br><span class="line">+--------------------------+</span><br><span class="line">| align of T |</span><br><span class="line">+--------------------------+</span><br><span class="line">| TypeId of T |</span><br><span class="line">+--------------------------+</span><br></pre></td></tr></table></figure>
<p>在执行向下转换时,程序首先检查转换到的类型是否与虚表中给出的TypeId 所标识的类型一致。若类型检查通过,向下转换操作可以直接返回胖指针中的指针域作为转换结果。</p>
<h3 id="REFERENCE"><a class="header-anchor" href="#REFERENCE">¶</a>REFERENCE</h3>
<ol>
<li>Vtable format to support dyn upcasting coercion <a href="https://rust-lang.github.io/dyn-upcasting-coercion-initiative/design-discussions/vtable-layout.*html">https://rust-lang.github.io/dyn-upcasting-coercion-initiative/design-discussions/vtable-layout.*html</a>*</li>
</ol>
]]></content>
<tags>
<tag>Technique</tag>
</tags>
</entry>
<entry>
<title>Rust 闭包 lifetime may not live long enough 问题</title>
<url>/2023/05/24/Rust-%E9%97%AD%E5%8C%85-lifetime-may-not-live-long-enough-%E9%97%AE%E9%A2%98/</url>
<content><![CDATA[<p>代码:</p>
<figure class="highlight rust"><table><tr><td class="code"><pre><span class="line">...</span><br><span class="line"> <span class="keyword">fn</span> <span class="title function_">handlers</span>(<span class="keyword">self</span>) <span class="punctuation">-></span> crate::server::request::Handlers {</span><br><span class="line"> <span class="built_in">vec!</span>[(</span><br><span class="line"> <span class="string">"/tree"</span>,</span><br><span class="line"> routing::<span class="title function_ invoke__">get</span>(<span class="keyword">move</span> || <span class="keyword">async</span> {</span><br><span class="line"> (</span><br><span class="line"> StatusCode::OK,</span><br><span class="line"> <span class="title function_ invoke__">Json</span>(json!(<span class="keyword">self</span>.<span class="title function_ invoke__">clone</span>().<span class="title function_ invoke__">tree</span>(<span class="keyword">self</span>.<span class="title function_ invoke__">clone</span>().root))),</span><br><span class="line"> )</span><br><span class="line"> }),</span><br><span class="line"> )]</span><br><span class="line"> }</span><br><span class="line">...</span><br></pre></td></tr></table></figure>
<p>编译错误:</p>
<figure class="highlight text"><table><tr><td class="code"><pre><span class="line">error: lifetime may not live long enough</span><br><span class="line"> --> src/storage/filesystem/mod.rs:45:34</span><br><span class="line"> |</span><br><span class="line">45 | routing::get(move || async {</span><br><span class="line"> | __________________________-------_^</span><br><span class="line"> | | | |</span><br><span class="line"> | | | return type of closure `{async block@src/storage/filesystem/mod.rs:45:34: 50:14}` contains a lifetime `'2`</span><br><span class="line"> | | lifetime `'1` represents this closure's body</span><br><span class="line">46 | | (</span><br><span class="line">47 | | StatusCode::OK,</span><br><span class="line">48 | | Json(json!(self.clone().tree(self.clone().root))),</span><br><span class="line">49 | | )</span><br><span class="line">50 | | }),</span><br><span class="line"> | |_____________^ returning this value requires that `'1` must outlive `'2`</span><br><span class="line"> |</span><br><span class="line"> = note: closure implements `Fn`, so references to captured variables can't escape the closure</span><br></pre></td></tr></table></figure>
<p>这是因为 <code>handlers</code> 里面的闭包捕获了一个引用,并且尝试返回一个包含该引用的值导致的。</p>
<p>细说就是:闭包内部使用了 <code>self.clone()</code> 来获取一个新的实例,然后在异步块中返回一个 JSON 对象,这个 JSON 对象依赖于 <code>self.tree()</code> 的结果。因为闭包捕获了 <code>self</code> 的引用,所以它必须保证 <code>self</code> 在闭包执行完毕后仍然有效。</p>
<p>解决这个问题的思路是:确保闭包中的所有引用都在闭包执行完毕之前就不再被使用。<br>
就是说,要将闭包的作用域限制在一个更短的生命周期内,或者使用其他方式来避免闭包捕获长期存在的引用:</p>
<figure class="highlight rust"><table><tr><td class="code"><pre><span class="line"></span><br><span class="line">...</span><br><span class="line"> <span class="keyword">fn</span> <span class="title function_">handlers</span>(<span class="keyword">self</span>) <span class="punctuation">-></span> crate::server::request::Handlers {</span><br><span class="line"> <span class="keyword">let</span> <span class="variable">tree</span> = json!(<span class="keyword">self</span>.<span class="title function_ invoke__">clone</span>().<span class="title function_ invoke__">tree</span>(<span class="keyword">self</span>.<span class="title function_ invoke__">clone</span>().root));</span><br><span class="line"> <span class="built_in">vec!</span>[(</span><br><span class="line"> <span class="string">"/tree"</span>,</span><br><span class="line"> routing::<span class="title function_ invoke__">get</span>(<span class="keyword">move</span> || <span class="keyword">async</span> { (StatusCode::OK, <span class="title function_ invoke__">Json</span>(tree)) }),</span><br><span class="line"> )]</span><br><span class="line"> }</span><br><span class="line">...</span><br></pre></td></tr></table></figure>]]></content>
<tags>
<tag>Technique</tag>
</tags>
</entry>
<entry>
<title>Scala 3 (Capture Checking)</title>
<url>/2025/03/25/Scala-3-Capture-Checking/</url>
<content><: <span class="type">T</span> =</span><br><span class="line"> <span class="keyword">val</span> logFile = <span class="type">FileOutputStream</span>(<span class="string">"log"</span>)</span><br><span class="line"> <span class="keyword">val</span> result = op(logFile)</span><br><span class="line"> logFile.close()</span><br><span class="line"> result</span><br></pre></td></tr></table></figure>
<figure class="highlight scala"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">usingLogFile</span></span>[<span class="type">T</span>](op: <span class="type">FileOutputStream</span>^ => <span class="type">T</span>): <span class="type">T</span> =</span><br><span class="line"> <span class="comment">// same body as before</span></span><br></pre></td></tr></table></figure>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">| val later = usingLogFile { f => () => f.write(0) }</span><br><span class="line">| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^</span><br><span class="line">|The expression's type () => Unit is not allowed to capture the root capability `cap`.</span><br><span class="line">|This usually means that a capability persists longer than its allowed lifetime.</span><br></pre></td></tr></table></figure>
<ul>
<li>Effect Polymorphism: Capture Checking 提供了一种更灵活和精确地推理和控制副作用的机制。它可以被视为一种 Effect system,允许类型系统跟踪和控制代码可能产生的副作用。</li>
<li>“函数的颜色”问题: 在异步编程中,区分同步和异步操作一直是一个挑战。Capture Checking 有可能帮助区分和管理同步与异步计算,这通过跟踪与异步操作相关的 Capabilities 来实现。</li>
<li>基于区域的内存分配: Capture Checking 可以通过推理与内存位置相关的Capability,来促进更安全的内存管理。这暗示了 Capture Checking 与内存管理之间可能存在更深层次的集成,从而可能实现更高效和更安全的内存使用。</li>
<li>CE: Scala 3 通过使用 <code>CanThrow</code> 跟踪抛出特定异常的Capability,实现了一个干净且完全安全的 CE 系统。这提供了一种类型安全的替代方案,相较于传统的 CE,这种方式可能更加灵活和有原则。</li>
<li>逃逸分析: Capture Checking 可以防止局部Capability逃逸其预期的作用域。例如,当一个闭包捕获了一个局部Capability,并且这个闭包被赋值给一个全局变量或者以不安全的方式从函数返回时,Capture Checking 可以检测到这种潜在的风险。</li>
</ul>
<hr>
<p>在 Scala 3 中,函数类型 <code>A => B</code> 被认为是不纯的,它可以捕获任意 Capability。实际上,<code>A => B</code> 是 <code>A ->{cap} B</code> 的别名,明确地表明了它可能捕获 “通用 Capability”。这种默认行为反映了 Scala 过去函数可以拥有任意副作用的特点。然而,随着 Capture Checking 的引入,开发者被鼓励更明确地表达函数的纯度。</p>
<p>与不纯函数相对的是纯函数,其类型为 <code>A -> B</code>,表示该函数不能捕获任何 Capability。纯函数是函数式编程中的核心概念,Capture Checking 提供了一种在类型层面强制执行纯性的方法。确保函数的纯性可以使代码更具可预测性和可测试性,因为纯函数的输出完全取决于其输入,并且没有副作用。</p>
<p>开发者还可以指定函数可以捕获的特定 Capability,语法为 <code>A ->{c, d} B</code>,表示该函数可以捕获 Capability <code>c</code> 和 <code>d</code>。这种语法允许对函数可以使用的Capability 进行精确控制,从而提高了资源管理的细粒度。通过显式列出捕获的 Capability,编译器可以验证函数是否遵守这些约束,并防止其意外访问其他资源。</p>
<p>捕获注解 <code>^</code> 的优先级高于 <code>-></code> 。理解运算符的优先级对于正确解释和编写带有捕获注解的函数类型至关重要。不正确的解析可能导致意想不到的行为或类型错误。例如,<code>A ^ C -> B</code> 表示一个从捕获的 <code>A</code> 到 <code>B</code> 的纯函数。</p>
<figure class="highlight scala"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">f</span></span>(x: ->{c} <span class="type">Int</span>): <span class="type">Int</span></span><br></pre></td></tr></table></figure>
<p>Capture Checking 也适用于上下文函数。不纯的上下文函数使用 <code>?=></code>,行为类似于 <code>=></code>,可以捕获任意Capability。纯的上下文函数使用 <code>?-></code>,行为类似于 <code>-></code>,不能捕获任何Capability。这表明,Capture Checking扩展到了上下文函数,允许控制在它们的隐式参数作用域内捕获的 Capability。</p>
<p>值得注意的是,方法本身并不是值,因此它们不直接捕获Capability。相反,它们对 Capability 的引用会被计入封闭对象的捕获集中。这种区分很重要,因为方法的捕获行为与它们所属对象的状态和Capability相关联,这反映了 Scala 的面向对象特性。</p>
<p>与函数类型类似,Capture Checking的概念也延伸到了命名参数类型。<code>=> Int</code> 允许任意Capability引用,类似于不纯函数类型。<code>-> Int</code> 禁止任何Capability引用,类似于纯函数类型。而 <code>->{c} Int</code> 则只允许引用Capability <code>c</code>。这种一致性确保了即使是延迟求值的表达式也遵循Capability约束。</p>
<p>子捕获(<code>C₁ <: C₂</code>)定义了捕获集之间的关系。捕获集 <code>C₁</code> 是 <code>C₂</code> 的子类型,如果 <code>C₂</code> 包含了 <code>C₁</code> 中的每一个元素,并且满足以下条件之一:<code>c ∈ C₂</code>(直接包含);<code>c</code> 是一个类参数,且 <code>C₂</code> 包含 <code>Cls.this</code>(Capability 来源于封闭的类实例);<code>c</code> 的类型具有捕获集 <code>C</code>,且 <code>C <: C₂</code>(基于 Capability 类型的递归子捕获)。子捕获定义了 Capability 依赖的层级结构,这对于类型系统判断一个需要特定 Capability 集合的值是否可以在提供不同 Capability 集合的上下文中使用至关重要。</p>
<p>对于捕获类型的子类型,存在以下规则:纯类型是捕获类型的子类型(<code>T <: C T</code>);较小的捕获集会产生子类型(如果 <code>C₁ <: C₂</code> 且 <code>T₁ <: T₂</code>,则 <code>C₁ T₁ <: C₂ T₂</code>)。这意味着一个依赖较少 Capability 的值通常更通用,可以在更广泛的场景中使用。根 Capability <code>{cap}</code> 覆盖了所有其他捕获集,因此任何特定的捕获集都是 <code>{cap}</code> 的子类型。这允许具有特定捕获要求的类型在允许任何 Capability 的上下文中使用。</p>
<p>Capability widening(也称为 <em>avoidance</em>)是一种简化局部变量类型的机制。局部变量的类型会被 widening 到不提及该变量本身的最小超类型,这个过程通常会涉及到变量的捕获集。这种加宽有助于改善类型推断和代码清晰度,避免局部变量的类型变得过于复杂。</p>
<figure class="highlight scala"><table><tr><td class="code"><pre><span class="line">fs: <span class="type">FileSystem</span>^</span><br><span class="line">ct: <span class="type">CanThrow</span>[<span class="type">Exception</span>]^</span><br><span class="line">l : <span class="type">Logger</span>^{fs}</span><br><span class="line"></span><br><span class="line">{l} <: {fs} <: {cap}</span><br><span class="line">{fs} <: {fs, ct} <: {cap}</span><br><span class="line">{ct} <: {fs, ct} <: {cap}</span><br></pre></td></tr></table></figure>
<p>继承自 <code>caps.Capability</code> 的类具有隐式的 <code>{cap}</code> 捕获集。这表明这些类的实例本质上代表了一种 Capability。Capability 类提供了一种将 Capability 显式定义和管理为类型系统中的一等公民的方式。开发者可以创建具有特定语义和使用模式的自定义 Capability。</p>
<p>在使用 Capability 类的场景中,通常会结合 using clauses 和隐式参数来减少在代码中显式传递 Capability 的需要。这两种方法提供了一种自动将必要的 Capability 提供给函数和方法的方式,从而减少了样板代码并提高了代码的可读性。</p>
<p>闭包会捕获在其主体中引用的来自其周围环境的 Capability。这导致闭包的函数类型中包含捕获集。例如,如果一个闭包引用了一个局部变量 <code>fs</code>,而 <code>fs</code> 是一个 Capability,那么该闭包的类型可能就是 <code>String ->{fs} Unit</code>。这意味着闭包继承了其封闭代码的 Capability 要求,确保它们只能在这些 Capability 可用的上下文中被使用。</p>
<p>此外,闭包还会捕获它们调用的函数的 Capability。如果一个闭包调用了一个需要特定 Capability 的函数,那么该闭包的捕获集也会包含该 Capability。这种传递性的捕获机制确保了所有 Capability 依赖,无论是直接的还是间接的,都会被闭包所跟踪。</p>
<p>类会将其方法中使用的 Capability 保留为(私有)字段。这意味着如果一个方法使用了作为构造函数参数传递的Capability,该类很可能会存储对它的引用。这会导致类的类型中包含捕获集。例如,一个使用文件系统Capability <code>xfs</code> 的 <code>Logger</code> 类可能具有类型 <code>Logger^{xfs}</code>。这表明类封装了它们所依赖的Capability,并将这些依赖作为其类型签名的一部分。</p>
<p><code>@constructorOnly</code> 注解可以用于标记一个仅在构造函数中使用而不会作为字段保留的类参数。这有助于减少类的捕获集。如果一个参数仅用于初始化,后续不再访问,那么类就没有必要将其保留为一种 Capability。</p>
<p>类的捕获引用包括从类外部使用的局部 Capability 以及具有捕获类型的构造函数参数(参数Capability)。局部Capability会被内部类继承。</p>
<p>类实例的 <code>this</code> 的捕获集是根据捕获的引用、父类以及类内部的使用约束来推断的 1。这种自动推断机制在很多情况下减少了手动指定类捕获集的需要。</p>
<figure class="highlight scala"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> caps.<span class="type">Capability</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">FileSystem</span> <span class="keyword">extends</span> <span class="title">Capability</span></span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Logger</span>(<span class="params">using <span class="type">FileSystem</span></span>)</span>:</span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">log</span></span>(s: <span class="type">String</span>): <span class="type">Unit</span> = ???</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">test</span></span>(<span class="keyword">using</span> fs: <span class="type">FileSystem</span>) =</span><br><span class="line"> <span class="keyword">val</span> l: <span class="type">Logger</span>^{fs} = <span class="type">Logger</span>()</span><br><span class="line"> ...</span><br></pre></td></tr></table></figure>
<figure class="highlight scala"><table><tr><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Logger</span>(<span class="params">using <span class="type">FileSystem</span>^{cap}</span>)</span>:</span><br><span class="line"> ^^^^^^^^^^^^^^</span><br><span class="line"> redundant capture: <span class="type">FileSystem</span> already accounts <span class="keyword">for</span> cap</span><br></pre></td></tr></table></figure>
<p>捕获隧道 (Capture Tunnelling) 是指当一个类型变量被一个捕获类型实例化时,捕获信息不会立即传播到外层的泛型类型。相反,捕获会“穿过隧道”,并在类型变量被访问或其成员被使用时重新出现。这种机制有助于以更简洁和可管理的方式处理泛型代码中的捕获集,避免类型签名过于复杂。</p>
<p>逃逸检查施加了一些限制。作为类型变量实例的捕获类型不能携带通用Capability <code>cap</code> 。可变变量也不能拥有通用捕获集 。逃逸检查阻止了在参数化类型的参数中返回或分配带有局部 Capability 的闭包,因为这可能导致 Capability 逃逸其预期的作用域。单调性规则指出,在一个带有字段 <code>f</code> 的类中,<code>{this}</code> 覆盖了 <code>{this.f}</code> 以及 <code>this.f</code> 对纯参数的应用。这意味着如果类实例本身被视为一种 Capability,那么其字段所持有的任何 Capability 也会被隐式地覆盖。逃逸检查对于维护 Capability 跟踪的完整性至关重要,它可以防止 Capability 在其预期生命周期或作用域之外被使用,尤其是在泛型和可变状态的上下文中。</p>
<p>受检异常可以通过导入 <code>language.experimental.saferExceptions</code> 来启用。方法上的 <code>throws</code> 子句会扩展为一个隐式的 <code>CanThrow</code> Capability参数,表明该方法可能抛出指定类型的异常。<code>throw</code> 表达式需要 <code>CanThrow</code> Capability,而 <code>try</code> 表达式会创建这种Capability。在 <code>language.experimental.captureChecking</code> 下,由于逃逸的 Capability 而导致未处理异常的代码会被拒绝。为了实现这种集成,<code>CanThrow</code> 需要继承 <code>Capability</code>,并且需要将逃逸检查扩展到 <code>try</code> 表达式,以防止捕获 <code>cap</code>。Capture Checking 与受检异常的集成确保了异常的可能性也被作为一种 Capability 需求来跟踪,从而加强了语言的整体资源管理和错误处理 Capability。</p>
<hr>
<p>文档中提供了一个关于惰性列表(Lazy Lists)的较大示例,很好地展示了 Capture Checking 如何在更复杂的数据结构中工作。<code>LzyList</code> 的 <code>tail</code> 具有一个捕获注解,表明它可以捕获与列表相同的引用。诸如 <code>map</code>、<code>filter</code> 和 <code>concat</code> 等操作在惰性列表上能够正确地推断出捕获集,这取决于原始列表和所使用的函数。值得注意的是 effect polymorphism的概念,传递给这些操作的纯函数不会出现在结果的捕获集中。这与严格列表形成对比,严格列表通常不需要捕获注解,因为它们的副作用不会被延迟。惰性列表的例子具体说明了Capture Checking 如何应用于非平凡的数据结构,展示了其在涉及延迟求值的复杂场景中跟踪依赖关系的Capability。effect polymorphism 是一个显著的优点,它允许在不影响捕获集的情况下使用纯计算。</p>
<hr>
<p>表 1: 代码示例与演示概念</p>
<table>
<thead>
<tr>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td>代码片段</td>
<td>演示概念</td>
<td>捕获行为解释</td>
</tr>
<tr>
<td><code>import language.experimental.captureChecking</code></td>
<td>启用Capture Checking</td>
<td>必须通过此导入才能使用Capture Checking特性。</td>
</tr>
<tr>
<td><code>T^{c₁, ..., cᵢ}</code></td>
<td>捕获类型的声明</td>
<td>表示类型 <code>T</code> 的值可能依赖或访问Capability <code>c₁, ..., cᵢ</code>。</td>
</tr>
<tr>
<td><code>A => B</code></td>
<td>不纯函数类型</td>
<td>函数可以捕获任意Capability,是 <code>A ->{cap} B</code> 的别名。</td>
</tr>
<tr>
<td><code>A -> B</code></td>
<td>纯函数类型</td>
<td>函数不能捕获任何Capability。</td>
</tr>
<tr>
<td><code>A ->{c, d} B</code></td>
<td>指定捕获Capability的函数类型</td>
<td>函数只能捕获Capability <code>c</code> 和 <code>d</code>。</td>
</tr>
<tr>
<td><code>=> Int</code></td>
<td>允许任意Capability引用的命名参数类型</td>
<td>类似于不纯函数类型。</td>
</tr>
<tr>
<td><code>-> Int</code></td>
<td>禁止任何Capability引用的命名参数类型</td>
<td>类似于纯函数类型。</td>
</tr>
<tr>
<td><code>->{c} Int</code></td>
<td>只允许特定Capability引用的命名参数类型</td>
<td>只允许引用Capability <code>c</code>。</td>
</tr>
<tr>
<td><code>class C extends caps.Capability</code></td>
<td>Capability类的定义</td>
<td>类 <code>C</code> 的实例本身代表一种Capability,具有隐式的 <code>{cap}</code> 捕获集。</td>
</tr>
<tr>
<td><code>@constructorOnly val p: Int</code></td>
<td>构造函数专用参数</td>
<td>参数 <code>p</code> 仅在构造函数中使用,不会作为类的字段保留,有助于减少类的捕获集。</td>
</tr>
<tr>
<td><code>() -> Iterator^</code></td>
<td>具有存在性Capability的函数结果类型</td>
<td>返回一个迭代器,该迭代器可能捕获某种Capability,但具体是哪种Capability在编译时未知。</td>
</tr>
<tr>
<td><code>ops*</code></td>
<td>可达Capability</td>
<td>表示通过Capability <code>ops</code> 可访问的任何协变出现的Capability。</td>
</tr>
<tr>
<td><code>type Source[X^] = ...</code></td>
<td>使用Capability多态的类型定义</td>
<td>类型 <code>Source</code> 被参数化为可以持有Capability集 <code>X^</code>。</td>
</tr>
</tbody>
</table>
<p>当 <code>cap</code> 出现在函数的结果类型中时,通常表示一个由存在性量词绑定的未知类型(例如,<code>() -> Iterator^</code> 意味着 <code>() -> Exists x. Iterator^x</code>)。这表明返回的迭代器可能捕获了某种 Capability,但具体的哪种 Capability 在静态类型检查时是未知的。在内部,这种存在性 Capability 使用带有 sealed trait <code>Exists</code> 的依赖函数类型来表示 。结果类型中协变的 <code>cap</code> 会被替换为一个新的 existential variable。当应用一个具有 existential result 类型 <code>Exists ex.T</code> 的函数时,结果是 <code>T</code>,其中 <code>ex</code> 被 <code>cap</code> 替换。Existential Capability 允许类型系统表达在编译时具体捕获的 Capability 未知的情况,从而提供了灵活性,同时仍然保持了一定程度的跟踪。</p>
<p>Reach Capability 用于表达一个变量引用了通过另一个Capability“Reach”的任何操作。例如,如果 <code>ops</code> 是一个表示一组操作的Capability,那么 <code>ops*</code> 就表示出现在 <code>ops</code> 类型中且通过 <code>ops</code> 访问的任何协变 Capability。Reach Capability 提供了一种间接推理和跟踪 Capability 的方式,这对于建模具有相互连接资源的复杂系统非常有用。</p>
<p>Capability 多态允许使用带有上界 <code>CapSet</code> 的类型变量来参数化操作的捕获集。这使得定义诸如 <code>Source[X^]</code> 这样的类型成为可能,其中 <code>X^</code> 表示监听器可以持有的一组 Capability。Capability 多态增强了代码的表达性和可重用性,因为它允许函数和数据结构在它们可能依赖的 Capability 集上进行参数化。</p>
<hr>
<p>Scala 3 提供了几个与 Capture Checking 相关的编译选项,。<code>-Xprint:cc</code> 选项会打印出带有推断捕获类型的程序代码 1。这对于理解编译器是如何推断捕获集的以及调试与类型相关的错误非常有用。另一个选项是 <code>-Ycc-debug</code>,它提供了关于 Capture Checking 过程的详细的、面向实现的的信息。这个选项对于需要深入了解 Capture Checking 器工作原理或者遇到复杂问题的开发者来说很有帮助。这些编译选项是使用 Capture Checking 的开发者的重要工具,它们允许开发者检查编译器的推理并诊断问题。</p>
<p>Capture Checking被实现为一个传播约束求解器,它在标准的类型检查阶段之后运行,未知的捕获集由约束变量表示。在类型中显式编写的捕获集被求解器视为常量。类型之间的子类型要求会转化为它们各自捕获集上的子捕获测试。求解器根据程序的结构和类型约束,将 Capability 传播给约束变量及其超集。捕获集的映射受到类型参数的变性(协变、逆变、不变)的影响。装箱(boxing)和拆箱(unboxing)是用于隐藏和恢复捕获集的虚拟操作,特别是在类型参数的上下文中用于管理捕获隧道。<code>-Ycc-debug</code> 的输出提供了关于变量依赖关系和Capture Checking器在编译过程中的状态的深入信息。</p>
]]></content>
<tags>
<tag>Technique</tag>
</tags>
</entry>
<entry>
<title>TDD 和 DDD 的一些小想法</title>
<url>/2025/03/20/TDD-%E5%92%8C-DDD-%E7%9A%84%E4%B8%80%E4%BA%9B%E5%B0%8F%E6%83%B3%E6%B3%95/</url>
<content><![CDATA[<p>Test-Driven Development (TDD) 和 Domain-Driven Design (DDD) 是两种不同的软件开发方法论,各自有其独特的优缺点和应用场景。</p>
<h3 id="Test-Driven-Development-TDD"><a class="header-anchor" href="#Test-Driven-Development-TDD">¶</a>Test-Driven Development (TDD)</h3>
<h4 id="优点:"><a class="header-anchor" href="#优点:">¶</a>优点:</h4>
<ol>
<li>提高代码质量:通过编写测试来驱动开发,可以确保代码的正确性和可靠性。</li>
<li>减少缺陷:早期发现和修复缺陷,减少后期的调试和维护成本。</li>
<li>促进可维护性:代码更加模块化和可测试,便于后续的维护和扩展。</li>
<li>文档化:测试代码本身就是一种文档,描述了系统的行为和期望。</li>
<li>提高开发速度:虽然初期可能会慢一些,但长期来看,由于减少了重构和调试的时间,开发速度会提高。</li>
</ol>
<h4 id="缺点:"><a class="header-anchor" href="#缺点:">¶</a>缺点:</h4>
<ol>
<li>初期投入大:需要在开发前编写测试,初期投入的时间和精力较大。</li>
<li>学习曲线陡峭:对新手开发者来说,学习和掌握TDD需要一定的时间。</li>
<li>过度依赖测试:可能导致过度依赖单元测试,忽略了系统的整体性能和用户体验。</li>
</ol>
<h3 id="Domain-Driven-Design-DDD"><a class="header-anchor" href="#Domain-Driven-Design-DDD">¶</a>Domain-Driven Design (DDD)</h3>
<h4 id="优点:-v2"><a class="header-anchor" href="#优点:-v2">¶</a>优点:</h4>
<ol>
<li>聚焦领域:通过深入理解业务领域,确保软件系统与业务需求紧密结合。</li>
<li>模型驱动:建立一个清晰的领域模型,帮助开发者和业务专家共同理解系统。</li>
<li>可维护性:通过明确的领域模型和边界,系统结构更加清晰,便于维护和扩展。</li>
<li>沟通桥梁:提供了一种通用的语言(Ubiquitous Language),促进开发团队和业务团队之间的沟通。</li>
</ol>
<h4 id="缺点:-v2"><a class="header-anchor" href="#缺点:-v2">¶</a>缺点:</h4>
<ol>
<li>复杂性高:DDD方法论较为复杂,需要深入理解领域模型和设计模式。</li>
<li>初期投入大:需要大量的时间和精力进行领域分析和建模。</li>
<li>适用范围有限:对于简单的系统或小型项目,DDD可能显得过于复杂和冗余。</li>
</ol>
<hr>
<p>TDD 和 DDD 并不冲突,实际上它们可以互补。TDD 可以帮助确保代码的正确性和可靠性,而 DDD 可以确保系统与业务需求紧密结合。在进行 TDD 时,可以使用 DDD 的领域模型和 Ubiquitous Language 来编写测试用例,确保测试覆盖了业务需求。在进行 DDD 时,可以使用 TDD 来驱动实现,确保每个领域模型的实现。</p>
]]></content>
<tags>
<tag>Technique</tag>
</tags>
</entry>
<entry>
<title>Turborepo 简述</title>
<url>/2024/09/27/Turborepo-%E7%AE%80%E8%BF%B0/</url>
<content><![CDATA[<h2 id="Turborepo-简介"><a class="header-anchor" href="#Turborepo-简介">¶</a>Turborepo 简介</h2>
<p>Monorepos 有很多优势,但它们难以扩展。每个工作区都有自己的测试套件、自己的 linting 和构建过程。单个 monorepo 可能有数千个任务要执行。</p>
<p>Turborepo 是一个专为 JavaScript 和 TypeScript 代码库设计的构建系统,旨在优化 monorepos 和 single-package workspace 中的任务。它通过远程缓存(remote caching)和高效的任务调度(task scheduling)来解决 monorepos 中的扩展问题。Turborepo 也可以增量部署(adopted incrementally),并与各种包管理器配合使用。</p>
<ul>
<li>远程缓存: 存储所有任务的结果,CI 就不需要重复执行相同的工作了。</li>
<li>任务调度:利用所有核心的性能并行处理任务,尽可能的加速。</li>
</ul>
<p><code>turbo</code> 基于 workspace 构建,workspaces 是 JavaScript 生态系统中包管理器的一项功能,允许将多个包分组到一个存储库中:</p>
<h3 id="Workspace"><a class="header-anchor" href="#Workspace">¶</a>Workspace</h3>
<p>在 JavaScript 中,Workspace 是指仓库中的特定实体,可以是<a href="https://vercel.com/docs/vercel-platform/glossary#single-package-workspace">单个包</a>或<a href="https://vercel.com/docs/vercel-platform/glossary#multi-package-workspace">包的集合</a>。<br>
包管理器的 root lock 文件(例如 <code>pnpm-lock.yaml</code>)以及任何其他配置都位于 Wrokspace 的根目录。在 Monorepo 中可以有多个工作区,每个工作区位于存储库的子目录中。</p>
<h3 id="Single-package-workspace"><a class="header-anchor" href="#Single-package-workspace">¶</a><a href="https://vercel.com/docs/vercel-platform/glossary#single-package-workspace">Single-package workspace</a></h3>
<p>只有一个独立包的工作区,在工作区根目录下有一个 <code>package.json</code> 文件。</p>
<h3 id="Multi-package-workspace"><a class="header-anchor" href="#Multi-package-workspace">¶</a><a href="https://vercel.com/docs/vercel-platform/glossary#multi-package-workspace">Multi-package workspace</a></h3>
<p>包含多个包的工作区,包含多个 <code>package.json</code> 文件,其中一个位于工作区根目录中用于全局配置,其他位于每个包目录中。</p>
<blockquote>
<p>这种类型的工作区通常称为 monorepo</p>
</blockquote>
<hr>
<p>以 npm 为例,turbo 会初始化一个这样的目录结构使其成为有效的 workspace:</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">|- package.json</span><br><span class="line">|- package-lock.json</span><br><span class="line">|- turbo.json</span><br><span class="line">|- apps</span><br><span class="line">|-- docs</span><br><span class="line">|--- package.json</span><br><span class="line">|-- web</span><br><span class="line">|--- package.json</span><br><span class="line">|- packages</span><br><span class="line">|--- ui</span><br></pre></td></tr></table></figure>
<p>一个 “有效的” turbo 项目至少要有:</p>
<ul>
<li>包管理器描述的包</li>
<li>包管理器的 lock 文件</li>
<li>根目录下的 <code>package.json</code></li>
<li>根目录下的 <code>turbo.json</code></li>
<li>每个包中的 <code>package.json</code></li>
</ul>
<p>例如,在根目录的 <code>package.json</code> 中配置:</p>
<figure class="highlight json"><table><tr><td class="code"><pre><span class="line"><span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"workspaces"</span><span class="punctuation">:</span> <span class="punctuation">[</span></span><br><span class="line"> <span class="string">"apps/*"</span><span class="punctuation">,</span></span><br><span class="line"> <span class="string">"packages/*"</span></span><br><span class="line"> <span class="punctuation">]</span></span><br><span class="line"><span class="punctuation">}</span></span><br></pre></td></tr></table></figure>
<p>那么 <code>apps</code> 或 <code>packages</code> 目录中有 <code>package.json</code> 的每个目录都将被视为一个包。</p>
<blockquote>
<p>注意:Turborepo 不支持嵌套包,例如 <code>apps/</code> 或 <code>packages/</code> 这种,将一个包放在<code>apps/a</code> 并将另一个包放在 <code>apps/a/b</code> 的结构将导致错误。</p>
<p>如果想按目录对包进行分组,可以使用 <code>packages/*</code> 和 <code>packages/group/*</code> 等 glob 来完成此操作,而不是创建 <code>packages/group/package.json</code> 文件。</p>
</blockquote>
<p>根目录的 <code>package.json</code> 是 workspace 的基础,常见的配置:</p>
<figure class="highlight json"><table><tr><td class="code"><pre><span class="line"><span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"private"</span><span class="punctuation">:</span> <span class="literal"><span class="keyword">true</span></span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">"scripts"</span><span class="punctuation">:</span> <span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"build"</span><span class="punctuation">:</span> <span class="string">"turbo run build"</span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">"dev"</span><span class="punctuation">:</span> <span class="string">"turbo run dev"</span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">"lint"</span><span class="punctuation">:</span> <span class="string">"turbo run lint"</span></span><br><span class="line"> <span class="punctuation">}</span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">"devDependencies"</span><span class="punctuation">:</span> <span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"turbo"</span><span class="punctuation">:</span> <span class="string">"latest"</span></span><br><span class="line"> <span class="punctuation">}</span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">"packageManager"</span><span class="punctuation">:</span> <span class="string">"[email protected]"</span></span><br><span class="line"><span class="punctuation">}</span></span><br></pre></td></tr></table></figure>
<p>而根目录的 <code>turbo.json</code> 用于配置 <code>turbo</code> 的行为。那些 lock 文件是包管理器和 <code>turbo</code> 用于 reproducible 的关键。此外,Turborepo 还利用它们分析工作区中<a href="https://turbo.build/repo/docs/core-concepts/internal-packages">内部包</a>之间的依赖关系。</p>
<hr>
<h3 id="包中的-package-json"><a class="header-anchor" href="#包中的-package-json">¶</a>包中的 <code>package.json</code></h3>
<p><a href="https://nodejs.org/api/packages.html#name"><code>name</code> 字段</a>用于标识包。它在 workspace 中应该是唯一的。</p>
<blockquote>
<p>最佳做法是为<a href="https://turbo.build/repo/docs/core-concepts/internal-packages">内部包</a>使用命名空间前缀,以避免与 npm 注册表上的其他包发生冲突。例如,如果组织名为 <code>clin</code>,则可以将包命名为 <code>@clin/package-name</code>。</p>
</blockquote>
<p><code>scripts</code> 字段用于定义可在包的上下文中运行的脚本。Turborepo 将使用这些脚本的名称来确定要在包中运行的脚本(如果有)。</p>
<p><a href="https://nodejs.org/api/packages.html#exports"><code>exports</code> 字段</a>用于指定要使用该包的其他包的入口点。如果要在另一个包中使用一个包中的代码,将从该入口点导入。</p>
<p>例如,如果有一个 <code>@repo/math</code> 包,则可以这么写 <code>exports</code> 字段:</p>
<figure class="highlight json"><table><tr><td class="code"><pre><span class="line"><span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"exports"</span><span class="punctuation">:</span> <span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"."</span><span class="punctuation">:</span> <span class="string">"./dist/constants.ts"</span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">"./add"</span><span class="punctuation">:</span> <span class="string">"./dist/add.ts"</span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">"./subtract"</span><span class="punctuation">:</span> <span class="string">"./dist/subtract.ts"</span></span><br><span class="line"> <span class="punctuation">}</span></span><br><span class="line"><span class="punctuation">}</span></span><br></pre></td></tr></table></figure>
<p>然后就可以从 <code>@repo/math</code> 包中导入 <code>add</code> 和 <code>subtract</code> 函数了:</p>
<figure class="highlight typescript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> { <span class="variable constant_">GRAVITATIONAL_CONSTANT</span>, <span class="variable constant_">SPEED_OF_LIGHT</span> } <span class="keyword">from</span> <span class="string">'@repo/math'</span>;</span><br><span class="line"><span class="keyword">import</span> { add } <span class="keyword">from</span> <span class="string">'@repo/math/add'</span>;</span><br><span class="line"><span class="keyword">import</span> { subtract } <span class="keyword">from</span> <span class="string">'@repo/math/subtract'</span>;</span><br></pre></td></tr></table></figure>
<p>以这种方式使用导出有三个主要好处:</p>
<ul>
<li>避免 barrel 文件:barrel 文件是重新导出同一包中其他文件的文件,从而为整个包创建一个入口点。虽然它们可能看起来很方便,但编译器<a href="https://vercel.com/blog/how-we-optimized-package-imports-in-next-js#what's-the-problem-with-barrel-files">和捆绑程序很难处理</a>它们,并且可能很快导致性能问题。</li>
<li>更强大的功能:与<a href="https://nodejs.org/api/packages.html#main"><code>主</code>字段</a>(如 <a href="https://nodejs.org/api/packages.html#conditional-exports">Conditional Exports</a>)相比,<code>exports</code> 还具有其他强大的功能。一般来说,尽可能使用 <code>exports</code> 而不是 <code>main</code> 就行了。</li>
<li>IDE 友好:通过使用 <code>export</code> 指定包的入口点,代码编辑器可以为包的导出提供自动完成。</li>
</ul>
<hr>
<p>除此之外还有 <code>import</code> 字段,也就是一种创建包中其他模块的子路径的方法。可以简单地视为 “快捷方式” ,用于编写更简单的导入路径,这些路径对日后文件被移动后的重构更具弹性。</p>
<blockquote>
<p>其他:包通常使用 <code>src</code> 目录来存储其源代码并编译到 <code>dist</code> 目录(也应位于包中)。</p>
</blockquote>
<h2 id="管理依赖项"><a class="header-anchor" href="#管理依赖项">¶</a>管理依赖项</h2>
<figure class="highlight json"><table><tr><td class="code"><pre><span class="line"><span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"dependencies"</span><span class="punctuation">:</span> <span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"next"</span><span class="punctuation">:</span> <span class="string">"latest"</span><span class="punctuation">,</span> <span class="comment">// 外部依赖</span></span><br><span class="line"> <span class="attr">"@repo/ui"</span><span class="punctuation">:</span> <span class="string">"*"</span> <span class="comment">// 内部依赖</span></span><br><span class="line"> <span class="punctuation">}</span></span><br><span class="line"><span class="punctuation">}</span></span><br></pre></td></tr></table></figure>
<p>在存储库中安装依赖项时,应将其直接安装在使用它的软件包中。包的 <code>package.json</code> 将包含所需的每个依赖项。外部和内部依赖项都是如此。</p>
<p>要在多个包中快速安装依赖项,可以:</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">npm install jest --workspace=web --workspace=@repo/ui --save-dev</span><br></pre></td></tr></table></figure>
<p>这种做法有几个好处:</p>
<ul>
<li>更清晰:当软件包的依赖项列在其 <code>package.json</code> 中时,更容易理解软件包所依赖的内容。在存储库中工作的开发人员可以一目了然地看到包中使用了哪些依赖项。</li>
<li>更具灵活性:在大规模的 monorepo 中,想让每个包都使用相同版本的外部依赖项可能是不现实的。当有许多团队在同一个代码库中工作时,优先级、时间表和需求会有所不同。通过在 “使用它们的包” 中安装依赖项,可以让 <code>UI</code> 团队能够升级到最新版本的 TypeScript,而 <code>Web</code> 团队可以优先发布新功能并在以后使用 TypeScript。</li>
<li>更好的缓存能力:如果在存储库的根目录中安装了太多依赖项,则每当添加、更新或删除依赖项时,都会更改工作区根目录,从而导致不必要的缓存未命中。</li>
<li>修剪未使用的依赖项:对于 Docker 用户,<a href="https://turbo.build/repo/docs/reference/prune">Turborepo 的修剪功能</a>可以从 Docker 镜像中删除未使用的依赖项,以创建更轻量级的镜像。当依赖项安装在它们所适用的包中时,Turborepo 可以读取锁文件并删除所需的包中未使用的依赖项。</li>
</ul>
<blockquote>
<p>属于工作区根目录的唯一依赖项是用于管理存储库的工具,而用于构建应用程序和库的依赖项安装在各自的包中。一些适合安装在根中的依赖项示例包括 <a href="https://www.npmjs.com/package/turbo"><code>turbo</code></a>、<a href="https://www.npmjs.com/package/husky"><code>husky</code></a> 或 <a href="https://www.npmjs.com/package/lint-staged"><code>lint-staged</code></a>。</p>
</blockquote>
<h3 id="保持同一版本的依赖"><a class="header-anchor" href="#保持同一版本的依赖">¶</a>保持同一版本的依赖</h3>
<p>一些 monorepo 维护者更喜欢按照规则在所有软件包中保持对相同版本的依赖关系。有几种方法可以实现此目的:</p>
<ul>
<li>使用专用的工具,比如 <a href="https://www.npmjs.com/package/syncpack"><code>syncpack</code></a>、<a href="https://www.npmjs.com/package/@manypkg/cli"><code>manypkg</code></a> 和 <a href="https://www.npmjs.com/package/sherif"><code>sherif</code></a> 等工具可用于此特定目的。</li>
<li>或者单纯的使用包管理器,可以使用软件包管理器通过一个命令更新依赖项版本:
<ul>
<li><code>npm install typescript@latest --workspaces</code></li>
</ul>
</li>
<li>或者最粗暴的用编辑器一次查找并替换存储库中所有 <code>package.json</code> 文件的依赖项版本。用 <code>“next”: “.*”</code> 之类的正则表达式来查找并替换为所需的版本。完成后再运行包管理器的 install 命令来更新 lock 文件.</li>
</ul>
<h2 id="内部包"><a class="header-anchor" href="#内部包">¶</a>内部包</h2>
<p><a href="https://turbo.build/repo/docs/core-concepts/internal-packages">内部包</a>是工作区的构建块(building blocks),是一种在存储库中共享代码的强大方式。Turborepo 读取 <code>package.json</code> 中的依赖项来分析内部包之间的关系,并在后台创建 <a href="https://turbo.build/repo/docs/core-concepts/package-and-task-graph#package-graph">Package Graph</a> 以优化存储库的工作流程。</p>
<p>在创建内部包时,建议创建具有单一 “用途” 的包。这是最佳实践,具体取决于存储库的规模、组织、团队需求等。此策略具有以下几个优点:</p>
<ul>
<li>更易于理解:随着存储库的扩展,在存储库中工作的开发人员将能够更轻松地找到他们需要的代码。</li>
<li>减少每个包的依赖项:每个包使用更少的依赖项,以便 Turborepo 可以更有效地<a href="https://turbo.build/repo/docs/reference/prune">修剪包图的依赖项</a>。</li>
</ul>
<p>在创建<a href="https://turbo.build/repo/docs/core-concepts/package-types#application-packages">应用程序包</a>时,最好避免将共享代码放在这些包中。相反,应该为共享代码创建一个单独的包,并让应用程序包依赖于该包。<br>
此外,应用程序包不应安装到其他包中。相反,应将它们视为 <a href="https://turbo.build/repo/docs/core-concepts/package-and-task-graph#package-graph">Package Graph</a> 的入口点。</p>
<h2 id="配置任务"><a class="header-anchor" href="#配置任务">¶</a>配置任务</h2>
<p>Turborepo 将始终按照 <a href="https://turbo.build/repo/docs/reference/configuration"><code>turbo.json</code> 配置</a>和 <a href="https://turbo.build/repo/docs/core-concepts/package-and-task-graph#package-graph">Package Graph</a> 中描述的顺序运行任务,并尽可能并行化工作以确保一切尽可能快地运行。</p>
<p>根目录的 <code>turbo.json</code> 文件是注册 Turborepo 将运行的任务的位置。定义任务后,将能够使用 <a href="https://turbo.build/repo/docs/reference/run"><code>turbo run</code></a> 运行一个或多个任务。</p>
<p><code>tasks</code> 对象中的每个 key 都是一个可以通过 <code>turbo run</code> 执行的任务。Turborepo 将在 <code>package.json</code> 中搜索与任务同名的软件包:</p>
<p><a href="https://turbo.build/repo/docs/reference/configuration#dependson"><code>dependsOn</code> 键</a>用于指定在其他任务开始运行之前必须完成的任务。在大多数情况下,库的<code>build</code>脚本在应用程序的<code>build</code>脚本运行之前完成,所以可以这么写:</p>
<figure class="highlight json"><table><tr><td class="code"><pre><span class="line"><span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"tasks"</span><span class="punctuation">:</span> <span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"build"</span><span class="punctuation">:</span> <span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"dependsOn"</span><span class="punctuation">:</span> <span class="punctuation">[</span><span class="string">"^build"</span><span class="punctuation">]</span> </span><br><span class="line"> <span class="punctuation">}</span></span><br><span class="line"> <span class="punctuation">}</span></span><br><span class="line"><span class="punctuation">}</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p><code>^</code> 这个语法告诉 Turborepo 从依赖关系图的底部开始运行任务。如果应用程序依赖于名为 <code>ui</code> 的库,并且该库具有<code>build</code>任务,则 <code>ui</code> 中的<code>build</code>脚本将首先运行。成功完成后,才会运行应用程序中的<code>build</code>任务。<br>
这是一个重要的形式,因为它可以确保应用程序的<code>build</code>任务具有编译所需的所有必要依赖项。当依赖关系图发展到具有多个级别的任务依赖关系的更复杂的结构时,此概念也适用。</p>
</blockquote>
<p>有时可能需要确保同一包中的两个任务按特定顺序运行。例如需要先在库中运行<code>build</code>任务,然后再在同一库中运行<code>test</code>任务。这种情况删掉 <code>^</code> 就行了:</p>
<figure class="highlight json"><table><tr><td class="code"><pre><span class="line"><span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"tasks"</span><span class="punctuation">:</span> <span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"test"</span><span class="punctuation">:</span> <span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"dependsOn"</span><span class="punctuation">:</span> <span class="punctuation">[</span><span class="string">"build"</span><span class="punctuation">]</span> </span><br><span class="line"> <span class="punctuation">}</span></span><br><span class="line"> <span class="punctuation">}</span></span><br><span class="line"><span class="punctuation">}</span></span><br></pre></td></tr></table></figure>
<p>还可以在特定包中指定要依赖的单个任务。例如在任何 <code>lint</code> 任务之前运行 <code>utils</code> 中的<code>build</code>任务:</p>
<figure class="highlight json"><table><tr><td class="code"><pre><span class="line"><span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"tasks"</span><span class="punctuation">:</span> <span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"lint"</span><span class="punctuation">:</span> <span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"dependsOn"</span><span class="punctuation">:</span> <span class="punctuation">[</span><span class="string">"utils#build"</span><span class="punctuation">]</span> </span><br><span class="line"> <span class="punctuation">}</span></span><br><span class="line"> <span class="punctuation">}</span></span><br><span class="line"><span class="punctuation">}</span></span><br></pre></td></tr></table></figure>
<p>或者更加细致的限定 <code>lint</code>:</p>
<figure class="highlight json"><table><tr><td class="code"><pre><span class="line"><span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"tasks"</span><span class="punctuation">:</span> <span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"web#lint"</span><span class="punctuation">:</span> <span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"dependsOn"</span><span class="punctuation">:</span> <span class="punctuation">[</span><span class="string">"utils#build"</span><span class="punctuation">]</span> </span><br><span class="line"> <span class="punctuation">}</span></span><br><span class="line"> <span class="punctuation">}</span></span><br><span class="line"><span class="punctuation">}</span></span><br></pre></td></tr></table></figure>
<p>即 <code>Web 包中的</code> <code>lint</code> 任务只能在 <code>utils</code> 包中的<code>build</code>任务完成后运行。</p>
<p>某些任务可能没有任何依赖项。例如用于在 Markdown 文件中查找拼写错误的任务可能不需要关心其他任务的状态。在这种情况下,省略 <code>dependsOn</code> 键或给个空数组就行了:</p>
<figure class="highlight json"><table><tr><td class="code"><pre><span class="line"><span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"tasks"</span><span class="punctuation">:</span> <span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"spell-check"</span><span class="punctuation">:</span> <span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"dependsOn"</span><span class="punctuation">:</span> <span class="punctuation">[</span><span class="punctuation">]</span> </span><br><span class="line"> <span class="punctuation">}</span></span><br><span class="line"> <span class="punctuation">}</span></span><br><span class="line"><span class="punctuation">}</span></span><br></pre></td></tr></table></figure>
<h3 id="指定输入输出"><a class="header-anchor" href="#指定输入输出">¶</a>指定输入输出</h3>
<p><code>outputs</code> 键告诉 Turborepo 文件和目录在任务成功完成时应该缓存在哪。如果未定义此 key,Turborepo 将不会缓存任何文件。</p>
<p>例如缓存 vite 的输出一般可以这么写:</p>
<figure class="highlight json"><table><tr><td class="code"><pre><span class="line"><span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"tasks"</span><span class="punctuation">:</span> <span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"build"</span><span class="punctuation">:</span> <span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"outputs"</span><span class="punctuation">:</span> <span class="punctuation">[</span><span class="string">"dist/"</span><span class="punctuation">]</span> </span><br><span class="line"> <span class="punctuation">}</span></span><br><span class="line"> <span class="punctuation">}</span></span><br><span class="line"><span class="punctuation">}</span></span><br></pre></td></tr></table></figure>
<p><code>inputs</code> 键用于指定要包含在任务哈希中以进行<a href="https://turbo.build/repo/docs/crafting-your-repository/caching">缓存</a>的文件。默认情况下,Turborepo 将包含包中由 Git 跟踪的所有文件。但是也可以使用 <code>inputs</code> 键更具体地说明哈希中包含哪些文件, 例如,在 Markdown 文件中查找拼写错误的任务可以定义如下::</p>
<figure class="highlight json"><table><tr><td class="code"><pre><span class="line"><span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"tasks"</span><span class="punctuation">:</span> <span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"spell-check"</span><span class="punctuation">:</span> <span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"inputs"</span><span class="punctuation">:</span> <span class="punctuation">[</span><span class="string">"/*.md"</span><span class="punctuation">,</span> <span class="string">"/*.mdx"</span><span class="punctuation">]</span> </span><br><span class="line"> <span class="punctuation">}</span></span><br><span class="line"> <span class="punctuation">}</span></span><br><span class="line"><span class="punctuation">}</span></span><br></pre></td></tr></table></figure>
<p>可以通过微调 <code>input</code> 以忽略对已知不会影响任务输出的文件的更改来提高某些任务的缓存命中率, 可以使用 <code>$TURBO_DEFAULT$</code> 微语法来微调默认 <code>input</code> 行为:</p>
<figure class="highlight json"><table><tr><td class="code"><pre><span class="line"><span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"tasks"</span><span class="punctuation">:</span> <span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"build"</span><span class="punctuation">:</span> <span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"inputs"</span><span class="punctuation">:</span> <span class="punctuation">[</span><span class="string">"$TURBO_DEFAULT$"</span><span class="punctuation">,</span> <span class="string">"!README.md"</span><span class="punctuation">]</span> </span><br><span class="line"> <span class="punctuation">}</span></span><br><span class="line"> <span class="punctuation">}</span></span><br><span class="line"><span class="punctuation">}</span></span><br></pre></td></tr></table></figure>
<p>这里 Turborepo 使用<code>build</code>任务的默认<code>input</code>,但会忽略对 <code>README.md</code> 文件的更改。如果 <code>README.md</code> 文件发生更改,任务仍将用上缓存。</p>
<h3 id="Root-任务"><a class="header-anchor" href="#Root-任务">¶</a>Root 任务</h3>
<p>还可以使用 <code>turbo</code> 在 Workspace 根的 <code>package.json</code>中运行脚本。例如,除了每个软件包中的 <code>lint</code> 任务外,可能还需要对 Workspace 根目录中的文件运行 <code>lint:root</code> 任务:</p>
<figure class="highlight json"><table><tr><td class="code"><pre><span class="line"><span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"tasks"</span><span class="punctuation">:</span> <span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"lint"</span><span class="punctuation">:</span> <span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"dependsOn"</span><span class="punctuation">:</span> <span class="punctuation">[</span><span class="string">"^lint"</span><span class="punctuation">]</span></span><br><span class="line"> <span class="punctuation">}</span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">"//#lint:root"</span><span class="punctuation">:</span> <span class="punctuation">{</span><span class="punctuation">}</span> </span><br><span class="line"> <span class="punctuation">}</span></span><br><span class="line"><span class="punctuation">}</span></span><br></pre></td></tr></table></figure>
<h3 id="其他"><a class="header-anchor" href="#其他">¶</a>其他</h3>
<ul>
<li><a href="https://turbo.build/repo/docs/reference/package-configurations">包配置</a>是直接放入包中的<code>turbo.json</code>文件。这允许软件包为其自己的任务定义特定行为,而不会影响存储库的其余部分。</li>
<li>有一些始终需要运行的任务,例如缓存生成后的部署脚本。对于这些任务,用 <code>“cache”: false</code> :</li>
</ul>
<figure class="highlight json"><table><tr><td class="code"><pre><span class="line"><span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"tasks"</span><span class="punctuation">:</span> <span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"deploy"</span><span class="punctuation">:</span> <span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"dependsOn"</span><span class="punctuation">:</span> <span class="punctuation">[</span><span class="string">"^build"</span><span class="punctuation">]</span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">"cache"</span><span class="punctuation">:</span> <span class="literal"><span class="keyword">false</span></span></span><br><span class="line"> <span class="punctuation">}</span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">"build"</span><span class="punctuation">:</span> <span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"outputs"</span><span class="punctuation">:</span> <span class="punctuation">[</span><span class="string">"dist/"</span><span class="punctuation">]</span></span><br><span class="line"> <span class="punctuation">}</span></span><br><span class="line"> <span class="punctuation">}</span></span><br><span class="line"><span class="punctuation">}</span></span><br></pre></td></tr></table></figure>
<ul>
<li>某些任务可以并行运行,例如 Linter 不需要等待依赖项中的输出成功才能运行:</li>
</ul>
<figure class="highlight json"><table><tr><td class="code"><pre><span class="line"><span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"tasks"</span><span class="punctuation">:</span> <span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"transit"</span><span class="punctuation">:</span> <span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"dependsOn"</span><span class="punctuation">:</span> <span class="punctuation">[</span><span class="string">"^transit"</span><span class="punctuation">]</span></span><br><span class="line"> <span class="punctuation">}</span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">"check-types"</span><span class="punctuation">:</span> <span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"dependsOn"</span><span class="punctuation">:</span> <span class="punctuation">[</span><span class="string">"transit"</span><span class="punctuation">]</span></span><br><span class="line"> <span class="punctuation">}</span><span class="punctuation">,</span></span><br><span class="line"> <span class="punctuation">}</span><span class="punctuation">,</span></span><br><span class="line"><span class="punctuation">}</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p>这里用到了 <a href="https://turbo.build/repo/docs/core-concepts/package-and-task-graph#transit-nodes">Transit Nodes</a> (就是名为 <code>transit</code> 的任务),这些 Transit Node 使用不执行任何操作的任务在软件包依赖项之间创建关系,这里用了名称 <code>transit</code>,但可以将任务命名为 Workspace 中尚未包含脚本的任何名称。</p>
</blockquote>
<h2 id="正在运行的任务"><a class="header-anchor" href="#正在运行的任务">¶</a>正在运行的任务</h2>
<p>当在软件包的目录中时,<code>turbo</code> 会自动将命令范围限定为该软件包的 <a href="https://turbo.build/repo/docs/core-concepts/package-and-task-graph#package-graph">Package Graph</a>:</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">cd apps/docs</span><br><span class="line">turbo build</span><br></pre></td></tr></table></figure>
<p>将使用 <code>turbo.json</code> 中注册的<code>build</code>任务运行 <code>docs</code> 包的<code>build</code>任务。</p>
<blockquote>
<p>但也可以<a href="https://turbo.build/repo/docs/crafting-your-repository/running-tasks#using-filters">使用过滤器</a>覆盖 Automatic Package Scoping。</p>
</blockquote>
<h3 id="运行多个任务"><a class="header-anchor" href="#运行多个任务">¶</a>运行多个任务</h3>
<p><code>Turbo</code> 能够运行多个任务,并尽可能并行化:</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">turbo run build test lint check-types</span><br></pre></td></tr></table></figure>
<h2 id="缓存"><a class="header-anchor" href="#缓存">¶</a>缓存</h2>
<p>Turborepo 的缓存在本地工作时可以节省大量时间 - 启用<a href="https://turbo.build/repo/docs/core-concepts/remote-caching">远程缓存</a>时,它的功能更加强大,可在整个团队和 CI 之间共享缓存。</p>
<h3 id="缓存什么?"><a class="header-anchor" href="#缓存什么?">¶</a>缓存什么?</h3>
<ul>
<li>在 <a href="https://turbo.build/repo/docs/reference/configuration#outputs"><code>turbo.json 的 outputs</code> 键</a>中定义的任务的文件输出。</li>
<li>任务的终端输出,从任务第一次运行时开始将这些日志恢复到终端。</li>
<li>对输入进行哈希处理,为任务运行创建一个 “fingerprints”。当 “fingerprints” 匹配时,运行任务将命中缓存。</li>
</ul>
<h3 id="其他-v2"><a class="header-anchor" href="#其他-v2">¶</a>其他</h3>
<ul>
<li><a href="https://turbo.build/repo/docs/reference/run#--dry----dry-run"><code>--dry</code> 标志</a>,可用于查看如果在没有实际运行任务的情况下运行任务会发生什么。当不确定正在运行的任务时,这对于调试缓存问题非常有用。</li>
<li><a href="https://turbo.build/repo/docs/reference/run#--summarize"><code>--summarize</code> 标志</a>,可用于获取任务的所有输入、输出等的概览。比较两个摘要将揭示两个任务的哈希值不同的原因。</li>
<li>强制 <code>turbo</code> 重新执行已缓存的任务,请使用 <a href="https://turbo.build/repo/docs/reference/run#--force"><code>--force</code> 标志</a>。请注意,这将禁用读取缓存,而不是写入。</li>
</ul>
<h2 id="开发工作流"><a class="header-anchor" href="#开发工作流">¶</a>开发工作流</h2>
<p>在 <code>turbo.json</code> 中定义开发任务 (development task) 会告诉 Turborepo 将运行一个长期任务。这对于运行开发服务器、运行测试或构建应用程序等操作非常有用:</p>
<figure class="highlight json"><table><tr><td class="code"><pre><span class="line"><span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"tasks"</span><span class="punctuation">:</span> <span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"dev"</span><span class="punctuation">:</span> <span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"cache"</span><span class="punctuation">:</span> <span class="literal"><span class="keyword">false</span></span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">"persistent"</span><span class="punctuation">:</span> <span class="literal"><span class="keyword">true</span></span></span><br><span class="line"> <span class="punctuation">}</span></span><br><span class="line"> <span class="punctuation">}</span></span><br><span class="line"><span class="punctuation">}</span></span><br></pre></td></tr></table></figure>
<ul>
<li><code>"cache": false</code>:告诉 Turborepo 不要尝试缓存任务的结果。由于这是一项开发任务,可能会频繁更改代码,因此缓存结果没有用。</li>
<li><code>"persistent": true</code>:告诉 Turborepo 保持任务运行,直到停止它。此键用作终端 UI 的信号,用于将任务视为长时间运行和交互式任务。此外,它还可以防止意外依赖不会退出的任务。</li>
</ul>
<p>一些脚本允许使用 <code>stdin</code> 在其中键入以进行交互式输入。使用<a href="https://turbo.build/repo/docs/reference/configuration#ui">终端 UI</a>,可以选择一个任务,输入它,然后像往常一样使用 <code>stdin</code>。</p>
<p>需要运行用于设置开发环境或预构建包的脚本。可以使用 <code>dependsOn</code> 确保这些任务在 <code>dev</code> 任务之前运行:</p>
<figure class="highlight json"><table><tr><td class="code"><pre><span class="line"><span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"tasks"</span><span class="punctuation">:</span> <span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"dev"</span><span class="punctuation">:</span> <span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"cache"</span><span class="punctuation">:</span> <span class="literal"><span class="keyword">false</span></span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">"persistent"</span><span class="punctuation">:</span> <span class="literal"><span class="keyword">true</span></span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">"dependsOn"</span><span class="punctuation">:</span> <span class="punctuation">[</span><span class="string">"//#dev:setup"</span><span class="punctuation">]</span></span><br><span class="line"> <span class="punctuation">}</span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">"//#dev:setup"</span><span class="punctuation">:</span> <span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"outputs"</span><span class="punctuation">:</span> <span class="punctuation">[</span><span class="string">".codegen/"</span><span class="punctuation">]</span></span><br><span class="line"> <span class="punctuation">}</span></span><br><span class="line"> <span class="punctuation">}</span></span><br><span class="line"><span class="punctuation">}</span></span><br></pre></td></tr></table></figure>
<p>这里用的是 <a href="https://turbo.build/repo/docs/crafting-your-repository/configuring-tasks#registering-root-tasks">Root Task</a>,但可以对 <a href="https://turbo.build/repo/docs/crafting-your-repository/configuring-tasks#depending-on-a-specific-task-in-a-specific-package">packages 中的任意任务</a>使用相同的思路。</p>
<h3 id="Watch-mode"><a class="header-anchor" href="#Watch-mode">¶</a>Watch mode</h3>
<p>许多工具都有一个内置的 watcher,比如 <a href="https://www.typescriptlang.org/docs/handbook/compiler-options.html#compiler-options"><code>tsc --watch</code></a>,它会响应源代码中的更改。有些则没有,<code>Turbo Watch</code> 为任何工具添加了依赖项感知的 Watcher。对源代码的更改将遵循在 <code>turbo.json</code> 中描述的 <a href="https://turbo.build/repo/docs/core-concepts/package-and-task-graph#task-graph">Task Graph (任务图</a>),例如:</p>
<figure class="highlight json"><table><tr><td class="code"><pre><span class="line"><span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"tasks"</span><span class="punctuation">:</span> <span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"dev"</span><span class="punctuation">:</span> <span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"persistent"</span><span class="punctuation">:</span> <span class="literal"><span class="keyword">true</span></span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">"cache"</span><span class="punctuation">:</span> <span class="literal"><span class="keyword">false</span></span></span><br><span class="line"> <span class="punctuation">}</span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">"lint"</span><span class="punctuation">:</span> <span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"dependsOn"</span><span class="punctuation">:</span> <span class="punctuation">[</span><span class="string">"^lint"</span><span class="punctuation">]</span></span><br><span class="line"> <span class="punctuation">}</span></span><br><span class="line"> <span class="punctuation">}</span></span><br><span class="line"><span class="punctuation">}</span></span><br></pre></td></tr></table></figure>
<p>当运行 <code>turbo watch dev lint</code> 时,会看到每当更改源代码时,<code>lint</code> 脚本都会重新运行,尽管 ESLint 没有内置的 watcher。<code>Turbo Watch</code> 还知道内部依赖关系,因此 <code>@repo/UI</code> 中的代码更改将在 <code>@repo/UI</code> 和 <code>Web</code> 中重新运行任务。</p>
<h2 id="环境变量"><a class="header-anchor" href="#环境变量">¶</a>环境变量</h2>
<p>Turborepo 需要根据环境变量来决定是否改变应用程序的行为。在 <code>turbo.json</code> 文件中使用 <code>env</code> 和 <code>globalEnv</code> 键:</p>
<figure class="highlight json"><table><tr><td class="code"><pre><span class="line"><span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"globalEnv"</span><span class="punctuation">:</span> <span class="punctuation">[</span><span class="string">"IMPORTANT_GLOBAL_VARIABLE"</span><span class="punctuation">]</span><span class="punctuation">,</span></span><br><span class="line"> <span class="attr">"tasks"</span><span class="punctuation">:</span> <span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"build"</span><span class="punctuation">:</span> <span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"env"</span><span class="punctuation">:</span> <span class="punctuation">[</span><span class="string">"MY_API_URL"</span><span class="punctuation">,</span> <span class="string">"MY_API_KEY"</span><span class="punctuation">]</span></span><br><span class="line"> <span class="punctuation">}</span></span><br><span class="line"> <span class="punctuation">}</span></span><br><span class="line"><span class="punctuation">}</span></span><br></pre></td></tr></table></figure>
<ul>
<li><code>globalEnv</code>:更改此列表中任何环境变量的值都将更改所有任务的哈希值。</li>
<li><code>env</code>:包括对影响任务的环境变量值的更改,从而实现更好的粒度。例如,当 <code>API_KEY</code> 的值发生变化时,<code>lint</code> 任务可以继续用缓存,但<code>build</code>任务应该不用。</li>
</ul>
<blockquote>
<p>Turborepo 会自动将前缀通配符添加到常见框架的 <a href="https://turbo.build/repo/docs/reference/configuration#env"><code>env</code></a> 键中 (### <a href="https://turbo.build/repo/docs/crafting-your-repository/using-environment-variables#framework-inference">Framework Inference</a>)</p>
</blockquote>
<h3 id="Environment-mode"><a class="header-anchor" href="#Environment-mode">¶</a>Environment mode</h3>
<p>Turborepo 的 Environment Mode 允许控制哪些环境变量在运行时可用于任务:</p>
<ul>
<li><a href="https://turbo.build/repo/docs/crafting-your-repository/using-environment-variables#strict-mode">严格模式</a>(默认):将环境变量过滤为仅在 <code>turbo.json</code> 的 <code>env</code> 和 <code>globalEnv</code> 键中指定的环境变量。</li>
<li><a href="https://turbo.build/repo/docs/crafting-your-repository/using-environment-variables#loose-mode">宽松模式</a>:允许进程的所有环境变量可用。</li>
</ul>
<h3 id="其他-v3"><a class="header-anchor" href="#其他-v3">¶</a>其他</h3>
<ul>
<li><code>.env</code> 文件非常适合在本地处理应用程序。Turborepo 不会将 .env 文件加载到任务的运行时中,而是让它们由框架或 <a href="https://www.npmjs.com/package/dotenv"><code>dotenv</code></a> 等工具处理。但是,<code>turbo</code> 必须知道 <code>.env</code> 文件中值的更改,以便它可以将它们用于哈希。如果在两次构建之间更改 <code>.env</code> 文件中的变量,则 <code>build</code> 任务应该不会用上缓存。所以可以将其添加到 <code>input</code> 键中:</li>
</ul>
<figure class="highlight json"><table><tr><td class="code"><pre><span class="line"><span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"globalDependencies"</span><span class="punctuation">:</span> <span class="punctuation">[</span><span class="string">".env"</span><span class="punctuation">]</span><span class="punctuation">,</span> <span class="comment">// All task hashes</span></span><br><span class="line"> <span class="attr">"tasks"</span><span class="punctuation">:</span> <span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"build"</span><span class="punctuation">:</span> <span class="punctuation">{</span></span><br><span class="line"> <span class="attr">"inputs"</span><span class="punctuation">:</span> <span class="punctuation">[</span><span class="string">"$TURBO_DEFAULT$"</span><span class="punctuation">,</span> <span class="string">".env"</span><span class="punctuation">,</span> <span class="string">".env.local"</span><span class="punctuation">]</span> <span class="comment">// Only the `build` task hash</span></span><br><span class="line"> <span class="punctuation">}</span></span><br><span class="line"> <span class="punctuation">}</span></span><br><span class="line"><span class="punctuation">}</span></span><br></pre></td></tr></table></figure>
<ul>
<li>不建议在存储库的根目录中使用 <code>.env</code> 文件。相反,建议将 <code>.env</code> 文件放入使用它们的包中。</li>
<li><a href="https://turbo.build/repo/docs/reference/eslint-config-turbo"><code>eslint-config-turbo</code> 软件包</a>可帮助查找代码中使用但未在 <code>turbo.json</code>中列出的环境变量。这有助于确保在配置中考虑所有环境变量。</li>
<li>Turborepo 在任务开始时对任务的环境变量进行哈希处理。如果在任务期间创建或更改环境变量,Turborepo 将不知道这些更改,也不会在任务哈希中考虑这些更改。</li>
</ul>
<hr>
<h2 id="最后"><a class="header-anchor" href="#最后">¶</a>最后</h2>
<ul>
<li><a href="https://turbo.build/repo/docs/crafting-your-repository/constructing-ci">配置 CI</a></li>
<li><a href="https://turbo.build/repo/docs/core-concepts">核心概念</a></li>
</ul>
<blockquote>
<p>本快速入门文档参照 Turborepo 2.x 官方文档: <a href="https://turbo.build/repo/docs">https://turbo.build/repo/docs</a><br>
最后一次编辑:二〇二四年九月二十七日下午六点〇七分</p>
</blockquote>
]]></content>
<tags>
<tag>Technique</tag>
</tags>
</entry>
<entry>
<title>TypeScript With Rust Errors, No Try Catch, Heresy</title>
<url>/2023/04/30/TypeScript-With-Rust-Errors-No-Try-Catch-Heresy/</url>
<content><![CDATA[<blockquote>
<p>It’s hard to miss things when you don’t know different things exist</p>
</blockquote>
<p>The first problem is, and personally, I believe it’s the biggest JavaScript problem ever: we don’t know what can throw an error. From a JavaScript error perspective, it’s the same as the following:</p>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">try</span> {</span><br><span class="line"> <span class="keyword">let</span> data = “<span class="title class_">Hello</span>”;</span><br><span class="line">} <span class="keyword">catch</span> (err) {</span><br><span class="line"> <span class="variable language_">console</span>.<span class="title function_">error</span>(err);</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>JavaScript doesn’t know; JavaScript doesn’t care. You should know.</p>
<p>Second thing, this is perfectly viable code:</p>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">const</span> request = { <span class="attr">name</span>: “test”, <span class="attr">value</span>: <span class="number">2n</span> };</span><br><span class="line"><span class="keyword">const</span> body = <span class="title class_">JSON</span>.<span class="title function_">stringify</span>(request);</span><br><span class="line"><span class="keyword">const</span> response = <span class="keyword">await</span> <span class="title function_">fetch</span>(<span class="string">"https://example.com"</span>, {</span><br><span class="line"> <span class="attr">method</span>: “<span class="variable constant_">POST</span>”,</span><br><span class="line"> body,</span><br><span class="line">});</span><br><span class="line"><span class="keyword">if</span> (!response.<span class="property">ok</span>) {</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>No errors, no linters, even though this can break your app.</p>
<p>Right now, in my head, I can hear, “What’s the problem, just use try/catch everywhere.” Here comes the third problem: we don’t know which one is thrown. Of course, we can somehow guess by the error message, but what about bigger services/functions with many places where errors can happen? Are you sure you are handling all of them properly with one try/catch?</p>
<hr>
<figure class="highlight rust"><table><tr><td class="code"><pre><span class="line"><span class="keyword">let</span> <span class="variable">greeting_file_result</span> = File::<span class="title function_ invoke__">open</span>(“hello.txt”); </span><br><span class="line"><span class="keyword">let</span> <span class="variable">greeting_file</span> = <span class="keyword">match</span> greeting_file_result { </span><br><span class="line"> <span class="title function_ invoke__">Ok</span>(file) => file, </span><br><span class="line"> <span class="title function_ invoke__">Err</span>(error) => <span class="built_in">panic!</span>(<span class="string">"Problem opening the file: {:?}"</span>, error), </span><br><span class="line">};</span><br></pre></td></tr></table></figure>
<p>The most verbose of the three shown here and, ironically, the best one. So, first of all, Rust handles the errors using its amazing enums (they are not the same as TypeScript enums!). Without going into detail, what is important here is that it uses an enum called <code>Result</code> with two variants: <code>Ok</code> and <code>Err</code>. As you might guess, <code>Ok</code> holds a value and <code>Err</code> holds…surprise, an error :D.</p>
<p>The summary here is that Rust always know where there might be an error. And it force you to deal with it right where it appears (mostly). No hidden ones, no guessing, no breaking app with a surprise face.</p>
<p>And this approach is just better. By A MILE.</p>
<p>We cannot make TypeScript errors work like the Rust. The limiting factor here is the language itself; it doesn’t have the proper tools to do that.</p>
<p>But what we can do is try to make it similar. And make it simple:</p>
<figure class="highlight typescript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">type</span> <span class="title class_">Safe</span><T> = </span><br><span class="line"> | { </span><br><span class="line"> <span class="attr">success</span>: <span class="literal">true</span>; </span><br><span class="line"> <span class="attr">data</span>: T; </span><br><span class="line"> } </span><br><span class="line"> | { </span><br><span class="line"> <span class="attr">success</span>: <span class="literal">false</span>; </span><br><span class="line"> <span class="attr">error</span>: <span class="built_in">string</span>; </span><br><span class="line"> };</span><br></pre></td></tr></table></figure>
<p>we do need a few try/catches. The good thing is we only need about two, not 100,000:</p>
<figure class="highlight typescript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">function</span> safe<T>(<span class="attr">promise</span>: <span class="title class_">Promise</span><T>, <span class="attr">err</span>?: <span class="built_in">string</span>): <span class="title class_">Promise</span><<span class="title class_">Safe</span><T>>;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">function</span> safe<T>(<span class="attr">func</span>: <span class="function">() =></span> T, <span class="attr">err</span>?: <span class="built_in">string</span>): <span class="title class_">Safe</span><T>;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">function</span> safe<T>(</span><br><span class="line"> <span class="attr">promiseOrFunc</span>: <span class="title class_">Promise</span><T> | (<span class="function">() =></span> T),</span><br><span class="line"> <span class="attr">err</span>?: <span class="built_in">string</span>,</span><br><span class="line">): <span class="title class_">Promise</span><<span class="title class_">Safe</span><T>> | <span class="title class_">Safe</span><T> {</span><br><span class="line"> <span class="keyword">if</span> (promiseOrFunc <span class="keyword">instanceof</span> <span class="title class_">Promise</span>) {</span><br><span class="line"> <span class="keyword">return</span> <span class="title function_">safeAsync</span>(promiseOrFunc, err);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="title function_">safeSync</span>(promiseOrFunc, err);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">async</span> <span class="keyword">function</span> safeAsync<T>(</span><br><span class="line"> <span class="attr">promise</span>: <span class="title class_">Promise</span><T>, </span><br><span class="line"> <span class="attr">err</span>?: <span class="built_in">string</span></span><br><span class="line">): <span class="title class_">Promise</span><<span class="title class_">Safe</span><T>> {</span><br><span class="line"> <span class="keyword">try</span> {</span><br><span class="line"> <span class="keyword">const</span> data = <span class="keyword">await</span> promise;</span><br><span class="line"> <span class="keyword">return</span> { data, <span class="attr">success</span>: <span class="literal">true</span> };</span><br><span class="line"> } <span class="keyword">catch</span> (e) {</span><br><span class="line"> <span class="variable language_">console</span>.<span class="title function_">error</span>(e);</span><br><span class="line"> <span class="keyword">if</span> (err !== <span class="literal">undefined</span>) {</span><br><span class="line"> <span class="keyword">return</span> { <span class="attr">success</span>: <span class="literal">false</span>, <span class="attr">error</span>: err };</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span> (e <span class="keyword">instanceof</span> <span class="title class_">Error</span>) {</span><br><span class="line"> <span class="keyword">return</span> { <span class="attr">success</span>: <span class="literal">false</span>, <span class="attr">error</span>: e.<span class="property">message</span> };</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> { <span class="attr">success</span>: <span class="literal">false</span>, <span class="attr">error</span>: <span class="string">"Something went wrong"</span> };</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> safeSync<T>(</span><br><span class="line"> <span class="attr">func</span>: <span class="function">() =></span> T, </span><br><span class="line"> <span class="attr">err</span>?: <span class="built_in">string</span></span><br><span class="line">): <span class="title class_">Safe</span><T> {</span><br><span class="line"> <span class="keyword">try</span> {</span><br><span class="line"> <span class="keyword">const</span> data = <span class="title function_">func</span>();</span><br><span class="line"> <span class="keyword">return</span> { data, <span class="attr">success</span>: <span class="literal">true</span> };</span><br><span class="line"> } <span class="keyword">catch</span> (e) {</span><br><span class="line"> <span class="variable language_">console</span>.<span class="title function_">error</span>(e);</span><br><span class="line"> <span class="keyword">if</span> (err !== <span class="literal">undefined</span>) {</span><br><span class="line"> <span class="keyword">return</span> { <span class="attr">success</span>: <span class="literal">false</span>, <span class="attr">error</span>: err };</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span> (e <span class="keyword">instanceof</span> <span class="title class_">Error</span>) {</span><br><span class="line"> <span class="keyword">return</span> { <span class="attr">success</span>: <span class="literal">false</span>, <span class="attr">error</span>: e.<span class="property">message</span> };</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> { <span class="attr">success</span>: <span class="literal">false</span>, <span class="attr">error</span>: <span class="string">"Something went wrong"</span> };</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>This is just a wrapper with our <code>Safe</code> type as the return one. But sometimes simple things are all you need. Let’s combine them with the example from above.</p>
<figure class="highlight typescript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">const</span> request = { <span class="attr">name</span>: “test”, <span class="attr">value</span>: <span class="number">2n</span> }; </span><br><span class="line"><span class="keyword">const</span> body = <span class="title function_">safe</span>( </span><br><span class="line"> <span class="function">() =></span> <span class="title class_">JSON</span>.<span class="title function_">stringify</span>(request), </span><br><span class="line"> “<span class="title class_">Failed</span> to serialize request”, </span><br><span class="line">); </span><br><span class="line"><span class="keyword">if</span> (!body.<span class="property">success</span>) { </span><br><span class="line"> <span class="comment">// handle error (body.error) </span></span><br><span class="line"> <span class="keyword">return</span>; </span><br><span class="line">} </span><br><span class="line"><span class="keyword">const</span> response = <span class="keyword">await</span> <span class="title function_">safe</span>( </span><br><span class="line"> <span class="title function_">fetch</span>(<span class="string">"https://example.com"</span>, { </span><br><span class="line"> <span class="attr">method</span>: “<span class="variable constant_">POST</span>”, </span><br><span class="line"> <span class="attr">body</span>: body.<span class="property">data</span>, </span><br><span class="line"> }), </span><br><span class="line">); </span><br><span class="line"><span class="keyword">if</span> (!response.<span class="property">success</span>) { </span><br><span class="line"> <span class="comment">// handle error (response.error) </span></span><br><span class="line"> <span class="keyword">return</span>; </span><br><span class="line">} </span><br><span class="line"><span class="keyword">if</span> (!response.<span class="property">data</span>.<span class="property">ok</span>) { </span><br><span class="line"> <span class="comment">// handle network error </span></span><br><span class="line"> <span class="keyword">return</span>; </span><br><span class="line">} </span><br><span class="line"><span class="comment">// handle response (body.data)</span></span><br></pre></td></tr></table></figure>
<p>New solution is longer, but it performs better because of the following reasons:</p>
<ul>
<li>no try/catch</li>
<li>we handle each error where it occurs</li>
<li>we can specify an error message for a specific function</li>
<li>we have a nice top-to-bottom logic, all errors on top, then only the response at the bottom</li>
</ul>
]]></content>
<tags>
<tag>Technique</tag>
</tags>
</entry>
<entry>
<title>WiscKey: Separating Keys from Values in SSD-conscious Storage</title>
<url>/2023/07/21/WiscKey-Separating-Keys-from-Values-in-SSD-conscious-Storage/</url>
<content><![CDATA[<p>We present WiscKey, a persistent LSM-tree-based key-value store with a performance-oriented data layoutthat separates keys from values to minimize I/O amplifi-cation. The design of WiscKey is highly SSD optimized, leveraging both the sequential and random performance characteristics of the device.</p>
<ul>
<li><a href="/papers/WiscKey:%20Separating%20Keys%20from%20Values%20in%20SSD-conscious%20Storage.pdf">原始论文</a></li>
</ul>
<p>WiscKey是一种基于LSM-Tree的kv存储,它的实现很简单: 将键和值分开存储, 这么做可以显著的减少I/O放大。</p>
<p>WiscKey的设计是针对SSD优化的,充分利用了SSD的性能并提出了一种轻量的GC方案, 除此之外还分析了现代存储硬件的特性和LSM树的读写放大的问题。</p>
<p>总的来说,WiscKey是基于SSD硬件特性而重新设计的存储结构。虽然LevelDB也是一种键值存储引擎,但它并没有像WiscKey那样专门为SSD硬件而设计。因此,如果将LevelDB的存储结构按照SSD硬件的特性进行优化,可能会提高一些性能,但是不一定能够达到WiscKey的性能水平。</p>
<h2 id="笔记"><a class="header-anchor" href="#笔记">¶</a>笔记</h2>
<ul>
<li>
<p>The main data structures in LevelDB are an on-disk log file, two in-memory sorted skiplists (memtableand immutable memtable), and seven levels (L0 to L6) of on-disk Sorted String Table (SSTable) files</p>
<blockquote>
<p>LSM-trees的内部的各种组件可以使用任意索引结构来实现。通常内存组件会使用skiplist或B+树等并发数据结构,而磁盘组件会使用B+树或SSTables(SSTable包含一个数据块列表和一个索引块:数据块存储按键排序的键值对,索引块存储所有数据块的键范围)</p>
</blockquote>
</li>
<li>
<p>how LSM-trees maintainsequential I/O access by increasing I/O amplification.</p>
<blockquote>
<p>具体来说,LSM-trees的写放大是指每次写入操作都需要将数据追加到写前日志中,并加入到内存组件C0中。当C0的大小达到一定阈值时,C0会与磁盘上的C1进行归并排序操作,生成新的组件new-C1,并将其顺序写入磁盘,取代旧版本的C1。类似地,当C1的大小达到一定阈值时,C1会与下一层组件Ci+1合并。这种归并排序的过程可以保证数据在磁盘上的存储是有序的,从而保持顺序I/O访问</p>
</blockquote>
</li>
<li>
<p>First, WiscKey separates keys from values, keeping only keys in theLSM-tree and the values in a separate log file. Second,to deal with unsorted values (which necessitate random access during range queries), WiscKey uses the parallel random-read characteristic of SSD devices. Third, WiscKey utilizes unique crash-consistency and garbage-collection techniques to efficiently manage the value log.Finally, WiscKey optimizes performance by removingthe LSM-tree log without sacrificing consistency, thusreducing system-call overhead from small writes.</p>
<blockquote>
<p>这是WiscKey的主要优化方式</p>
</blockquote>
</li>
<li>
<p>For range queries, LevelDB provides the user with an iterator-based interface with Seek(key), Next(), Prev(),Key() and Value() operations. To scan a range of key-value pairs, users can first Seek() to the starting key, then call Next() or Prev() to search keys one by one. To retrieve the key or the value of the current iterator position, users call Key() or Value(), respectively.</p>
<blockquote>
<p>如果要查询某个key范围的数据,LevelDB会先找到该范围的起始key在哪个SSTable中,然后按照顺序读取该SSTable中的数据,直到读取到范围之外的key为止。</p>
<p>而读取SSTable中的数据是通过Binary Search来实现的。为了加速Search,LevelDB为每个SSTable建立了一个稀疏索引,用来记录每个数据块的最大key和最小key。</p>
</blockquote>
</li>
</ul>
]]></content>
<tags>
<tag>Technique</tag>
</tags>
</entry>
<entry>
<title>caml_release_runtime_system导致的死锁问题</title>
<url>/2023/06/28/caml-release-runtime-system%E5%AF%BC%E8%87%B4%E7%9A%84%E6%AD%BB%E9%94%81%E9%97%AE%E9%A2%98/</url>
<content><![CDATA[<p>给出一个<code>test.ml</code>:</p>
<figure class="highlight ocaml"><table><tr><td class="code"><pre><span class="line"><span class="type">Printf</span>.eprintf <span class="string">"hello from OCaml\n%!"</span></span><br></pre></td></tr></table></figure>
<p>和<code>test-ocaml-5.c</code>:</p>
<figure class="highlight c"><table><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdio.h></span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><stdlib.h></span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><caml/misc.h></span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><caml/callback.h></span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string"><caml/threads.h></span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span></span><br><span class="line"><span class="title function_">main</span> <span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line"> {</span><br><span class="line"> <span class="built_in">fprintf</span> (<span class="built_in">stderr</span>, <span class="string">"starting up ...\n"</span>);</span><br><span class="line"> caml_startup (argv);</span><br><span class="line"> <span class="built_in">fprintf</span> (<span class="built_in">stderr</span>, <span class="string">"acquiring ...\n"</span>);</span><br><span class="line"> caml_acquire_runtime_system ();</span><br><span class="line"> <span class="built_in">fprintf</span> (<span class="built_in">stderr</span>, <span class="string">"acquired\n"</span>);</span><br><span class="line"> <span class="comment">// here is where I would be calling an OCaml callback</span></span><br><span class="line"> <span class="comment">// omitted for simplicity</span></span><br><span class="line"> caml_release_runtime_system ();</span><br><span class="line"> <span class="built_in">exit</span> (<span class="number">0</span>);</span><br><span class="line"> }</span><br></pre></td></tr></table></figure>
<p>然后用OCaml5的ocaml native compiler编译一下(这里我用的是<code>ocaml-variants.5.0.0+options</code>):<br>
<code>ocamlopt -g test-ocaml-5.c test.ml -o test-ocaml-5</code></p>
<p>运行<code>test-ocaml-5</code> 会出现:</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">starting up ...</span><br><span class="line">hello from OCaml</span><br><span class="line">acquiring ...</span><br><span class="line">Fatal error: Fatal error during lock: Resource deadlock avoided</span><br><span class="line"></span><br><span class="line">[1] 8998 IOT instruction (core dumped) ./test-ocaml-5</span><br></pre></td></tr></table></figure>
<p>这里我尝试了一下 4.14.0 和 4.14.1 , 都没有出现这个情况,而如果用<code>threads</code>编译的话:<br>
<code>ocamlopt -g -I +unix unix.cmxa -I +threads threads.cmxa test-ocaml-5.c test.ml -o test-ocaml-5</code></p>
<p>无论在5.0还是4.14.x,都会挂起。</p>
<p>出现这个问题的一个可能原因是,在<code>test-ocaml-5.c</code>中,我在开头调用了<code>caml_startup()</code>,这会让当前线程获取锁,而<code>caml_acquire_runtime_system()</code>会再次获取它,<code>caml_acquire_runtime_system()</code> 应该在<code>caml_release_runtime_system()</code>之后调用:</p>
<figure class="highlight ocaml"><table><tr><td class="code"><pre><span class="line"><span class="built_in">int</span></span><br><span class="line">main (<span class="built_in">int</span> argc, <span class="built_in">char</span> *argv<span class="literal">[]</span>)</span><br><span class="line"> {</span><br><span class="line"> fprintf (stderr, <span class="string">"starting up ...\n"</span>);</span><br><span class="line"> caml_startup (argv);</span><br><span class="line"> fprintf (stderr, <span class="string">"acquiring ...\n"</span>);</span><br><span class="line"> caml_release_runtime_system <span class="literal">()</span>;</span><br><span class="line"> caml_acquire_runtime_system <span class="literal">()</span>;</span><br><span class="line"> fprintf (stderr, <span class="string">"acquired\n"</span>);</span><br><span class="line"> exit (<span class="number">0</span>);</span><br><span class="line"> }</span><br></pre></td></tr></table></figure>
<p>运行结果为:</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">starting up ...</span><br><span class="line">hello from OCaml</span><br><span class="line">acquiring ...</span><br><span class="line">acquired</span><br></pre></td></tr></table></figure>]]></content>
<tags>
<tag>Technique</tag>
</tags>
</entry>
<entry>
<title>DADGAD 定弦</title>
<url>/2025/07/08/dadgad/</url>
<content><![CDATA[<p>模态定弦的空弦音不直接构成传统意义上的大调或小调和弦,而是形成一种听感上更为空灵、悬浮的“挂留和弦”(Suspended Chord)效果。</p>
<p><strong>DADGAD (发音为 “dad-gad”):</strong> 这是凯尔特音乐(Celtic Music)中最具代表性的定弦。它的空弦音给人一种既非大调也非小调的模糊感,充满了神秘和悠远的色彩,非常适合营造氛围音乐和复杂的指弹旋律。</p>
<p>挂留和弦(Suspended Chord),通常简写为 <strong>“sus”</strong>,是一种在传统和弦结构上进行了巧妙“修改”的和弦。它的核心特点是:<strong>用一个非三度的音,去“替代”或“悬挂”了原本决定和弦大/小调色彩的三度音</strong>,从而创造出一种独特的声音效果。</p>
<p>想象一个标准的大三和弦或小三和弦,它由<strong>根音、三度音和五度音</strong>构成。其中,<strong>三度音</strong>是决定这个和弦听起来是明亮、开心的“大调”,还是忧郁、伤感的“小调”的关键。</p>
<p>而挂留和弦,就是把这个关键的“三度音”暂时拿掉,换上别的音。最常见的替代者有两个:<br>
一是 <strong>纯四度音 (Perfect 4th)</strong> ,二是 <strong>大二度音 (Major 2nd)</strong> 。</p>
<p><strong>sus4 和弦 (挂四和弦)</strong> 由 <strong>根音 (1) + 纯四度音 (4) + 纯五度音 (5)</strong> 构成,即用<strong>四度音</strong>替代了原本的三度音。听感上,这是最常见的挂留和弦。它创造出一种<strong>强烈而悬浮的张力感</strong>,声音既不属于大调也不属于小调,听起来非常“开阔”。例如,一个 <strong>Csus4</strong> 和弦,就是由 C、F、G 三个音构成,取代了标准C大调和弦(C-E-G)中的 E 音。</p>
<p><strong>sus2 和弦 (挂二和弦)</strong> 由 <strong>根音 (1) + 大二度音 (2) + 纯五度音 (5)</strong> 构成,特点是用二度音替代了原本的三度音。听感上,sus2和弦的声音比sus4更柔和、更明亮一些。它同样具有开放、模糊的色彩,但张力感稍弱,听起来更像是一种平静、梦幻的色彩和弦。例如,一个 <strong>Csus2</strong> 和弦,就是由 C、D、G 三个音构成,取代了标准C大调和弦(C-E-G)中的 E 音。</p>
<p>而 DADGAD 定弦方式的空弦音,其核心就是一个挂留和弦,具体来说是一个 Dsus4 和弦。当你弹响 DADGAD 的所有空弦时,你实际上听到的就是 <strong>D、G、A</strong> 这三个音在不同八度上的叠加和共鸣。这三个音不多不少,正好构成了 <strong>Dsus4</strong> 和弦。</p>
<p>DADGAD 定弦的精髓,就在于它将整个吉他的开放状态变成了一个巨大的、共鸣丰富的 <strong>Dsus4</strong> 挂留和弦。</p>
<p>这正是为什么它能产生那种“既非大调也非小调的模糊感”和“空灵、悬浮”的听感。因为三度音(F# 或 F)的缺失,和弦的“身份”被悬挂了起来,没有明确地告诉你它是快乐的(大调)还是悲伤的(小调),而是创造出一种充满想象空间、广阔而悠远的音景。</p>
]]></content>
<tags>
<tag>Life</tag>
</tags>
</entry>
<entry>
<title>在临床医疗领域中的数据库外键设计取舍</title>
<url>/2025/09/09/database-fk-design-in-clinical-medicine/</url>
<content><![CDATA[<p>从关系模型的理论视角看,外键作为参照完整性约束的实现机制,理论上能确保跨表数据的逻辑一致性,避免出现孤立记录(orphaned records)<sup class="footnote-ref"><a href="#fn1" id="fnref1">[1]</a></sup>。但在高并发、分布式、快速迭代的业务场景中,强制外键约束会引入显著的运行时开销:每次写操作都需要执行跨表的锁检查与索引查询,在 OLTP 系统中尤其影响吞吐量。<a href="https://planetscale.com/docs/learn/operating-without-foreign-key-constraints">planetscale.com</a> 的文档指出,MySQL 的外键会引入对数据和元数据更改的额外锁定,从而导致性能下降,特别是在高并发工作负载中<sup class="footnote-ref"><a href="#fn2" id="fnref2">[2]</a></sup>。以 TPC-C 基准测试为例,启用外键的订单创建事务延迟可能增加,因为数据库引擎必须验证客户表与订单项表的关联有效性,这在每秒数千次写入的场景下会成为锁竞争瓶颈。</p>
<p>而在分库分表架构中,外键约束跨物理节点时的实现复杂度呈指数级上升——分布式事务的两阶段提交协议(2PC/3PC)不仅降低性能,还可能因网络分区导致事务悬挂,这与 CAP 定理中“分布式系统无法同时满足一致性、可用性与分区容错性”的根本限制直接冲突<sup class="footnote-ref"><a href="#fn3" id="fnref3">[3]</a></sup>。</p>
<p>互联网行业的大规模实践表明,当单表数据量超过千万级或需要水平扩展时,放弃外键往往成为必然选择。这在 Hacker News 的相关讨论中得到了许多工程师的印证,他们出于对迁移复杂性和性能的担忧,会选择在设计中避免使用外键<sup class="footnote-ref"><a href="#fn4" id="fnref4">[4]</a></sup>。</p>
<p>这种设计取舍背后还隐藏着更深层的工程哲学转变。传统单体架构中,数据库承担了业务规则的核心验证职责,而微服务与领域驱动设计(DDD)的兴起将数据一致性边界从存储层上移到应用层。例如在电商订单履约流程中,订单服务与库存服务的关联不再依赖数据库外键,而是通过 Saga 事务模式或事件溯源(Event Sourcing)机制实现最终一致性。</p>
<p>应用层通过领域事件(如 OrderCreated 事件)触发库存预占操作,并在消息队列保障下实现跨服务协调。这种方式虽然增加了业务代码的复杂度,但换取了服务解耦与独立部署能力。即,当库存服务需要重构时,无需协调订单服务的数据库变更,这大大提升了敏捷开发效率。</p>
<p>然而放弃外键绝非没有代价。</p>
<p>最直接的影响是数据一致性的保障责任从 DBA 转移至应用开发团队,当业务逻辑存在缺陷时,极易产生逻辑断裂的数据,例如支付成功但订单状态未更新的场景<sup class="footnote-ref"><a href="#fn5" id="fnref5">[5]</a></sup>。这类问题往往在特定异常路径下才暴露,调试难度远高于数据库层面的即时约束报错。</p>
<p>对于历史数据迁移和数据分析,缺乏外键约束的模型在构建数仓时,ETL 过程必须额外实现参照验证逻辑,否则维度表与事实表的断裂关联会导致分析结论失真。</p>
<blockquote>
<p>真正专业的架构决策需要基于量化指标进行场景化评估。</p>
</blockquote>
<p>对于交易系统等强一致性场景,<a href="https://planetscale.com/docs/learn/strategies-for-maintaining-referential-integrity">planetscale.com</a> 建议,若决定使用外键约束,可在数据库设置中启用;若不用,则需在应用层通过代码(如使用事务)或额外系统来维护参照完整性<sup class="footnote-ref"><a href="#fn6" id="fnref6">[6]</a></sup>。对于分析型系统或写入吞吐要求极高的场景(如IoT设备数据采集),可完全放弃外键,但必须配套实施保障措施。</p>
<blockquote>
<p>现代云数据库如 Amazon Aurora 已提供逻辑外键(logical foreign keys)的折中方案:它不强制运行时约束,但通过存储过程与触发器记录关联规则,在数据导出或特定查询时触发验证,这在保持写性能的同时保留了部分数据治理能力。</p>
</blockquote>
<p>所以最终判断是否使用外键应基于四个维度的具体测量:</p>
<p>一、业务容忍的数据不一致窗口(如金融系统要求秒级,内容推荐可接受小时级)。<br>
二、峰值QPS与事务复杂度。<br>
三、团队对分布式一致性的掌控能力。<br>
四、监控修复工具链的完备性。</p>
<blockquote>
<p>当系统处于初创期时保留外键可降低认知负担,但进入高速增长期后需有计划地将约束责任前移至应用层。</p>
</blockquote>
<p>在临床医疗领域的药物研发项目与慢病管理系统中,数据库外键的取舍决策必须超越传统性能权衡,这是因为医疗数据承担着生命安全关联性、法规强制性约束与临床逻辑不可妥协性这些特殊情况。</p>
<p>这类系统的核心矛盾在于:医疗数据的完整性缺陷可能直接导致误诊、用药错误甚至患者死亡,而过度依赖外键又可能阻碍紧急场景下的操作敏捷性(如 ICU 实时数据录入)。因此,外键策略需分层设计,依据数据域的风险等级与业务场景动态调整。</p>
<p>以药物研发项目数据库为例,其数据模型涉及化合物结构、临床试验阶段、受试者信息及不良事件报告等强依赖实体。在 I 期临床试验阶段(单中心小规模数据),保留外键是合规刚需:当录入受试者用药事件时,必须强制关联有效的伦理委员会批准编号(protocol_id)和药品批号(lot_number)。这并非仅出于数据规范性,FDA 21 CFR Part 11 明确规定电子记录需具备“可靠的归属关系”,若不良事件记录无法追溯到具体药物批次,将导致整个试验数据被判定为无效<sup class="footnote-ref"><a href="#fn7" id="fnref7">[7]</a></sup>。</p>
<p>然而当进入 III 期多中心试验阶段,分布式数据采集就会出现问题。此处的取舍在于“约束分级”:对患者身份标识符等关键关系保留外键,而对非致命性关联改用应用层校验。</p>
<p>更前沿的实践是采用基于 <strong>FHIR</strong> (Fast Healthcare Interoperability Resources) 标准的松散耦合架构——通过 HL7 FHIR 的 Reference 机制调用中央服务的实时验证 API。这既满足了数据关联可追溯性,又避免了传统外键的分布式锁竞争。</p>
<p>慢病管理系统的决策逻辑更为精细。以糖尿病患者管理系统为例,血糖监测记录表与患者档案表的关联存在多种典型场景,需根据场景的紧急性和重要性决定是否使用外键或应用层校验。</p>
<p>对于法规与安全维度。HIPAA 要求所有患者数据关联必须可审计,而 GDPR“被遗忘权”又要求能彻底解耦数据。若使用传统 ON DELETE CASCADE 外键,删除患者记录会级联抹除所有医疗历史,这可能违反数据保留原则。</p>
<p>一个可行的方案是设计<strong>策略性外键</strong>,并结合逻辑删除标记和异步校验机制。</p>
<blockquote>
<p>核心原则是:<strong>外键的存在与否取决于业务操作的后果严重性,而非单纯的技术指标</strong>。当数据断裂可能直接伤害患者时,必须用外键;当系统响应速度关乎生命时,则需设计更智能的补偿机制。</p>
</blockquote>
<p>不过现代云医疗数据库(如AWS HealthLake)已内置此类混合策略:在 OLTP 层保留关键外键,同时提供 FHIR 资源引用的逻辑一致性检查。</p>
<h3 id="Refs"><a class="header-anchor" href="#Refs">¶</a>Refs.</h3>
<hr class="footnotes-sep">
<section class="footnotes">
<ol class="footnotes-list">
<li id="fn1" class="footnote-item"><p>C. J. Date, “An Introduction to Database Systems”, 8th Edition, Addison-Wesley, 2003. (经典教材,阐述关系型数据库理论基础) <a href="#fnref1" class="footnote-backref">↩︎</a></p>
</li>
<li id="fn2" class="footnote-item"><p><a href="https://planetscale.com/docs/learn/operating-without-foreign-key-constraints">planetscale.com</a> - “Operating without foreign key constraints”. (关于放弃外键的性能理由的权威技术文档) <a href="#fnref2" class="footnote-backref">↩︎</a></p>
</li>
<li id="fn3" class="footnote-item"><p>Eric Brewer, “CAP Twelve Years Later: How the ‘Rules’ Have Changed”, Computer, 2012. (CAP定理提出者的后续反思,是理解分布式系统局限性的基础) <a href="#fnref3" class="footnote-backref">↩︎</a></p>
</li>
<li id="fn4" class="footnote-item"><p><a href="https://news.ycombinator.com/item?id=32731916">news.ycombinator.com</a> - “Ask HN: Do you use foreign keys in relational databases?”. (2022年关于外键使用实践的广泛开发者讨论,反映了业界的真实权衡) <a href="#fnref4" class="footnote-backref">↩︎</a></p>
</li>
<li id="fn5" class="footnote-item"><p>Martin Kleppmann, “Designing Data-Intensive Applications”, O’Reilly, 2017. (该书籍是数据系统设计的权威指南,详细讨论了分布式系统中的数据一致性问题) <a href="#fnref5" class="footnote-backref">↩︎</a></p>
</li>
<li id="fn6" class="footnote-item"><p><a href="https://planetscale.com/docs/learn/strategies-for-maintaining-referential-integrity">planetscale.com</a> - “Strategies for maintaining referential integrity”. (提供了外键替代方案的具体策略) <a href="#fnref6" class="footnote-backref">↩︎</a></p>
</li>
<li id="fn7" class="footnote-item"><p>U.S. Food and Drug Administration, “21 CFR Part 11 - Electronic Records; Electronic Signatures”. (FDA关于电子记录合规性的官方法规,是医疗系统设计的强制性要求) <a href="#fnref7" class="footnote-backref">↩︎</a></p>
</li>
</ol>
</section>
]]></content>
<tags>
<tag>Technique</tag>
<tag>Medicine</tag>
</tags>
</entry>
<entry>
<title>由甲醛开始的碎碎念</title>
<url>/2025/07/11/fuck-hcho/</url>
<content><![CDATA[<p>前几天和群友聊到甲醛相关的东东,有点好奇甲醛进入人体之后的一系列反应,于逝,<s>我去吸了两口</s> ,先来看看一些无聊的总结:</p>
<p>吸入甲醛后,人体之所以会产生眼部灼痛、喉咙刺痒、呼吸困难等一系列强烈的不适感,是因为甲醛作为一种高活性、强刺激性的化学物质,对我们的身体构成了从宏观到微观的多层次、多途径的直接攻击。其影响方式主要可以归结为两大方面:<strong>直接的刺激与腐蚀作用</strong>和<strong>深层的细胞毒性与遗传毒性</strong>。</p>
<p>当甲醛气体通过呼吸进入人体,首当其冲的是我们的眼睛和呼吸道黏膜。这些部位的细胞直接暴露在甲醛的攻击下,引发一系列即时且强烈的不适症状:</p>
<ul>
<li><strong>对黏膜的强烈刺激:</strong> 甲醛具有极强的亲水性和反应活性,能迅速与眼睛结膜、鼻腔、咽喉和气管等部位黏膜表面的蛋白质和水分发生反应。这种反应会直接损伤黏膜组织,导致神经末梢受到强烈刺激。
<ul>
<li><strong>眼部症状:</strong> 眼睛会感到灼烧、刺痛、发痒和不自主流泪。</li>
<li><strong>呼吸道症状:</strong> 喉咙会感到干燥、刺痛、咳嗽,高浓度下更会引发胸闷、气喘,甚至喉头水肿和肺水肿,导致呼吸极度困难。</li>
</ul>
</li>
<li><strong>引发急性炎症:</strong> 甲醛的刺激作用会激活人体的免疫系统,将甲醛识别为有害入侵物。这会引发局部的急性炎症反应,表现为相关组织充血、水肿和分泌物增多(如流鼻涕)。这也是为什么在甲醛超标的环境里,人们会感觉“乌烟瘴气”,并出现类似重感冒的症状。</li>
</ul>
<p>除了表层的直接刺激,甲醛还能穿透细胞膜,对身体造成更深层次的损害。这解释了为何长期低剂量接触甲醛同样会带来严重的健康问题:</p>
<ul>
<li><strong>蛋白质变性:</strong> 甲醛能够与构成生命基础的蛋白质中的氨基(-NH2)发生反应,形成“亚甲基桥”,破坏蛋白质原有的三维结构,使其失去正常的生理功能。细胞的功能依赖于各种蛋白质(酶、结构蛋白等)的正常工作,蛋白质变性意味着细胞代谢紊乱、功能障碍,甚至死亡。这在本质上是一种“腐蚀”作用,也是福尔马林(甲醛水溶液)能用作防腐剂的根本原因。</li>
<li><strong>诱导细胞凋亡与氧化应激:</strong> 研究表明,甲醛会损伤细胞内的能量工厂——线粒体,诱导细胞产生大量的活性氧(ROS)。过量的活性氧会破坏细胞膜、蛋白质和DNA,引发“氧化应激”反应,并最终启动细胞的程序性死亡(凋亡)机制。</li>
<li><strong>遗传毒性与致癌性:</strong> 甲醛最具危害性的影响在于其遗传毒性。它可以穿过细胞核,与遗传物质DNA和组蛋白(包裹DNA的蛋白质)形成“加合物”,这会干扰DNA的正常复制和修复过程,导致基因突变。
<ul>
<li><strong>致癌风险:</strong> 当这种基因损伤发生在关键的癌基因或抑癌基因上,并且人体的修复机制无法有效清除时,就可能导致细胞的异常增殖,最终引发癌症。世界卫生组织(WHO)下属的国际癌症研究机构(IARC)已将甲醛列为<strong>一类致癌物</strong>,有充分证据表明其与鼻咽癌、白血病等恶性肿瘤的发生密切相关。</li>
</ul>
</li>
<li><strong>神经与免疫毒性:</strong> 甲醛还能对中枢神经系统产生毒性作用,导致头晕、头痛、记忆力减退、失眠等症状。同时,它也可能干扰免疫系统的正常功能,使人更容易过敏(如引发过敏性皮炎、哮喘)或抵抗力下降。</li>
</ul>
<hr>
<h2 id="甲醛与粘膜表面的蛋白质和水分发生反应?"><a class="header-anchor" href="#甲醛与粘膜表面的蛋白质和水分发生反应?">¶</a>甲醛与粘膜表面的蛋白质和水分发生反应?</h2>
<p>当甲醛(HCHO)这个小而活泼的分子,遇到我们湿润的眼睛、鼻腔和喉咙黏膜时,它首先会迅速溶于黏膜表面的水分,形成甲二醇 (CH₂(OH)₂) 。这个形态的甲醛会将目标转向 <strong>蛋白质</strong>。</p>
<p>这个反应并非简单的接触,而是一种被称为“交联反应”(Cross-linking)的化学过程。具体来说:甲醛分子会精准地找到蛋白质分子链上的氨基(-NH₂),这是许多氨基酸的共同特征。接着与蛋白质的氨基发生反应,形成一个不稳定的中间体。这个中间体可以进一步与邻近的另一个蛋白质的氨基反应,从而在两个原本独立的蛋白质(或同一个蛋白质的不同部分)之间,强行架起一座“亚甲基桥”(-CH₂-)。</p>
<p>蛋白质就像一团经过精确折叠、有着特定三维结构的精密机器,它的功能完全依赖于这个构型。而甲醛的“交联”反应,就像是往这台精密机器的各个活动关节之间,甚至和旁边的机器之间倒 502,给它们粘死了。</p>
<h3 id="为什么甲醛分子能精准地找到蛋白质分子链上的氨基?"><a class="header-anchor" href="#为什么甲醛分子能精准地找到蛋白质分子链上的氨基?">¶</a>为什么甲醛分子能精准地找到蛋白质分子链上的氨基?</h3>
<p>“精准”这两个字,听起来像是一个有思想、有目标的动作,但实际上是一场由分子结构和电荷分布主导的化学反应。</p>
<p>甲醛分子(HCHO)的核心是一个<strong>羰基(C=O)</strong>。在这个结构中,氧原子(O)是一个电负性非常强的元素,它对电子有着极强的吸引力。它会大力地将碳氧双键中的共用电子云拉向自己。这种电子云的偏移,导致了与它相连的碳原子(C)周围的电子变得非常稀薄,从而带上了部分正电荷(δ+)。</p>
<p>在化学上,这样一个“缺少电子”、对电子充满渴望的原子或基团,被称为“亲电体”(Electrophile)。</p>
<p>氨基(-NH₂)在蛋白质中广泛存在,尤其是在赖氨酸、精氨酸等氨基酸的侧链上。氨基的核心是氮原子(N)。根据它的电子排布,氮原子的最外层有一对没有参与形成化学键的电子,这叫做 “孤对电子”(Lone Pair Electrons)。 这对孤对电子,使得氮原子成了一个电子云密度很高的区域,带上了<strong>部分负电荷(δ-)</strong>。</p>
<p>这样一个拥有“富余”电子、并愿意用它去攻击其他正电中心的原子或基团,在化学上被称为 “亲核体”(Nucleophile)。</p>
<p>当甲醛分子靠近蛋白质时,甲醛分子上带部分正电荷的碳原子(亲电体),与蛋白质氨基上带部分负电荷的氮原子(亲核体),会因基本的静电引力而相互靠近。一旦距离足够近,氨基上慷慨的“孤对电子”会毫不犹豫地“攻击”甲醛上那个渴望电子的碳原子,并与它形成一个新的化学键。</p>
<p>这个过程,就是化学中的“亲核加成反应”。</p>
<h3 id="这个反应为何会损伤黏膜组织?"><a class="header-anchor" href="#这个反应为何会损伤黏膜组织?">¶</a>这个反应为何会损伤黏膜组织?</h3>
<p>蛋白质被甲醛“交联” 这个过程在化学上称为 “蛋白质变性”,它直接导致了组织损伤。</p>
<p>首先黏膜细胞需要各种酶(本身也是蛋白质)来维持新陈代谢。被交联后,这些酶的结构被破坏,失去了活性,细胞的正常生理活动瞬间停滞。而细胞的骨架、细胞膜上的离子通道等结构蛋白,同样会被交联破坏。这导致细胞失去原有的形态,细胞膜的通透性被改变,甚至直接破裂。当大量的细胞功能瘫痪、结构崩解后,它们会迅速走向死亡。成片的细胞死亡,就意味着我们宏观可见的<strong>组织损伤</strong>。这本质上是一种<strong>微观的化学性灼伤</strong>。</p>
<p>这也是为什么福尔马林(甲醛水溶液)能用来制作标本——它通过“交联”反应,将生物组织的所有蛋白质“固化”,使其永不腐败,但这对于活体组织而言,就是彻头彻尾的毁灭。</p>
<h3 id="为何神经末梢会受到强烈刺激?"><a class="header-anchor" href="#为何神经末梢会受到强烈刺激?">¶</a>为何神经末梢会受到强烈刺激?</h3>
<p>我们的黏膜组织下,密布着丰富的神经末梢。</p>
<p>完好的黏膜上皮细胞是神经末梢的保护层。当这层保护被甲醛破坏,下方的神经末梢就直接暴露了出来。此时,高活性的甲醛分子可以直接攻击这些裸露的神经末梢,就像用化学品直接触碰神经一样,瞬间产生剧烈的刺激信号。</p>
<p>被损伤和死亡的细胞,在“弥留之际”会释放出大量的化学信号物质,如前列腺素、缓激肽、组胺等。这些物质被称为“炎症介质”。而这些炎症介质本身,就是神经末梢上痛觉感受器的强效激活剂。它们会放大并持续地刺激神经,导致灼痛、瘙痒和刺痛感。</p>
<h3 id="什么是活性氧?"><a class="header-anchor" href="#什么是活性氧?">¶</a>什么是活性氧?</h3>
<p><strong>活性氧(Reactive Oxygen Species, ROS)</strong> 是一类含氧的、化学性质极其活泼的分子或自由基的总称。常见的活性氧有 超氧阴离子(O₂⁻•)、羟自由基(•OH)、过氧化氢(H₂O₂)等。它们的特点之一是其都带有“未配对的电子”,这使得它们在化学上极不稳定,迫切地想要从其他分子那里“抢夺”电子来让自己稳定下来。</p>
<p>在正常情况下,我们身体会产生少量活性氧。它们是重要的信号分子,参与调节细胞生长、分化,并且是免疫细胞(如巨噬细胞)用来消灭入侵病原体(细菌、病毒)的“化学武器”。</p>
<p>一旦活性氧的产生<strong>远远超过</strong>了细胞自身的清除能力(细胞内有超氧化物歧化酶SOD等“警察”来控制它们),就会形成“<strong>氧化应激</strong>”(Oxidative Stress)状态。这些失控的“小流氓”就会在细胞内肆意破坏,攻击一切它们遇到的分子。</p>
<h3 id="甲醛如何诱导线粒体产生大量活性氧?"><a class="header-anchor" href="#甲醛如何诱导线粒体产生大量活性氧?">¶</a>甲醛如何诱导线粒体产生大量活性氧?</h3>
<p>线粒体是通过一个叫做“电子传递链”的复杂过程来产生能量(ATP)的。就像一条生产线,电子(e⁻)在这条生产线上被一步步传递,最终与我们吸入的氧气(O₂)结合生成水(H₂O),同时释放出大量能量。</p>
<p>而甲醛及其代谢产物(如甲酸)会直接损伤电子传递链上的复合体蛋白(特别是复合体I和III)。这就像是破坏了生产线上的关键机器。</p>
<p>当生产线上的机器受损或堵塞时,本该被有序传递的电子就会从传递链上“泄漏”出来。</p>
<p>这些泄漏出来的高能电子,会直接传递给旁边无辜的氧气分子(O₂)。正常的氧气分子得到一个额外的电子后,就成为了极不稳定的<strong>超氧阴离子(O₂⁻•)</strong> (活性氧)。</p>
<h3 id="为什么活性氧能破坏细胞膜、蛋白质和DNA?"><a class="header-anchor" href="#为什么活性氧能破坏细胞膜、蛋白质和DNA?">¶</a>为什么活性氧能破坏细胞膜、蛋白质和DNA?</h3>
<p>失控的活性氧会疯狂地从周围的生物大分子上抢夺电子,这个过程会导致这些大分子的化学结构被破坏,失去原有功能。</p>
<p>细胞膜主要由<strong>磷脂双分子层</strong>构成,其中含有大量的<strong>不饱和脂肪酸</strong>。活性氧(特别是羟自由基•OH)会攻击不饱和脂肪酸中的双键,抢走一个电子,引发一个叫做“脂质过氧化”的链式反应。脂质过氧化会破坏细胞膜的流动性和完整性,使其变得僵硬、脆裂,最终导致细胞膜破裂,细胞内容物泄漏。</p>
<p>而活性氧会攻击蛋白质侧链上的氨基酸(特别是半胱氨酸、甲硫氨酸等),使其氧化、交联(和甲醛的作用类似但原理不同),导致蛋白质变性,失去作为酶、结构蛋白或信号分子的功能。</p>
<p>活性氧还会攻击DNA碱基(特别是鸟嘌呤G)和脱氧核糖骨架,导致<strong>碱基修饰、DNA单链或双链断裂</strong>。这会直接导致基因突变,干扰DNA的复制和转录,是诱发癌症的重要原因。</p>
<h3 id="什么是细胞的程序性死亡?"><a class="header-anchor" href="#什么是细胞的程序性死亡?">¶</a>什么是细胞的程序性死亡?</h3>
<p><strong>细胞程序性死亡(Programmed Cell Death)</strong>,最主要的形式是<strong>细胞凋亡(Apoptosis)</strong>。这并非细胞因损伤而发生的被动解体(那是“坏死”),而是细胞在接收到特定信号后,主动启动的一套基因编码的“自杀程序”。目的是清除体内不再需要、衰老或受到严重损伤(如无法修复的DNA损伤)的细胞,从而维持组织器官的稳定和健康。这是一个对机体有利的主动行为。</p>
<p>在这个过程中每,细胞会主动收缩、染色质固缩、细胞核碎裂,最终形成多个被完整细胞膜包裹的“凋亡小体”,等待被免疫细胞吞噬清理。整个过程非常“干净”,不会引发周围的炎症。</p>
<p>在甲醛诱导的氧化应激中,由活性氧造成的大量、无法修复的DNA损伤和蛋白质损伤,本身就是最强烈的“死亡信号”。这个信号会激活细胞内一类叫做Bax和Bak的“促凋亡蛋白”。被激活的Bax/Bak蛋白会聚集到线粒体外膜上,像一个“打孔器”一样,在线粒体外膜上形成孔道。线粒体外膜被打孔后,原本储存在线粒体内部的一种关键蛋白——<strong>细胞色素C(Cytochrome C)</strong>,就会被释放到细胞质中。释放到细胞质中的细胞色素C,会激活一系列被称为 <strong>Caspase</strong> 的蛋白酶。它们一旦被激活,就会形成一个级联反应,像多米诺骨牌一样,一个激活下一个。被激活的Caspase会系统性地降解细胞内的关键蛋白质和DNA,最终导致细胞有序地解体,完成凋亡。</p>
<p><strong>“激活”在这里指的是一个剧烈的“构象变化”(Conformational Change)</strong>,即蛋白质从一种三维折叠形态,转变为另一种形态。</p>
<h4 id="什么是Bax-Bak蛋白?"><a class="header-anchor" href="#什么是Bax-Bak蛋白?">¶</a>什么是Bax/Bak蛋白?</h4>
<p><strong>Bax和Bak</strong>是细胞内一个庞大家族——<strong>Bcl-2蛋白家族</strong>中的两个关键成员。Bax和Bak是在这里具有代表性的促凋亡派(Pro-apoptotic),Bcl-2、Bcl-xL 是 <strong>抗凋亡派(Anti-apoptotic):</strong></p>
<p>在正常健康的细胞中,它们处于一种<strong>无害的、折叠起来的休眠状态</strong>。<strong>Bax</strong>大部分时间游荡在细胞质中。<strong>Bak</strong> 则停靠在线粒体的外膜上,但同样处于被抑制的休眠状态。</p>
<p>当细胞遭受了大量、无法修复的DNA或蛋白质损伤时,这个信号会被细胞内的 <strong>p53蛋白</strong> 感知到。被激活的p53蛋白,会生产大量的一类被称为 “仅含BH3结构域蛋白”(BH3-only proteins)的小蛋白,例如 PUMA 和 Noxa。</p>
<h4 id="Bax-Bak-蛋白如何打孔?"><a class="header-anchor" href="#Bax-Bak-蛋白如何打孔?">¶</a>Bax/Bak 蛋白如何打孔?</h4>
<p>当Bax被激活后,它会暴露内部的<strong>疏水结构域</strong>。这个结构域极度“讨厌”水(细胞质的主要成分是水),而非常“喜欢”油性的环境。线粒体外膜正是一个由磷脂双分子层构成的“油性”环境。 因此,被激活的Bax会像一滴油会自动融入另一滴油一样,<strong>自发地从细胞质中转移并插入到线粒体外膜上</strong>。而已在膜上的Bak,被激活后则会更深、更稳定地嵌入膜中。</p>
<p>这是一个寡聚化(Oligomerization)过程。多个被激活的Bax/Bak分子在膜上相遇,会像乐高积木一样,通过彼此暴露出来的结构域互相识别并拼接在一起,形成大小不一的聚合体(Oligomer),有的像线形,有的像弧形,有的像不完整的环。这些Bax/Bak聚合体并<strong>不是</strong>贯穿膜形成一个隧道。相反,它们像楔子一样,大量地、浅浅地插入到线粒体外膜的<strong>外层</strong>。当越来越多的蛋白质“楔子”挤进这一层薄薄的膜时,会产生巨大的<strong>物理张力和弯曲应力</strong>。</p>
<p>最终,线粒体外膜因为无法承受这种内部应力,就像一个被过度拉伸的气球一样被撕裂,形成的“孔道”或“裂口”,其边缘是由蛋白质和脂质分子共同构成的,被称为 “蛋白-脂质孔道”(Proteolipidic Pore)。</p>
<h4 id="为什么“严重损伤”会激活p53蛋白?"><a class="header-anchor" href="#为什么“严重损伤”会激活p53蛋白?">¶</a>为什么“严重损伤”会激活p53蛋白?</h4>
<p>在正常、安逸的细胞中,p53蛋白一被生产出来,就会被一个叫做 <strong>MDM2</strong> 的蛋白“盯上”。MDM2会给p53贴上一个叫做 “泛素”(Ubiquitin)的标签,被贴上标签的p53会被送到细胞的“垃圾处理厂”——<strong>蛋白酶体</strong>中迅速降解。因此,正常细胞里的p53蛋白水平极低。</p>
<p>当 “严重损伤”<strong>,尤其是</strong>DNA双链断裂(这被视为细胞最危急的损伤之一)发生时,DNA断裂处会立刻吸引并激活一批蛋白,其中最关键的是一类激酶(Kinase),比如<strong>ATM</strong>和<strong>ATR</strong>。激酶是一种能给其他蛋白质“安装”上磷酸基团(-PO₃²⁻)的工具酶。</p>
<p>被激活的ATM/ATR激酶,会立刻找到 MDM2,并给它装上好几个磷酸基团。被磷酸化后的MDM2结构发生改变,无法再识别和结合p53。 与此同时,ATM/ATR 等激酶也会给 p53 蛋白本身装上磷酸基团。这个过程不仅让p53无法被MDM2识别,更重要的是,它改变了p53的构象。</p>
<p>对于p53来说,最基础的激活,就是摆脱 MDM2 的控制,不再被降解。这使得 p53 蛋白可以在细胞核内大量、稳定地积累起来,被磷酸化等一系列化学修饰后,p53蛋白的三维结构会发生精妙的变化。这种变化会暴露p53内部一个关键的功能区——<strong>“DNA结合域”</strong>。同时,它会促使四个p53蛋白分子结合在一起,形成一个<strong>四聚体(Tetramer)</strong>。</p>
<h5 id="为什么-MDM2-会盯上-p53?"><a class="header-anchor" href="#为什么-MDM2-会盯上-p53?">¶</a>为什么 MDM2 会盯上 p53?</h5>
<p>在MDM2蛋白的N端(头部),有一个精心构造的、深邃的<strong>疏水性口袋(hydrophobic pocket)</strong>,就像手套一样。而在p53蛋白的N端(反式激活域),有一段α-螺旋区域。这段螺旋上有三个关键的氨基酸残基——<strong>苯丙氨酸(Phe19)、色氨酸(Trp23)和亮氨酸(Leu26)</strong>。这三个氨基酸像三根关键的“手指”,完美地伸入并契合MDM2的那个手套中。</p>
<h5 id="p53被激活后如何“下令”生产PUMA等蛋白?"><a class="header-anchor" href="#p53被激活后如何“下令”生产PUMA等蛋白?">¶</a>p53被激活后如何“下令”生产PUMA等蛋白?</h5>
<p>PUMA这个蛋白,它的“设计图纸”编码在一段叫做 <em>PUMA</em>(也称 <em>BBC3</em>)的基因里。在这段基因的上游,有一段特殊的DNA序列,叫做 “启动子”(Promoter)。而PUMA基因的启动子上,有 p53 蛋白能够识别并结合的特定“停泊位点”,称为 “p53响应元件”(p53 Response Element)。</p>
<p>被激活的p53四聚体,凭借其暴露出来的DNA结合域,会精准地找到并牢牢地结合在PUMA基因的这个“停泊位点”上。一旦结合,p53 自身就会像一块磁铁,吸引来细胞核内负责“抄写”基因的庞大机器——<strong>RNA聚合酶II</strong>以及其他大量的辅助蛋白。这个庞大的蛋白质复合体,就是负责执行命令的“抄写员”。</p>
<p>“抄写员”以DNA为模板,“抄写”出一段信使RNA(mRNA)。这段mRNA会离开细胞核进入细胞质,并被核糖体(蛋白质的“生产工厂”)读取,最终翻译、生产出我们需要的<strong>PUMA蛋白</strong>。</p>
<p>这就是基因转录。</p>
<h4 id="为什么-DNA-的断裂会吸引激酶?"><a class="header-anchor" href="#为什么-DNA-的断裂会吸引激酶?">¶</a>为什么 DNA 的断裂会吸引激酶?</h4>
<p>在正常细胞中,ATM是以无活性的二聚体(两个ATM蛋白抱在一起)形式存在的。这个“拥抱”的姿态,恰好就掩盖了彼此的“催化核心区”。 激活,就是让这俩分开。</p>
<p>当DNA发生双链断裂时,会暴露出两个<strong>DNA断端</strong>。这在正常的、连续的DNA双螺旋结构中是绝对不会出现的。这个裸露的、不连续的断端,就是一个<strong>强烈的物理异常信号</strong>。</p>
<p>细胞内有一组蛋白质,叫做<strong>MRN复合体</strong>(由Mre11、Rad50、Nbs1三个蛋白组成)。</p>
<p><strong>Mre11和Rad50</strong> 能特异性地识别并抓住DNA的断端,像桥梁一样将两个断端物理性地拉近,防止它们飘远。MRN复合体一旦抓住了DNA断端,它自身的构象就会发生改变。这种改变使得它能够吸引在细胞核内游走的、处于休眠二聚体状态的ATM。MRN复合体(特别是其中的Nbs1蛋白)上会暴露出一个能与ATM二聚体结合的“接口”。这种分子间的特异性亲和力,使得ATM被“吸引”并<strong>物理性地富集</strong>到DNA断裂的地方。</p>
<p>当大量的ATM二聚体被招募到MRN复合体标记的DNA断端附近时,DNA断端的物理存在,加上MRN复合体的“撬动”作用,会诱导ATM二聚体的构象发生变化,促使这俩解离,变成两个独立的单体。</p>
<p>解离后的ATM单体,其被隐藏的“催化核心区”立刻暴露了出来。一个ATM单体会立刻给另一个(或自己)的特定位点(如丝氨酸1981位点)“安装”上一个磷酸基团。这是个 “自我磷酸化” 的过程,会使其构象进一步稳定在完全激活的状态。</p>
<h1>先到这儿吧,我要吃炒粉了</h1>
]]></content>
<tags>
<tag>Medicine</tag>
</tags>
</entry>
<entry>
<title>肠易激综合征 (IBS) 的病理学与药物治疗</title>
<url>/2025/06/30/ibs/</url>
<content><![CDATA[<p>肠易激综合征 (Irritable Bowel Syndrome, IBS) 是一种常见的功能性胃肠病。其核心特征是在没有发现任何器质性病变(如炎症、溃疡或肿瘤)的情况下,患者长期经历腹痛、腹胀,并伴有排便习惯的改变(腹泻、便秘或两者交替)。</p>
<h3 id="一、-肠易激综合征的病理生理学"><a class="header-anchor" href="#一、-肠易激综合征的病理生理学">¶</a>一、 肠易激综合征的病理生理学</h3>
<p>IBS 的病理生理机制非常复杂,并非由单一原因导致,而是多种因素相互作用的结果。目前的医学研究认为,其主要与以下几个核心机制有关:</p>
<h4 id="1-脑-肠轴功能紊乱-Disorders-of-the-Brain-Gut-Axis"><a class="header-anchor" href="#1-脑-肠轴功能紊乱-Disorders-of-the-Brain-Gut-Axis">¶</a>1. 脑-肠轴功能紊乱 (Disorders of the Brain-Gut Axis)</h4>
<p>这是 IBS 最核心的病理机制。大脑和肠道通过神经、内分泌和免疫系统形成一个双向沟通的“脑-肠轴”。在 IBS 患者中,这个轴的功能出现紊乱。</p>
<ul>
<li><strong>自上而下:</strong> 心理压力、焦虑、抑郁等负面情绪可以直接通过大脑影响肠道的运动、感觉和分泌功能,从而诱发或加重症状。</li>
<li><strong>自下而上:</strong> 肠道内的信号(如食物、气体、肠道菌群代谢产物)被过度放大后传给大脑,导致大脑对这些正常刺激产生过度反应,表现为疼痛和不适。</li>
</ul>
<h4 id="2-内脏高敏感性-Visceral-Hypersensitivity"><a class="header-anchor" href="#2-内脏高敏感性-Visceral-Hypersensitivity">¶</a>2. 内脏高敏感性 (Visceral Hypersensitivity)</h4>
<p>这是指 IBS 患者的肠道对各种刺激(如食物、气体、肠壁的牵拉)异常敏感。正常人可以耐受的肠道内压力或活动,在 IBS 患者身上却会引发强烈的腹痛、急便感或不适。这种“放大”的感觉是 IBS 腹痛的主要原因。</p>
<h4 id="3-胃肠动力异常-Altered-Gut-Motility"><a class="header-anchor" href="#3-胃肠动力异常-Altered-Gut-Motility">¶</a>3. 胃肠动力异常 (Altered Gut Motility)</h4>
<p>肠道肌肉的收缩和舒张节律出现问题,导致食物和粪便在肠道内的转运速度异常。</p>
<ul>
<li><strong>转运过快:</strong> 肠道内容物过快通过结肠,水分来不及被充分吸收,导致腹泻(IBS-D)。</li>
<li><strong>转运过慢:</strong> 肠道蠕动乏力,粪便在结肠内停留时间过长,水分被过度吸收,导致便秘(IBS-C)。</li>
<li><strong>节律紊乱:</strong> 有时快有时慢,导致腹泻与便秘交替出现(IBS-M)。</li>
</ul>
<h4 id="4-肠道微生态失调-Alteration-in-Gut-Microbiota"><a class="header-anchor" href="#4-肠道微生态失调-Alteration-in-Gut-Microbiota">¶</a>4. 肠道微生态失调 (Alteration in Gut Microbiota)</h4>
<p>IBS 患者的肠道菌群组成和功能与健康人存在差异。有害菌可能增多,有益菌(如双歧杆菌、乳酸杆菌)可能减少。这些失调的菌群会影响肠道屏障功能、产生过多气体、并激活免疫系统,导致腹胀和腹泻。</p>
<h4 id="5-肠道低度炎症与免疫激活-Low-Grade-Inflammation-and-Immune-Activation"><a class="header-anchor" href="#5-肠道低度炎症与免疫激活-Low-Grade-Inflammation-and-Immune-Activation">¶</a>5. 肠道低度炎症与免疫激活 (Low-Grade Inflammation and Immune Activation)</h4>
<p>部分 IBS 患者的肠道黏膜中可以观察到轻微的炎症反应,例如肥大细胞等免疫细胞数量增多并被激活。这些细胞会释放组胺、5-羟色胺等物质,直接刺激肠道神经,引起疼痛和动力异常。这种情况尤其在感染性肠炎后发生的 IBS(PI-IBS)中更为常见。</p>
<h4 id="6-遗传与饮食因素"><a class="header-anchor" href="#6-遗传与饮食因素">¶</a>6. 遗传与饮食因素</h4>
<ul>
<li><strong>遗传:</strong> IBS具有一定的家族聚集性,提示可能存在遗传易感性。</li>
<li><strong>饮食:</strong> 某些食物,特别是含有高<strong>FODMAPs</strong>(可发酵的寡糖、双糖、单糖和多元醇)的食物,在小肠内难以被吸收,进入大肠后被细菌快速发酵,产生大量气体并增加肠道渗透压,从而诱发腹胀、腹痛和腹泻。</li>
</ul>
<h3 id="二、-肠易激综合征的药物治疗"><a class="header-anchor" href="#二、-肠易激综合征的药物治疗">¶</a>二、 肠易激综合征的药物治疗</h3>
<blockquote>
<p>IBS的治疗需要个体化,所有药物都应在医生指导下使用。医生应该根据具体的 IBS 亚型(腹泻型、便秘型、混合型)和主要症状来选择最合适的药物。</p>
</blockquote>
<h4 id="针对主要症状的药物"><a class="header-anchor" href="#针对主要症状的药物">¶</a>针对主要症状的药物</h4>
<p><strong>1. 针对腹痛和腹胀(解痉药)</strong></p>
<p>这类药物通过放松肠道平滑肌,缓解肠道痉挛来减轻疼痛。</p>
<ul>
<li><strong>匹维溴铵 (Pinaverium Bromide):</strong> 作用于肠道平滑肌的钙离子通道,选择性高,副作用较小。</li>
<li><strong>奥替溴铵 (Otilonium Bromide):</strong> 与匹维溴铵类似,对结肠有较高的亲和力。</li>
<li><strong>马来酸曲美布汀 (Trimebutine Maleate):</strong> 具有双向调节作用,在肠道动力过快时抑制它,过慢时促进它,对腹痛、腹泻和便秘都有一定效果。</li>
<li><strong>薄荷油 (Peppermint Oil):</strong> 是一种天然的解痉剂,其肠溶胶囊可以有效缓解腹痛和腹胀。</li>
</ul>
<p><strong>2. 针对腹泻 (IBS-D)</strong></p>
<ul>
<li><strong>洛哌丁胺 (Loperamide):</strong> 是一种止泻药,能减慢肠道蠕动,增加水分吸收。适用于急性腹泻发作,但不宜长期连续使用。</li>
<li><strong>蒙脱石散 (Montmorillonite Powder):</strong> 是一种肠道黏膜保护剂,能吸附肠道内的病菌和毒素,并覆盖在肠壁上形成保护层。</li>
<li><strong>利福昔明 (Rifaximin):</strong> 一种几乎不被肠道吸收的抗生素,可以调节肠道菌群,减少有害菌和产气,对改善腹泻和腹胀有良好效果。</li>
<li><strong>5-HT₃受体拮抗剂(如阿洛司琼 Alosetron):</strong> 能显著减缓肠道转运、减轻内脏敏感性。因可能引起严重便秘和缺血性结肠炎等副作用,使用受到严格限制,通常仅用于其他治疗无效的重度女性IBS-D患者。</li>
<li><strong>胆汁酸螯合剂(如考来烯胺 Cholestyramine):</strong> 适用于因胆汁酸吸收不良导致的腹泻型IBS。</li>
</ul>
<p><strong>3. 针对便秘 (IBS-C)</strong></p>
<ul>
<li><strong>容积性泻药(如欧车前、聚卡波非钙):</strong> 通过吸收水分增加粪便体积,刺激肠道蠕动。使用时需饮用足量水,否则可能加重便秘。</li>
<li><strong>渗透性泻药(如聚乙二醇 PEG、乳果糖):</strong> 通过在肠道内形成高渗环境,将水分保留在肠腔内,软化粪便,促进排便。相对安全,适合长期使用。</li>
<li><strong>促分泌药:</strong>
<ul>
<li><strong>鲁比前列酮 (Lubiprostone):</strong> 激活肠道氯离子通道,增加肠液分泌,软化粪便。</li>
<li><strong>利那洛肽 (Linaclotide)、普卡那肽 (Plecanatide):</strong> 激活鸟苷酸环化酶-C,促进肠液分泌,并能一定程度上缓解腹痛。</li>
</ul>
</li>
</ul>
<p><strong>4. 调节脑-肠轴功能的药物(精神类药物)</strong></p>
<p>对于伴有明显焦虑、抑郁或常规治疗无效的顽固性腹痛患者,医生可能会考虑使用低剂量的抗抑郁药。</p>
<ul>
<li><strong>三环类抗抑郁药 (TCAs)(如阿米替林):</strong> 低剂量使用可以有效缓解IBS的慢性腹痛,其抗胆碱能作用也有助于控制腹泻。</li>
<li><strong>选择性5-羟色胺再摄取抑制剂 (SSRIs)(如氟西汀、帕罗西汀):</strong> 可用于伴有便秘和焦虑的IBS患者。</li>
</ul>
<ol start="5">
<li>益生菌 (Probiotics)</li>
</ol>
<p>补充特定的益生菌菌株(如某些双歧杆菌和乳酸杆菌)有助于恢复肠道微生态平衡,改善腹胀和总体症状。不同菌株效果各异,需根据产品说明和医生建议选用。</p>
]]></content>
<tags>
<tag>Medicine</tag>
</tags>
</entry>
<entry>
<title>Linux 下 AMD 显卡屏幕画面闪烁问题修复方案</title>
<url>/2025/04/20/linux-amd-screen-boom/</url>
<content><![CDATA[<p>不知道我们所说的闪烁是不是一个东西,在我的笔记本上,闪烁是指偶发的屏幕中出现部分彩色雪花。</p>
<p>我使用 Pop!_OS,此发行版使用 EFI 启动,故而可以在 <code>/boot/efi/loader/loader.conf</code> 中加上:</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">options quiet splash amdgpu.dcdebugmask=0x10 amdgpu.sg_display=0</span><br></pre></td></tr></table></figure>
<p>对于其他使用 EFI 或 GRUB 启动的发行版也可以添加类似的参数尝试尝试。</p>
]]></content>
<tags>
<tag>Technique</tag>
</tags>
</entry>
<entry>
<title>NestJS bullmq 邮件发送业务中的小 tips</title>
<url>/2025/05/07/nestjs-bullmq-mail-business/</url>
<content><![CDATA[<p>在 BullMQ(以及它在 NestJS 里包装的 <code>@Processor</code>/<code>WorkerHost</code>)里,整个生命周期大致是这样的:</p>
<ol>
<li>队列(在 NestJS 里由 <code>@Processor</code> 装饰的类)会被一个底层的 <code>Worker</code> 订阅。</li>
<li>有新任务(job)进来时,Worker 会调用写在该类里的 <code>async process(job: Job)</code> 方法。</li>
<li>如果 <code>process()</code> 正常返回(即没有抛异常),Job 就被标记为 completed,然后才会去触发所有注册了 <code>@OnWorkerEvent('completed')</code> 的回调。</li>
</ol>
<p>也就是说:</p>
<ul>
<li><code>process</code>:是真正“干活”的地方,收到 job 之后立刻被调用,任何主业务逻辑(发邮件/写数据库/第三方请求等)都应该放这里。</li>
<li><code>onCompleted</code>:只是一个事件监听器,在 job 已经成功完成之后 才会被触发,不会影响 job 的重试逻辑(也就是说,在这里抛错,job 已经算完成了,也不会重试)。</li>
</ul>
<p>而我在此处的业务目的是 “用队列来做可靠的、可重试的邮件发送”,那么一定要把发送邮件的逻辑写到 <code>process()</code> 里,这样在 <code>commandBus.execute(new SendMailCommand(...))</code> 抛错时,BullMQ 会根据创建 JOB 时的重试策略(retry、backoff 等)自动重新入队。而把它放到 <code>onCompleted()</code>,只相当于 job 成功完成后的“事后通知”,一旦失败不会再重试,也无法利用 BullMQ 的锁、超时、重试机制。</p>
<p>举个最简化的调整示例,删掉 <code>onCompleted</code>,把真正的发信放到 <code>process</code>:</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">// ... existing imports ...</span><br><span class="line"></span><br><span class="line">@Processor(process.env.MAILER_QUEUE_NAME || "gcpm-mailer")</span><br><span class="line">export class BullMQMailerProcesser extends WorkerHost {</span><br><span class="line"> constructor(</span><br><span class="line"> private readonly commandBus: CommandBus,</span><br><span class="line"> private readonly logger: LoggingService,</span><br><span class="line"> ) {</span><br><span class="line"> super();</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> // ① 当有新 job 拉取到时,这个方法会被调用</span><br><span class="line"> public async process(job: Job): Promise<void> {</span><br><span class="line"> const mailAggregate = new Mail(job.data.mail);</span><br><span class="line"> try {</span><br><span class="line"> await this.commandBus.execute(new SendMailCommand(mailAggregate));</span><br><span class="line"> } catch (err) {</span><br><span class="line"> this.logger.error(`邮件发送失败,jobId=${job.id}`, err);</span><br><span class="line"> // 抛出错误,触发重试或失败</span><br><span class="line"> throw err;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> // ② onCompleted 仅在 process() 正常返回后触发,</span><br><span class="line"> // 不建议在这里执行核心业务(也无法触发重试)。</span><br><span class="line"> // @OnWorkerEvent("completed")</span><br><span class="line"> // async onCompleted(job: Job) { … }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>参考 BullMQ 官方文档:</p>
<ul>
<li>“Workers → Sandboxed processors”:Worker 拉到 job 就调用注册的处理函数,然后根据返回/抛错把 job 标记成 completed 或 failed。</li>
<li>“Events → OnJobCompleted”:completed 事件只是一个监听钩子,不会参与重试。</li>
</ul>
<hr>
<p>而 重试次数本身并没有一个硬性上限,完全由添加 Job 时通过 <code>attempts</code> 这个选项来控制:</p>
<ul>
<li>默认情况下,如果不传 <code>attempts</code>(或不在 <code>defaultJobOptions</code> 里配置),Job 不会自动重试(相当于 <code>attempts = 0</code>)。</li>
<li>如果在 <code>queue.add()</code>(或全局 <code>defaultJobOptions</code>)里设置了 <code>attempts: N</code>,那么 BullMQ 最多会让该 Job 运行 N 次(也就是初始执行 + N−1 次重试,或者根据文档含义最多触发 N 次失败) ,失败后才算真正移入失败集合。</li>
<li><code>attempts</code> 可以是任意的正整数(受 JavaScript <code>Number</code> 范围限制),BullMQ 本身不会再做额外的上限检查。</li>
</ul>
<p>示例(给某封邮件最多重试 3 次):</p>
<figure class="highlight ts"><table><tr><td class="code"><pre><span class="line"><span class="keyword">await</span> <span class="variable language_">this</span>.<span class="property">mailerQueue</span>.<span class="title function_">add</span>(</span><br><span class="line"> id,</span><br><span class="line"> { <span class="attr">mail</span>: <span class="keyword">new</span> <span class="title class_">Mail</span>(<span class="comment">/*…*/</span> ) },</span><br><span class="line"> {</span><br><span class="line"> <span class="attr">attempts</span>: <span class="number">3</span>, <span class="comment">// 最多尝试 3 次</span></span><br><span class="line"> <span class="attr">backoff</span>: { <span class="comment">// 重试时的延迟策略(可选)</span></span><br><span class="line"> <span class="attr">type</span>: <span class="string">'exponential'</span>,</span><br><span class="line"> <span class="attr">delay</span>: <span class="number">1000</span>,</span><br><span class="line"> },</span><br><span class="line"> },</span><br><span class="line">);</span><br></pre></td></tr></table></figure>
<hr>
<p>还有一个需要注意的地方,在我的业务中,邮件发送的是一种时间区间报告,这个报告包含了过去二十四小时的一些系统中的事件,但如果重试有延迟策略或重试本身就有计算成本的话,这封邮件就不是 “过去二十四小时” 的了,因为重试带来了一个真空期。</p>
<p>换言之,这个问题本质上是——重试导致「发送时刻」与「原始 24 小时窗口」错开,从而让邮件里报出来的数据不再精确。常见的解决思路就是:把「窗口定义」或者「报表内容」在调度时就固化下来,真正的队列任务只负责发送,而不再实时去重新计算时间区间。</p>
<p>我想到了两种解决方案:</p>
<p>一、任务参数里带上「时间区间」<br>
在 enqueue 的时候,就算出 windowStart/windowEnd,然后把它放到 <code>job.data</code> 里。无论后面 <code>process</code> 什么时候真正跑,都是基于同一个时间区间去查询:</p>
<figure class="highlight ts"><table><tr><td class="code"><pre><span class="line"><span class="comment">// 调度时</span></span><br><span class="line"><span class="keyword">const</span> now = <span class="keyword">new</span> <span class="title class_">Date</span>();</span><br><span class="line"><span class="keyword">const</span> windowStart = <span class="keyword">new</span> <span class="title class_">Date</span>(now.<span class="title function_">getTime</span>() - <span class="number">24</span> * <span class="number">60</span> * <span class="number">60</span> * <span class="number">1000</span>);</span><br><span class="line"><span class="keyword">await</span> <span class="variable language_">this</span>.<span class="property">mailerQueue</span>.<span class="title function_">add</span>(</span><br><span class="line"> id,</span><br><span class="line"> {</span><br><span class="line"> <span class="attr">mail</span>: <span class="keyword">new</span> <span class="title class_">Mail</span>({</span><br><span class="line"> ...options,</span><br><span class="line"> id,</span><br><span class="line"> <span class="attr">sentAt</span>: now,</span><br><span class="line"> <span class="attr">status</span>: <span class="title class_">MailStatus</span>.<span class="property">PENDING</span>,</span><br><span class="line"> windowStart,</span><br><span class="line"> <span class="attr">windowEnd</span>: now,</span><br><span class="line"> }),</span><br><span class="line"> },</span><br><span class="line"> {</span><br><span class="line"> <span class="attr">attempts</span>: <span class="number">3</span>,</span><br><span class="line"> <span class="attr">backoff</span>: { <span class="attr">type</span>: <span class="string">'exponential'</span>, <span class="attr">delay</span>: <span class="number">1000</span> },</span><br><span class="line"> },</span><br><span class="line">);</span><br><span class="line"></span><br><span class="line"><span class="comment">// process 里</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">async</span> <span class="title function_">process</span>(<span class="params"><span class="attr">job</span>: <span class="title class_">Job</span></span>) {</span><br><span class="line"> <span class="keyword">const</span> { windowStart, windowEnd } = job.<span class="property">data</span>.<span class="property">mail</span>;</span><br><span class="line"> <span class="comment">// ① 只查询 [windowStart, windowEnd] 的事件</span></span><br><span class="line"> <span class="keyword">const</span> events = <span class="keyword">await</span> <span class="variable language_">this</span>.<span class="property">reportService</span>.<span class="title function_">findEvents</span>(windowStart, windowEnd);</span><br><span class="line"> <span class="keyword">const</span> reportHtml = <span class="keyword">await</span> <span class="variable language_">this</span>.<span class="property">reportService</span>.<span class="title function_">renderReport</span>(events);</span><br><span class="line"> <span class="keyword">await</span> <span class="variable language_">this</span>.<span class="property">commandBus</span>.<span class="title function_">execute</span>(<span class="keyword">new</span> <span class="title class_">SendMailCommand</span>(job.<span class="property">data</span>.<span class="property">mail</span>, reportHtml));</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>➜ 这样无是马上执行还是几次重试后才执行,数据规则都不会变。</p>
<p>二、预先生成「静态报表内容」,挂到队列里<br>
如果计算成本很高,或者怕重复查询数据开销大,也可以在调度时就把最终的 HTML/Text/附件 都先打好,然后作为 <code>job.data</code> 传进去,真正的 <code>process()</code> 只做一次“发送”即可:</p>
<figure class="highlight ts"><table><tr><td class="code"><pre><span class="line"><span class="comment">// 调度时:先生成报告</span></span><br><span class="line"><span class="keyword">const</span> now = <span class="keyword">new</span> <span class="title class_">Date</span>();</span><br><span class="line"><span class="keyword">const</span> windowStart = <span class="keyword">new</span> <span class="title class_">Date</span>(now.<span class="title function_">getTime</span>() - <span class="number">24</span>*<span class="number">3600</span>*<span class="number">1000</span>);</span><br><span class="line"><span class="keyword">const</span> events = <span class="keyword">await</span> <span class="variable language_">this</span>.<span class="property">reportService</span>.<span class="title function_">findEvents</span>(windowStart, now);</span><br><span class="line"><span class="keyword">const</span> reportHtml = <span class="keyword">await</span> <span class="variable language_">this</span>.<span class="property">reportService</span>.<span class="title function_">renderReport</span>(events);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 把静态内容塞到队列</span></span><br><span class="line"><span class="keyword">await</span> <span class="variable language_">this</span>.<span class="property">mailerQueue</span>.<span class="title function_">add</span>(</span><br><span class="line"> id,</span><br><span class="line"> {</span><br><span class="line"> <span class="attr">mail</span>: <span class="keyword">new</span> <span class="title class_">Mail</span>({ <span class="comment">/*…*/</span>, windowStart, <span class="attr">windowEnd</span>: now }),</span><br><span class="line"> reportHtml, <span class="comment">// <- 预渲染好的文本/HTML</span></span><br><span class="line"> <span class="attr">attachments</span>: […], <span class="comment">// <- 如果有附件也一并塞</span></span><br><span class="line"> },</span><br><span class="line"> { <span class="attr">attempts</span>: <span class="number">3</span>, <span class="attr">backoff</span>: { <span class="attr">type</span>: <span class="string">'fixed'</span>, <span class="attr">delay</span>: <span class="number">5_000</span> } },</span><br><span class="line">);</span><br><span class="line"></span><br><span class="line"><span class="comment">// process 里只关注发送</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">async</span> <span class="title function_">process</span>(<span class="params"><span class="attr">job</span>: <span class="title class_">Job</span></span>) {</span><br><span class="line"> <span class="keyword">try</span> {</span><br><span class="line"> <span class="keyword">await</span> <span class="variable language_">this</span>.<span class="property">mailService</span>.<span class="title function_">send</span>({</span><br><span class="line"> <span class="attr">to</span>: job.<span class="property">data</span>.<span class="property">mail</span>.<span class="property">to</span>,</span><br><span class="line"> <span class="attr">subject</span>: <span class="string">`系统 24h 报表`</span>,</span><br><span class="line"> <span class="attr">html</span>: job.<span class="property">data</span>.<span class="property">reportHtml</span>,</span><br><span class="line"> <span class="attr">attachments</span>: job.<span class="property">data</span>.<span class="property">attachments</span>,</span><br><span class="line"> });</span><br><span class="line"> } <span class="keyword">catch</span> (e) {</span><br><span class="line"> <span class="keyword">throw</span> e; <span class="comment">// 触发重试</span></span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>➜ 重试带来的任何延迟,都不影响邮件正文,始终是一份「事先约定好、并且静态化」的报告。</p>
<p>这两种模式都能保证最终发送时的数据窗口或内容,与当初调度时的预期完全一致,不会因为重试延迟而出现“数据真空”或“多算/少算”问题。</p>
<h2 id="参考文档:"><a class="header-anchor" href="#参考文档:">¶</a>参考文档:</h2>
<ul>
<li>“Retrying failing jobs” · BullMQ Guide<br>
<a href="https://docs.bullmq.io/guide/retrying-failing-jobs">https://docs.bullmq.io/guide/retrying-failing-jobs</a></li>
<li>BullMQ Guide & Patterns · Process Step Jobs (completed event only fires after process resolves)<br>
<a href="https://docs.bullmq.io/patterns/process-step-jobs">https://docs.bullmq.io/patterns/process-step-jobs</a></li>
</ul>
]]></content>
<tags>
<tag>Technique</tag>
</tags>
</entry>
<entry>
<title>NestJS 中 enableImplicitConversion 与 @Transform 的冲突</title>
<url>/2025/08/13/nestjs-enableImplicitConversion-and-transform/</url>
<content><![CDATA[<p>在 NestJS 生态中,<code>class-validator</code> 和 <code>class-transformer</code> 这两个库提供了以声明式的方式对 DTO 进行验证和转换。然而在处理布尔值时,如果在全局验证管道或仅仅是在局部同时开启了 <code>enableImplicitConversion</code>,可能会引入一个极其隐蔽且违反直觉的 Bug:前端传过来的布尔值恒为 true。</p>
<h2 id="一个简单的筛选功能"><a class="header-anchor" href="#一个简单的筛选功能">¶</a>一个简单的筛选功能</h2>
<p>假设正在开发一个电子商务平台的 API,需要实现一个产品列表的筛选功能。希望能够根据产品是否有库存 (<code>hasStock</code>)、是否为特色产品 (<code>isFeatured</code>) 等布尔条件进行筛选。</p>
<p>前端发出的请求 URL 可能如下所示: <code>/products?filter[hasStock]=true&filter[isFeatured]=false</code></p>
<p>在 NestJS 后端,首先会在 <code>main.ts</code> 中配置一个全局的 <code>ValidationPipe</code>,以自动处理 DTO 的验证和转换。为了方便,通常会启用 <code>enableImplicitConversion</code>,期望它能自动将 URL 查询参数中的字符串(如 <code>"123"</code>, <code>"true"</code>)转换为 DTO 中定义的类型(<code>number</code>, <code>boolean</code>):</p>
<figure class="highlight typescript"><table><tr><td class="code"><pre><span class="line"><span class="comment">// main.ts</span></span><br><span class="line"><span class="keyword">import</span> { <span class="title class_">ValidationPipe</span> } <span class="keyword">from</span> <span class="string">'@nestjs/common'</span>;</span><br><span class="line"><span class="keyword">import</span> { <span class="title class_">NestFactory</span> } <span class="keyword">from</span> <span class="string">'@nestjs/core'</span>;</span><br><span class="line"><span class="keyword">import</span> { <span class="title class_">AppModule</span> } <span class="keyword">from</span> <span class="string">'./app.module'</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">async</span> <span class="keyword">function</span> <span class="title function_">bootstrap</span>(<span class="params"></span>) {</span><br><span class="line"> <span class="keyword">const</span> app = <span class="keyword">await</span> <span class="title class_">NestFactory</span>.<span class="title function_">create</span>(<span class="title class_">AppModule</span>);</span><br><span class="line"></span><br><span class="line"> app.<span class="title function_">useGlobalPipes</span>(</span><br><span class="line"> <span class="keyword">new</span> <span class="title class_">ValidationPipe</span>({</span><br><span class="line"> <span class="attr">transform</span>: <span class="literal">true</span>, <span class="comment">// 启用转换</span></span><br><span class="line"> <span class="attr">whitelist</span>: <span class="literal">true</span>,</span><br><span class="line"> <span class="attr">forbidNonWhitelisted</span>: <span class="literal">true</span>,</span><br><span class="line"> <span class="attr">transformOptions</span>: {</span><br><span class="line"> <span class="comment">// 启用基于 TypeScript 类型的隐式转换</span></span><br><span class="line"> <span class="attr">enableImplicitConversion</span>: <span class="literal">true</span>, </span><br><span class="line"> },</span><br><span class="line"> }),</span><br><span class="line"> );</span><br><span class="line"> </span><br><span class="line"> <span class="comment">// ... 其他配置</span></span><br><span class="line"> <span class="keyword">await</span> app.<span class="title function_">listen</span>(<span class="number">3000</span>);</span><br><span class="line">}</span><br><span class="line"><span class="title function_">bootstrap</span>();</span><br></pre></td></tr></table></figure>
<p>接着,定义一个 <code>ProductFilterDto</code> 来接收这些筛选条件。</p>
<p><strong>一个看似正确的 DTO 定义:</strong></p>
<figure class="highlight typescript"><table><tr><td class="code"><pre><span class="line"><span class="comment">// product-filter.dto.ts</span></span><br><span class="line"><span class="keyword">import</span> { <span class="title class_">IsBoolean</span>, <span class="title class_">IsOptional</span> } <span class="keyword">from</span> <span class="string">'class-validator'</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">class</span> <span class="title class_">ProductFilterDto</span> {</span><br><span class="line"> <span class="meta">@IsOptional</span>()</span><br><span class="line"> <span class="meta">@IsBoolean</span>()</span><br><span class="line"> <span class="attr">hasStock</span>?: <span class="built_in">boolean</span>;</span><br><span class="line"></span><br><span class="line"> <span class="meta">@IsOptional</span>()</span><br><span class="line"> <span class="meta">@IsBoolean</span>()</span><br><span class="line"> <span class="attr">isFeatured</span>?: <span class="built_in">boolean</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>在控制器中使用这个 DTO:</p>
<figure class="highlight typescript"><table><tr><td class="code"><pre><span class="line"><span class="comment">// products.controller.ts</span></span><br><span class="line"><span class="meta">@Controller</span>(<span class="string">'products'</span>)</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">class</span> <span class="title class_">ProductsController</span> {</span><br><span class="line"> <span class="meta">@Get</span>()</span><br><span class="line"> <span class="title function_">find</span>(<span class="params"><span class="meta">@Query</span>(<span class="string">'filter'</span>) <span class="attr">filter</span>: <span class="title class_">ProductFilterDto</span></span>) {</span><br><span class="line"> <span class="comment">// 期望 filter.isFeatured 的值为 boolean false</span></span><br><span class="line"> <span class="variable language_">console</span>.<span class="title function_">log</span>(filter); </span><br><span class="line"> <span class="comment">// ... 业务逻辑</span></span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>当请求 <code>.../products?filter[isFeatured]=false</code> 到达时,本来期望在 <code>find</code> 方法中得到的 <code>filter.isFeatured</code> 的值是布尔类型的 <code>false</code>。然而,控制台输出的结果却令人意外:<code>{ isFeatured: true }</code></p>
<h2 id="问题剖析"><a class="header-anchor" href="#问题剖析">¶</a>问题剖析</h2>
<p>这个问题的根源在于 <code>class-transformer</code> 内部的转换执行顺序,以及 JavaScript 中 <code>Boolean</code> 函数的类型转换行为。</p>
<p>所有通过 URL 查询参数传递的值,其本质都是字符串。当 NestJS 接收到请求时,<code>filter.isFeatured</code> 的原始值是字符串 <code>"false"</code>。</p>
<p><code>ValidationPipe</code> 启动 <code>class-transformer</code> 的转换流程。由于在全局管道中设置了 <code>enableImplicitConversion: true</code>,转换器会首先检查 DTO 属性的 TypeScript 类型。</p>
<ol>
<li><strong>隐式转换优先执行</strong>:<code>class-transformer</code> 看到 <code>ProductFilterDto</code> 中的 <code>isFeatured</code> 属性被声明为 <code>boolean</code> 类型。</li>
<li><strong>错误的类型转换</strong>:它立即尝试将字符串 <code>"false"</code> 转换为布尔值。这个转换等同于执行 <code>Boolean("false")</code>。在 JavaScript 中,任何非空字符串(包括 <code>"false"</code>)通过 <code>Boolean()</code> 构造函数转换后都会得到 <code>true</code>。</li>
<li><strong>结果覆盖</strong>:这个错误的 <code>true</code> 值被作为该属性的转换结果。</li>
</ol>
<p>此时,即使尝试添加一个自定义的 <code>@Transform</code> 装饰器来手动处理这个问题,也为时已晚。</p>
<p>例如,定义一个 <code>booleanTransformer</code>:</p>
<figure class="highlight typescript"><table><tr><td class="code"><pre><span class="line"><span class="comment">// boolean-transformer.ts</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">booleanTransformer</span> = (<span class="params">{ value }: { value: <span class="built_in">any</span> }</span>) => {</span><br><span class="line"> <span class="keyword">if</span> (<span class="keyword">typeof</span> value === <span class="string">'string'</span>) {</span><br><span class="line"> <span class="keyword">return</span> value === <span class="string">'true'</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> value;</span><br><span class="line">};</span><br></pre></td></tr></table></figure>
<p>然后更新 dto:</p>
<figure class="highlight typescript"><table><tr><td class="code"><pre><span class="line"><span class="comment">// product-filter.dto.ts (错误的尝试)</span></span><br><span class="line"><span class="keyword">import</span> { <span class="title class_">Transform</span> } <span class="keyword">from</span> <span class="string">'class-transformer'</span>;</span><br><span class="line"><span class="keyword">import</span> { <span class="title class_">IsBoolean</span>, <span class="title class_">IsOptional</span> } <span class="keyword">from</span> <span class="string">'class-validator'</span>;</span><br><span class="line"><span class="keyword">import</span> { booleanTransformer } <span class="keyword">from</span> <span class="string">'./boolean-transformer'</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">class</span> <span class="title class_">ProductFilterDto</span> {</span><br><span class="line"> <span class="comment">// ...</span></span><br><span class="line"> <span class="meta">@IsOptional</span>()</span><br><span class="line"> <span class="meta">@Transform</span>(booleanTransformer) <span class="comment">// 添加自定义转换</span></span><br><span class="line"> <span class="meta">@IsBoolean</span>()</span><br><span class="line"> <span class="attr">isFeatured</span>?: <span class="built_in">boolean</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>流程会变成这样:</p>
<ol>
<li>隐式转换首先执行:<code>Boolean("false")</code> -> <code>true</code>。</li>
<li><code>@Transform</code> 装饰器执行:此时传递给 <code>booleanTransformer</code> 的 <code>value</code> 已经是上一步错误转换后的布尔值 <code>true</code>,而不是原始的字符串 <code>"false"</code>。转换函数无从下手。</li>
</ol>
<p>最终结果依然是 <code>true</code>。</p>
<h2 id="解决方案:用-any-绕过隐式转换"><a class="header-anchor" href="#解决方案:用-any-绕过隐式转换">¶</a>## 解决方案:用 <code>any</code> 绕过隐式转换</h2>
<p>要解决这个问题,核心在于阻止 <code>class-transformer</code> 进行那次错误的、优先的隐式转换,从而确保自定义 <code>@Transform</code> 函数能接收到最原始的字符串值。</p>
<p>最直接且侵入性最小的方法,是将 DTO 中相关属性的 TypeScript 类型从 <code>boolean</code> 改为 <code>any</code>。</p>
<p><strong>修正后的 DTO 定义:</strong></p>
<figure class="highlight typescript"><table><tr><td class="code"><pre><span class="line"><span class="comment">// product-filter.dto.ts (正确的实现)</span></span><br><span class="line"><span class="keyword">import</span> { <span class="title class_">Transform</span> } <span class="keyword">from</span> <span class="string">'class-transformer'</span>;</span><br><span class="line"><span class="keyword">import</span> { <span class="title class_">IsBoolean</span>, <span class="title class_">IsOptional</span> } <span class="keyword">from</span> <span class="string">'class-validator'</span>;</span><br><span class="line"><span class="keyword">import</span> { booleanTransformer } <span class="keyword">from</span> <span class="string">'./boolean-transformer'</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 一个更健壮的 booleanTransformer</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">robustBooleanTransformer</span> = (<span class="params">{ value }: { value: <span class="built_in">string</span> }</span>) =></span><br><span class="line"> value === <span class="string">'true'</span> ? <span class="literal">true</span> : value === <span class="string">'false'</span> ? <span class="literal">false</span> : value;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">class</span> <span class="title class_">ProductFilterDto</span> {</span><br><span class="line"> <span class="meta">@IsOptional</span>()</span><br><span class="line"> <span class="meta">@Transform</span>(robustBooleanTransformer)</span><br><span class="line"> <span class="meta">@IsBoolean</span>()</span><br><span class="line"> <span class="attr">hasStock</span>?: <span class="built_in">any</span>; <span class="comment">// <-- 类型从 boolean 改为 any</span></span><br><span class="line"></span><br><span class="line"> <span class="meta">@IsOptional</span>()</span><br><span class="line"> <span class="meta">@Transform</span>(robustBooleanTransformer)</span><br><span class="line"> <span class="meta">@IsBoolean</span>()</span><br><span class="line"> <span class="attr">isFeatured</span>?: <span class="built_in">any</span>; <span class="comment">// <-- 类型从 boolean 改为 any</span></span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>这个改动虽然看起来放弃了 TypeScript 的类型检查,但在这个特定的场景下,它非常安全且有效。原因如下:</p>
<ol>
<li><strong>阻止隐式转换</strong>:当 <code>class-transformer</code> 看到属性类型是 <code>any</code> 时,它不知道该隐式转换成什么目标类型,因此会“跳过”这个属性的隐式转换步骤。</li>
<li><strong><code>@Transform</code> 接管</strong>:如此一来,原始的字符串值(<code>"true"</code> 或 <code>"false"</code>)就能原封不动地传递给 <code>robustBooleanTransformer</code> 函数。该函数现在可以正确地将字符串转换为期望的布尔值。</li>
<li><strong><code>@IsBoolean</code> 守门</strong>:在自定义转换完成后,<code>@IsBoolean()</code> 装饰器会进行最后的验证,确保存入 DTO 的最终值必须是 <code>true</code> 或 <code>false</code>。这保证了在业务逻辑中,该属性的类型是绝对安全的。</li>
</ol>
<p>好用,爱用。</p>
]]></content>
<tags>
<tag>Technique</tag>
</tags>
</entry>
<entry>
<title>NestJS 嵌套查询参数解析错误分析与解决方案</title>
<url>/2025/07/29/nestjs-nested-object-dto/</url>
<content><![CDATA[<p>在开发 NestJS 应用程序时,一个常见的场景是根据复杂的条件筛选资源。当使用嵌套查询参数来表达这些条件时,例如通过 URL <code>http://localhost:3000/products/filter?filters[price][min]=100</code>,可能会意外地遇到 <code>property filters[price][min] should not exist</code> 错误。本文将深入剖析此问题背后的原因,并提供针对 Express 和 Fastify 两种底层框架的解决方案。</p>
<h4 id="问题背景"><a class="header-anchor" href="#问题背景">¶</a>问题背景</h4>
<p>假设我们正在构建一个电子商务平台的后端,需要实现一个商品筛选接口 <code>/products/filter</code>。该接口应允许客户端根据不同的属性(如价格 <code>price</code>、库存 <code>stock</code>)进行筛选,并支持对数值型属性(如价格)指定一个范围。</p>
<p>为了实现这一功能,我们定义了以下 NestJS 数据传输对象 (DTO) 结构:</p>
<figure class="highlight typescript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> { <span class="title class_">ApiPropertyOptional</span> } <span class="keyword">from</span> <span class="string">"@nestjs/swagger"</span>;</span><br><span class="line"><span class="keyword">import</span> {</span><br><span class="line"> <span class="title class_">IsNumber</span>,</span><br><span class="line"> <span class="title class_">IsOptional</span>,</span><br><span class="line"> <span class="title class_">Min</span>,</span><br><span class="line"> <span class="title class_">ValidateNested</span>,</span><br><span class="line">} <span class="keyword">from</span> <span class="string">"class-validator"</span>;</span><br><span class="line"><span class="keyword">import</span> { <span class="title class_">Type</span> } <span class="keyword">from</span> <span class="string">"class-transformer"</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">RangeFilterDto</span> {</span><br><span class="line"> <span class="meta">@ApiPropertyOptional</span>({ <span class="attr">description</span>: <span class="string">"最小值 (大于等于)"</span> })</span><br><span class="line"> <span class="meta">@IsOptional</span>()</span><br><span class="line"> <span class="meta">@Type</span>(<span class="function">() =></span> <span class="title class_">Number</span>)</span><br><span class="line"> <span class="meta">@IsNumber</span>()</span><br><span class="line"> <span class="meta">@Min</span>(<span class="number">0</span>)</span><br><span class="line"> <span class="attr">min</span>?: <span class="built_in">number</span>;</span><br><span class="line"></span><br><span class="line"> <span class="meta">@ApiPropertyOptional</span>({ <span class="attr">description</span>: <span class="string">"最大值 (小于等于)"</span> })</span><br><span class="line"> <span class="meta">@IsOptional</span>()</span><br><span class="line"> <span class="meta">@Type</span>(<span class="function">() =></span> <span class="title class_">Number</span>)</span><br><span class="line"> <span class="meta">@IsNumber</span>()</span><br><span class="line"> <span class="meta">@Min</span>(<span class="number">0</span>)</span><br><span class="line"> <span class="attr">max</span>?: <span class="built_in">number</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">ProductAttributeFilterDto</span> {</span><br><span class="line"> <span class="meta">@ApiPropertyOptional</span>({</span><br><span class="line"> <span class="attr">description</span>: <span class="string">"价格范围"</span>,</span><br><span class="line"> <span class="attr">type</span>: <span class="title class_">RangeFilterDto</span>,</span><br><span class="line"> })</span><br><span class="line"> <span class="meta">@IsOptional</span>()</span><br><span class="line"> <span class="meta">@ValidateNested</span>()</span><br><span class="line"> <span class="meta">@Type</span>(<span class="function">() =></span> <span class="title class_">RangeFilterDto</span>)</span><br><span class="line"> <span class="attr">price</span>?: <span class="title class_">RangeFilterDto</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">class</span> <span class="title class_">ProductFilterDto</span> {</span><br><span class="line"> <span class="meta">@ApiPropertyOptional</span>({ <span class="attr">description</span>: <span class="string">"商品属性筛选条件"</span> })</span><br><span class="line"> <span class="meta">@IsOptional</span>()</span><br><span class="line"> <span class="meta">@ValidateNested</span>()</span><br><span class="line"> <span class="meta">@Type</span>(<span class="function">() =></span> <span class="title class_">ProductAttributeFilterDto</span>)</span><br><span class="line"> <span class="attr">filters</span>?: <span class="title class_">ProductAttributeFilterDto</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>对应的控制器代码如下:</p>
<figure class="highlight typescript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> { <span class="title class_">Controller</span>, <span class="title class_">Get</span>, <span class="title class_">Query</span> } <span class="keyword">from</span> <span class="string">'@nestjs/common'</span>;</span><br><span class="line"><span class="keyword">import</span> { <span class="title class_">ApiOperation</span>, <span class="title class_">ApiResponse</span>, <span class="title class_">ApiTags</span> } <span class="keyword">from</span> <span class="string">'@nestjs/swagger'</span>;</span><br><span class="line"><span class="keyword">import</span> { <span class="title class_">ProductFilterDto</span> } <span class="keyword">from</span> <span class="string">'./dtos/product-filter.dto'</span>;</span><br><span class="line"></span><br><span class="line"><span class="meta">@ApiTags</span>(<span class="string">'products'</span>)</span><br><span class="line"><span class="meta">@Controller</span>(<span class="string">'products'</span>)</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">class</span> <span class="title class_">ProductsController</span> {</span><br><span class="line"> <span class="meta">@Get</span>(<span class="string">"filter"</span>)</span><br><span class="line"> <span class="meta">@ApiOperation</span>({ <span class="attr">summary</span>: <span class="string">"根据条件筛选商品"</span> })</span><br><span class="line"> <span class="meta">@ApiResponse</span>({ <span class="attr">status</span>: <span class="number">200</span>, <span class="attr">description</span>: <span class="string">"筛选商品成功"</span> })</span><br><span class="line"> <span class="meta">@ApiResponse</span>({ <span class="attr">status</span>: <span class="number">400</span>, <span class="attr">description</span>: <span class="string">"请求参数错误"</span> })</span><br><span class="line"> <span class="keyword">async</span> <span class="title function_">getFilteredProducts</span>(</span><br><span class="line"> <span class="meta">@Query</span>() <span class="attr">query</span>: <span class="title class_">ProductFilterDto</span>,</span><br><span class="line"> ): <span class="title class_">Promise</span><<span class="built_in">any</span>> {</span><br><span class="line"> <span class="comment">// 在实际应用中,这里会调用服务处理筛选逻辑</span></span><br><span class="line"> <span class="variable language_">console</span>.<span class="title function_">log</span>(query);</span><br><span class="line"> <span class="keyword">return</span> {</span><br><span class="line"> <span class="attr">message</span>: <span class="string">"Products filtered successfully."</span>,</span><br><span class="line"> <span class="attr">data</span>: query,</span><br><span class="line"> };</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>当客户端通过以下 URL 发送请求时:</p>
<p><code>http://localhost:3000/products/filter?filters[price][min]=100</code></p>
<p>后端返回了 HTTP 400 错误,并附带以下日志:</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">[Nest] 12345 - 07/29/2025, 11:00:00 AM ERROR [HttpExceptionFilter] [GET /products/filter?filters%5Bprice%5D%5Bmin%5D=100] HTTP 400 Error: property filters[price][min] should not exist</span><br><span class="line">BadRequestException: Bad Request Exception</span><br><span class="line"> at ValidationPipe.exceptionFactory (/path/to/project/node_modules/@nestjs/common/pipes/validation.pipe.js:107:20)</span><br><span class="line"> at ValidationPipe.transform (/path/to/project/node_modules/@nestjs/common/pipes/validation.pipe.js:74:30)</span><br><span class="line"> ... (其他堆栈信息)</span><br></pre></td></tr></table></figure>
<h4 id="错误分析"><a class="header-anchor" href="#错误分析">¶</a>错误分析</h4>
<p>此错误的核心在于 NestJS 的 <code>@Query()</code> 装饰器与 <code>ValidationPipe</code> 在处理 URL 查询字符串时的默认行为,未能正确地将客户端提供的嵌套方括号语法 (<code>filters[price][min]</code>) 解析为 DTO 所期望的 JavaScript 嵌套对象结构。</p>
<ol>
<li>
<p><strong>DTO 的预期结构:</strong><br>
<code>ProductFilterDto</code> 定义了 <code>filters</code> 属性,其类型为 <code>ProductAttributeFilterDto</code>。<code>ProductAttributeFilterDto</code> 又包含了 <code>price</code> 属性,类型为 <code>RangeFilterDto</code>,最终含有 <code>min</code> 和 <code>max</code> 属性。这意味着 DTO 期望接收的数据结构应为:<code>{ filters: { price: { min: 100 } } }</code>。</p>
</li>
<li>
<p><strong>URL 查询参数的扁平化解析:</strong><br>
HTTP 协议的查询字符串本质上是扁平的键值对集合。虽然 <code>key[nestedKey]=value</code> 这种方括号语法被许多 Web 框架(如 PHP、Ruby on Rails)和库(如 <code>qs</code>)广泛用于表示嵌套数据,但这并非 HTTP 协议的标准。<br>
NestJS 依赖其底层 HTTP 适配器(默认为 Express)来解析传入的请求。在默认配置下,Express 不会自动地、递归地将 <code>filters[price][min]</code> 这样的字符串键名解析成一个具有正确嵌套层级的 JavaScript 对象。相反,它会将其视为一个完整的、扁平的字符串键名 <code>filters[price][min]</code>,并将其值 <code>100</code> 与之对应。</p>
</li>
<li>
<p><strong><code>ValidationPipe</code> 的验证失败:</strong><br>
当 <code>ValidationPipe</code> 接收到被扁平化解析后的查询参数时,它会在 <code>ProductFilterDto</code> 中寻找一个名为 <code>filters[price][min]</code> 的顶层属性。由于 DTO 中并未直接定义这样一个扁平的属性,并且 <code>ValidationPipe</code> 通常会启用 <code>forbidNonWhitelisted: true</code> 或 <code>whitelist: true</code> 选项来拒绝 DTO 中未明确声明的属性,因此验证过程失败,并抛出 <code>property filters[price][min] should not exist</code> 的错误。这表明 <code>ValidationPipe</code> 将 <code>filters[price][min]</code> 视为一个非法的、未在白名单中的属性,而不是预期的嵌套对象 <code>filters</code> 下的深层子属性。</p>
</li>
</ol>
<p>简而言之,问题不在于 DTO 定义或 <code>@Query()</code> 装饰器本身,而在于底层 HTTP 框架对查询字符串的默认解析行为与 NestJS <code>ValidationPipe</code> 对复杂嵌套 DTO 结构的需求不匹配。</p>
<h4 id="解决方案"><a class="header-anchor" href="#解决方案">¶</a>解决方案</h4>
<p>要解决此问题,关键在于配置 NestJS 应用的底层 HTTP 适配器,使其能够正确地解析包含方括号语法的嵌套查询参数,将其转换为嵌套的 JavaScript 对象。</p>
<h5 id="针对-Express-适配器"><a class="header-anchor" href="#针对-Express-适配器">¶</a>针对 Express 适配器</h5>
<p>如果您的 NestJS 应用使用 Express 作为底层 HTTP 框架(这是新项目的默认配置),您需要在应用的入口文件(通常是 <code>main.ts</code>)中,通过 <code>app.set('query parser', 'extended');</code> 显式启用 Express 的扩展查询字符串解析功能。此配置会指示 Express 使用 <code>qs</code> 库(Express 内置依赖)来处理查询字符串,该库能够正确处理嵌套结构。</p>
<p>示例 (<code>main.ts</code>):</p>
<figure class="highlight typescript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> { <span class="title class_">NestFactory</span> } <span class="keyword">from</span> <span class="string">'@nestjs/core'</span>;</span><br><span class="line"><span class="keyword">import</span> { <span class="title class_">AppModule</span> } <span class="keyword">from</span> <span class="string">'./app.module'</span>;</span><br><span class="line"><span class="keyword">import</span> { <span class="title class_">ValidationPipe</span> } <span class="keyword">from</span> <span class="string">'@nestjs/common'</span>;</span><br><span class="line"><span class="keyword">import</span> { <span class="title class_">NestExpressApplication</span> } <span class="keyword">from</span> <span class="string">'@nestjs/platform-express'</span>; <span class="comment">// 导入此类型</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">async</span> <span class="keyword">function</span> <span class="title function_">bootstrap</span>(<span class="params"></span>) {</span><br><span class="line"> <span class="keyword">const</span> app = <span class="keyword">await</span> <span class="title class_">NestFactory</span>.<span class="property">create</span><<span class="title class_">NestExpressApplication</span>>(<span class="title class_">AppModule</span>);</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 启用 Express 的扩展查询参数解析</span></span><br><span class="line"> <span class="comment">// 'simple' (默认) | 'extended'</span></span><br><span class="line"> app.<span class="title function_">set</span>(<span class="string">'query parser'</span>, <span class="string">'extended'</span>);</span><br><span class="line"></span><br><span class="line"> app.<span class="title function_">useGlobalPipes</span>(<span class="keyword">new</span> <span class="title class_">ValidationPipe</span>({</span><br><span class="line"> <span class="attr">whitelist</span>: <span class="literal">true</span>, <span class="comment">// 自动剥离 DTO 中未定义的属性</span></span><br><span class="line"> <span class="attr">forbidNonWhitelisted</span>: <span class="literal">true</span>, <span class="comment">// 如果传入 DTO 中未定义的属性,则抛出错误</span></span><br><span class="line"> <span class="attr">transform</span>: <span class="literal">true</span>, <span class="comment">// 自动将负载转换为 DTO 类的实例</span></span><br><span class="line"> <span class="attr">transformOptions</span>: {</span><br><span class="line"> <span class="attr">enableImplicitConversion</span>: <span class="literal">true</span>, <span class="comment">// 允许隐式类型转换,配合 @Type()</span></span><br><span class="line"> },</span><br><span class="line"> }));</span><br><span class="line"></span><br><span class="line"> <span class="comment">// ... 其他配置</span></span><br><span class="line"> <span class="keyword">await</span> app.<span class="title function_">listen</span>(<span class="number">3000</span>);</span><br><span class="line">}</span><br><span class="line"><span class="title function_">bootstrap</span>();</span><br></pre></td></tr></table></figure>
<h5 id="针对-Fastify-适配器"><a class="header-anchor" href="#针对-Fastify-适配器">¶</a>针对 Fastify 适配器</h5>
<p>如果您的 NestJS 应用使用 Fastify 作为底层 HTTP 框架,您需要在创建 Fastify 适配器实例时,通过 <code>querystringParser</code> 选项提供一个自定义的查询字符串解析函数。通常,我们会利用 <code>qs</code> 这样的成熟库来完成递归解析。</p>
<p>首先,确保已安装 <code>qs</code> 库及其类型定义:<br>
<code>npm install qs</code><br>
<code>npm install -D @types/qs</code></p>
<p>示例 (<code>main.ts</code>):</p>
<figure class="highlight typescript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> { <span class="title class_">NestFactory</span> } <span class="keyword">from</span> <span class="string">'@nestjs/core'</span>;</span><br><span class="line"><span class="keyword">import</span> { <span class="title class_">AppModule</span> } <span class="keyword">from</span> <span class="string">'./app.module'</span>;</span><br><span class="line"><span class="keyword">import</span> { <span class="title class_">ValidationPipe</span> } <span class="keyword">from</span> <span class="string">'@nestjs/common'</span>;</span><br><span class="line"><span class="keyword">import</span> {</span><br><span class="line"> <span class="title class_">FastifyAdapter</span>,</span><br><span class="line"> <span class="title class_">NestFastifyApplication</span>,</span><br><span class="line">} <span class="keyword">from</span> <span class="string">'@nestjs/platform-fastify'</span>;</span><br><span class="line"><span class="keyword">import</span> * <span class="keyword">as</span> qs <span class="keyword">from</span> <span class="string">'qs'</span>; <span class="comment">// 引入 qs 库</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">async</span> <span class="keyword">function</span> <span class="title function_">bootstrap</span>(<span class="params"></span>) {</span><br><span class="line"> <span class="keyword">const</span> app = <span class="keyword">await</span> <span class="title class_">NestFactory</span>.<span class="property">create</span><<span class="title class_">NestFastifyApplication</span>>(</span><br><span class="line"> <span class="title class_">AppModule</span>,</span><br><span class="line"> <span class="keyword">new</span> <span class="title class_">FastifyAdapter</span>({</span><br><span class="line"> <span class="comment">// 配置 Fastify 的查询字符串解析器</span></span><br><span class="line"> <span class="attr">querystringParser</span>: <span class="function">(<span class="params">str</span>) =></span> qs.<span class="title function_">parse</span>(str),</span><br><span class="line"> }),</span><br><span class="line"> );</span><br><span class="line"></span><br><span class="line"> app.<span class="title function_">useGlobalPipes</span>(<span class="keyword">new</span> <span class="title class_">ValidationPipe</span>({</span><br><span class="line"> <span class="attr">whitelist</span>: <span class="literal">true</span>,</span><br><span class="line"> <span class="attr">forbidNonWhitelisted</span>: <span class="literal">true</span>,</span><br><span class="line"> <span class="attr">transform</span>: <span class="literal">true</span>,</span><br><span class="line"> <span class="attr">transformOptions</span>: {</span><br><span class="line"> <span class="attr">enableImplicitConversion</span>: <span class="literal">true</span>,</span><br><span class="line"> },</span><br><span class="line"> }));</span><br><span class="line"></span><br><span class="line"> <span class="comment">// ... 其他配置</span></span><br><span class="line"> <span class="keyword">await</span> app.<span class="title function_">listen</span>(<span class="number">3000</span>);</span><br><span class="line">}</span><br><span class="line"><span class="title function_">bootstrap</span>();</span><br></pre></td></tr></table></figure>
<p>通过上述配置,无论是使用 Express 还是 Fastify,NestJS 应用都将能够正确地将 <code>?filters[price][min]=100</code> 解析为 <code>{ filters: { price: { min: '100' } } }</code>。随后,在 <code>ValidationPipe</code> 的 <code>transform</code> 阶段,<code>@Type(() => Number)</code> 装饰器会确保 <code>min</code> 的值从字符串 <code>'100'</code> 转换为数字 <code>100</code>,从而顺利通过验证并注入到控制器方法中。</p>
<h3 id="引用"><a class="header-anchor" href="#引用">¶</a>引用</h3>
<ul>
<li><a href="https://stackoverflow.com/questions/29960764/what-does-extended-mean-in-express-4-0">What does ‘extended’ mean in express 4.0? - Stack Overflow</a></li>
<li><a href="https://expressjs.com/en/5x/api.html#app.set">Express 5.0 - API Reference (<code>app.set</code>)</a></li>
</ul>
]]></content>
<tags>
<tag>Technique</tag>
</tags>
</entry>
<entry>
<title>使用 [@poll error] 实现线程安全的数据结构</title>
<url>/2023/08/15/poll-error-attribute-in-OCaml/</url>
<content><![CDATA[<p>OCaml的标准库提供了许多mutable的数据结构,比如Hashtbl, Queue, Stack之类的,但是这些数据结构都不是线程安全的。在 OCaml 4 和 OCaml 5 中,单个Domain中一次只能运行一个线程。换句话说,单个Domain中的线程仍然不会并行运行,除非在不同所的Domain中。</p>
<p>而在单个Domain中 OCaml 的 runtime 是通过在safe point期间半抢占式的切换线程。也就是说,线程切换只会发生在safe point期间。例如内存分配就是是safe point。这意味着在没有safe point的代码块内,可以在Domain内原子性地进行多次读写或访问操作,因为线程没被切换。</p>
<p>OCaml 编译器提供了一个名为 <a href="https://github.com/ocaml/ocaml/pull/10462"><code>[@poll error]</code></a> 的annotation,可以在函数中使用它来确保该函数不包含safe point。</p>
<p>所以通过使用 <code>[@poll error]</code> 就可以创建在Domain内原子性执行的函数,也就是说,基于此特性可以实现单个Domain内线程安全的数据结构,例如 thread-table 便是使用这个特性实现的 Hash Table。可以看看它的 add 函数的实现:</p>
<figure class="highlight ocaml"><table><tr><td class="code"><pre><span class="line"><span class="keyword">let</span>[@poll error] add_atomically t buckets n i before after =</span><br><span class="line"> t.rehash = <span class="number">0</span> && buckets == t.buckets</span><br><span class="line"> && before == <span class="type">Array</span>.unsafe_get buckets i</span><br><span class="line"> && <span class="keyword">begin</span></span><br><span class="line"> <span class="type">Array</span>.unsafe_set buckets i after;</span><br><span class="line"> <span class="keyword">let</span> length = t.length + <span class="number">1</span> <span class="keyword">in</span></span><br><span class="line"> t.length <- length;</span><br><span class="line"> <span class="keyword">if</span> n < length && n < max_buckets_div_2 <span class="keyword">then</span> t.rehash <- n * <span class="number">2</span>;</span><br><span class="line"> <span class="literal">true</span></span><br><span class="line"> <span class="keyword">end</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> <span class="keyword">rec</span> add t k' v' =</span><br><span class="line"> <span class="keyword">let</span> h = <span class="type">Mix</span>.<span class="built_in">int</span> k' <span class="keyword">in</span></span><br><span class="line"> maybe_rehash t;</span><br><span class="line"> <span class="keyword">let</span> buckets = t.buckets <span class="keyword">in</span></span><br><span class="line"> <span class="keyword">let</span> n = <span class="type">Array</span>.length buckets <span class="keyword">in</span></span><br><span class="line"> <span class="keyword">let</span> i = h <span class="keyword">land</span> (n - <span class="number">1</span>) <span class="keyword">in</span></span><br><span class="line"> <span class="keyword">let</span> before = <span class="type">Array</span>.unsafe_get buckets i <span class="keyword">in</span></span><br><span class="line"> <span class="keyword">let</span> after = <span class="type">Cons</span> (k', v', before) <span class="keyword">in</span></span><br><span class="line"> <span class="keyword">if</span> not (add_atomically t buckets n i before after) <span class="keyword">then</span> add t k' v'</span><br></pre></td></tr></table></figure>
<p>相比使用 Stdlib.Mutex,这种无锁实现会有更好的性能(特别是对于只读操作),并且还允许例如信号处理之类的上下文操作。</p>
]]></content>
<tags>
<tag>Technique</tag>
</tags>
</entry>
<entry>
<title>除权除息日,股价调整与分红</title>
<url>/2025/07/11/rep-date/</url>
<content><![CDATA[<p>例如:</p>
<ul>
<li><strong>除权除息日:</strong> 2025年7月14日</li>
<li><strong>分红方案:</strong> 税后10实派人民币1.3元(每持有10股,实际获得1.3元人民币现金)</li>
</ul>
<p><strong>除息 (Ex-dividend):</strong> 公司派发现金红利,这笔钱是从公司的账户里拿出来的。这意味着公司的总资产减少了。在财务逻辑上,公司的价值降低了,那么股价自然也应该相应地下调。<strong>“除息”就是在这一天,把即将派发的这部分现金价值从股票价格中剔除掉。</strong></p>
<p><strong>除权 (Ex-rights):</strong> 如果公司不是派现金,而是送股票(比如“10送3股”),那么总股本会增加,每股代表的实际价值就被稀释了。<strong>“除权”就是调整股价,以反映这种股本增加带来的稀释效应。</strong></p>
<p>现金分红主要是除息。</p>
<p>在2025年7月14日这一天,该股票的开盘参考价会人为地向下调整。</p>
<p>如果在除息日前一天(假设是T日)持有这只股票,那么资产 = 股票市值 + 即将到手的现金分红。 到了除息日(T+1日,即7月14日),市场会重新定价。新的股价 = 原股价 - 每股分红。</p>
<p><strong>所以,除权除息日,本质上是股价因分红派息而进行调整的日子。</strong> 股价会下跌,但这并不意味着亏钱,因为股价减少的部分,恰好等于即将收到的现金红利。总资产在理论上保持不变。</p>
<p>不过 “除权除息日” 并不是钱到账的日子。这一天发生的是<strong>股价的调整</strong>,而不是现金的发放。</p>
<p>完整的分红流程如下:</p>
<ol>
<li>股权登记日 (Record Date)
<ul>
<li>按照A股市场的惯例(T+1制度),<strong>除权除息日通常是股权登记日的下一个交易日。</strong></li>
<li>因此,如果除权除息日是2025年7月14日(周一),那么<strong>股权登记日很可能就是前一个交易日,即2025年7月11日(周五)。</strong> (注:这需要以公司正式公告为准,但逻辑如此)。</li>
<li><strong>所以必须在股权登记日当天收盘时持有该股票,才能拿到分红。</strong></li>
</ul>
</li>
<li>除权除息日 (Ex-Date) — 股价调整的日子
<ul>
<li><strong>股价调整:</strong> 股票价格会下跌,反映红利已被剔除。</li>
<li><strong>权利分割:</strong>
<ul>
<li>如果在这一天<strong>买入</strong>这只股票,无法享受这次分红。因为买的是已经“打折”后的股票。</li>
<li>如果在这一天<strong>卖出</strong>在登记日持有的股票,依然可以拿到分红!因为在登记日是股东。</li>
</ul>
</li>
</ul>
</li>
<li>派息日 (Payment Date)
<ul>
<li>现金红利会在这一天自动打入你的证券资金账户。</li>
<li>派息日通常在除权除息日之后几天,有时甚至是同一天,但这需要看上市公司的具体安排和公告。</li>
</ul>
</li>
</ol>
]]></content>
<tags>
<tag>Life</tag>
</tags>
</entry>
<entry>
<title>shadow-cljs 怎么设置代理</title>
<url>/2024/04/03/shadow-cljs-proxy/</url>
<content><![CDATA[<p>The library pomegranate used for retrieving maven dependencies does not read <code>.m2/settings.xml</code>. You can however set the :proxy config either in <code>shadow-cljs.edn</code> directly or <code>~/.shadow-cljs/config.edn</code>.</p>
<p>See <a href="https://github.com/cemerick/pomegranate/blob/master/src/main/clojure/cemerick/pomegranate/aether.clj#L757-L765">https://github.com/cemerick/pomegranate/blob/master/src/main/clojure/cemerick/pomegranate/aether.clj#L757-L765</a>.</p>
<p><em>shadow-cljs.edn:</em></p>
<figure class="highlight clojure"><table><tr><td class="code"><pre><span class="line">{<span class="symbol">:source-paths</span> [<span class="string">"src/main"</span></span><br><span class="line"> <span class="string">"src/test"</span>]</span><br><span class="line"></span><br><span class="line"> <span class="symbol">:dependencies</span> [[reagent <span class="string">"1.2.0"</span>]</span><br><span class="line"> [re-frame <span class="string">"1.3.0"</span>]]</span><br><span class="line"></span><br><span class="line"> <span class="comment">;; Here</span></span><br><span class="line"> <span class="symbol">:proxy</span> {<span class="symbol">:host</span> <span class="string">"localhost"</span></span><br><span class="line"> <span class="symbol">:port</span> <span class="number">20171</span>}</span><br><span class="line"></span><br><span class="line"> <span class="symbol">:builds</span> {<span class="symbol">:app</span> {<span class="symbol">:target</span> <span class="symbol">:react-native</span></span><br><span class="line"> <span class="symbol">:init-fn</span> example.app/init</span><br><span class="line"> <span class="symbol">:output-dir</span> <span class="string">"app"</span></span><br><span class="line"> <span class="symbol">:compiler-options</span> {<span class="symbol">:infer-externs</span> <span class="symbol">:auto</span>}</span><br><span class="line"> <span class="symbol">:devtools</span> {<span class="symbol">:autoload</span> <span class="literal">true</span>}}}}</span><br></pre></td></tr></table></figure>]]></content>
<tags>
<tag>Technique</tag>
</tags>
</entry>
<entry>
<title>使用 Runed intersection observer 与 tanstack query 实现通用的惰性加载组件</title>
<url>/2025/07/31/svelte-lazyquery/</url>
<content><![CDATA[<p>在前端,一个常见的性能瓶颈是初始加载时请求了用户当前 viewpoint 之外的非必要数据。惰性加载(Lazy Loading)是一种关键策略,它将数据获取推迟到组件进入 viewpoint 时才执行。本文将阐述如何结合使用 Svelte 5、<code>@tanstack/svelte-query</code> 和 <code>runed.dev</code> 的 <code>useIntersectionObserver</code>,构建一个通用的、可复用的惰性加载组件。</p>
<p><strong>核心依赖与环境</strong></p>
<ul>
<li>
<p><strong>Svelte 5:</strong> 本实现依赖于 Svelte 5 的符文(Runes)特性,它提供了更精细、更直观的状态管理能力。</p>
</li>
<li>
<p><strong>@tanstack/svelte-query:</strong> TanStack Query 的 Svelte 适配版,用户获取数据。</p>
</li>
<li>
<p><strong>runed.dev:</strong> 一个提供多种 Svelte 5 实用工具的库,本文主要使用其 <code>useIntersectionObserver</code>。</p>
</li>
</ul>
<h4 id="设计哲学:分离关注点"><a class="header-anchor" href="#设计哲学:分离关注点">¶</a><strong>设计哲学:分离关注点</strong></h4>
<p>该方案的核心思想是将“何时加载”与“如何加载”这两个关注点进行解耦。</p>
<ol>
<li>
<p><strong>何时加载 (When to Load):</strong> 组件的可见性决定了数据加载的时机。我们利用 <code>Intersection Observer API</code> 来精确、高效地监听一个元素是否进入 viewpoint。<code>runed.dev</code> 库为此提供了名为 <code>useIntersectionObserver</code> 的便捷封装。</p>
</li>
<li>
<p><strong>如何加载 (How to Load):</strong> 数据获取、缓存、同步和状态管理的复杂性由 <code>@tanstack/svelte-query</code> (TanStack Query) 处理。它提供了一套强大的工具集来管理异步数据。</p>
</li>
</ol>
<p>通过将这两者结合,可以创建一个名为 <code>LazyQuery</code> 的抽象组件。该组件内部处理可见性检测,并根据检测结果动态控制 TanStack Query 的执行,而将具体的查询逻辑(<code>queryFn</code>)和键(<code>queryKey</code>)完全交由使用者定义。</p>
<h4 id="LazyQuery-组件的实现"><a class="header-anchor" href="#LazyQuery-组件的实现">¶</a><code>LazyQuery</code> 组件的实现</h4>
<blockquote>
<p>目标:封装惰性加载逻辑,并向外暴露一个标准的 TanStack Query 接口。</p>
</blockquote>
<p>一、对组件的接口类型做如下定义:</p>
<figure class="highlight typescript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> <span class="keyword">type</span> { <span class="title class_">CreateQueryOptions</span>, <span class="title class_">QueryKey</span> } <span class="keyword">from</span> <span class="string">'@tanstack/svelte-query'</span>;</span><br><span class="line"><span class="keyword">import</span> <span class="keyword">type</span> { <span class="title class_">Snippet</span> } <span class="keyword">from</span> <span class="string">'svelte'</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">type</span> <span class="title class_">Props</span><</span><br><span class="line"> <span class="title class_">TQueryFnData</span> = <span class="built_in">unknown</span>,</span><br><span class="line"> <span class="title class_">TError</span> = <span class="title class_">Error</span>,</span><br><span class="line"> <span class="title class_">TData</span> = <span class="title class_">TQueryFnData</span>,</span><br><span class="line"> <span class="title class_">TQueryKey</span> <span class="keyword">extends</span> <span class="title class_">QueryKey</span> = <span class="title class_">QueryKey</span></span><br><span class="line">> = {</span><br><span class="line"> <span class="comment">/// 一个标准的 TanStack Query 配置对象</span></span><br><span class="line"> <span class="attr">queryOptions</span>: <span class="title class_">CreateQueryOptions</span><<span class="title class_">TQueryFnData</span>, <span class="title class_">TError</span>, <span class="title class_">TData</span>, <span class="title class_">TQueryKey</span>>;</span><br><span class="line"></span><br><span class="line"> <span class="comment">/// Svelte 5 的 `Snippet` 类型,它允许父组件向子组件传递一段可执行的 UI 模板。</span></span><br><span class="line"> <span class="comment">/// 这个 `Snippet` 会接收到 `createQuery` 返回的完整查询对象 `query`,</span></span><br><span class="line"> <span class="comment">/// 从而可以访问 `data`, `isLoading`, `error` 等所有状态。</span></span><br><span class="line"> <span class="attr">children</span>: <span class="title class_">Snippet</span><{</span><br><span class="line"> <span class="attr">query</span>: <span class="title class_">ReturnType</span><<span class="keyword">typeof</span> createQuery<<span class="title class_">TQueryFnData</span>, <span class="title class_">TError</span>, <span class="title class_">TData</span>, <span class="title class_">TQueryKey</span>>>;</span><br><span class="line"> }>;</span><br><span class="line">};</span><br></pre></td></tr></table></figure>
<p>二、组件逻辑:</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag"><<span class="name">script</span> <span class="attr">lang</span>=<span class="string">"ts"</span>></span><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript"> <span class="keyword">import</span> { createQuery, type <span class="title class_">CreateQueryOptions</span>, type <span class="title class_">QueryKey</span> } <span class="keyword">from</span> <span class="string">'@tanstack/svelte-query'</span>;</span></span><br><span class="line"><span class="language-javascript"> <span class="keyword">import</span> { useIntersectionObserver } <span class="keyword">from</span> <span class="string">'runed'</span>;</span></span><br><span class="line"><span class="language-javascript"> <span class="keyword">import</span> type { <span class="title class_">Snippet</span> } <span class="keyword">from</span> <span class="string">'svelte'</span>;</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript"> <span class="comment">// [上述的类型定义]</span></span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript"> <span class="keyword">let</span> { queryOptions, children }: <span class="title class_">Props</span> = $props();</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript"> <span class="keyword">let</span> <span class="attr">el</span>: <span class="title class_">Element</span>;</span></span><br><span class="line"><span class="language-javascript"> <span class="keyword">const</span> { isIntersecting } = <span class="title function_">useIntersectionObserver</span>(</span></span><br><span class="line"><span class="language-javascript"> <span class="function">() =></span> el,</span></span><br><span class="line"><span class="language-javascript"> {</span></span><br><span class="line"><span class="language-javascript"> <span class="attr">rootMargin</span>: <span class="string">'200px'</span>,</span></span><br><span class="line"><span class="language-javascript"> }</span></span><br><span class="line"><span class="language-javascript"> );</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript"> <span class="keyword">const</span> query = <span class="title function_">createQuery</span>({</span></span><br><span class="line"><span class="language-javascript"> ...queryOptions,</span></span><br><span class="line"><span class="language-javascript"> <span class="keyword">get</span> <span class="title function_">enabled</span>() {</span></span><br><span class="line"><span class="language-javascript"> <span class="keyword">return</span> ($state.<span class="title function_">is</span>(isIntersecting) && (queryOptions.<span class="property">enabled</span> ?? <span class="literal">true</span>));</span></span><br><span class="line"><span class="language-javascript"> }</span></span><br><span class="line"><span class="language-javascript"> });</span></span><br><span class="line"><span class="language-javascript"></span><span class="tag"></<span class="name">script</span>></span></span><br><span class="line"></span><br><span class="line"><span class="tag"><<span class="name">div</span> <span class="attr">bind:this</span>=<span class="string">{el}</span>></span></span><br><span class="line"> {#snippet children({ query })}</span><br><span class="line"> {/snippet}</span><br><span class="line"><span class="tag"></<span class="name">div</span>></span></span><br></pre></td></tr></table></figure>
<ul>
<li>我们创建一个 <code>div</code> 元素作为哨兵(sentinel),并用 <code>bind:this={el}</code> 将其 DOM 引用绑定到变量 <code>el</code>。</li>
<li><code>useIntersectionObserver</code> 接收一个返回目标元素的函数 <code>() => el</code>。
<ul>
<li>它返回一个响应式的状态对象,其中 <code>isIntersecting</code> 是一个布尔值的符文(rune),当 <code>div</code> 元素进入 viewpoint 时为 <code>true</code>,否则为 <code>false</code>。</li>
</ul>
</li>
<li><code>rootMargin: '200px'</code> 是一个优化选项,它会在元素距离 viewpoint 还有 200px 时就触发加载,从而提升用户体验。</li>
</ul>
<h4 id="使用"><a class="header-anchor" href="#使用">¶</a>使用</h4>
<p>使用 <code>LazyQuery</code> 组件非常直观。开发者只需关注数据获取的业务逻辑,而无需关心惰性加载的实现细节,假设有一个获取图表数据的场景:</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag"><<span class="name">script</span> <span class="attr">lang</span>=<span class="string">"ts"</span>></span><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript"> <span class="keyword">import</span> { <span class="title class_">QueryClientProvider</span>, <span class="title class_">QueryClient</span> } <span class="keyword">from</span> <span class="string">'@tanstack/svelte-query'</span>;</span></span><br><span class="line"><span class="language-javascript"> <span class="keyword">import</span> <span class="title class_">LazyQuery</span> <span class="keyword">from</span> <span class="string">'./LazyQuery.svelte'</span>;</span></span><br><span class="line"><span class="language-javascript"> <span class="keyword">import</span> <span class="title class_">CopdGoldGradingChart</span> <span class="keyword">from</span> <span class="string">'./CopdGoldGradingChart.svelte'</span>;</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript"> <span class="keyword">const</span> queryClient = <span class="keyword">new</span> <span class="title class_">QueryClient</span>();</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript"> <span class="comment">// Define the query configuration object, just like with a standard `createQuery`.</span></span></span><br><span class="line"><span class="language-javascript"> <span class="keyword">const</span> queryOptions = {</span></span><br><span class="line"><span class="language-javascript"> <span class="attr">queryKey</span>: [<span class="string">'goldGradingData'</span>],</span></span><br><span class="line"><span class="language-javascript"> <span class="attr">queryFn</span>: <span class="title function_">async</span> () => {</span></span><br><span class="line"><span class="language-javascript"> <span class="comment">// Simulate a network request</span></span></span><br><span class="line"><span class="language-javascript"> <span class="keyword">await</span> <span class="keyword">new</span> <span class="title class_">Promise</span>(<span class="function"><span class="params">resolve</span> =></span> <span class="built_in">setTimeout</span>(resolve, <span class="number">1500</span>));</span></span><br><span class="line"><span class="language-javascript"> <span class="comment">// In a real application, this would be an API call</span></span></span><br><span class="line"><span class="language-javascript"> <span class="comment">// const response = await fetch('/api/gold-grading');</span></span></span><br><span class="line"><span class="language-javascript"> <span class="comment">// return await response.json();</span></span></span><br><span class="line"><span class="language-javascript"> <span class="keyword">return</span> { <span class="attr">data</span>: { <span class="attr">totalPatients</span>: <span class="number">1234</span>, <span class="attr">chartPoints</span>: [<span class="comment">/* ... */</span>] } };</span></span><br><span class="line"><span class="language-javascript"> }</span></span><br><span class="line"><span class="language-javascript"> };</span></span><br><span class="line"><span class="language-javascript"></span><span class="tag"></<span class="name">script</span>></span></span><br><span class="line"></span><br><span class="line"><span class="tag"><<span class="name">QueryClientProvider</span> <span class="attr">client</span>=<span class="string">{queryClient}</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">div</span> <span class="attr">style</span>=<span class="string">"height: 200vh;"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">p</span>></span>Scroll down to see the chart...<span class="tag"></<span class="name">p</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">div</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="tag"><<span class="name">LazyQuery</span> {<span class="attr">queryOptions</span>}></span></span><br><span class="line"> {#snippet children({ query })}</span><br><span class="line"> {#if query.isLoading}</span><br><span class="line"> <span class="tag"><<span class="name">p</span>></span>Loading chart data...<span class="tag"></<span class="name">p</span>></span></span><br><span class="line"> {:else if query.error}</span><br><span class="line"> <span class="tag"><<span class="name">p</span>></span>Error: {query.error.message}<span class="tag"></<span class="name">p</span>></span></span><br><span class="line"> {:else if query.data}</span><br><span class="line"> <span class="tag"><<span class="name">CopdGoldGradingChart</span></span></span><br><span class="line"><span class="tag"> <span class="attr">isLoading</span>=<span class="string">{query.isFetching}</span></span></span><br><span class="line"><span class="tag"> <span class="attr">error</span>=<span class="string">{query.error}</span></span></span><br><span class="line"><span class="tag"> <span class="attr">refetch</span>=<span class="string">{()</span> =></span> query.refetch()}</span><br><span class="line"> data={query.data.data}</span><br><span class="line"> totalPatients={query.data.data.totalPatients}</span><br><span class="line"> /></span><br><span class="line"> {/if}</span><br><span class="line"> {/snippet}</span><br><span class="line"> <span class="tag"></<span class="name">LazyQuery</span>></span></span><br><span class="line"><span class="tag"></<span class="name">QueryClientProvider</span>></span></span><br></pre></td></tr></table></figure>
<ul>
<li>页面初始加载时,<code>LazyQuery</code> 组件被渲染,但由于其 <code>div</code> 在 viewpoint 之外,<code>isIntersecting</code> 为 <code>false</code>。</li>
<li><code>createQuery</code> 被调用,但因为 <code>enabled</code> 条件为 <code>false</code>,查询处于禁用状态,不会发起任何网络请求。</li>
<li><code>children</code> 片段被渲染,此时 <code>query.isLoading</code> 为 <code>true</code>(这是 TanStack Query 禁用查询时的初始状态),显示 “Loading chart data…”。</li>
<li>当用户向下滚动,<code>div</code> 元素进入 viewpoint(或进入 <code>200px</code> 的预加载区域)。</li>
<li><code>useIntersectionObserver</code> 将 <code>isIntersecting</code> 的值更新为 <code>true</code>。</li>
<li>这个变化被 <code>createQuery</code> 的 <code>enabled</code> 访问器捕获,查询被自动激活,<code>queryFn</code> 开始执行。</li>
<li>TanStack Query 负责管理后续的状态变化(<code>isFetching</code>, <code>data</code>, <code>error</code>),并驱动 <code>children</code> 片段内的 UI 自动更新。</li>
</ul>
<h4 id="参考"><a class="header-anchor" href="#参考">¶</a>参考</h4>
<ul>
<li><a href="https://runed.dev/docs/utilities/use-intersection-observer">https://runed.dev/docs/utilities/use-intersection-observer</a></li>
<li><a href="https://tanstack.com/table/latest/docs/introduction">https://tanstack.com/table/latest/docs/introduction</a></li>
</ul>
]]></content>
<tags>
<tag>Technique</tag>
</tags>
</entry>
<entry>
<title>tick thread在Multicore OCaml中的作用</title>
<url>/2023/08/15/tick-thread%E5%9C%A8Multicore-OCaml%E4%B8%AD%E7%9A%84%E4%BD%9C%E7%94%A8/</url>
<content><![CDATA[<p>Multicore OCaml的程序在启动时会运行一个 tick thread,其实现如下:</p>
<figure class="highlight c"><table><tr><td class="code"><pre><span class="line"><span class="comment">/* The tick thread: posts a SIGPREEMPTION signal periodically */</span></span><br><span class="line"></span><br><span class="line"><span class="type">static</span> <span class="type">void</span> * <span class="title function_">caml_thread_tick</span><span class="params">(<span class="type">void</span> * arg)</span></span><br><span class="line">{</span><br><span class="line"> <span class="class"><span class="keyword">struct</span> <span class="title">timeval</span> <span class="title">timeout</span>;</span></span><br><span class="line"> <span class="type">sigset_t</span> mask;</span><br><span class="line"></span><br><span class="line"> <span class="comment">/* Block all signals so that we don't try to execute an OCaml signal handler*/</span></span><br><span class="line"> sigfillset(&mask);</span><br><span class="line"> pthread_sigmask(SIG_BLOCK, &mask, <span class="literal">NULL</span>);</span><br><span class="line"> <span class="keyword">while</span>(! caml_tick_thread_stop) {</span><br><span class="line"> <span class="comment">/* select() seems to be the most efficient way to suspend the</span></span><br><span class="line"><span class="comment"> thread for sub-second intervals */</span></span><br><span class="line"> timeout.tv_sec = <span class="number">0</span>;</span><br><span class="line"> timeout.tv_usec = Thread_timeout * <span class="number">1000</span>;</span><br><span class="line"> select(<span class="number">0</span>, <span class="literal">NULL</span>, <span class="literal">NULL</span>, <span class="literal">NULL</span>, &timeout);</span><br><span class="line"> <span class="comment">/* The preemption signal should never cause a callback, so don't</span></span><br><span class="line"><span class="comment"> go through caml_handle_signal(), just record signal delivery via</span></span><br><span class="line"><span class="comment"> caml_record_signal(). */</span></span><br><span class="line"> caml_record_signal(SIGPREEMPTION);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="literal">NULL</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>这是因为Multicore OCaml的GC目前需要一个进程(或一个Domain)中的所有线程一起参与以避免并发访问。如果一个线程在system call上被阻塞,那么整个Domain就会被卡住,直到该线程可以参与当前的垃圾收集。为了避免这个问题,tick 线程可以代替被阻塞的线程执行垃圾收集操作。</p>
]]></content>
<tags>
<tag>Technique</tag>
</tags>
</entry>
<entry>
<title>UUID v7 是如何加快 RDBMS 索引速度的</title>
<url>/2025/04/13/uuidv7-rdbms/</url>
<content><![CDATA[<p>UUID v7 之所以能显著提升关系型数据库(尤其是采用聚集索引的 MySQL InnoDB)在插入和查询速度,主要归功于它在 ID 中引入了“可排序的时间戳前缀”,从而大幅减少了 B-Tree 索引页分裂(page split)和数据碎片化。这里分几点来说:</p>
<p>一、 聚集索引(Clustered Index)的插入机制</p>
<p>在 InnoDB 中,聚集索引的叶子节点同时存储了行数据,且按照索引键(主键)顺序物理排序。</p>
<p>当新记录的主键完全随机(如 UUID v4)时,每次插入都会随机落在 B-Tree 的不同叶子页,导致频繁的页分裂和指针重排,而页分裂和随机 I/O 会带来大量的磁盘写放大和缓存抖动(cache churn),削弱吞吐并拉高延迟。</p>
<p>二、UUID v7 的时间排序特性</p>
<p>UUID v7 在高位(前 48 位)嵌入了以毫秒级精度的 Unix 时间戳,剩下的位用于随机数或序列号,这样生成的 ID 保持全局唯一性的同时,随着时间自然递增(即“近似单调递增”),新插入的记录几乎总是追加到 B-Tree 的最右端叶子节点。</p>
<p>参见 <a href="http://dbaplus.cn">dbaplus.cn</a> 的分析:</p>
<blockquote>
<p>“UUID v7 的创新之处在于其时间排序特性,它在前 48 位中嵌入了以毫秒为单位的 Unix 时间戳……可能在插入和查询操作上提供更好的性能”【1】。</p>
</blockquote>
<p>三、降低页分裂与碎片化</p>
<p>顺序或近似顺序的主键能使叶子节点连续增长,极少触发页分裂,并且更少的页分裂意味着更低的写放大(write amplification)和更稳定的插入延迟,同时,减少了空洞和链表重排,提高了磁盘和内存缓存的命中率。</p>
<p>四、提升查询局部性与缓存命中</p>
<p>因为数据物理上是按时间顺序紧凑写入,时间范围查询(如 “最近 1 小时的日志”)可以快速定位连续的叶子页,I/O 更聚集,内存缓冲池(buffer pool)或操作系统页缓存能更有效地缓存最近热数据,进一步加速查询。</p>
<hr>
<p>Rimon Tawadrous 在其 GitHub repo 中的测试,对比 100 万条逐条插入实验,UUID v7 相较 UUID v4 在单线程插入上速度快约 3.24%,多线程下更可观【1】。</p>
<hr>
<p>参考链接<br>
[1] “为什么 UUID 7 比 UUID 4 更适合作为 RDBMS 的聚集索引?” <a href="http://dbaplus.cn">dbaplus.cn</a><br>
<a href="https://dbaplus.cn/news-160-6313-1.html">https://dbaplus.cn/news-160-6313-1.html</a><br>
[2] “PostgreSQL and UUID as primary key” maciejwalkowiak<br>
<a href="https://maciejwalkowiak.com/blog/postgres-uuid-primary-key/">https://maciejwalkowiak.com/blog/postgres-uuid-primary-key/</a><br>
[3] “Optimised UUIDs in mysql” stitcher<br>
<a href="https://stitcher.io/blog/optimised-uuids-in-mysql">https://stitcher.io/blog/optimised-uuids-in-mysql</a><br>
[3] “Storing UUID Values in MySQL” percona<br>
<a href="https://www.percona.com/blog/store-uuid-optimized-way/">https://www.percona.com/blog/store-uuid-optimized-way/</a></p>
]]></content>
<tags>
<tag>Technique</tag>
</tags>
</entry>
<entry>
<title>v8中的Number.toString()</title>
<url>/2023/10/05/v8%E4%B8%AD%E7%9A%84Number-toString/</url>
<content><![CDATA[<p>这里讲一下JavaScript中<code>Number.toString()</code>的实现, 以V8为例。</p>
<ul>
<li><a href="https://github.com/v8/v8/">The official mirror of the V8 Git repository</a></li>
</ul>
<p>在很多地方都能看到:</p>
<figure class="highlight c++"><table><tr><td class="code"><pre><span class="line">*isolate-><span class="built_in">factory</span>()-><span class="built_in">NumberToString</span>(value);</span><br></pre></td></tr></table></figure>
<p>例如 <a href="https://github.com/v8/v8/blob/df99ca37a9d02a3318d3e5487c7803f6230b8f5f/src/builtins/builtins-number.cc#L140">/src/builtins/builtins-number.cc</a> 中。</p>
<p>下面看看<code>NumberToString</code>的定义, 应该是在 <a href="https://github.com/v8/v8/blob/829e8d18d73e21807c78223b0cc107c76c80da27/src/heap/factory-base.cc#L963-L978">src/heap/factory-base.cc</a> 中:</p>
<figure class="highlight c++"><table><tr><td class="code"><pre><span class="line"><span class="keyword">template</span> <<span class="keyword">typename</span> Impl></span><br><span class="line">Handle<String> FactoryBase<Impl>::<span class="built_in">NumberToString</span>(Handle<Object> number,</span><br><span class="line"> NumberCacheMode mode) {</span><br><span class="line"> <span class="built_in">SLOW_DCHECK</span>(<span class="built_in">IsNumber</span>(*number));</span><br><span class="line"> <span class="keyword">if</span> (<span class="built_in">IsSmi</span>(*number)) <span class="keyword">return</span> <span class="built_in">SmiToString</span>(Smi::<span class="built_in">cast</span>(*number), mode);</span><br><span class="line"></span><br><span class="line"> <span class="type">double</span> double_value = Handle<HeapNumber>::<span class="built_in">cast</span>(number)-><span class="built_in">value</span>();</span><br><span class="line"> <span class="comment">// Try to canonicalize doubles.</span></span><br><span class="line"> <span class="type">int</span> smi_value;</span><br><span class="line"> <span class="keyword">if</span> (<span class="built_in">DoubleToSmiInteger</span>(double_value, &smi_value)) {</span><br><span class="line"> <span class="keyword">return</span> <span class="built_in">SmiToString</span>(Smi::<span class="built_in">FromInt</span>(smi_value), mode);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="built_in">HeapNumberToString</span>(Handle<HeapNumber>::<span class="built_in">cast</span>(number), double_value,</span><br><span class="line"> mode);</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>可以看到这里调用了 <code>SmiToString</code>, 这里不往下翻这个函数的定义, 只需要知道Smi是什么即可。 Smi 是一种特殊的整数类型,它被用于表示较小的整数值,通常在 32 位系统中是 31 位有符号整数。Smi 类型的值存储在指针的低位,而指针的高位用于标记该值是一个 Smi 类型。<code>IsSmi</code> 函数会检查给定的值是否为 Smi 类型,如果是,则返回 true,否则返回 false。这个函数通常用于 V8 引擎内部的优化和性能优化。</p>
<p>所以<code>NumberToString</code>会判断number是否是一个smi, 如果是的话就调用SmiToString, 否则会尝试将其转换为double再去调用<code>DoubleToSmiInteger</code>, 将<code>DoubleToSmiInteger</code>的调用结果存在<code>smi_value</code>里面, 再通过调用<code>SmiToString</code>将<code>smi_value</code>转换为字符串。</p>
<p>如果这两条路都行不通的话,就直接调用<code>HeapNumberToString</code>了。</p>
<p><code>HeapNumberToString</code>的定义如下:</p>
<figure class="highlight c++"><table><tr><td class="code"><pre><span class="line"><span class="keyword">template</span> <<span class="keyword">typename</span> Impl></span><br><span class="line">Handle<String> FactoryBase<Impl>::<span class="built_in">HeapNumberToString</span>(Handle<HeapNumber> number,</span><br><span class="line"> <span class="type">double</span> value,</span><br><span class="line"> NumberCacheMode mode) {</span><br><span class="line"> <span class="type">int</span> hash = mode == NumberCacheMode::kIgnore</span><br><span class="line"> ? <span class="number">0</span></span><br><span class="line"> : <span class="built_in">impl</span>()-><span class="built_in">NumberToStringCacheHash</span>(value);</span><br><span class="line"></span><br><span class="line"> <span class="keyword">if</span> (mode == NumberCacheMode::kBoth) {</span><br><span class="line"> Handle<Object> cached = <span class="built_in">impl</span>()-><span class="built_in">NumberToStringCacheGet</span>(*number, hash);</span><br><span class="line"> <span class="keyword">if</span> (!<span class="built_in">IsUndefined</span>(*cached, <span class="built_in">isolate</span>())) <span class="keyword">return</span> Handle<String>::<span class="built_in">cast</span>(cached);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> Handle<String> result;</span><br><span class="line"> <span class="keyword">if</span> (value == <span class="number">0</span>) {</span><br><span class="line"> result = <span class="built_in">zero_string</span>();</span><br><span class="line"> } <span class="keyword">else</span> <span class="keyword">if</span> (std::<span class="built_in">isnan</span>(value)) {</span><br><span class="line"> result = <span class="built_in">NaN_string</span>();</span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> <span class="type">char</span> arr[kNumberToStringBufferSize];</span><br><span class="line"> <span class="function">base::Vector<<span class="type">char</span>> <span class="title">buffer</span><span class="params">(arr, arraysize(arr))</span></span>;</span><br><span class="line"> <span class="type">const</span> <span class="type">char</span>* string = <span class="built_in">DoubleToCString</span>(value, buffer);</span><br><span class="line"> result = <span class="built_in">CharToString</span>(<span class="keyword">this</span>, string, mode);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span> (mode != NumberCacheMode::kIgnore) {</span><br><span class="line"> <span class="built_in">impl</span>()-><span class="built_in">NumberToStringCacheSet</span>(number, hash, result);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> result;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>就是熟知的NaN, Undefined处理,重点在:</p>
<figure class="highlight c++"><table><tr><td class="code"><pre><span class="line"><span class="type">char</span> arr[kNumberToStringBufferSize];</span><br><span class="line"><span class="function">base::Vector<<span class="type">char</span>> <span class="title">buffer</span><span class="params">(arr, arraysize(arr))</span></span>;</span><br><span class="line"><span class="type">const</span> <span class="type">char</span>* string = <span class="built_in">DoubleToCString</span>(value, buffer);</span><br><span class="line">result = <span class="built_in">CharToString</span>(<span class="keyword">this</span>, string, mode);</span><br></pre></td></tr></table></figure>
<p>这里调用了<code>DoubleToCString</code>, 其定义在 <a href="https://github.com/v8/v8/blob/829e8d18d73e21807c78223b0cc107c76c80da27/src/numbers/conversions.cc#L1064-L1126">/src/numbers/conversions.cc</a> 中:</p>
<figure class="highlight c++"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="type">const</span> <span class="type">char</span>* <span class="title">DoubleToCString</span><span class="params">(<span class="type">double</span> v, base::Vector<<span class="type">char</span>> buffer)</span> </span>{</span><br><span class="line"> <span class="keyword">switch</span> (FPCLASSIFY_NAMESPACE::<span class="built_in">fpclassify</span>(v)) {</span><br><span class="line"> <span class="keyword">case</span> FP_NAN:</span><br><span class="line"> <span class="keyword">return</span> <span class="string">"NaN"</span>;</span><br><span class="line"> <span class="keyword">case</span> FP_INFINITE:</span><br><span class="line"> <span class="keyword">return</span> (v < <span class="number">0.0</span> ? <span class="string">"-Infinity"</span> : <span class="string">"Infinity"</span>);</span><br><span class="line"> <span class="keyword">case</span> FP_ZERO:</span><br><span class="line"> <span class="keyword">return</span> <span class="string">"0"</span>;</span><br><span class="line"> <span class="keyword">default</span>: {</span><br><span class="line"> <span class="keyword">if</span> (<span class="built_in">IsInt32Double</span>(v)) {</span><br><span class="line"> <span class="comment">// This will trigger if v is -0 and -0.0 is stringified to "0".</span></span><br><span class="line"> <span class="comment">// (see ES section 7.1.12.1 #sec-tostring-applied-to-the-number-type)</span></span><br><span class="line"> <span class="keyword">return</span> <span class="built_in">IntToCString</span>(<span class="built_in">FastD2I</span>(v), buffer);</span><br><span class="line"> }</span><br><span class="line"> <span class="function">SimpleStringBuilder <span class="title">builder</span><span class="params">(buffer.begin(), buffer.length())</span></span>;</span><br><span class="line"> <span class="type">int</span> decimal_point;</span><br><span class="line"> <span class="type">int</span> sign;</span><br><span class="line"> <span class="type">const</span> <span class="type">int</span> kV8DtoaBufferCapacity = base::kBase10MaximalLength + <span class="number">1</span>;</span><br><span class="line"> <span class="type">char</span> decimal_rep[kV8DtoaBufferCapacity];</span><br><span class="line"> <span class="type">int</span> length;</span><br><span class="line"></span><br><span class="line"> base::<span class="built_in">DoubleToAscii</span>(</span><br><span class="line"> v, base::DTOA_SHORTEST, <span class="number">0</span>,</span><br><span class="line"> base::<span class="built_in">Vector</span><<span class="type">char</span>>(decimal_rep, kV8DtoaBufferCapacity), &sign,</span><br><span class="line"> &length, &decimal_point);</span><br><span class="line"></span><br><span class="line"> <span class="keyword">if</span> (sign) builder.<span class="built_in">AddCharacter</span>(<span class="string">'-'</span>);</span><br><span class="line"></span><br><span class="line"> <span class="keyword">if</span> (length <= decimal_point && decimal_point <= <span class="number">21</span>) {</span><br><span class="line"> <span class="comment">// ECMA-262 section 9.8.1 step 6.</span></span><br><span class="line"> builder.<span class="built_in">AddString</span>(decimal_rep);</span><br><span class="line"> builder.<span class="built_in">AddPadding</span>(<span class="string">'0'</span>, decimal_point - length);</span><br><span class="line"></span><br><span class="line"> } <span class="keyword">else</span> <span class="keyword">if</span> (<span class="number">0</span> < decimal_point && decimal_point <= <span class="number">21</span>) {</span><br><span class="line"> <span class="comment">// ECMA-262 section 9.8.1 step 7.</span></span><br><span class="line"> builder.<span class="built_in">AddSubstring</span>(decimal_rep, decimal_point);</span><br><span class="line"> builder.<span class="built_in">AddCharacter</span>(<span class="string">'.'</span>);</span><br><span class="line"> builder.<span class="built_in">AddString</span>(decimal_rep + decimal_point);</span><br><span class="line"></span><br><span class="line"> } <span class="keyword">else</span> <span class="keyword">if</span> (decimal_point <= <span class="number">0</span> && decimal_point > <span class="number">-6</span>) {</span><br><span class="line"> <span class="comment">// ECMA-262 section 9.8.1 step 8.</span></span><br><span class="line"> builder.<span class="built_in">AddString</span>(<span class="string">"0."</span>);</span><br><span class="line"> builder.<span class="built_in">AddPadding</span>(<span class="string">'0'</span>, -decimal_point);</span><br><span class="line"> builder.<span class="built_in">AddString</span>(decimal_rep);</span><br><span class="line"></span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> <span class="comment">// ECMA-262 section 9.8.1 step 9 and 10 combined.</span></span><br><span class="line"> builder.<span class="built_in">AddCharacter</span>(decimal_rep[<span class="number">0</span>]);</span><br><span class="line"> <span class="keyword">if</span> (length != <span class="number">1</span>) {</span><br><span class="line"> builder.<span class="built_in">AddCharacter</span>(<span class="string">'.'</span>);</span><br><span class="line"> builder.<span class="built_in">AddString</span>(decimal_rep + <span class="number">1</span>);</span><br><span class="line"> }</span><br><span class="line"> builder.<span class="built_in">AddCharacter</span>(<span class="string">'e'</span>);</span><br><span class="line"> builder.<span class="built_in">AddCharacter</span>((decimal_point >= <span class="number">0</span>) ? <span class="string">'+'</span> : <span class="string">'-'</span>);</span><br><span class="line"> <span class="type">int</span> exponent = decimal_point - <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">if</span> (exponent < <span class="number">0</span>) exponent = -exponent;</span><br><span class="line"> builder.<span class="built_in">AddDecimalInteger</span>(exponent);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> builder.<span class="built_in">Finalize</span>();</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>不用过多解释, 已经很清晰了, <code>FastD2I</code> 就是 Fast Double to Integer的意思, 定义如下, 注释也很详尽:</p>
<figure class="highlight c++"><table><tr><td class="code"><pre><span class="line"><span class="comment">// The fast double-to-(unsigned-)int conversion routine does not guarantee</span></span><br><span class="line"><span class="comment">// rounding towards zero.</span></span><br><span class="line"><span class="comment">// The result is undefined if x is infinite or NaN, or if the rounded</span></span><br><span class="line"><span class="comment">// integer value is outside the range of type int.</span></span><br><span class="line"><span class="function"><span class="keyword">inline</span> <span class="type">int</span> <span class="title">FastD2I</span><span class="params">(<span class="type">double</span> x)</span> </span>{</span><br><span class="line"> <span class="built_in">DCHECK</span>(x <= INT_MAX);</span><br><span class="line"> <span class="built_in">DCHECK</span>(x >= INT_MIN);</span><br><span class="line"> <span class="keyword">return</span> <span class="built_in">static_cast</span><<span class="type">int32_t</span>>(x);</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>以上</p>
]]></content>
<tags>
<tag>Technique</tag>
</tags>
</entry>
<entry>
<title>Vertical slicing 在开发中的实践探索</title>
<url>/2025/05/27/vertical-slicing-practice/</url>
<content><![CDATA[<p>垂直切片 (Vertical Slicing) 是一种在敏捷软件开发中将产品需求(通常是用户故事)拆分为可独立交付的、具有端到端功能的小块的方法。这意味着每个“切片”都包含了从用户界面 (UI) 到底层数据库,以及中间所有业务逻辑层所需的工作。</p>
<p>与水平切片(即按技术分层,如先完成所有 UI,再完成所有后端逻辑)不同,垂直切片的目标是尽快交付一个虽小但完整可用的功能。</p>
<hr>
<p>想象一个蛋糕,垂直切片就像切下一块完整的蛋糕,包含从顶部到底部的每一层。在软件开发中,这意味着一个任务或用户故事的完成会涉及到:</p>
<ul>
<li>用户界面 (UI):用户能看到并与之交互的部分。</li>
<li>业务逻辑层 (Business Logic Layer):处理数据和执行核心功能的部分。</li>
<li>数据访问层 (Data Access Layer):与数据库或其他数据存储交互的部分。</li>
<li>数据库 (Database):存储数据的部分。</li>
</ul>
<p>所以一个垂直切片代表了一个可以独立运行、测试和向用户展示的小功能。</p>
<hr>
<p>在实践中,用垂直切片进行任务拆分或许可以按照如下步骤来实现:</p>
<p>一、从用户故事开始 (Start with User Stories):明确用户需要什么功能以及这个功能为用户带来的价值。例如:“作为一个注册用户,我希望能用我的邮箱和密码登录系统,以便访问我的个人资料。”</p>
<p>二、识别涉及的技术层面 (Identify Affected Layers)</p>
<p>对于登录功能,需要考虑:</p>
<ul>
<li>UI 层:登录表单(输入邮箱、密码的地方)、提交按钮、错误提示信息。</li>
<li>API/服务层:接收登录请求、验证用户凭证的接口。</li>
<li>业务逻辑层:校验输入格式、查询用户信息、验证密码、生成会话(Session)或令牌(Token)。</li>
<li>数据访问层:从数据库中读取用户信息。</li>
</ul>
<p>三、创建可交付的小功能块 (Create Small, Deliverable Chunks)</p>
<p>就是将一个大的用户故事拆分成更小的、但仍然是垂直的、可独立交付的故事。例如,可以将“用户登录”进一步细化:</p>
<p>切片1 (基础登录):用户可以使用正确的邮箱和密码成功登录。这包含了 UI 输入、后端验证和数据库查询。</p>
<p>切片2 (错误处理):用户输入错误的邮箱或密码时,系统给出明确的错误提示。这可能只涉及 UI 和业务逻辑层的少量修改。</p>
<p>切片3 (“记住我” 功能):用户可以选择“记住我”,下次访问时自动登录。这可能涉及 UI、业务逻辑和客户端存储。</p>
<p>四、确保每个切片都有价值 (Ensure Each Slice Has Value):每完成一个切片,都应该为用户或产品带来可感知的价值,并且理想情况下是可以演示给利益相关者看的。</p>
<p>五、保持切片足够小 (Keep Slices Small Enough):每个切片的工作量应该小到可以在一个迭代周期(例如 Sprint)内完成。这有助于团队保持专注,并快速获得反馈。</p>
<hr>
<p>这里有一些拆分技巧:</p>
<ul>
<li>按操作流程拆分:例如,一个复杂的表单提交可以先实现基本信息的提交,后续再添加高级选项的提交。</li>
<li>按业务规则拆分:先实现核心的业务规则,再逐步添加次要的或复杂的规则。</li>
<li>按数据类型或参数拆分:先支持一种数据类型或最常用的参数,再扩展到其他类型。</li>
<li>按用户角色或权限拆分:先实现某个核心角色的功能,再实现其他角色的特定功能。</li>
<li>简化错误处理或用户体验:先实现基本功能,再完善错误处理和用户体验细节。</li>
</ul>
]]></content>
<tags>
<tag>Technique</tag>
</tags>
</entry>
<entry>
<title>中老年人沉迷手机的问题</title>
<url>/2024/09/08/%E4%B8%AD%E8%80%81%E5%B9%B4%E4%BA%BA%E6%B2%89%E8%BF%B7%E6%89%8B%E6%9C%BA%E7%9A%84%E9%97%AE%E9%A2%98/</url>
<content><![CDATA[<p>沉迷于手机的老年人,其实很多都是因为儿女不在身边,他们只能把这份孤独转嫁到手机上。</p>
<p>如果减少了这份孤独感和寂寞感,才能真正让他们从手机里解放出来。</p>
<p>其实不是不让他们看手机,而是一定要控制好量和度。</p>
<p>一味的打击,反对,也许只会将两代人之间的关系越推越远。</p>
<p>唯一的办法,是看见。</p>
<p>看见他们的孤独,看见他们对生命流逝的恐惧,看见他们在尊重和陪伴之中折射出来的爱。</p>
]]></content>
<tags>
<tag>Life</tag>
</tags>
</entry>
<entry>
<title>二〇二三年八月一日</title>
<url>/2023/08/01/%E4%BA%8C%E3%80%87%E4%BA%8C%E4%B8%89%E5%B9%B4%E5%85%AB%E6%9C%88%E4%B8%80%E6%97%A5/</url>
<content><![CDATA[<p>阳光给他病态一般白的皮肤注入了一些生机,左手手臂上斑驳的伤痕,每一道都诉诸着他对家人浓重的思念。那是在暗无天日的岁月里,他通过痛楚得到的慰藉。</p>
<p>时光对谁都不温柔,愿意对我温柔的,只有身边陪我一起迎接时光洗礼的人。</p>
<p>宇宙中也不会有什么声音, 视觉上再震撼的毁灭也只是发生在沉默之中。</p>
<blockquote>
<p>一切重新沉寂下来。</p>
</blockquote>
]]></content>
<categories>
<category>gallery</category>
</categories>
</entry>
<entry>
<title>二〇二三年六月二十九日</title>
<url>/2023/06/29/%E4%BA%8C%E3%80%87%E4%BA%8C%E4%B8%89%E5%B9%B4%E5%85%AD%E6%9C%88%E4%BA%8C%E5%8D%81%E4%B9%9D%E6%97%A5/</url>
<content><![CDATA[<p>我能走遍大街小巷,但我走不出时间,这里有太多东西网住我了,每一个不知所措的老人家,每一个满身泥土的农民,每一只嬉戏打闹的猫狗和每一寸土地下的家人。</p>
<p>我知道,能明确感受到,远处的风力发电机每转一圈我对这里的牵挂就更深一点,太阳每次升到我头顶都会把我的身体压进泥土里一点。</p>
<p>夕阳下的街头空无一人,窗户间的缝隙吹来寒风,隔壁回锅肉的香味混在其中,困顿中还有美好的可能,所以别让自己太过消沉。</p>
<p>所以别让自己太过消沉。</p>
]]></content>
<categories>
<category>gallery</category>
</categories>
</entry>
<entry>
<title>二〇二三年十一月二十九日</title>
<url>/2023/11/29/%E4%BA%8C%E3%80%87%E4%BA%8C%E4%B8%89%E5%B9%B4%E5%8D%81%E4%B8%80%E6%9C%88%E4%BA%8C%E5%8D%81%E4%B9%9D%E6%97%A5/</url>
<content><![CDATA[<p>我们的人生,都是某座不知名的山上某片不知名的乱林里的某颗不知名姓的杂草,春带来霉湿,夏带来毒辣,秋带来阴沉,冬带来寒冰。</p>
<p>我们渴望什么,生活就拿走什么。我们害怕什么,时间就带来什么。我们不断下沉,下沉。仿佛同这漫长的人生一样,等不到尽头。</p>
<p>而衣冠立于浊世,先是个体,然后结对,最后成群,这一生总要有这样那样的分别,但迈过这一年,再过下一年,有多少这样的每一年,都会被我们尽力握住,不会再让风雪吹散。</p>
<p>过去我觉得自己是一朵丑云,黑漆漆,阴沉沉,总是独自下雨,世间所有美好都离我那样的远,永远都不会注意到我。于是我开始追逐太阳,我把自己和太阳绑起来,系在我的食指上。</p>
<p>这世上也不是所有事都算得准的。</p>
<p>云仰望着太阳,羡慕他的炙热,或许太阳也在仰望着云,渴求他的停留。</p>
<p>地球距离太阳1.5亿公里,如无意外,两者此生不会相遇,但从此以后,我与世间美好再也不会分离。</p>
]]></content>
<categories>
<category>gallery</category>
</categories>
</entry>
<entry>
<title>二〇二三年十二月一日</title>
<url>/2023/12/01/%E4%BA%8C%E3%80%87%E4%BA%8C%E4%B8%89%E5%B9%B4%E5%8D%81%E4%BA%8C%E6%9C%88%E4%B8%80%E6%97%A5/</url>
<content><![CDATA[<p>他说:“路挺远的,你慢走啊。”</p>
<p>我朗声回答:“放心,我与月光并肩走的,丢不了。”</p>
<p>又坐地铁回去,太累,把帽檐压下来一点儿, 在地铁上靠着栏杆睡觉。</p>
<p>长夜漫漫,星河天悬。</p>
<p>这年,我与世间的美好正式走到一起。以自由的姿态捱到结尾,新的篇章重新启航。</p>
<p>我是江湖河海上唯一的灯塔,不放过世间每一处美好的船。</p>
<p>我的衣服从衬衫变成布衣。</p>
<p>能空闲下来与家人朋友联系时间的很少,偶尔在群里冒个泡,大多都是报平安的话。</p>
<p>时间一久,我就把自己的近况写成长段形式,用毛笔写在信纸上。最后永远落款“一切安好”</p>
<p>生活琐碎,偶尔看看手机,时不时拍几张在山中穿行的照片,还有最近的蔬菜瓜果树木的成长迹象。</p>
<p>我和毛毛说还记得外面的世界,记得那些披星戴月的日子。</p>
<p>还说,你知不知道灰灰,它比你聪明,没你好看,你也就剩好看了。</p>
<p>修长的身影顶着斗笠在山里中飞奔,一刀刀砍断荆棘,踩空时摔在松软的泥土上、失神时跌进干燥的枯枝中,但仍然记得曾经骑车过天桥马路的感觉。</p>
<p>在山顶时,自由给了我一切。</p>
<p>身处在青春里,就做感受它的事。爱恨、稚嫩、稳重与勇气,推动每个人脚下的每一步,影响一生的轨迹。</p>
<p>我把曾经的全家福裱起来放在家里的储物架上。</p>
<p>上边除了它,还有我在外面的世界时留下的一些纪念品,比如建模比赛的奖杯,玉龙雪山上的树枝,还有那腾格里沙漠中沙粒做成的沙漏。</p>
<p>长段长段的近况记录被放在最显眼的位置,它并不是一次写成的,而是日复一日的点滴记录汇聚而成。</p>
<p>山上的房子挺小的,坐北朝南,但有一处简陋的书房,依着阳光而建,里边儿有我拿来放吉他的架子。</p>
<p>那把锈迹斑斑的横刀,被放在了地下室,我还专门上了油保养。</p>
<p>我时不时也拿出来看看,毛毛总会蹭到上面的油。</p>
<hr>
<p>我毕业于二零二五年,</p>
<p>毕业后,我回高中的教师宿舍见了一次老班,他的女儿已经结婚,有孕在身,家庭美满。</p>
<p>我送上那只玉龙雪山上的枯木枝,待她或他出生后,可与胎毛一起制成毛笔,书写一趟新的人生。</p>
<p>和他说了很多话,从六年前到现在,从十六岁到二十二岁。</p>
<p>离开他家时,我路过学生宿舍,和一个陌生的又熟悉的身影不约而同地停下脚步。</p>
<p>我拿出手机想看看时间,偶然发现朋友圈有一些陌生人发的新动态,讲他们在这个世界的其他角落遇见的事情,有好有坏。</p>
<p>“咔。”</p>
<p>锁屏,关上手机。</p>
<p>把头别过去深呼吸,再转眼看那道身影。我站在他身边看他,用指腹抹掉不自知的泪。也许是泪,也许是打了个哈欠,我自己都不知道。</p>
<p>世间还会有很多美好。</p>
<p>我把家里的窗帘换成黑色,每周换一种阳台的花草,雕了一个无脸之人,把西瓜整个放进冰箱里冷冻,把家里大门的密码锁设成仰望天空的固定日期。在冬天里光脚奔跑,在夏天里见血见汗,见山林里的瓢泼大雨。</p>
<p>或许有一天,我能给这个世界写一封信。</p>
<p>非要说“热爱”是从什么时候开始的,我真的没有概念。</p>
<p>恍惚中,想得起那一年除夕,我在街头看着万家灯火,抬头是烟火映衬的星空,低头是星空点亮的马路,</p>
<p>那时候,我摘下了帽子,为了看星空。眼神和现在一样。</p>
<p>谢谢你,这个世界。</p>
<p>也谢谢自己,成为了自己的光。</p>
<p>我是这一方小世界的月亮。</p>
<p>还是广袤宇宙中的一颗有名有姓的星星,</p>
<p>宇宙、银河、太阳,都不重要。</p>
<p>热爱这个世界,是我成长里最美妙的勇敢事迹。</p>
]]></content>
<categories>
<category>gallery</category>
</categories>
</entry>
<entry>
<title>二〇二三年十二月二日</title>
<url>/2023/12/02/%E4%BA%8C%E3%80%87%E4%BA%8C%E4%B8%89%E5%B9%B4%E5%8D%81%E4%BA%8C%E6%9C%88%E4%BA%8C%E6%97%A5/</url>
<content><![CDATA[<p>夏日凌晨的海风,裹挟着季风带的温热的潮气不远万里奔赴而来,搅碎了一汪流动的星星。</p>
<p>天空泛起一丝鱼肚白,那是每年夏至,凌晨四点多就会早起的日出。</p>
<p>而身后那栋为我而建的世界上独一无二的小屋子,正通宵达旦地亮着灯火,里面是我的痕迹,我的热爱,我的温暖和祝福。</p>
<p>身前,我的山林和小狗,正笨拙地试图从自然的馈赠里找到那份属于它们的礼物。</p>
<p>它们那样爱着我,那样忠诚于我。</p>
<p>那一刻,我突然就想起了曾经在书上看过的一句很喜欢的话——“世界先爱了我,我不能不爱他.”</p>
<p>曾经在冬季日出的时候,我见过海岸线浮满碎冰的模样,那是连太阳都会显得寂寥和落寞的冷清。</p>
<p>可是太阳始终当着太阳,守着一个恒星的职责,不知疲倦地用自己炽热的温度和光芒试图唤醒沉睡的冬日。</p>
<p>直到终于有一天,有人在冬夜里复苏,爱上了那个比夏天更炽烈的温度,然后海浪成了新娘白色的花环,我成了太阳一生的爱人。</p>
<p>所以我始终愿意相信,是这个世界先温柔地爱了我。</p>
<p>哪怕世界给予我的这份爱,在最初的时候,来得并不那么明显,也并不那么浓烈,我也曾因此孤独过,无助过,迷茫过,放弃过。</p>
<p>可是那份爱最终还是随着冰雪消融,春暖花开,随着夏天剧烈摇晃过后的气泡水,滋滋地冒了出来,连盖上盖子,也没有办法捂住。</p>
<p>所以我拥有了世界上最好的义无反顾的爱。</p>
<p>看着前方,轻轻叫了一声:“毛毛”</p>
<p>狗狗立马回了头,和乍出的日光,和突然转动的风力发电机,和突然被风吹动的山林,同步发生。好像只要我一声令下,这个世界就愿意为我而生动。</p>
<p>可这世间哪里会在乎茫茫人海中这不值一提的爱</p>
<p>我们都只不过是平凡世界里平凡生活着的人们,如果非要说有什么不同,那就是我只爱着这个世界而已。</p>
<p>于是我看着那只大笨狗,看着并没有被虚化成背景的世界,温柔地弯起了唇角:“有句话今天我一直忘记告诉你了。”</p>
<p>狗狗叼起一根树枝,歪头不解地看着我。</p>
<p>然后我就在山峰浮现出第一缕阳光时,笑着对这个世界说:“我爱你,会永远爱你,永远最爱你。”</p>
<p>我爱这个因为有我而变得温柔的世界。</p>
<p>这将是我与周遭的一切,热爱一生,共度一生的地方。</p>
]]></content>
<categories>
<category>gallery</category>
</categories>
</entry>
<entry>
<title>二〇二五年九月二十四日</title>
<url>/2025/09/24/%E4%BA%8C%E3%80%87%E4%BA%8C%E4%BA%94%E5%B9%B4%E4%B9%9D%E6%9C%88%E4%BA%8C%E5%8D%81%E5%9B%9B%E6%97%A5/</url>
<content><![CDATA[<p>从小到大,我好像一直是个独立的人。</p>
<p>一个人看书、走路,一个人发呆、考试。偶尔去江边吹吹晚风,看看天上的云,在街角听着突然传来的美妙音乐。</p>
<p>有时候会自己去看场电影,散场月落才回来。在校园里寻找猫的足迹,逛逛闹市,找寻自己喜欢的玩意。</p>
<p>偶尔一个人出趟远门,去见一见我从未见过的蓝天白云,和清风野草说说话。书上文人墨客笔下的云波流霞,大山大河……总是使我心之向往。</p>
<p>柴米油盐,生活上的一地鸡毛我好像都能自己解决。</p>
<p>生病了,也会一个人去医院。</p>
<p>人生抉择上的事,我不想与太多的人讨论。感觉除了大病生死,我不需要依靠任何人。</p>
<p>别人看我的状态总是很好,无牵无挂,自由自在,好像没有什么遗憾与忧愁。就像只气球样晃晃悠悠在天空飘着,在孤独的境遇里一边失去一边长大。</p>
<p>以前我常常觉得自己清高地与这个世界格格不入,自诩浪漫至死不渝。</p>
<p>后来,我才发现一个人的时候,所见到的星空斑斓,树影婆娑……固然美好,但只有自己目睹,总觉得有些空落落的。</p>
<p>其实根本不需要那么多的社交动态,大家也不会多关注我半分,身边有趣的事只要有一个人分享就好。</p>
<p>一个人可以完成的事,其实两个人做完也不赖。</p>
<p>那些我曾以为需要跋山涉水,花费很多心思来获取的快乐,有时候或许只要有了这样一个人就能轻松抵达。</p>
<p>有人说独处是件很享受的事,孤独是一种境遇。</p>
<p>诚然。</p>
<p>但是我从来就不是什么多有抱负的人,也不想取得多大成绩,此生可能只想要开心快乐的活,俗气而热烈。</p>
<p>纵然我生来平庸,长相普通,自身还有许多的毛病,但是内心却依旧渴望爱。</p>
<p>是我渴望而又爱面子,懦弱而又要强。</p>
<p>不是我不喜欢,而是没人愿意聆听我的琐碎。</p>
<p>恋爱的意义何在?这是一个可以写很多文章的话题。</p>
<p>于我而言,与其说是喜欢一个人,不如说是喜欢和她在一起的我自己。那时候的我勇敢快乐,内心一触碰都是柔软。</p>
<p>即使生活已经磨掉了我一部分的勇气和温柔。但是有人依然让我相信,失去的还会再长回来,新长出来的依旧闪闪发亮。</p>
<p>是这么一个人给了我久违的温暖,让我不再害怕逃避,心有所依。</p>
<p>是这么一个人让我明白了勇气并不是无所畏惧,而是懂得了有除了畏惧以外更重要的事。</p>
<p>在晚风与白雪覆盖的地方,我想要趁着窗外的月光,趁着满天的繁星,拥抱、亲吻,充满欲望。</p>
<p>有人说恋爱会让人变得脆弱且敏感。</p>
<p>在这个世界上,有时我再怎么努力懂事,还是会有很多人不喜欢我,妒忌总是多于欢喜。遇见困难的时候,大家都是在独自前行。</p>
<p>可是只有这个人看见了我独自坚强的背后,愿意安静地坐在我的身旁。在她那小小的笨拙里,藏着许多孩子般的真诚与可爱。</p>
<p>很多低成本的付出,却往往让人感动,但这并不稀有。</p>
<p>从对方的谈吐、视野、知识、情绪等方面,让我看到从未了解过的一面,愿意进步。</p>
<p>相互成为彼此的庇佑,风雨不改。</p>
<p>当我真实地去爱一个人,意味着除了家人朋友,我还拥有了另一种完全不同的爱。这是一个独立人格,所具有的最宝贵的能力。</p>
<p>去爱一个人,并得到她同样的爱。无私又默契,热情而坦荡。</p>
<p>怎样过一生,这一生都会过去。或许我们在一起是浪费了很多时间,但是和她在一起浪费的时间,我不觉得没有意义。这是我的选择,我愿意过这样的一生。</p>
<p>流水,飞鸟,星空……古人用一首首爱之诗歌、散文、小说写尽爱情,千年万年如一日。或是林间绿意,流水潺潺,或是日出灿烂,日落漫漫,或是东升的月,林前的雪水,如初春的樱花……</p>
<p>古往今来,爱情所拥有的魅力令无数的文人墨客如痴如醉,甘之如饴。就像是刚刚走过朦胧的雨天,阳光温柔淅沥的洒在脸上,所有阴霾一扫而空。</p>
<p>尽管享受着独行时的美丽,却也愿意真诚、浪漫地爱一个人。</p>
<p>虽然我早已不强求世界上有这么一个人,</p>
<p>但我会一直等待,甚至期待你的到来。</p>
<p>我叫韩暮秋,幸会。</p>
]]></content>
<categories>
<category>gallery</category>
</categories>
</entry>
<entry>
<title>二〇二五年三月十七日</title>
<url>/2025/03/17/%E4%BA%8C%E3%80%87%E4%BA%8C%E4%BA%94%E5%B9%B4%E4%B8%89%E6%9C%88%E5%8D%81%E4%B8%83%E6%97%A5/</url>
<content><![CDATA[<p>人类其实根本无法同时拥有对死亡的坦然和对生活的眷恋。</p>
<p>看见死亡,看见新生,看见朝露凝于叶尖,看见惊雷震碎山月,</p>
<p>它们是因缘聚散吗?</p>
<p>那露珠消散后去了哪里?</p>
<p>此有故彼有,此生故彼灭。</p>
<p>当晨光加热露珠表面至 <mjx-container class="MathJax" jax="SVG"><svg style="vertical-align: -0.489ex;" xmlns="http://www.w3.org/2000/svg" width="6.242ex" height="1.995ex" role="img" focusable="false" viewBox="0 -666 2759 882"><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="scale(1,-1)"><g data-mml-node="math"><g data-mml-node="mn"><path data-c="30" d="M96 585Q152 666 249 666Q297 666 345 640T423 548Q460 465 460 320Q460 165 417 83Q397 41 362 16T301 -15T250 -22Q224 -22 198 -16T137 16T82 83Q39 165 39 320Q39 494 96 585ZM321 597Q291 629 250 629Q208 629 178 597Q153 571 145 525T137 333Q137 175 145 125T181 46Q209 16 250 16Q290 16 318 46Q347 76 354 130T362 333Q362 478 354 524T321 597Z"></path><path data-c="2E" d="M78 60Q78 84 95 102T138 120Q162 120 180 104T199 61Q199 36 182 18T139 0T96 17T78 60Z" transform="translate(500,0)"></path><path data-c="31" d="M213 578L200 573Q186 568 160 563T102 556H83V602H102Q149 604 189 617T245 641T273 663Q275 666 285 666Q294 666 302 660V361L303 61Q310 54 315 52T339 48T401 46H427V0H416Q395 3 257 3Q121 3 100 0H88V46H114Q136 46 152 46T177 47T193 50T201 52T207 57T213 61V578Z" transform="translate(778,0)"></path></g><g data-mml-node="mi" transform="translate(1278,0)"><path data-c="1D707" d="M58 -216Q44 -216 34 -208T23 -186Q23 -176 96 116T173 414Q186 442 219 442Q231 441 239 435T249 423T251 413Q251 401 220 279T187 142Q185 131 185 107V99Q185 26 252 26Q261 26 270 27T287 31T302 38T315 45T327 55T338 65T348 77T356 88T365 100L372 110L408 253Q444 395 448 404Q461 431 491 431Q504 431 512 424T523 412T525 402L449 84Q448 79 448 68Q448 43 455 35T476 26Q485 27 496 35Q517 55 537 131Q543 151 547 152Q549 153 557 153H561Q580 153 580 144Q580 138 575 117T555 63T523 13Q510 0 491 -8Q483 -10 467 -10Q446 -10 429 -4T402 11T385 29T376 44T374 51L368 45Q362 39 350 30T324 12T288 -4T246 -11Q199 -11 153 12L129 -85Q108 -167 104 -180T92 -202Q76 -216 58 -216Z"></path></g><g data-mml-node="mi" transform="translate(1881,0)"><path data-c="1D45A" d="M21 287Q22 293 24 303T36 341T56 388T88 425T132 442T175 435T205 417T221 395T229 376L231 369Q231 367 232 367L243 378Q303 442 384 442Q401 442 415 440T441 433T460 423T475 411T485 398T493 385T497 373T500 364T502 357L510 367Q573 442 659 442Q713 442 746 415T780 336Q780 285 742 178T704 50Q705 36 709 31T724 26Q752 26 776 56T815 138Q818 149 821 151T837 153Q857 153 857 145Q857 144 853 130Q845 101 831 73T785 17T716 -10Q669 -10 648 17T627 73Q627 92 663 193T700 345Q700 404 656 404H651Q565 404 506 303L499 291L466 157Q433 26 428 16Q415 -11 385 -11Q372 -11 364 -4T353 8T350 18Q350 29 384 161L420 307Q423 322 423 345Q423 404 379 404H374Q288 404 229 303L222 291L189 157Q156 26 151 16Q138 -11 108 -11Q95 -11 87 -5T76 7T74 17Q74 30 112 181Q151 335 151 342Q154 357 154 369Q154 405 129 405Q107 405 92 377T69 316T57 280Q55 278 41 278H27Q21 284 21 287Z"></path></g></g></g></svg></mjx-container> 深度时,表层水分子动能突破氢键束缚(键能约 <mjx-container class="MathJax" jax="SVG"><svg style="vertical-align: -0.566ex;" xmlns="http://www.w3.org/2000/svg" width="9.762ex" height="2.262ex" role="img" focusable="false" viewBox="0 -750 4315 1000"><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="scale(1,-1)"><g data-mml-node="math"><g data-mml-node="mn"><path data-c="32" d="M109 429Q82 429 66 447T50 491Q50 562 103 614T235 666Q326 666 387 610T449 465Q449 422 429 383T381 315T301 241Q265 210 201 149L142 93L218 92Q375 92 385 97Q392 99 409 186V189H449V186Q448 183 436 95T421 3V0H50V19V31Q50 38 56 46T86 81Q115 113 136 137Q145 147 170 174T204 211T233 244T261 278T284 308T305 340T320 369T333 401T340 431T343 464Q343 527 309 573T212 619Q179 619 154 602T119 569T109 550Q109 549 114 549Q132 549 151 535T170 489Q170 464 154 447T109 429Z"></path><path data-c="33" d="M127 463Q100 463 85 480T69 524Q69 579 117 622T233 665Q268 665 277 664Q351 652 390 611T430 522Q430 470 396 421T302 350L299 348Q299 347 308 345T337 336T375 315Q457 262 457 175Q457 96 395 37T238 -22Q158 -22 100 21T42 130Q42 158 60 175T105 193Q133 193 151 175T169 130Q169 119 166 110T159 94T148 82T136 74T126 70T118 67L114 66Q165 21 238 21Q293 21 321 74Q338 107 338 175V195Q338 290 274 322Q259 328 213 329L171 330L168 332Q166 335 166 348Q166 366 174 366Q202 366 232 371Q266 376 294 413T322 525V533Q322 590 287 612Q265 626 240 626Q208 626 181 615T143 592T132 580H135Q138 579 143 578T153 573T165 566T175 555T183 540T186 520Q186 498 172 481T127 463Z" transform="translate(500,0)"></path></g><g data-mml-node="mi" transform="translate(1000,0)"><path data-c="1D458" d="M121 647Q121 657 125 670T137 683Q138 683 209 688T282 694Q294 694 294 686Q294 679 244 477Q194 279 194 272Q213 282 223 291Q247 309 292 354T362 415Q402 442 438 442Q468 442 485 423T503 369Q503 344 496 327T477 302T456 291T438 288Q418 288 406 299T394 328Q394 353 410 369T442 390L458 393Q446 405 434 405H430Q398 402 367 380T294 316T228 255Q230 254 243 252T267 246T293 238T320 224T342 206T359 180T365 147Q365 130 360 106T354 66Q354 26 381 26Q429 26 459 145Q461 153 479 153H483Q499 153 499 144Q499 139 496 130Q455 -11 378 -11Q333 -11 305 15T277 90Q277 108 280 121T283 145Q283 167 269 183T234 206T200 217T182 220H180Q168 178 159 139T145 81T136 44T129 20T122 7T111 -2Q98 -11 83 -11Q66 -11 57 -1T48 16Q48 26 85 176T158 471L195 616Q196 629 188 632T149 637H144Q134 637 131 637T124 640T121 647Z"></path></g><g data-mml-node="mi" transform="translate(1521,0)"><path data-c="1D43D" d="M447 625Q447 637 354 637H329Q323 642 323 645T325 664Q329 677 335 683H352Q393 681 498 681Q541 681 568 681T605 682T619 682Q633 682 633 672Q633 670 630 658Q626 642 623 640T604 637Q552 637 545 623Q541 610 483 376Q420 128 419 127Q397 64 333 21T195 -22Q137 -22 97 8T57 88Q57 130 80 152T132 174Q177 174 182 130Q182 98 164 80T123 56Q115 54 115 53T122 44Q148 15 197 15Q235 15 271 47T324 130Q328 142 387 380T447 625Z"></path></g><g data-mml-node="TeXAtom" data-mjx-texclass="ORD" transform="translate(2154,0)"><g data-mml-node="mo"><path data-c="2F" d="M423 750Q432 750 438 744T444 730Q444 725 271 248T92 -240Q85 -250 75 -250Q68 -250 62 -245T56 -231Q56 -221 230 257T407 740Q411 750 423 750Z"></path></g></g><g data-mml-node="mi" transform="translate(2654,0)"><path data-c="1D45A" d="M21 287Q22 293 24 303T36 341T56 388T88 425T132 442T175 435T205 417T221 395T229 376L231 369Q231 367 232 367L243 378Q303 442 384 442Q401 442 415 440T441 433T460 423T475 411T485 398T493 385T497 373T500 364T502 357L510 367Q573 442 659 442Q713 442 746 415T780 336Q780 285 742 178T704 50Q705 36 709 31T724 26Q752 26 776 56T815 138Q818 149 821 151T837 153Q857 153 857 145Q857 144 853 130Q845 101 831 73T785 17T716 -10Q669 -10 648 17T627 73Q627 92 663 193T700 345Q700 404 656 404H651Q565 404 506 303L499 291L466 157Q433 26 428 16Q415 -11 385 -11Q372 -11 364 -4T353 8T350 18Q350 29 384 161L420 307Q423 322 423 345Q423 404 379 404H374Q288 404 229 303L222 291L189 157Q156 26 151 16Q138 -11 108 -11Q95 -11 87 -5T76 7T74 17Q74 30 112 181Q151 335 151 342Q154 357 154 369Q154 405 129 405Q107 405 92 377T69 316T57 280Q55 278 41 278H27Q21 284 21 287Z"></path></g><g data-mml-node="mi" transform="translate(3532,0)"><path data-c="1D45C" d="M201 -11Q126 -11 80 38T34 156Q34 221 64 279T146 380Q222 441 301 441Q333 441 341 440Q354 437 367 433T402 417T438 387T464 338T476 268Q476 161 390 75T201 -11ZM121 120Q121 70 147 48T206 26Q250 26 289 58T351 142Q360 163 374 216T388 308Q388 352 370 375Q346 405 306 405Q243 405 195 347Q158 303 140 230T121 120Z"></path></g><g data-mml-node="mi" transform="translate(4017,0)"><path data-c="1D459" d="M117 59Q117 26 142 26Q179 26 205 131Q211 151 215 152Q217 153 225 153H229Q238 153 241 153T246 151T248 144Q247 138 245 128T234 90T214 43T183 6T137 -11Q101 -11 70 11T38 85Q38 97 39 102L104 360Q167 615 167 623Q167 626 166 628T162 632T157 634T149 635T141 636T132 637T122 637Q112 637 109 637T101 638T95 641T94 647Q94 649 96 661Q101 680 107 682T179 688Q194 689 213 690T243 693T254 694Q266 694 266 686Q266 675 193 386T118 83Q118 81 118 75T117 65V59Z"></path></g></g></g></svg></mjx-container>)。<br>
这种相变并非整齐划一的队列解散,而是呈现量子隧穿效应——单个水分子以 <mjx-container class="MathJax" jax="SVG"><svg style="vertical-align: -0.05ex;" xmlns="http://www.w3.org/2000/svg" width="6.356ex" height="2.005ex" role="img" focusable="false" viewBox="0 -864 2809.2 886"><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="scale(1,-1)"><g data-mml-node="math"><g data-mml-node="msup"><g data-mml-node="mn"><path data-c="31" d="M213 578L200 573Q186 568 160 563T102 556H83V602H102Q149 604 189 617T245 641T273 663Q275 666 285 666Q294 666 302 660V361L303 61Q310 54 315 52T339 48T401 46H427V0H416Q395 3 257 3Q121 3 100 0H88V46H114Q136 46 152 46T177 47T193 50T201 52T207 57T213 61V578Z"></path><path data-c="30" d="M96 585Q152 666 249 666Q297 666 345 640T423 548Q460 465 460 320Q460 165 417 83Q397 41 362 16T301 -15T250 -22Q224 -22 198 -16T137 16T82 83Q39 165 39 320Q39 494 96 585ZM321 597Q291 629 250 629Q208 629 178 597Q153 571 145 525T137 333Q137 175 145 125T181 46Q209 16 250 16Q290 16 318 46Q347 76 354 130T362 333Q362 478 354 524T321 597Z" transform="translate(500,0)"></path></g><g data-mml-node="TeXAtom" transform="translate(1033,393.1) scale(0.707)" data-mjx-texclass="ORD"><g data-mml-node="mo"><path data-c="2212" d="M84 237T84 250T98 270H679Q694 262 694 250T679 230H98Q84 237 84 250Z"></path></g><g data-mml-node="mn" transform="translate(778,0)"><path data-c="31" d="M213 578L200 573Q186 568 160 563T102 556H83V602H102Q149 604 189 617T245 641T273 663Q275 666 285 666Q294 666 302 660V361L303 61Q310 54 315 52T339 48T401 46H427V0H416Q395 3 257 3Q121 3 100 0H88V46H114Q136 46 152 46T177 47T193 50T201 52T207 57T213 61V578Z"></path><path data-c="32" d="M109 429Q82 429 66 447T50 491Q50 562 103 614T235 666Q326 666 387 610T449 465Q449 422 429 383T381 315T301 241Q265 210 201 149L142 93L218 92Q375 92 385 97Q392 99 409 186V189H449V186Q448 183 436 95T421 3V0H50V19V31Q50 38 56 46T86 81Q115 113 136 137Q145 147 170 174T204 211T233 244T261 278T284 308T305 340T320 369T333 401T340 431T343 464Q343 527 309 573T212 619Q179 619 154 602T119 569T109 550Q109 549 114 549Q132 549 151 535T170 489Q170 464 154 447T109 429Z" transform="translate(500,0)"></path></g></g></g><g data-mml-node="mi" transform="translate(2340.2,0)"><path data-c="1D460" d="M131 289Q131 321 147 354T203 415T300 442Q362 442 390 415T419 355Q419 323 402 308T364 292Q351 292 340 300T328 326Q328 342 337 354T354 372T367 378Q368 378 368 379Q368 382 361 388T336 399T297 405Q249 405 227 379T204 326Q204 301 223 291T278 274T330 259Q396 230 396 163Q396 135 385 107T352 51T289 7T195 -10Q118 -10 86 19T53 87Q53 126 74 143T118 160Q133 160 146 151T160 120Q160 94 142 76T111 58Q109 57 108 57T107 55Q108 52 115 47T146 34T201 27Q237 27 263 38T301 66T318 97T323 122Q323 150 302 164T254 181T195 196T148 231Q131 256 131 289Z"></path></g></g></g></svg></mjx-container> 量级的涨落,在液态与气态间振荡,直到完全脱离范德华力作用半径(约 <mjx-container class="MathJax" jax="SVG"><svg style="vertical-align: -0.05ex;" xmlns="http://www.w3.org/2000/svg" width="6.235ex" height="1.557ex" role="img" focusable="false" viewBox="0 -666 2756 688"><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="scale(1,-1)"><g data-mml-node="math"><g data-mml-node="mn"><path data-c="30" d="M96 585Q152 666 249 666Q297 666 345 640T423 548Q460 465 460 320Q460 165 417 83Q397 41 362 16T301 -15T250 -22Q224 -22 198 -16T137 16T82 83Q39 165 39 320Q39 494 96 585ZM321 597Q291 629 250 629Q208 629 178 597Q153 571 145 525T137 333Q137 175 145 125T181 46Q209 16 250 16Q290 16 318 46Q347 76 354 130T362 333Q362 478 354 524T321 597Z"></path><path data-c="2E" d="M78 60Q78 84 95 102T138 120Q162 120 180 104T199 61Q199 36 182 18T139 0T96 17T78 60Z" transform="translate(500,0)"></path><path data-c="33" d="M127 463Q100 463 85 480T69 524Q69 579 117 622T233 665Q268 665 277 664Q351 652 390 611T430 522Q430 470 396 421T302 350L299 348Q299 347 308 345T337 336T375 315Q457 262 457 175Q457 96 395 37T238 -22Q158 -22 100 21T42 130Q42 158 60 175T105 193Q133 193 151 175T169 130Q169 119 166 110T159 94T148 82T136 74T126 70T118 67L114 66Q165 21 238 21Q293 21 321 74Q338 107 338 175V195Q338 290 274 322Q259 328 213 329L171 330L168 332Q166 335 166 348Q166 366 174 366Q202 366 232 371Q266 376 294 413T322 525V533Q322 590 287 612Q265 626 240 626Q208 626 181 615T143 592T132 580H135Q138 579 143 578T153 573T165 566T175 555T183 540T186 520Q186 498 172 481T127 463Z" transform="translate(778,0)"></path></g><g data-mml-node="mi" transform="translate(1278,0)"><path data-c="1D45B" d="M21 287Q22 293 24 303T36 341T56 388T89 425T135 442Q171 442 195 424T225 390T231 369Q231 367 232 367L243 378Q304 442 382 442Q436 442 469 415T503 336T465 179T427 52Q427 26 444 26Q450 26 453 27Q482 32 505 65T540 145Q542 153 560 153Q580 153 580 145Q580 144 576 130Q568 101 554 73T508 17T439 -10Q392 -10 371 17T350 73Q350 92 386 193T423 345Q423 404 379 404H374Q288 404 229 303L222 291L189 157Q156 26 151 16Q138 -11 108 -11Q95 -11 87 -5T76 7T74 17Q74 30 112 180T152 343Q153 348 153 366Q153 405 129 405Q91 405 66 305Q60 285 60 284Q58 278 41 278H27Q21 284 21 287Z"></path></g><g data-mml-node="mi" transform="translate(1878,0)"><path data-c="1D45A" d="M21 287Q22 293 24 303T36 341T56 388T88 425T132 442T175 435T205 417T221 395T229 376L231 369Q231 367 232 367L243 378Q303 442 384 442Q401 442 415 440T441 433T460 423T475 411T485 398T493 385T497 373T500 364T502 357L510 367Q573 442 659 442Q713 442 746 415T780 336Q780 285 742 178T704 50Q705 36 709 31T724 26Q752 26 776 56T815 138Q818 149 821 151T837 153Q857 153 857 145Q857 144 853 130Q845 101 831 73T785 17T716 -10Q669 -10 648 17T627 73Q627 92 663 193T700 345Q700 404 656 404H651Q565 404 506 303L499 291L466 157Q433 26 428 16Q415 -11 385 -11Q372 -11 364 -4T353 8T350 18Q350 29 384 161L420 307Q423 322 423 345Q423 404 379 404H374Q288 404 229 303L222 291L189 157Q156 26 151 16Q138 -11 108 -11Q95 -11 87 -5T76 7T74 17Q74 30 112 181Q151 335 151 342Q154 357 154 369Q154 405 129 405Q107 405 92 377T69 316T57 280Q55 278 41 278H27Q21 284 21 287Z"></path></g></g></g></svg></mjx-container>)。</p>
<p>逃逸的 H2O 分子并非直线升空,而是在空气分子碰撞下进行三维随机游走。<br>
根据爱因斯坦-斯托克斯方程,其扩散系数 <mjx-container class="MathJax" jax="SVG"><svg style="vertical-align: -0.566ex;" xmlns="http://www.w3.org/2000/svg" width="16.19ex" height="2.452ex" role="img" focusable="false" viewBox="0 -833.9 7156.1 1083.9"><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="scale(1,-1)"><g data-mml-node="math"><g data-mml-node="mi"><path data-c="1D437" d="M287 628Q287 635 230 637Q207 637 200 638T193 647Q193 655 197 667T204 682Q206 683 403 683Q570 682 590 682T630 676Q702 659 752 597T803 431Q803 275 696 151T444 3L430 1L236 0H125H72Q48 0 41 2T33 11Q33 13 36 25Q40 41 44 43T67 46Q94 46 127 49Q141 52 146 61Q149 65 218 339T287 628ZM703 469Q703 507 692 537T666 584T629 613T590 629T555 636Q553 636 541 636T512 636T479 637H436Q392 637 386 627Q384 623 313 339T242 52Q242 48 253 48T330 47Q335 47 349 47T373 46Q499 46 581 128Q617 164 640 212T683 339T703 469Z"></path></g><g data-mml-node="mo" transform="translate(1105.8,0)"><path data-c="2248" d="M55 319Q55 360 72 393T114 444T163 472T205 482Q207 482 213 482T223 483Q262 483 296 468T393 413L443 381Q502 346 553 346Q609 346 649 375T694 454Q694 465 698 474T708 483Q722 483 722 452Q722 386 675 338T555 289Q514 289 468 310T388 357T308 404T224 426Q164 426 125 393T83 318Q81 289 69 289Q55 289 55 319ZM55 85Q55 126 72 159T114 210T163 238T205 248Q207 248 213 248T223 249Q262 249 296 234T393 179L443 147Q502 112 553 112Q609 112 649 141T694 220Q694 249 708 249T722 217Q722 153 675 104T555 55Q514 55 468 76T388 123T308 170T224 192Q164 192 125 159T83 84Q80 55 69 55Q55 55 55 85Z"></path></g><g data-mml-node="mn" transform="translate(2161.6,0)"><path data-c="30" d="M96 585Q152 666 249 666Q297 666 345 640T423 548Q460 465 460 320Q460 165 417 83Q397 41 362 16T301 -15T250 -22Q224 -22 198 -16T137 16T82 83Q39 165 39 320Q39 494 96 585ZM321 597Q291 629 250 629Q208 629 178 597Q153 571 145 525T137 333Q137 175 145 125T181 46Q209 16 250 16Q290 16 318 46Q347 76 354 130T362 333Q362 478 354 524T321 597Z"></path><path data-c="2E" d="M78 60Q78 84 95 102T138 120Q162 120 180 104T199 61Q199 36 182 18T139 0T96 17T78 60Z" transform="translate(500,0)"></path><path data-c="32" d="M109 429Q82 429 66 447T50 491Q50 562 103 614T235 666Q326 666 387 610T449 465Q449 422 429 383T381 315T301 241Q265 210 201 149L142 93L218 92Q375 92 385 97Q392 99 409 186V189H449V186Q448 183 436 95T421 3V0H50V19V31Q50 38 56 46T86 81Q115 113 136 137Q145 147 170 174T204 211T233 244T261 278T284 308T305 340T320 369T333 401T340 431T343 464Q343 527 309 573T212 619Q179 619 154 602T119 569T109 550Q109 549 114 549Q132 549 151 535T170 489Q170 464 154 447T109 429Z" transform="translate(778,0)"></path><path data-c="38" d="M70 417T70 494T124 618T248 666Q319 666 374 624T429 515Q429 485 418 459T392 417T361 389T335 371T324 363L338 354Q352 344 366 334T382 323Q457 264 457 174Q457 95 399 37T249 -22Q159 -22 101 29T43 155Q43 263 172 335L154 348Q133 361 127 368Q70 417 70 494ZM286 386L292 390Q298 394 301 396T311 403T323 413T334 425T345 438T355 454T364 471T369 491T371 513Q371 556 342 586T275 624Q268 625 242 625Q201 625 165 599T128 534Q128 511 141 492T167 463T217 431Q224 426 228 424L286 386ZM250 21Q308 21 350 55T392 137Q392 154 387 169T375 194T353 216T330 234T301 253T274 270Q260 279 244 289T218 306L210 311Q204 311 181 294T133 239T107 157Q107 98 150 60T250 21Z" transform="translate(1278,0)"></path><path data-c="32" d="M109 429Q82 429 66 447T50 491Q50 562 103 614T235 666Q326 666 387 610T449 465Q449 422 429 383T381 315T301 241Q265 210 201 149L142 93L218 92Q375 92 385 97Q392 99 409 186V189H449V186Q448 183 436 95T421 3V0H50V19V31Q50 38 56 46T86 81Q115 113 136 137Q145 147 170 174T204 211T233 244T261 278T284 308T305 340T320 369T333 401T340 431T343 464Q343 527 309 573T212 619Q179 619 154 602T119 569T109 550Q109 549 114 549Q132 549 151 535T170 489Q170 464 154 447T109 429Z" transform="translate(1778,0)"></path></g><g data-mml-node="mi" transform="translate(4439.6,0)"><path data-c="1D450" d="M34 159Q34 268 120 355T306 442Q362 442 394 418T427 355Q427 326 408 306T360 285Q341 285 330 295T319 325T330 359T352 380T366 386H367Q367 388 361 392T340 400T306 404Q276 404 249 390Q228 381 206 359Q162 315 142 235T121 119Q121 73 147 50Q169 26 205 26H209Q321 26 394 111Q403 121 406 121Q410 121 419 112T429 98T420 83T391 55T346 25T282 0T202 -11Q127 -11 81 37T34 159Z"></path></g><g data-mml-node="msup" transform="translate(4872.6,0)"><g data-mml-node="mi"><path data-c="1D45A" d="M21 287Q22 293 24 303T36 341T56 388T88 425T132 442T175 435T205 417T221 395T229 376L231 369Q231 367 232 367L243 378Q303 442 384 442Q401 442 415 440T441 433T460 423T475 411T485 398T493 385T497 373T500 364T502 357L510 367Q573 442 659 442Q713 442 746 415T780 336Q780 285 742 178T704 50Q705 36 709 31T724 26Q752 26 776 56T815 138Q818 149 821 151T837 153Q857 153 857 145Q857 144 853 130Q845 101 831 73T785 17T716 -10Q669 -10 648 17T627 73Q627 92 663 193T700 345Q700 404 656 404H651Q565 404 506 303L499 291L466 157Q433 26 428 16Q415 -11 385 -11Q372 -11 364 -4T353 8T350 18Q350 29 384 161L420 307Q423 322 423 345Q423 404 379 404H374Q288 404 229 303L222 291L189 157Q156 26 151 16Q138 -11 108 -11Q95 -11 87 -5T76 7T74 17Q74 30 112 181Q151 335 151 342Q154 357 154 369Q154 405 129 405Q107 405 92 377T69 316T57 280Q55 278 41 278H27Q21 284 21 287Z"></path></g><g data-mml-node="mn" transform="translate(911,363) scale(0.707)"><path data-c="32" d="M109 429Q82 429 66 447T50 491Q50 562 103 614T235 666Q326 666 387 610T449 465Q449 422 429 383T381 315T301 241Q265 210 201 149L142 93L218 92Q375 92 385 97Q392 99 409 186V189H449V186Q448 183 436 95T421 3V0H50V19V31Q50 38 56 46T86 81Q115 113 136 137Q145 147 170 174T204 211T233 244T261 278T284 308T305 340T320 369T333 401T340 431T343 464Q343 527 309 573T212 619Q179 619 154 602T119 569T109 550Q109 549 114 549Q132 549 151 535T170 489Q170 464 154 447T109 429Z"></path></g></g><g data-mml-node="TeXAtom" data-mjx-texclass="ORD" transform="translate(6187.1,0)"><g data-mml-node="mo"><path data-c="2F" d="M423 750Q432 750 438 744T444 730Q444 725 271 248T92 -240Q85 -250 75 -250Q68 -250 62 -245T56 -231Q56 -221 230 257T407 740Q411 750 423 750Z"></path></g></g><g data-mml-node="mi" transform="translate(6687.1,0)"><path data-c="1D460" d="M131 289Q131 321 147 354T203 415T300 442Q362 442 390 415T419 355Q419 323 402 308T364 292Q351 292 340 300T328 326Q328 342 337 354T354 372T367 378Q368 378 368 379Q368 382 361 388T336 399T297 405Q249 405 227 379T204 326Q204 301 223 291T278 274T330 259Q396 230 396 163Q396 135 385 107T352 51T289 7T195 -10Q118 -10 86 19T53 87Q53 126 74 143T118 160Q133 160 146 151T160 120Q160 94 142 76T111 58Q109 57 108 57T107 55Q108 52 115 47T146 34T201 27Q237 27 263 38T301 66T318 97T323 122Q323 150 302 164T254 181T195 196T148 231Q131 256 131 289Z"></path></g></g></g></svg></mjx-container>(25℃标准大气压)。这意味着单个水分子在1秒内将形成半径约 <mjx-container class="MathJax" jax="SVG"><svg style="vertical-align: -0.025ex;" xmlns="http://www.w3.org/2000/svg" width="6.864ex" height="1.557ex" role="img" focusable="false" viewBox="0 -677 3034 688"><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="scale(1,-1)"><g data-mml-node="math"><g data-mml-node="mn"><path data-c="32" d="M109 429Q82 429 66 447T50 491Q50 562 103 614T235 666Q326 666 387 610T449 465Q449 422 429 383T381 315T301 241Q265 210 201 149L142 93L218 92Q375 92 385 97Q392 99 409 186V189H449V186Q448 183 436 95T421 3V0H50V19V31Q50 38 56 46T86 81Q115 113 136 137Q145 147 170 174T204 211T233 244T261 278T284 308T305 340T320 369T333 401T340 431T343 464Q343 527 309 573T212 619Q179 619 154 602T119 569T109 550Q109 549 114 549Q132 549 151 535T170 489Q170 464 154 447T109 429Z"></path><path data-c="2E" d="M78 60Q78 84 95 102T138 120Q162 120 180 104T199 61Q199 36 182 18T139 0T96 17T78 60Z" transform="translate(500,0)"></path><path data-c="34" d="M462 0Q444 3 333 3Q217 3 199 0H190V46H221Q241 46 248 46T265 48T279 53T286 61Q287 63 287 115V165H28V211L179 442Q332 674 334 675Q336 677 355 677H373L379 671V211H471V165H379V114Q379 73 379 66T385 54Q393 47 442 46H471V0H462ZM293 211V545L74 212L183 211H293Z" transform="translate(778,0)"></path></g><g data-mml-node="mi" transform="translate(1278,0)"><path data-c="1D45A" d="M21 287Q22 293 24 303T36 341T56 388T88 425T132 442T175 435T205 417T221 395T229 376L231 369Q231 367 232 367L243 378Q303 442 384 442Q401 442 415 440T441 433T460 423T475 411T485 398T493 385T497 373T500 364T502 357L510 367Q573 442 659 442Q713 442 746 415T780 336Q780 285 742 178T704 50Q705 36 709 31T724 26Q752 26 776 56T815 138Q818 149 821 151T837 153Q857 153 857 145Q857 144 853 130Q845 101 831 73T785 17T716 -10Q669 -10 648 17T627 73Q627 92 663 193T700 345Q700 404 656 404H651Q565 404 506 303L499 291L466 157Q433 26 428 16Q415 -11 385 -11Q372 -11 364 -4T353 8T350 18Q350 29 384 161L420 307Q423 322 423 345Q423 404 379 404H374Q288 404 229 303L222 291L189 157Q156 26 151 16Q138 -11 108 -11Q95 -11 87 -5T76 7T74 17Q74 30 112 181Q151 335 151 342Q154 357 154 369Q154 405 129 405Q107 405 92 377T69 316T57 280Q55 278 41 278H27Q21 284 21 287Z"></path></g><g data-mml-node="mi" transform="translate(2156,0)"><path data-c="1D45A" d="M21 287Q22 293 24 303T36 341T56 388T88 425T132 442T175 435T205 417T221 395T229 376L231 369Q231 367 232 367L243 378Q303 442 384 442Q401 442 415 440T441 433T460 423T475 411T485 398T493 385T497 373T500 364T502 357L510 367Q573 442 659 442Q713 442 746 415T780 336Q780 285 742 178T704 50Q705 36 709 31T724 26Q752 26 776 56T815 138Q818 149 821 151T837 153Q857 153 857 145Q857 144 853 130Q845 101 831 73T785 17T716 -10Q669 -10 648 17T627 73Q627 92 663 193T700 345Q700 404 656 404H651Q565 404 506 303L499 291L466 157Q433 26 428 16Q415 -11 385 -11Q372 -11 364 -4T353 8T350 18Q350 29 384 161L420 307Q423 322 423 345Q423 404 379 404H374Q288 404 229 303L222 291L189 157Q156 26 151 16Q138 -11 108 -11Q95 -11 87 -5T76 7T74 17Q74 30 112 181Q151 335 151 342Q154 357 154 369Q154 405 129 405Q107 405 92 377T69 316T57 280Q55 278 41 278H27Q21 284 21 287Z"></path></g></g></g></svg></mjx-container> 的概率云,与数十亿同伴共同形成水汽。</p>
<p>水中捞月,伸手时,涟漪碎了三千世界。</p>
<p>人类的眉睫处有十方虚空,三藏经书不过指月之指,</p>
<p>看见儿时门前溪水倒流,看见婴孩啼哭时眼底星河闪烁。</p>
<p>看见生死之幕薄如蝉翼,和众生颠倒梦想处。</p>
<p>某些水分子可能抵达对流层顶(约 <mjx-container class="MathJax" jax="SVG"><svg style="vertical-align: -0.025ex;" xmlns="http://www.w3.org/2000/svg" width="5.428ex" height="1.595ex" role="img" focusable="false" viewBox="0 -694 2399 705"><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="scale(1,-1)"><g data-mml-node="math"><g data-mml-node="mn"><path data-c="31" d="M213 578L200 573Q186 568 160 563T102 556H83V602H102Q149 604 189 617T245 641T273 663Q275 666 285 666Q294 666 302 660V361L303 61Q310 54 315 52T339 48T401 46H427V0H416Q395 3 257 3Q121 3 100 0H88V46H114Q136 46 152 46T177 47T193 50T201 52T207 57T213 61V578Z"></path><path data-c="32" d="M109 429Q82 429 66 447T50 491Q50 562 103 614T235 666Q326 666 387 610T449 465Q449 422 429 383T381 315T301 241Q265 210 201 149L142 93L218 92Q375 92 385 97Q392 99 409 186V189H449V186Q448 183 436 95T421 3V0H50V19V31Q50 38 56 46T86 81Q115 113 136 137Q145 147 170 174T204 211T233 244T261 278T284 308T305 340T320 369T333 401T340 431T343 464Q343 527 309 573T212 619Q179 619 154 602T119 569T109 550Q109 549 114 549Q132 549 151 535T170 489Q170 464 154 447T109 429Z" transform="translate(500,0)"></path></g><g data-mml-node="mi" transform="translate(1000,0)"><path data-c="1D458" d="M121 647Q121 657 125 670T137 683Q138 683 209 688T282 694Q294 694 294 686Q294 679 244 477Q194 279 194 272Q213 282 223 291Q247 309 292 354T362 415Q402 442 438 442Q468 442 485 423T503 369Q503 344 496 327T477 302T456 291T438 288Q418 288 406 299T394 328Q394 353 410 369T442 390L458 393Q446 405 434 405H430Q398 402 367 380T294 316T228 255Q230 254 243 252T267 246T293 238T320 224T342 206T359 180T365 147Q365 130 360 106T354 66Q354 26 381 26Q429 26 459 145Q461 153 479 153H483Q499 153 499 144Q499 139 496 130Q455 -11 378 -11Q333 -11 305 15T277 90Q277 108 280 121T283 145Q283 167 269 183T234 206T200 217T182 220H180Q168 178 159 139T145 81T136 44T129 20T122 7T111 -2Q98 -11 83 -11Q66 -11 57 -1T48 16Q48 26 85 176T158 471L195 616Q196 629 188 632T149 637H144Q134 637 131 637T124 640T121 647Z"></path></g><g data-mml-node="mi" transform="translate(1521,0)"><path data-c="1D45A" d="M21 287Q22 293 24 303T36 341T56 388T88 425T132 442T175 435T205 417T221 395T229 376L231 369Q231 367 232 367L243 378Q303 442 384 442Q401 442 415 440T441 433T460 423T475 411T485 398T493 385T497 373T500 364T502 357L510 367Q573 442 659 442Q713 442 746 415T780 336Q780 285 742 178T704 50Q705 36 709 31T724 26Q752 26 776 56T815 138Q818 149 821 151T837 153Q857 153 857 145Q857 144 853 130Q845 101 831 73T785 17T716 -10Q669 -10 648 17T627 73Q627 92 663 193T700 345Q700 404 656 404H651Q565 404 506 303L499 291L466 157Q433 26 428 16Q415 -11 385 -11Q372 -11 364 -4T353 8T350 18Q350 29 384 161L420 307Q423 322 423 345Q423 404 379 404H374Q288 404 229 303L222 291L189 157Q156 26 151 16Q138 -11 108 -11Q95 -11 87 -5T76 7T74 17Q74 30 112 181Q151 335 151 342Q154 357 154 369Q154 405 129 405Q107 405 92 377T69 316T57 280Q55 278 41 278H27Q21 284 21 287Z"></path></g></g></g></svg></mjx-container>),在那里被极地涡旋捕获。</p>
<p>经过 <mjx-container class="MathJax" jax="SVG"><svg style="vertical-align: -0.186ex;" xmlns="http://www.w3.org/2000/svg" width="8.422ex" height="1.692ex" role="img" focusable="false" viewBox="0 -666 3722.4 748"><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="scale(1,-1)"><g data-mml-node="math"><g data-mml-node="mn"><path data-c="35" d="M164 157Q164 133 148 117T109 101H102Q148 22 224 22Q294 22 326 82Q345 115 345 210Q345 313 318 349Q292 382 260 382H254Q176 382 136 314Q132 307 129 306T114 304Q97 304 95 310Q93 314 93 485V614Q93 664 98 664Q100 666 102 666Q103 666 123 658T178 642T253 634Q324 634 389 662Q397 666 402 666Q410 666 410 648V635Q328 538 205 538Q174 538 149 544L139 546V374Q158 388 169 396T205 412T256 420Q337 420 393 355T449 201Q449 109 385 44T229 -22Q148 -22 99 32T50 154Q50 178 61 192T84 210T107 214Q132 214 148 197T164 157Z"></path><path data-c="30" d="M96 585Q152 666 249 666Q297 666 345 640T423 548Q460 465 460 320Q460 165 417 83Q397 41 362 16T301 -15T250 -22Q224 -22 198 -16T137 16T82 83Q39 165 39 320Q39 494 96 585ZM321 597Q291 629 250 629Q208 629 178 597Q153 571 145 525T137 333Q137 175 145 125T181 46Q209 16 250 16Q290 16 318 46Q347 76 354 130T362 333Q362 478 354 524T321 597Z" transform="translate(500,0)"></path></g><g data-mml-node="mo" transform="translate(1222.2,0)"><path data-c="2212" d="M84 237T84 250T98 270H679Q694 262 694 250T679 230H98Q84 237 84 250Z"></path></g><g data-mml-node="mn" transform="translate(2222.4,0)"><path data-c="32" d="M109 429Q82 429 66 447T50 491Q50 562 103 614T235 666Q326 666 387 610T449 465Q449 422 429 383T381 315T301 241Q265 210 201 149L142 93L218 92Q375 92 385 97Q392 99 409 186V189H449V186Q448 183 436 95T421 3V0H50V19V31Q50 38 56 46T86 81Q115 113 136 137Q145 147 170 174T204 211T233 244T261 278T284 308T305 340T320 369T333 401T340 431T343 464Q343 527 309 573T212 619Q179 619 154 602T119 569T109 550Q109 549 114 549Q132 549 151 535T170 489Q170 464 154 447T109 429Z"></path><path data-c="30" d="M96 585Q152 666 249 666Q297 666 345 640T423 548Q460 465 460 320Q460 165 417 83Q397 41 362 16T301 -15T250 -22Q224 -22 198 -16T137 16T82 83Q39 165 39 320Q39 494 96 585ZM321 597Q291 629 250 629Q208 629 178 597Q153 571 145 525T137 333Q137 175 145 125T181 46Q209 16 250 16Q290 16 318 46Q347 76 354 130T362 333Q362 478 354 524T321 597Z" transform="translate(500,0)"></path><path data-c="30" d="M96 585Q152 666 249 666Q297 666 345 640T423 548Q460 465 460 320Q460 165 417 83Q397 41 362 16T301 -15T250 -22Q224 -22 198 -16T137 16T82 83Q39 165 39 320Q39 494 96 585ZM321 597Q291 629 250 629Q208 629 178 597Q153 571 145 525T137 333Q137 175 145 125T181 46Q209 16 250 16Q290 16 318 46Q347 76 354 130T362 333Q362 478 354 524T321 597Z" transform="translate(1000,0)"></path></g></g></g></svg></mjx-container> 天的环球旅行后,其中 <mjx-container class="MathJax" jax="SVG"><svg style="vertical-align: -0.05ex;" xmlns="http://www.w3.org/2000/svg" width="6.285ex" height="1.557ex" role="img" focusable="false" viewBox="0 -666 2778 688"><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="scale(1,-1)"><g data-mml-node="math"><g data-mml-node="mn"><path data-c="30" d="M96 585Q152 666 249 666Q297 666 345 640T423 548Q460 465 460 320Q460 165 417 83Q397 41 362 16T301 -15T250 -22Q224 -22 198 -16T137 16T82 83Q39 165 39 320Q39 494 96 585ZM321 597Q291 629 250 629Q208 629 178 597Q153 571 145 525T137 333Q137 175 145 125T181 46Q209 16 250 16Q290 16 318 46Q347 76 354 130T362 333Q362 478 354 524T321 597Z"></path><path data-c="2E" d="M78 60Q78 84 95 102T138 120Q162 120 180 104T199 61Q199 36 182 18T139 0T96 17T78 60Z" transform="translate(500,0)"></path><path data-c="30" d="M96 585Q152 666 249 666Q297 666 345 640T423 548Q460 465 460 320Q460 165 417 83Q397 41 362 16T301 -15T250 -22Q224 -22 198 -16T137 16T82 83Q39 165 39 320Q39 494 96 585ZM321 597Q291 629 250 629Q208 629 178 597Q153 571 145 525T137 333Q137 175 145 125T181 46Q209 16 250 16Q290 16 318 46Q347 76 354 130T362 333Q362 478 354 524T321 597Z" transform="translate(778,0)"></path><path data-c="30" d="M96 585Q152 666 249 666Q297 666 345 640T423 548Q460 465 460 320Q460 165 417 83Q397 41 362 16T301 -15T250 -22Q224 -22 198 -16T137 16T82 83Q39 165 39 320Q39 494 96 585ZM321 597Q291 629 250 629Q208 629 178 597Q153 571 145 525T137 333Q137 175 145 125T181 46Q209 16 250 16Q290 16 318 46Q347 76 354 130T362 333Q362 478 354 524T321 597Z" transform="translate(1278,0)"></path><path data-c="30" d="M96 585Q152 666 249 666Q297 666 345 640T423 548Q460 465 460 320Q460 165 417 83Q397 41 362 16T301 -15T250 -22Q224 -22 198 -16T137 16T82 83Q39 165 39 320Q39 494 96 585ZM321 597Q291 629 250 629Q208 629 178 597Q153 571 145 525T137 333Q137 175 145 125T181 46Q209 16 250 16Q290 16 318 46Q347 76 354 130T362 333Q362 478 354 524T321 597Z" transform="translate(1778,0)"></path><path data-c="33" d="M127 463Q100 463 85 480T69 524Q69 579 117 622T233 665Q268 665 277 664Q351 652 390 611T430 522Q430 470 396 421T302 350L299 348Q299 347 308 345T337 336T375 315Q457 262 457 175Q457 96 395 37T238 -22Q158 -22 100 21T42 130Q42 158 60 175T105 193Q133 193 151 175T169 130Q169 119 166 110T159 94T148 82T136 74T126 70T118 67L114 66Q165 21 238 21Q293 21 321 74Q338 107 338 175V195Q338 290 274 322Q259 328 213 329L171 330L168 332Q166 335 166 348Q166 366 174 366Q202 366 232 371Q266 376 294 413T322 525V533Q322 590 287 612Q265 626 240 626Q208 626 181 615T143 592T132 580H135Q138 579 143 578T153 573T165 566T175 555T183 540T186 520Q186 498 172 481T127 463Z" transform="translate(2278,0)"></path></g></g></g></svg></mjx-container> 的分子将逃逸到太空,携带着地球的熵增印记。</p>
<p>它们的氧原子核内,八个质子正以 <mjx-container class="MathJax" jax="SVG"><svg style="vertical-align: -0.05ex;" xmlns="http://www.w3.org/2000/svg" width="4.05ex" height="2.022ex" role="img" focusable="false" viewBox="0 -871.8 1790.1 893.8"><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="scale(1,-1)"><g data-mml-node="math"><g data-mml-node="msup"><g data-mml-node="mn"><path data-c="31" d="M213 578L200 573Q186 568 160 563T102 556H83V602H102Q149 604 189 617T245 641T273 663Q275 666 285 666Q294 666 302 660V361L303 61Q310 54 315 52T339 48T401 46H427V0H416Q395 3 257 3Q121 3 100 0H88V46H114Q136 46 152 46T177 47T193 50T201 52T207 57T213 61V578Z"></path><path data-c="30" d="M96 585Q152 666 249 666Q297 666 345 640T423 548Q460 465 460 320Q460 165 417 83Q397 41 362 16T301 -15T250 -22Q224 -22 198 -16T137 16T82 83Q39 165 39 320Q39 494 96 585ZM321 597Q291 629 250 629Q208 629 178 597Q153 571 145 525T137 333Q137 175 145 125T181 46Q209 16 250 16Q290 16 318 46Q347 76 354 130T362 333Q362 478 354 524T321 597Z" transform="translate(500,0)"></path></g><g data-mml-node="TeXAtom" transform="translate(1033,393.1) scale(0.707)" data-mjx-texclass="ORD"><g data-mml-node="mn"><path data-c="33" d="M127 463Q100 463 85 480T69 524Q69 579 117 622T233 665Q268 665 277 664Q351 652 390 611T430 522Q430 470 396 421T302 350L299 348Q299 347 308 345T337 336T375 315Q457 262 457 175Q457 96 395 37T238 -22Q158 -22 100 21T42 130Q42 158 60 175T105 193Q133 193 151 175T169 130Q169 119 166 110T159 94T148 82T136 74T126 70T118 67L114 66Q165 21 238 21Q293 21 321 74Q338 107 338 175V195Q338 290 274 322Q259 328 213 329L171 330L168 332Q166 335 166 348Q166 366 174 366Q202 366 232 371Q266 376 294 413T322 525V533Q322 590 287 612Q265 626 240 626Q208 626 181 615T143 592T132 580H135Q138 579 143 578T153 573T165 566T175 555T183 540T186 520Q186 498 172 481T127 463Z"></path><path data-c="34" d="M462 0Q444 3 333 3Q217 3 199 0H190V46H221Q241 46 248 46T265 48T279 53T286 61Q287 63 287 115V165H28V211L179 442Q332 674 334 675Q336 677 355 677H373L379 671V211H471V165H379V114Q379 73 379 66T385 54Q393 47 442 46H471V0H462ZM293 211V545L74 212L183 211H293Z" transform="translate(500,0)"></path></g></g></g></g></g></svg></mjx-container> 年半衰期等待质子衰变。</p>
<p>诸法从本来,常自寂灭相。</p>
<p>那在这之前呢?</p>
<p>在这之前我刚吃完一碗辣椒炒肉,</p>
<p>香</p>
]]></content>
<categories>
<category>gallery</category>
</categories>
</entry>
<entry>
<title>二〇二五年二月二十四日</title>
<url>/2025/02/24/%E4%BA%8C%E3%80%87%E4%BA%8C%E4%BA%94%E5%B9%B4%E4%BA%8C%E6%9C%88%E4%BA%8C%E5%8D%81%E5%9B%9B%E6%97%A5/</url>
<content><![CDATA[<p>街上的人躲进建筑,道路上的车变得缓慢。路边的黑猫坐在屋檐下张望,在雨天,它也有了思考的地方与时间。除了淅淅沥沥的雨声再无其他。</p>
<p>衣服买大了,他们走路好急,地铁上只有老人没看手机,天怎么黑那么快是我起晚了吗,那个蹦蹦跳跳的女孩耳机里在听什么歌,我还在原地吗,我要再等等吗。</p>
<p>家就像厚酒之后突然明晰的想象,时间不语,只是沉默,我沉默地走进时间,看着我和你的年龄相差越来越小。</p>
<p>这雨天,世界都温柔的可以掐出水来。</p>
]]></content>
<categories>
<category>gallery</category>
</categories>
</entry>
<entry>
<title>二〇二五年四月三日</title>
<url>/2025/04/03/%E4%BA%8C%E3%80%87%E4%BA%8C%E4%BA%94%E5%B9%B4%E4%BA%94%E6%9C%88%E4%B8%89%E6%97%A5/</url>
<content><![CDATA[<p>日历被一页页撕去,出钞口的暖风比任何毛毯更忠实,数字键盘的荧光就像圣徒光环般笼罩着他,这些废纸似乎可以延长家人存在的时间。</p>
<p>他机械地按下了冷冰的数字,机械地等待着钞票的滑落,而后站在深夜的街角,接到了医院打来的电话,在电话那头呼吸机的蜂鸣声中,机械地看着风将这些钞票细碎地吹向脚边,又将它们卷入城市的缝隙。</p>
<p>这是一出无声的告别仪式。</p>
<p>时间带走了一个人,带走了一个由无数粒子组成的,短暂观察了这个宇宙一瞬的意识,它们会在时间中无数次聚散,再无数次见这灿烂的一切。</p>
<p>梦里明明有六趣,觉后空空无大千,</p>
<p>又是一个难捱的冬天,为了脚不被冻住,我们只能起舞。</p>
]]></content>
<categories>
<category>gallery</category>
</categories>
</entry>
<entry>
<title>二〇二五年五月十二日</title>
<url>/2025/05/12/%E4%BA%8C%E3%80%87%E4%BA%8C%E4%BA%94%E5%B9%B4%E4%BA%94%E6%9C%88%E5%8D%81%E4%BA%8C%E6%97%A5/</url>
<content><![CDATA[<p>放假前的最后一晚,侧耳听了会他们聊游戏里的内容,把水杯放在书桌上上,浅浅扬唇:“好热闹。”</p>
<p>“毕竟要放假了。”上铺说。</p>
<p>“对啊,放假了。”</p>
<p>我说这话的时候表情恍惚了下,在想去年的国庆都发生了什么。</p>
<p>其实也是很平常的一天,全寝人聚在一起玩了会儿游戏,选餐厅,吃饭,回学校,睡觉。</p>
<p>跟以往放假前没什么两样。</p>
<p>当时只道是寻常。</p>
<p>这只是我在大学的第一个年头,我以为后面还会有三四个年头。</p>
<p>就像当时耳机中歌词里写的那样。</p>
<p>“就这样虚度着年华,没牵挂”</p>
<p>“只有晚风吹拂着晚霞”</p>
<p>只是片刻便惊醒了,我没有年华可以虚度,也满是牵挂。</p>
<p>但是看着他们热热闹闹的,我也好像浑身有活力了,不那么死气沉沉了。</p>
<p>我说出去透口气,在阳台看夜景,也在看他们。</p>
<p>阳台的玻璃窗上映出六盏台灯散发出的昏黄的暖橙色灯光,还有五位谈笑风生的少年。</p>
<p>直到困意渐渐把声音撞的稀稀碎碎。</p>
<p>直到他们在梦中期待新的一天中无限的未来。</p>
<p>可惜这都与我无关,那是我在学校的最后一天,我和他们一起放假,再也没回过学校。</p>
<p>但我当天的确睡了个好觉。</p>
]]></content>
<categories>
<category>gallery</category>
</categories>
</entry>
<entry>
<title>二〇二五年六月二十九日</title>
<url>/2025/06/29/%E4%BA%8C%E3%80%87%E4%BA%8C%E4%BA%94%E5%B9%B4%E5%85%AD%E6%9C%88%E4%BA%8C%E5%8D%81%E4%B9%9D%E6%97%A5/</url>
<content><![CDATA[<p>我好想你们呀,生命又过去了二十九天,掰着手指头数剩下的日子。</p>
<p>弓起背,低下头,试图把自己缩进这窄小的格子间里,缩进一个不被思念捕捉的角落。</p>
<p>可思念无孔不入,突然就裹挟着沉闷的哭声从耳道钻入,直抵脑中某个隐秘的角落,那个角落有你们的声音,样貌,有瘪了气的篮球,和被思念轻易击碎的稚嫩自尊。</p>
<p>泪水沉默地跌落下来,在键盘上,在摊开的文件上,纸张沉默地吸吮着这些滚烫的水滴,洇开一片片更深的阴影</p>
<p>思念并没有随着满溢的泪水散去,在模糊的屏幕光晕里缠绕。屏幕上的灰格子,水痕里的倒影,还有你们说话的声音,全都搅在了一起,混沌一片。</p>
<hr>
<p>你们走了之后,再也没有人会纯粹地,认真的爱我了。</p>
<p>我现在已经长的很高很高,比你们的坟墓还高了。</p>
<p>你们什么时候能来梦里看看我呀。</p>
<p>看看我呀,看看我骄傲的样子呀,看看我恨不得把全身力气用在紧闭的双眼上防止泪水流出来的样子呀,看看我再睁开眼时泪眼朦胧着四下寻你们的样子呀。</p>
<p>我有好好听你们的话,每一天醒来都前所未有的坚强,但我想你们,我好想你们,这是坚强永远无法企及的地方。</p>
<p>算了,不怪你们,我自己的问题。</p>
<p>晚安,我们还会再见的吧?</p>
<p>等我成为星星的那天。</p>
]]></content>
<categories>
<category>gallery</category>
</categories>
</entry>
<entry>
<title>二〇二五年六月十日</title>
<url>/2025/06/10/%E4%BA%8C%E3%80%87%E4%BA%8C%E4%BA%94%E5%B9%B4%E5%85%AD%E6%9C%88%E5%8D%81%E6%97%A5/</url>
<content><![CDATA[<p>头盔塑料面罩边缘积了一圈水,沉甸甸的。</p>
<p>我根本没在意雨是什么时候开始下的。毛毛雨,烦人得很,打不湿衣服,但能糊一脸。</p>
<p>脑子里塞满了刚加完班的疲惫、明天还要继续做的项目,还有路口那家可能还没关门的便利店中的泡面。</p>
<p>抬头看向漫长红灯的时候,视线边缘笼罩了一层光。</p>
<p>那圈水好像活了。</p>
<p>不是流动的水,是光。四面八方,路口乱七八糟的路灯、对面刺眼的车灯、旁边店铺招牌的霓虹……所有乱七八糟的光,像被磁铁吸住的铁屑,全挤进了那圈浑浊的水渍里。</p>
<p>它们在里面搅动、融合、炸开,又坍缩成一片无法形容的、晃动的、极其刺眼的绚烂。</p>
<p>这光不是在正前方,是在视线的边缘,余光扫到的角落。</p>
<p>可偏偏是这片余光,像一张湿透的糖纸蒙住了我的整个世界。</p>
<p>我好久没吃糖了。</p>
<p>车流声、雨点打在头盔上的哒哒声、旁边电动车不耐烦的喇叭声,全被这片无声的、在视线边缘燃烧的光吸走了。</p>
<p>我感觉自己不是停在潮湿黏腻的十字路口,而是悬在某个光怪陆离的隧道口。</p>
<p>身体很轻,脑子里那些塞得满满当当的烦心事,被这强光瞬间蒸发、漂白了。</p>
<p>时间没了刻度,只剩下那片在头盔边缘疯狂跳动的光河。</p>
<p>一种冰冷的平静攥住了我。</p>
<p>平静地回家,</p>
<p>平静地洗澡,</p>
<p>平静地坐在电脑前,</p>
<p>屏幕突然模糊了,看不清了,好像听到爸爸说,哟哟哟,真丑,快擦擦。</p>
<p>平静地合上电脑,</p>
<p>平静地躺在床上,</p>
<p>我一直在替你看看这个你曾喜欢过的世界,</p>
<p>也一直在想你,直到你出现在我的梦里。</p>
<p>眼前骤然清晰。</p>
<p>湿漉漉的柏油路面反着冷光,对面巨大的红色刹车灯刺得瞳孔一缩。</p>
<p>刚才那铺天盖地的绚烂,像被一块脏抹布瞬间抹去,连一丝水痕都没留下。</p>
<p>刚才那轻飘飘的悬空感消失得无影无踪,沉重的头盔重新压在脖子上,屁股下电动车的坐垫硌得难受,胃里空得发慌。</p>
<p>绿灯亮了,</p>
<p>你活着的时候,每一天都在离我远去。</p>
<p>你走了之后,我活着的每一天,都是在向你走去。</p>
<p>所以我拧着油门向前走,重复昨天的生活。</p>
<p>希望我们再见时,可以向你讲述沿途风景,讲述我感受到的世界与爱。</p>
<p>也希望你在那边一切都好。</p>
<p>在星空的另一端,思念从未停止。</p>
]]></content>
<categories>
<category>gallery</category>
</categories>
</entry>
<entry>
<title>二〇二五年十月十九日</title>
<url>/2025/10/19/%E4%BA%8C%E3%80%87%E4%BA%8C%E4%BA%94%E5%B9%B4%E5%8D%81%E6%9C%88%E5%8D%81%E4%B9%9D%E6%97%A5/</url>
<content><![CDATA[<p>第二十个除夕。</p>
<p>第六千五百六十天。</p>
<p>破阁楼的窗棂漏着风,将白炽灯忽明忽灭。他把身上的旧毛毯裹紧了些。</p>
<p>“该准备年夜饭了。”</p>
<p>脏污的灶台上,一个碗盛着半截胡萝卜,他用冻僵的手指甲仔细刻了朵歪斜的花。还有个豁口碗装着自己做的包子,做得很不好,根本不像奶奶,油脂凝结成浑浊的白霜。</p>
<p>他解开布袋数出几粒花生米。</p>
<p>“灰灰,开饭了。”</p>
<p>木板吱呀作响,他听到爪子挠地的声响。将从垃圾堆翻出的铁皮盒架在火盆上,薄薄的铁片立刻被烧得发红。把胡萝卜片铺上去时,幻觉里油星迸溅的声音让他下意识往旁边躲了躲。</p>
<p>“小心烫嘴啊。”,他对着左边的空椅叮嘱。</p>
<p>铁皮盒开始冒烟时,他用铁夹给右边的空碗添了块焦黑的肉渣。“你最爱吃这个。”,说完自己先捂着肚子笑起来,笑声撞在四壁空墙上来回反弹,最后变成呛咳。</p>
<p>“你们别愣着。”,他握起塑料瓶,将散装的酒分成三份。</p>
<p>“都开吃了不是?”,酒液划过喉咙时烧出热辣的痛感,让他在朦胧的雾气里看见火锅袅袅的热气,三个人影正隔着蒸汽对他笑。</p>
<p>角落里突然传来窸窣声。</p>
<p>他触电般跳起时碰翻了火盆,炭灰在霉斑交错的地面画出血脉似的纹路。连滚带爬扑向堆满废报纸的墙角,手指钻进冰凉刺骨的墙缝:“灰灰你回来了?”</p>
<p>死老鼠腐烂的头颅在指尖碎裂的瞬间,他整个人僵在原地。目光顺着污水痕迹延伸,终于看见破棉被下露出的半截黑色尾巴。</p>
<p>他猛地掀开棉被,而后缓慢、缓慢地蹲下。那条在旧货市场偷来给孩子当围脖的绒布,此刻正死气沉沉地缠着僵硬的犬尸。小狗的眼睛凝着白霜,嘴边还沾着三天前他喂的最后半块饼干屑。</p>
<p>楼外爆竹炸响的瞬间,他抱起那具小小尸身爬上阁楼天台。寒风将一些枯枝败叶一同带了上来,一次性饭盒里焦黑的肉渣正在积雪里渐渐变冷。</p>
<p>烟花照亮低矮的屋顶时,他把脸埋进冰冷僵硬的毛皮。“看见吗?那就是他们说的火树银花。”,他牙齿打颤地说着,手指机械地梳理着结成冰绺的狗毛。</p>
<p>璀璨光斑倒映在浑浊的瞳孔里,远处的笑声乘着风雪飘来。</p>
<p>他仔细辨认着其中的声音:“狗儿要听狗儿歌,毛毛下雨要回家…”</p>
<p>怀中尸体逐渐升腾起腐臭,他却抱得更紧些:“冷了吧?”,用冻裂的嘴唇碰了碰小狗冰凉的鼻尖,“明年除夕我们去求店主,让他…让他给咱个不要的炉子…”</p>
<p>最亮的烟花在空中炸裂时,他颤抖地更剧烈了些。</p>
<p>漫天光雨里,白色编织绳正死死勒着小狗发胀的脖颈——那是上周债主追上门时,他在混乱中失手系上的死结。</p>
<p>“对不起…”</p>
<p>“对不起啊…”</p>
<p>一遍遍呢喃,泪水在脸上结成透明的冰壳。新年的钟声穿过街巷抵达这里时,他终于发现怀里抱着的不过是条破毯子。</p>
<p>烟花熄灭后的黑暗从四面八方涌来。远处富贵人家的暖光透过窗棂,在积雪上切割出方方正正的金色牢笼。</p>
<p>他蜷缩在冰冷的水泥台上,看着自己的影子被月光钉死在斑驳的广告牌上。</p>
<p>"除夕快乐。"他对广告牌上褪色的月饼海报说。海报里笑得喜庆的孩子嘴角开裂,被寒风吹起一角。</p>
<p>雪落满了他的睫毛。</p>
]]></content>
<categories>
<category>gallery</category>
</categories>
</entry>
<entry>
<title>二〇二四年三月二十二日</title>
<url>/2024/03/22/%E4%BA%8C%E3%80%87%E4%BA%8C%E5%9B%9B%E5%B9%B4%E4%B8%89%E6%9C%88%E4%BA%8C%E5%8D%81%E4%BA%8C%E6%97%A5/</url>
<content><![CDATA[<p>毛毛在人群中牵着我,却很难融入人群,我在林中面对着毛毛,树木的思想却千姿百态地向我涌来。<br>
它说,它要将灵魂撕裂,让它暴露在最灼热的阳光下灼烧,要让风刺进来,硬生生扯开一道口子,要让原有的赤色消失殆尽,只留下一堆松散的灰色颗粒,任雨打浮萍。</p>
<p>毛毛和我看到了宇宙熵增,和我见到了熙攘的街道,现在变成了我耳边的风声。<br>
是啊,是啊,你不再身陷囹圄,自由不再是攀在高墙上爬山虎的叶子,不再是吹进风间的花粉,不再是藏躲进阴暗土地里的水,不再是破碎割手的杯子片块。<br>
但是这个世界还是越来越美了,我独自一人,却很自在,别无所求,只想被阳光晒透,被狂风穿过。渴望成熟,要准备好死去,也准备好重生。</p>
<hr>
<p>小土堆上长出了狗尾巴草,当我靠近时,狗尾巴草会向我轻轻晃动,就像它小时候向我跑来时那样,不同的是,当我向前跑的时候,背后没有一个小小的身影再追过来了,但当我回望小土堆,狗尾巴草依然在摇晃,就像当初给予我回应那样。</p>
<hr>
<p>狗儿要听狗儿歌,<br>
毛毛下雨要回家,<br>
小狗小狗画梅花,<br>
直走就是我们家。</p>
]]></content>
<categories>
<category>gallery</category>
</categories>
</entry>
<entry>
<title>二〇二四年一月二十三日</title>
<url>/2024/01/23/%E4%BA%8C%E3%80%87%E4%BA%8C%E5%9B%9B%E5%B9%B4%E4%B8%80%E6%9C%88%E4%BA%8C%E5%8D%81%E4%B8%89%E6%97%A5/</url>
<content><![CDATA[<p>有人在桥上并肩走过,孩子们举着风车大笑着跑开,热乎乎的米糕新鲜出炉,香气混着夜风传去老远。</p>
<p>桥上是喧闹繁杂的尘世万千,桥下是静谧安宁的人间留恋。</p>
<p>我有时候会无法控制的想,如果这一切都是梦,一觉醒来又回到了那暗无天日的日子…但转瞬即否定这个答案———那我会重新走到今天。</p>
<p>寒风拂过我的侧脸,把碎发拂去耳后。</p>
<p>如果你也只剩活着了。</p>
<p>就像一条小鱼,在这一方小世界中摆尾流浪,海波荡漾了许多年,也飘出去那么远。</p>
<p>这么些年的煎熬时光或许将你沉寂,或许掀起了惊涛骇浪,吞噬你的理智与晴空,令你彻底疯狂。</p>
<p>直到海浪卷起鱼群,波及到每一个地方,<br>
直到风平浪静,晴空如洗。</p>
<hr>
<p>但几个小时之后,天际黎明乍现的那一瞬,会有一只无形的大手,将最后一层青灰色的夜幕轻轻抹去。</p>
<p>晨光会从海平线迤逦而来,洒向鳞次栉比的高楼和错综复杂的街道。码头,树木,楼房,电线杆。</p>
<p>这个城市的每个角落都会渐渐苏醒,焕发出明亮的,生机勃勃的色彩。</p>
<p>这是我们此生最好的礼物。</p>
<p>那夜色深处所湮没的一切,都将随着黎明破晓的天光,向遥远虚空奔涌退去,再不回头。</p>
<p>而清晨的信风从天穹呼啸而至,掠过高高的公寓露台,遥远的城市正从我们脚下缓缓醒来。</p>
<hr>
<p>这个世界永远都在,我们也还未曾离开。</p>
<p>那就始于春夏,终于秋冬,循环往复。</p>
<p>让自己的生命力如海浪滔天,不曾停歇。</p>
<p>或许应该换句说法,</p>
<p>这个世界将会是我们义无反顾的绝对热爱。</p>
<hr>
]]></content>
<categories>
<category>gallery</category>
</categories>
</entry>
<entry>
<title>二〇二四年九月一日</title>
<url>/2024/09/01/%E4%BA%8C%E3%80%87%E4%BA%8C%E5%9B%9B%E5%B9%B4%E4%B9%9D%E6%9C%88%E4%B8%80%E6%97%A5/</url>
<content><![CDATA[<p>烦闷的夏,充斥着丰沛雨水的夏,挖了无数个西瓜的夏,牵着她有层薄汗手掌的夏,她还在的夏,终于还是过去了。</p>
<p>眼里噙着的、汇入的、环绕的、裹藏的,产于自身的液体,是加厚的镜片,人们笑着走来打招呼的轮廓亟其模糊,连唇齿语调也跟着散了光线。</p>
<p>不对,等下,我们不能一坐下来铺开纸就谈死亡,我们应该先把天空涂得橙黄,支开笔,喝几口发着陈味的汤。</p>
<p>悲欢离合,悲和离是净化,以使人更看重欢与合。</p>
<p>这个世界上最快而又最慢,最长而又最短,最平凡而又最珍贵,最易被忽视而又最令人后悔的就是时间。</p>
<p>我从母亲柔软漆黑的子宫里走进纷乱杂芜的花花世界,一路上我遇到了形形色色的人,拥有了深深浅浅的联系。但我最终一定会从喧嚣走向沉寂,从清晨踏入黑暗,</p>
<p>因为谁也不会活着离开这个世界,所以这碗汤就敬这必死无疑的一生,在无法重来的这段路上,请尽量快乐,被爱当然很好,没有也没关系,别说、别问,别期待。别会有失望,撑好伞,迈好每一步就行了。</p>
<hr>
<p>通过忠实地映射宇宙来隐藏自我,是融入永恒的唯一途径。</p>
]]></content>
<categories>
<category>gallery</category>
</categories>
</entry>
<entry>
<title>二〇二四年九月十二日</title>
<url>/2024/09/12/%E4%BA%8C%E3%80%87%E4%BA%8C%E5%9B%9B%E5%B9%B4%E4%B9%9D%E6%9C%88%E5%8D%81%E4%BA%8C%E6%97%A5/</url>
<content><![CDATA[<p>人只不过是一根苇草,是自然界最脆弱的东西,但却是一根能思想的苇草。用不着整个宇宙都拿起武器来才能毁灭人类,一口气、一滴水就足以致人类于死命了。然而纵使宇宙毁灭了人类,在人类的视角下自己却仍然要比致其于死命的东西高贵得多,因为人类知道自己要死亡,以及宇宙对其所具有的优势,还可以主动地认为宇宙对此一无所知。</p>
<p>寄蜉蝣于天地,渺沧海之一粟。人类在宇宙面前的大小不比一只金鱼大多少,金鱼不会知道在它生活的空间只是一名人类的玻璃缸,人类也无法得知在其看来无边无际的宇宙是不是只是高等文明收藏的一颗琥珀。</p>
<p>思想——人的全部尊严就在于思想。</p>
]]></content>
<categories>
<category>gallery</category>
</categories>
</entry>
<entry>
<title>二〇二四年二月七日</title>
<url>/2024/02/07/%E4%BA%8C%E3%80%87%E4%BA%8C%E5%9B%9B%E5%B9%B4%E4%BA%8C%E6%9C%88%E4%B8%83%E6%97%A5/</url>
<content><![CDATA[<p>瀑布的水逆流而上,<br>
蒲公英种子从远处飘回,聚成伞的模样,<br>
太阳从西边升起,落向东方。</p>
<p>子弹退回枪膛,<br>
运动员回到起跑线上,<br>
我交回录取通知书,忘了十年寒窗。</p>
<p>厨房里飘来饭菜的香,<br>
你把我的卷子签好名字,<br>
关掉电视,帮我把书包背上。</p>
<p>你还在我身旁。</p>
<hr>
<p>功利性主义总会引导我们思考结局,可生活本质上就是一场旅途。</p>
<p>别再去胡思乱想了,好好欣赏生活中的一切吧,去感受一朵鲜花的盛开,一束阳光的倾泻,一湖清水的静谧。</p>
<p>奶奶说,炉膛的柴火若是爆裂,那是有贵客要登门了。我总是不信。直到有一次,舅公伴着柴火的爆裂声,出现在家门口,我才将信将疑。</p>
<p>所以,让我们在下雨天烧柴火灶,在跳跃的火苗里,等待一种不确定的相逢,然后煮茶饮酒,自由一生 。</p>
]]></content>
<categories>
<category>gallery</category>
</categories>
</entry>
<entry>
<title>二〇二四年五月七日</title>
<url>/2024/05/07/%E4%BA%8C%E3%80%87%E4%BA%8C%E5%9B%9B%E5%B9%B4%E4%BA%94%E6%9C%88%E4%B8%83%E6%97%A5/</url>
<content><![CDATA[<p>我在写信<br>
寄给羊水<br>
信里提到<br>
宇宙称呼它为灰<br>
我们叫地球<br>
还提到<br>
风车不能骑<br>
但是石头可以打水漂<br>
你呀<br>
到时别忘了<br>
用小小的哭声款待我</p>
<hr>
<p>我们应该坐在一起发呆<br>
发很久的呆<br>
然后我说人类好无聊啊<br>
这个地球完蛋了<br>
你点点头</p>
]]></content>
<categories>
<category>gallery</category>
</categories>
</entry>
<entry>
<title>二〇二四年五月十六日</title>
<url>/2024/05/16/%E4%BA%8C%E3%80%87%E4%BA%8C%E5%9B%9B%E5%B9%B4%E4%BA%94%E6%9C%88%E5%8D%81%E5%85%AD%E6%97%A5/</url>
<content><![CDATA[<p>无眼耳鼻舌身意,无色声香味触法,四下皆空,但万物有实,活在真实世界,而非活在一系列参照物之中。</p>
<p>不要有所谓自信或自卑的概念。要意识到,心态这玩意本质就是从比较中产生的,镜花水月般的东西,毫无力量可言。</p>
<p>人若把自己的心态放置在客观事实和规律前面,无可避免就会产生内耗。而现代人这一生绝大多数的痛苦,都是内耗所带来的。</p>
<p>多尝试点事情吧,试得越多,就能越快判断出自己是不是这块料。如果是,很好;如果不是,过,下一个。在这期间不要有任何心态波澜,行就是行,不行就是不行。反正不是非行不可。</p>
<p>人自信不是样样精通、事事皆成,凡事都做得比别人更好;自信是一个高级陷阱,仰赖自信的人终有一日会崩塌于自信的毁灭。</p>
<p>用生命力来取代往日的自信心,用绝对强大的权力意志来取代往日相对成功的小胜小利。</p>
<p>不放弃的理由不是因为自信,而是因为知道这事必须得干/行得通;放弃的理由也不是因为不自信,而是因为知道这事不能去干/行不通。</p>
<p>而一旦在理性和感性层面都认为必须办成某件事,那就不顾一切去办成。哪怕绕再多的路,要办的事就是要办。</p>
<p>当不再局限于自信或自卑,而是立足于实事求是去布局和行动,一切都会变得更加清晰明了。若想成事,首先要花时间去搞明白自己的长处与短板,然后学会在面对具体事项之际快速判断自己行不行、是不是非做不可、要做的话又有几分把握、失败了要如何应对、要不要改变其中某些变量再多试几次。</p>
<p>当有了一个目标,需要的不是豪言壮志,不是任何心理建设,而是尽快上手去做。<br>
做一件事,无关心态,只要去做就是了。发挥最大的主观能动性,遇到问题就解决,碰到障碍就破开,需要帮助就求助。别因为所谓的自信就盲目冒进,也别因为所谓的自卑就胆小退缩,那俩玩意都不存在。</p>
<p>尊重事情本身的客观发展规律,什么样的人匹配什么样的事,什么样的条件匹配什么样的理想。别把心态想得太重要,更别花太多时间精力去建设心态,客观规律不会因为心态就发生奇迹般地转变。</p>
<p>你最好趁早学会尊重客观规律。</p>
]]></content>
<categories>
<category>gallery</category>
</categories>
</entry>
<entry>
<title>二〇二四年六月二十七日</title>
<url>/2024/06/27/%E4%BA%8C%E3%80%87%E4%BA%8C%E5%9B%9B%E5%B9%B4%E5%85%AD%E6%9C%88%E4%BA%8C%E5%8D%81%E4%B8%83%E6%97%A5/</url>
<content><![CDATA[<p>钟摆晃荡 走出时间,<br>
面包屑洒了一地 变成星空,<br>
玻璃杯留下的牛奶唇印、折射出极光,<br>
狂犬症发病具有潜伏期,<br>
宇宙说它也会倦怠、凋落,</p>
<p>身后伟岸的森林倒下了,我最大的遗失是没有了后悔的权利,我的忽视酿成了天地两别再无可相见的结局。</p>
<p>我的眼睛面积一定小于湖,我也很少哭,你若坐在我面前,就像站在湖边,细细的雾水就扯着地连着天。</p>
<blockquote>
<p>凌晨六点。</p>
</blockquote>
<p>远处的地平线出现了阳光,阳光穿透了它们的灵魂,将这一方小世界的一切印在了大地上,它们冰冷的身躯热烈地出现在了我的全家福上。</p>
<blockquote>
<p>早上七点。</p>
</blockquote>
<p>太阳完全绽放的光芒照亮了这个欣欣向荣的世界,有山、有海,有自由。</p>
]]></content>
<categories>
<category>gallery</category>
</categories>
</entry>
<entry>
<title>二〇二四年八月一日</title>
<url>/2024/08/02/%E4%BA%8C%E3%80%87%E4%BA%8C%E5%9B%9B%E5%B9%B4%E5%85%AB%E6%9C%88%E4%B8%80%E6%97%A5/</url>
<content><![CDATA[<p>阿公烧烟成老瘾,<br>
阿公饮酒会面红,<br>
他说日子要开心,<br>
他说人生要尽兴。</p>
]]></content>
<categories>
<category>gallery</category>
</categories>
</entry>
<entry>
<title>二〇二四年十一月十七日</title>
<url>/2024/11/17/%E4%BA%8C%E3%80%87%E4%BA%8C%E5%9B%9B%E5%B9%B4%E5%8D%81%E4%B8%80%E6%9C%88%E5%8D%81%E4%B8%83%E6%97%A5/</url>
<content><![CDATA[<p>快乐是正面情绪的原型,人类的所作所为,最终都是为了追求快乐。我们之所以想追求财富、健康或名声,都是为了借此得到快乐。然而,追求快乐也不是因为它可以带给我们其他好处,因为快乐本身就是目的。</p>
<p>“世人皆以痛楚、孤独、悲伤为大境界,以为这就是深邃的人生,殊不知快乐才是人生的真谛。一个人获取快乐的能力才是真正有用的能力。伤春悲秋、离愁别绪太容易了,读几首诗词即可,但获取欢乐太难了非大丈夫不可为之。”</p>
]]></content>
<categories>
<category>gallery</category>
</categories>
</entry>
<entry>
<title>二〇二四年十二月十八日</title>
<url>/2024/12/18/%E4%BA%8C%E3%80%87%E4%BA%8C%E5%9B%9B%E5%B9%B4%E5%8D%81%E4%BA%8C%E6%9C%88%E5%8D%81%E5%85%AB%E6%97%A5/</url>
<content><![CDATA[<p>一整个寒冷的冬季,一整个寒冷的纠缠时光,一整个寒冷的自我挣扎时间就要来了,</p>
<p>城市中独自等公车时灰白的空气,手背上青紫的针眼,嘈杂人群中的巴宝莉香水味,和长椅上听着音乐的人,都不是我。</p>
<p>我是从土里长出来的,是从一方山林中长出来的。</p>
<p>我在长大后成为了那一方小世界的神明,只要进了那片山林,就知道太阳从哪个方向升起,就知道每一阵风的走向,就知道从眼前经过的每一个生命从何而来。</p>
<p>我没有家,但是太阳升起的时候牵着老狗站在家人的坟包前,身后土地上的影子就是我的全家福。<br>
<br>
在无声的融雪中泪流满面的大地,在白驹过隙的生命中,我瞥见了永恒。</p>
<p>但我无法描述它,不能说,也不能想,却又不能忘。<br>
它不能变成语言,它也无法变成语言,一旦变成语言就不再是它了。<br>
它是一片朦胧的温馨与寂寥,是一片成熟的希望与绝望,它的领地只有心和我身后的坟包。</p>
<p>北风似万鬼过境,<br>
吹扤岭尾田堘。</p>
]]></content>
<categories>
<category>gallery</category>
</categories>
</entry>
<entry>
<title>二〇二四年四月八日</title>
<url>/2024/04/08/%E4%BA%8C%E3%80%87%E4%BA%8C%E5%9B%9B%E5%B9%B4%E5%9B%9B%E6%9C%88%E5%85%AB%E6%97%A5/</url>
<content><![CDATA[<p>我不预设和任何人事物共度一生,<br>
就自然的生活,<br>
命运把我带到哪里就到哪里,<br>
有缘的话,我会在世界中看见你,<br>
无缘的话,我会看见你在世界中。</p>
<p>我为人们的开心而开心,诚恳,真诚的请求看到这里的人们每天都要笑一笑哦,你们真的真的对我很重要。</p>
]]></content>
<categories>
<category>gallery</category>
</categories>
</entry>
<entry>
<title>二零二一年三月二十七日</title>
<url>/2021/03/27/%E4%BA%8C%E9%9B%B6%E4%BA%8C%E4%B8%80%E5%B9%B4%E4%B8%89%E6%9C%88%E4%BA%8C%E5%8D%81%E4%B8%83%E6%97%A5/</url>
<content><![CDATA[<p>学生时代总是有绕不完的操场和靠不够的走廊栏杆,我也失去了那个能将欣喜、期待、愉悦、阴郁、落空一一投掷的旧课桌。</p>
]]></content>
<categories>
<category>gallery</category>
</categories>
</entry>
<entry>
<title>二零二一年三月十五日</title>
<url>/2021/03/15/%E4%BA%8C%E9%9B%B6%E4%BA%8C%E4%B8%80%E5%B9%B4%E4%B8%89%E6%9C%88%E5%8D%81%E4%BA%94%E6%97%A5/</url>
<content><![CDATA[<p>我的每一支笔都记得我的名字,记得我吹不出褶皱的日子中的满腔热血,记得我手上的寸寸沟壑倒映着心中的万里山河。</p>
]]></content>
<categories>
<category>gallery</category>
</categories>
</entry>
<entry>
<title>二零二一年二月十六日</title>
<url>/2021/02/16/%E4%BA%8C%E9%9B%B6%E4%BA%8C%E4%B8%80%E5%B9%B4%E4%BA%8C%E6%9C%88%E5%8D%81%E5%85%AD%E6%97%A5/</url>
<content><![CDATA[<p>我们各司其职,完成自己来人间的使命,人们定义结果的好坏,有的人像神明矗立其中,可他们又是普通人,带着两三心灵呓语,做着自私的事情,恶魔般的做天使。</p>
]]></content>
<categories>
<category>gallery</category>
</categories>
</entry>
<entry>
<title>二零二一年二月十四日</title>
<url>/2021/02/14/%E4%BA%8C%E9%9B%B6%E4%BA%8C%E4%B8%80%E5%B9%B4%E4%BA%8C%E6%9C%88%E5%8D%81%E5%9B%9B%E6%97%A5/</url>
<content><![CDATA[<p>把酒从铁罐子里面赶出来,赶到脸上,被眼泪驱逐,混着干涸的血,揉着尘埃,尝尽酸甜苦辣</p>
]]></content>
<categories>
<category>gallery</category>
</categories>
</entry>
<entry>
<title>二零二三年一月二十一日</title>
<url>/2023/01/21/%E4%BA%8C%E9%9B%B6%E4%BA%8C%E4%B8%89%E5%B9%B4%E4%B8%80%E6%9C%88%E4%BA%8C%E5%8D%81%E4%B8%80%E6%97%A5/</url>
<content><![CDATA[<p>我踏着烟火若隐若现, 我生来就是为了把这个世界的消息带到下一个世界去。</p>
]]></content>
<categories>
<category>gallery</category>
</categories>
</entry>
<entry>
<title>二零二三年七月三十日</title>
<url>/2023/07/30/%E4%BA%8C%E9%9B%B6%E4%BA%8C%E4%B8%89%E5%B9%B4%E4%B8%83%E6%9C%88%E4%B8%89%E5%8D%81%E6%97%A5/</url>
<content><![CDATA[<p>那天阳光很好,她想离开病房, 离开躺了快一辈子的病床,她说想晒晒太阳,我带着她去楼下的草坪。她摸摸花,摸摸草,摸摸树。她给自己编了个很好看很好看的辫子,把她摘的花插在上面。可是还没绑好她就垂下了头。</p>
<blockquote>
<p>辫子散开了,我就知道她走了。</p>
</blockquote>
<hr>
<p>世界很大,大到容纳下云海山川与深海,一定要去看看所有的色彩。世界也很小,小到只能让我一个人存在,原地转一圈,就足以填补了人生所有的空白。</p>
<p>我的家人太早回归了群星,而我还贪恋着人间烟火。</p>
<blockquote>
<p>韩暮秋,息止安所。</p>
</blockquote>
]]></content>
<categories>
<category>gallery</category>
</categories>
</entry>
<entry>
<title>二零二三年九月十三日</title>
<url>/2023/09/13/%E4%BA%8C%E9%9B%B6%E4%BA%8C%E4%B8%89%E5%B9%B4%E4%B9%9D%E6%9C%88%E5%8D%81%E4%B8%89%E6%97%A5/</url>
<content><![CDATA[<p>“站好! 要拍了啊,倒数三秒,都不要眨眼。” 婚礼结束后, 门口路过的陌生男人拿着相机,充当起了临时的摄影师。</p>
<p>镜头里,她和抱着雪狮的希文站在中间,她手边是依次排开的五个哥哥,希文手边是暮秋, 培元, 世琪, 婧灵五人。<br>
在他们后排,其他人也各自找好位置挺直脊背站着。</p>
<p>“拍了啊,倒数,三、二、一……茄子!”</p>
<p>咔擦,画面定格,幸福定格,大家终于从苦难里彻底毕业了。</p>
<p>萤火虫的微光与星空交相辉映,黑夜中潜滋暗长的花香沁人心脾,晚风挠过波光粼粼的海面,彼此相爱的人相拥而眠。</p>
]]></content>
<categories>
<category>gallery</category>
</categories>
</entry>
<entry>
<title>二零二三年二月一日</title>
<url>/2023/02/01/%E4%BA%8C%E9%9B%B6%E4%BA%8C%E4%B8%89%E5%B9%B4%E4%BA%8C%E6%9C%88%E4%B8%80%E6%97%A5/</url>
<content><![CDATA[<p>我想在六点和日出打招呼,我想去菜市场看看卖蔬菜的阿婆,我想花一上午去准备一餐中午饭,我想下午能在阳台捧着一盏热茶慢慢看书,慢点好,再慢点,慢到能透过阳台栏杆的缝隙看看马路上车水马龙映衬下的老人家提着新鲜的一荤一素慢慢蹒跚走回家,看看贼几把大的夕阳在冒着热气的水杯中慢慢融化。</p>
]]></content>
<categories>
<category>gallery</category>
</categories>
</entry>
<entry>
<title>二零二三年二月二十二日</title>
<url>/2023/02/22/%E4%BA%8C%E9%9B%B6%E4%BA%8C%E4%B8%89%E5%B9%B4%E4%BA%8C%E6%9C%88%E4%BA%8C%E5%8D%81%E4%BA%8C%E6%97%A5/</url>
<content><![CDATA[<p>这个小城市还有很多老人家,她们跟不上这个时代的脚步,要有人带她们跟上这个时代,再目送她们去下一个时代,不只是老人家,还有我们的未来。</p>
]]></content>
<categories>
<category>gallery</category>
</categories>
</entry>
<entry>
<title>二零二三年二月二十五日</title>
<url>/2023/02/25/%E4%BA%8C%E9%9B%B6%E4%BA%8C%E4%B8%89%E5%B9%B4%E4%BA%8C%E6%9C%88%E4%BA%8C%E5%8D%81%E4%BA%94%E6%97%A5/</url>
<content><![CDATA[<p>我无法表达出我所处的时间概念,只是我的实际感受,睁眼闭眼就是过去和未来。</p>
<p>我闭上眼睛就能感受到不知道是过去还是未来的某个时刻,阳光从睫毛的缝隙钻进我的眼睛,闭上眼睛是一片光圈,我看不清周围,我也不需要看清周围,我只需要感觉到整个世界给我最热烈的拥抱。</p>
<p>我睁开眼睛也能感受到过去还是未来的某个时刻,我开车穿梭在灰蒙蒙的细雨笼罩中的城市,细雨模糊了整个城市的夜空,在我的车窗前汇成水珠晕开了红绿灯的光线,再被雨刮器一扫而空。</p>
]]></content>
<categories>
<category>gallery</category>
</categories>
</entry>
<entry>
<title>二零二三年二月二十六日</title>
<url>/2023/02/26/%E4%BA%8C%E9%9B%B6%E4%BA%8C%E4%B8%89%E5%B9%B4%E4%BA%8C%E6%9C%88%E4%BA%8C%E5%8D%81%E5%85%AD%E6%97%A5/</url>
<content><![CDATA[<p>翻滚的热汤接过焰火的能量把周围的蒸汽撕开一道裂缝,透过缝隙我见到一对爱人的时空交织在未来,他们幸运的错开孤独,人们举起手中的酒杯,祝因果与命运缠绵之人幸福</p>
]]></content>
<categories>
<category>gallery</category>
</categories>
</entry>
<entry>
<title>二零二三年二月二十四日</title>
<url>/2023/02/24/%E4%BA%8C%E9%9B%B6%E4%BA%8C%E4%B8%89%E5%B9%B4%E4%BA%8C%E6%9C%88%E4%BA%8C%E5%8D%81%E5%9B%9B%E6%97%A5/</url>
<content><![CDATA[<p>黑色的袋子里装满了药,这种袋子我都是用来装垃圾的,它被昏昏欲睡的老人家抱在怀里,里面的药丸随着颠簸的车厢敲打罐子,老人家醒了,她的眼睛比袋子还浑浊。</p>
<p>她看着我说到站了,我拉着她的手下车,该回家了,等下还要去买支圆珠笔,中性笔没法在药盒子上写用法用量。</p>
]]></content>
<categories>
<category>gallery</category>
</categories>
</entry>
<entry>
<title>二零二三年二月二日</title>
<url>/2023/02/02/%E4%BA%8C%E9%9B%B6%E4%BA%8C%E4%B8%89%E5%B9%B4%E4%BA%8C%E6%9C%88%E4%BA%8C%E6%97%A5/</url>
<content><![CDATA[<p>我不知道这些黑乎乎的东西从哪来的,它们填满了这黄铜色的硬币每一处,像机油,像泥泞,它们随着这五角钱硬币一起从我手上换走了一袋糖果,那是一个蓬头垢面,满身泥灰的小孩,他起身看着这个给他糖果的叔叔,我蹲下看着我拥有糖果的童年。<br>
<br>
这事很恐怖,意味着我在这个地方又多了一丝牵挂,多了一个不离开的理由。</p>
]]></content>
<categories>
<category>gallery</category>
</categories>
</entry>
<entry>
<title>二零二三年八月二十一日</title>
<url>/2023/08/21/%E4%BA%8C%E9%9B%B6%E4%BA%8C%E4%B8%89%E5%B9%B4%E5%85%AB%E6%9C%88%E4%BA%8C%E5%8D%81%E4%B8%80%E6%97%A5/</url>
<content><![CDATA[<p>他看了一眼远处的尸体,指挥着镰刀将远处的杂草割干净,到处都是鲜花,春天来了,漫山遍野。</p>
<p>远处爷爷奶奶在为一件小事儿吵架,爸爸劝架的大嗓门声音居然比吵架的还大。</p>
<p>我听见脚步声靠近,花香味浓了一些。</p>
<p>妈妈的声音依旧和记忆中一样,温和的像春风,暖意融融,似在聊家常,平淡又温馨:“今年花开了,你要不要醒来看看。”</p>
<p>我有心想给个惊喜,猛地睁开眼睛,一眼就看见了惊讶的花都掉了的妈妈,我抱住了她。</p>
<p>她恍惚了好久才反应过来。</p>
<p>“啊,真是,怎么越大越调皮了”,她唇角的笑意却是不断扩大。</p>
<p>“欢迎醒来,乐乐”</p>
<p>我抖了抖身上的花瓣,还未说话,就听见了爸爸兴奋的欢呼声。爷爷奶奶眼睛一亮,立刻跑了过来,春天的风似乎都柔软了,我摊了摊手,将他们卷到自己怀里。</p>
<p>春暖花开,一切都在,真好。</p>
]]></content>
<categories>
<category>gallery</category>
</categories>
</entry>
<entry>
<title>二零二三年八月二十七日</title>
<url>/2023/08/27/%E4%BA%8C%E9%9B%B6%E4%BA%8C%E4%B8%89%E5%B9%B4%E5%85%AB%E6%9C%88%E4%BA%8C%E5%8D%81%E4%B8%83%E6%97%A5/</url>
<content><![CDATA[<p>我从失去家人的那一刻就坠入了一个朦胧而美丽的梦,梦境没有主人,我是被梦境所留下的,永远不愿醒来的旅人。<br>
我足不出户,一直缩在一方小世界里面,我用了无数的理由和所有人说我的生活态度,其实勇敢的人根本不需要理由就能走出去,只有懦弱的人会用各种理由说服自己退缩</p>
<p>请以我为戒,大胆往前走,走到灯火通明。</p>
<p>何须纵酒方能舞翩迁,<br>
赤诚相见时便是少年。</p>
]]></content>
<categories>
<category>gallery</category>
</categories>
</entry>
<entry>
<title>二零二三年六月十日</title>
<url>/2023/06/10/%E4%BA%8C%E9%9B%B6%E4%BA%8C%E4%B8%89%E5%B9%B4%E5%85%AD%E6%9C%88%E5%8D%81%E6%97%A5/</url>
<content><![CDATA[<p>请绝对理性的面对所有事情,这是一个人工作学习生活的基础,但同时要感同身受的去阅读文学类作品,这是保留一丝感性的方式,感性是不可或缺的,理性可以让人控制感性,但也可以消灭它。</p>
<p>独处的世界很容易出现信息茧房,要不断的和人交流,把很多想法说出来,接收一切评论,不然会在长期的不分正确错误的信息堆叠中出现一团巨大的闭塞性的知识,这很不利于快乐的活下去。</p>
<p>接收到的评论不必急于改变,先存起来,让它们陪着自己,跟着时间走,路上会慢慢的和其他的事情连结起来,这样就能择其善者而从之,其不善者而改之了。</p>
<p>应当早日从这喧扰的世界冷静下来,让脑子里满是憧憬和情爱的灵魂得到一丝陈酿,理性点抬头看看世界上方的二氧化碳,自己晃晃头打破能回到最初的样子再重来的梦。</p>
<p>爱你们。</p>
]]></content>
<categories>
<category>gallery</category>
</categories>
</entry>
<entry>
<title>二零二三年十一月六日</title>
<url>/2023/11/06/%E4%BA%8C%E9%9B%B6%E4%BA%8C%E4%B8%89%E5%B9%B4%E5%8D%81%E4%B8%80%E6%9C%88%E5%85%AD%E6%97%A5/</url>
<content><![CDATA[<p>寒风打得他踉跄地摔进田野中,他颤抖着,疑惑的抬起头,云层捂住了月亮,摇曳着露出一角烛火般的月光,烛火同他的身体一般颤抖,那是落日的余晖,也是等待朝阳的希望。</p>
<p>世界同可怜的孩子一起闭上了眼睛,世界不那么开心,但是</p>
<blockquote>
<p>这个世界,我真的爱你</p>
</blockquote>
]]></content>
<categories>
<category>gallery</category>
</categories>
</entry>
<entry>
<title>二零二三年四月二十二日</title>
<url>/2023/04/22/%E4%BA%8C%E9%9B%B6%E4%BA%8C%E4%B8%89%E5%B9%B4%E5%9B%9B%E6%9C%88%E4%BA%8C%E5%8D%81%E4%BA%8C%E6%97%A5/</url>
<content><![CDATA[<p>二零二三年四月二十二日, 风沙把阳光的气息卷入帐篷, 现在是凌晨四点二十七,快天亮了, 仍有些微凉, 我即将踏入看不到边的大漠。</p>
<p>我的心思很简单, 忘了些什么, 记起来些什么, 痛苦的根源在哪里,</p>
<p>从这渲扰的世界沉静下来,好让我这满是凡尘虚伪的灵魂有一丝陈酿,还有世界的上方流泪的天空,和被二氧化碳覆盖的皮囊,该怎么回到最初的样子。</p>
<p>我也曾独自冒着冷风,去薄霜铺地的林子里,为听鸟语,为盼朝阳,为寻泥土里渐次苏醒的花草,但春信不至。我是如此单独而完整,在无数个夜晚,独自顶着冷风 伫立在老橘树下的桥头,只为听一曲夜莺的哀歌,倚暖了石栏上的青苔,青苔凉透了我的心坎,但夜莺不来,夜莺不来啊。</p>
<p>不多矫情, 秋风骀荡兮,我心向伊,漫漫黄沙兮,修我足迹!</p>
<p>不知道这漫无边际的黄沙下面埋葬了多少文明, 只觉神秘, 但未曾有过恐惧, 我敬畏着这个世界。</p>
<p>给岁月以文明,进入大漠只有往前,没有后路。</p>
<p>岁月中的某个文明中的某个人,曾经也骑着骆驼向远方,又是哪个人,吟着佛门心语站在我身旁,我听见大漠的念经声。</p>
<p>我不是个有耐心的人, 抄经书对我来说是种折磨, 我只能选择把经文抄在大地上, 黄沙上, 用自己的双腿一点点写, 用耳去倾听狂风卷积的声音。</p>
<p>我生来就是为了把这个世界的消息和秘密带到下一个世界去。</p>
]]></content>
<categories>
<category>gallery</category>
</categories>
</entry>
<entry>
<title>二零二三年四月二十五日</title>
<url>/2023/04/25/%E4%BA%8C%E9%9B%B6%E4%BA%8C%E4%B8%89%E5%B9%B4%E5%9B%9B%E6%9C%88%E4%BA%8C%E5%8D%81%E4%BA%94%E6%97%A5/</url>
<content><![CDATA[<p>一位真正的作家永远只为自己的内心写作,只有内心才会真实的告诉他,他的自私,他的高尚是多么的突出。内心让他真实地了解自己,一旦了解了自己也就了解了世界。</p>
<p>作家要表达与之朝夕相处的现实,他常常会感到难以承受,蜂拥而来的真实几乎都在诉说着阴险丑恶和黑暗,真正的作家所追寻的是真理,这是一种排斥道德判断的真理,不是发泄,不是控诉或揭露,而是向人们展示高尚,高尚不是单纯的美好,而是对一切事物理解之后的超然,对善和恶一视同仁,用同情的目光看世界。</p>
<p>活着的力量不是来自于喊叫,也不是来自于进攻,而是忍受,去忍受生命赋予我们的责任,去忍受现实给予我们的幸福和苦难、无聊和平庸。</p>
<p>人是为了活着本身而活着,而不是为了活着之外的任何事物而活着。</p>
<p>人的体验和欲望还有想象和理解,会填补所有不同的界限,会让一个人从他人的经历之中感受到自己的命运,就像是在不同的镜子里看到的都是自己的形象。</p>
<p>我们都是这个世界上的迷路者,我们都是按照自己认定的道路寻找方向,也许我们是对的,也许我们是错的,或者有时候对了有时候错了,在出生之前和死去之前,我们谁也不知道前面的时间里在等待我们的是什么。</p>
<p>生活是属于每个人自己的感受,不属于任何别人的看法。</p>
<p>人们时常流出浑浊的眼泪,并不是因为人们时常悲伤,人们在高兴时甚至是在什么事都没有的平静时刻,也会有泪流出,然后举起被社会,岁月刮伤的手指,擦去眼泪,如同掸去身上的稻草。</p>
<p>也许是困苦的生活损坏了人们的记忆,面对往事人们时常显得木讷,常常以不知所措的微笑搪塞过去,对自己的经历缺乏热情,仿佛是道听途说般只记得零星几点,即便是这零星几点也都是自身之外的记忆,用一两句话表达了一切。</p>
]]></content>
<categories>
<category>gallery</category>
</categories>
</entry>
<entry>
<title>二零二二年一月一日</title>
<url>/2022/01/01/%E4%BA%8C%E9%9B%B6%E4%BA%8C%E4%BA%8C%E5%B9%B4%E4%B8%80%E6%9C%88%E4%B8%80%E6%97%A5/</url>
<content><![CDATA[<p>我在一声叹息中恍过了宁静和繁华,目的明确的化成一捧黄土,被风带到这树荫底下,看草萌木长,看人来人往。</p>
]]></content>
<categories>
<category>gallery</category>
</categories>
</entry>
<entry>
<title>二零二二年三月十四日</title>
<url>/2022/03/14/%E4%BA%8C%E9%9B%B6%E4%BA%8C%E4%BA%8C%E5%B9%B4%E4%B8%89%E6%9C%88%E5%8D%81%E5%9B%9B%E6%97%A5/</url>
<content><![CDATA[<p>我很庆幸能在那年中秋的傍晚出门走走。我看到了落日藏进天边,看到了圆月晕开情绪,我感受到了小山包上的晚风,感受到了落叶翻进衣领清扫锁骨,它们让我留在这个小地方做个平凡且平庸的人,过着平静且平安的生活,尽管生活平常且饭菜平淡,但这日子的每分每秒都是我对生活的爱意。</p>
]]></content>
<categories>
<category>gallery</category>
</categories>
</entry>
<entry>
<title>二零二二年九月二十二日</title>
<url>/2022/09/22/%E4%BA%8C%E9%9B%B6%E4%BA%8C%E4%BA%8C%E5%B9%B4%E4%B9%9D%E6%9C%88%E4%BA%8C%E5%8D%81%E4%BA%8C%E6%97%A5/</url>
<content><![CDATA[<p>似是而非的脸,和我的吉他,低头呢喃细语,抬头语焉不详,麦浪,烈日,溪水,咳嗽,敲打,药丸碰撞罐壁。</p>
]]></content>
<categories>
<category>gallery</category>
</categories>
</entry>
<entry>
<title>二零二二年八月二十一日</title>
<url>/2022/08/21/%E4%BA%8C%E9%9B%B6%E4%BA%8C%E4%BA%8C%E5%B9%B4%E5%85%AB%E6%9C%88%E4%BA%8C%E5%8D%81%E4%B8%80%E6%97%A5/</url>
<content><![CDATA[<p>不管是什么样的生活,都只是为了让自己满意,我这样活着,没什么不满的,我很痛快。这世上所有东西,我留意它时,它就存在,不留意时,它就是虚无。幸运的是我留意的事物很少,不致于身心劳碌一生奔忙。岁月会锤炼我一副丰满的灵魂和清瘦的欲望,死的时候会闭眼的。</p>
]]></content>
<categories>
<category>gallery</category>
</categories>
</entry>
<entry>
<title>二零二二年十一月月三日</title>
<url>/2022/11/03/%E4%BA%8C%E9%9B%B6%E4%BA%8C%E4%BA%8C%E5%B9%B4%E5%8D%81%E4%B8%80%E6%9C%88%E6%9C%88%E4%B8%89%E6%97%A5/</url>
<content><![CDATA[<p>我独自冒着冷风,去薄霜铺地的林子里,为听鸟语,为盼朝阳,为寻泥土里渐次苏醒的花草,但春信不至。我是如此单独而完整,在无数个夜晚,独自顶着冷风 伫立在老橘树下的桥头,只为听一曲夜莺的哀歌,倚暖了石栏上的青苔,青苔凉透了我的心坎,但夜莺不来,夜莺不来啊。</p>
]]></content>
<categories>
<category>gallery</category>
</categories>
</entry>
<entry>
<title>二零二二年十二月三十一日</title>
<url>/2022/12/31/%E4%BA%8C%E9%9B%B6%E4%BA%8C%E4%BA%8C%E5%B9%B4%E5%8D%81%E4%BA%8C%E6%9C%88%E4%B8%89%E5%8D%81%E4%B8%80%E6%97%A5/</url>
<content><![CDATA[<p>我要做时间富人,肆意挥霍,飘在世界上空,什么都不做,静静看着周遭的一切,命运给我什么我就接着,命运拿走什么我就看着,到模糊不见,到夜色如烟。</p>
]]></content>
<categories>
<category>gallery</category>
</categories>
</entry>
<entry>
<title>二零二二年十月一日</title>
<url>/2022/10/01/%E4%BA%8C%E9%9B%B6%E4%BA%8C%E4%BA%8C%E5%B9%B4%E5%8D%81%E6%9C%88%E4%B8%80%E6%97%A5/</url>
<content><![CDATA[<p>巨大而又空洞的表达欲,失语后渴望把自己潦草在这个时代里。</p>
]]></content>
<categories>
<category>gallery</category>
</categories>
</entry>
<entry>
<title>二零二零年十二月九日</title>
<url>/2020/12/09/%E4%BA%8C%E9%9B%B6%E4%BA%8C%E9%9B%B6%E5%B9%B4%E5%8D%81%E4%BA%8C%E6%9C%88%E4%B9%9D%E6%97%A5/</url>
<content><![CDATA[<p>我们走在朝阳的洪流上, 一步一个脚印, 踏散阳光, 向南是北。</p>
]]></content>
<categories>
<category>gallery</category>
</categories>
</entry>
<entry>
<title>二零二零年十二月二十六日</title>
<url>/2020/12/26/%E4%BA%8C%E9%9B%B6%E4%BA%8C%E9%9B%B6%E5%B9%B4%E5%8D%81%E4%BA%8C%E6%9C%88%E4%BA%8C%E5%8D%81%E5%85%AD%E6%97%A5/</url>
<content><![CDATA[<p>我被包裹在吊床中,两颗香樟树支撑着它,仲夏的热浪砸在粗壮的香樟树上,叶子一屈,抱住了热浪,可随即而来的是不知从哪浇来的阳光,叶子承受不住这样的力量,被砸的孔洞百出,砸的香气落满树下的人间。</p>
]]></content>
<categories>
<category>gallery</category>
</categories>
</entry>
<entry>
<title>二零二零年十二月八日</title>
<url>/2020/12/08/%E4%BA%8C%E9%9B%B6%E4%BA%8C%E9%9B%B6%E5%B9%B4%E5%8D%81%E4%BA%8C%E6%9C%88%E5%85%AB%E6%97%A5/</url>
<content><![CDATA[<p>它们勇敢的站在烈日下, 像人类一样无所畏惧, 却又恐惧的躲在黑暗中, 像当初的它们一样无处可逃。</p>
]]></content>
<categories>
<category>gallery</category>
</categories>
</entry>
<entry>
<title>从事件风暴看领域驱动设计</title>
<url>/2025/02/11/%E4%BB%8E%E4%BA%8B%E4%BB%B6%E9%A3%8E%E6%9A%B4%E7%9C%8B%E9%A2%86%E5%9F%9F%E9%A9%B1%E5%8A%A8%E8%AE%BE%E8%AE%A1/</url>
<content><![CDATA[<p>事件风暴(Event Storming)是一种领域驱动设计(DDD)的实践方法,由 Alberto Brandolini 提出,旨在通过团队协作的方式快速理解和建模业务领域。</p>
<p>以下是事件风暴及相关领域驱动设计中的一些核心概念和知识点:</p>
<p>1. 领域(Domain):指的是业务相关知识的集合,可以进一步划分为子域。<br>
2. 子域(Subdomain):是领域的一部分,可以是核心域、支撑域或通用域。<br>
3. 核心域(Core Domain):指领域中最核心的部分,通常对应企业的核心业务。<br>
4. 通用语言(Ubiquitous Language):团队所有成员使用的一种语言,用于确保业务和软件之间的沟通一致性。<br>
5. 限界上下文(Bounded Context):定义了一组规则和协议,用于明确领域模型的适用范围。<br>
6. 实体(Entity):具有唯一标识和生命周期的领域对象。<br>
7. 值对象(Value Object):描述了某种特性或属性的对象,没有概念标识。<br>
8. 聚合(Aggregate):一组相关对象的集合,由一个聚合根(Aggregate Root)统一管理。<br>
9. 领域事件(Domain Event):领域中发生的重要事件,可以用于通知其他领域对象或跨限界上下文进行解耦和协作。<br>
10. 命令(Command):表示要执行的操作,通常与事件一一对应。<br>
11. 读模型(Read Model):为了优化读取操作而设计的模型,可能与写模型不同。<br>
12. 决策命令(Decision Command):在事件风暴中,直接导致事件发生的命令。<br>
13. 战略设计(Strategic Design):高层次的抽象和归类,包括理清上下文和子域的划分。<br>
14. 战术设计(Tactical Design):对特定上下文下的模型进行详细设计,包括聚合、实体和值对象。<br>
15. 贫血模型(Anemic Domain Model):领域对象只有属性及其getter/setter方法的纯数据类,业务逻辑通过服务实现。<br>
16. 充血模型(Rich Domain Model):领域对象包含业务逻辑,每个对象都是活跃的。<br>
17. 资源库(Repository):用于检索和持久化领域对象的机制。<br>
18. 服务(Service):在模型中独立的操作,可以是领域服务或应用服务。<br>
19. 固定规则(Invariant):为设计元素做出的断言,必须一直保持为真。</p>
<p>事件风暴通常包括以下步骤:</p>
<ul>
<li>识别领域事件</li>
<li>确定事件顺序</li>
<li>识别命令和触发器</li>
<li>识别聚合和实体</li>
<li>划分限界上下文</li>
<li>识别领域服务和资源库</li>
<li>通过这些步骤,团队可以共同创建出反映业务领域的领域模型,为软件设计和开发提供指导。</li>
</ul>
]]></content>
<tags>
<tag>Technique</tag>
</tags>
</entry>
<entry>
<title>儿童新型冠状病毒Omicron株和肺炎支原体混合感染重症肺炎1例</title>
<url>/2023/10/25/%E5%84%BF%E7%AB%A5%E6%96%B0%E5%9E%8B%E5%86%A0%E7%8A%B6%E7%97%85%E6%AF%92Omicron%E6%A0%AA%E5%92%8C%E8%82%BA%E7%82%8E%E6%94%AF%E5%8E%9F%E4%BD%93%E6%B7%B7%E5%90%88%E6%84%9F%E6%9F%93%E9%87%8D%E7%97%87%E8%82%BA%E7%82%8E1%E4%BE%8B/</url>
<content><![CDATA[<p>患儿 男, 3 岁 10 月龄,因“间歇发热 2 周伴咳嗽、气促”入上海交通大学医学院附属仁济医院(南部院区)定点医院隔离病房。<br>
患儿病初发热,确诊为新型冠状病毒肺炎(简称新冠肺炎),热退 2d 再次持续高热>3d 伴气促、吸气性凹陷,胸部 CT 示右肺下叶片状密度影,诊断“重型新冠肺炎”,行支气管镜肺泡灌洗,宏基因组病原检测确诊为混合肺炎支原体感染,治疗好转出院。<br>
提示临床医生诊治重型新冠肺炎时需警惕混合感染</p>
<ul>
<li><a href="../papers/%E5%84%BF%E7%AB%A5%E6%96%B0%E5%9E%8B%E5%86%A0%E7%8A%B6%E7%97%85%E6%AF%92Omicron%E6%A0%AA%E5%92%8C%E8%82%BA%E7%82%8E%E6%94%AF%E5%8E%9F%E4%BD%93%E6%B7%B7%E5%90%88%E6%84%9F%E6%9F%93%E9%87%8D%E7%97%87%E8%82%BA%E7%82%8E1%E4%BE%8B.pdf">报告原档</a></li>
</ul>
]]></content>
<tags>
<tag>Medicine</tag>
</tags>
</entry>
<entry>
<title>冬季咳嗽辨别指南</title>
<url>/2023/10/22/%E5%86%AC%E5%AD%A3%E5%92%B3%E5%97%BD%E8%BE%A8%E5%88%AB%E6%8C%87%E5%8D%97/</url>
<content><![CDATA[<p>这里给出一些在冬季,临床上简单诊断咳嗽的例子,仅供参考, 无论结果如何, 立刻就医.</p>
<p>这里简单分成有痰和无痰(或痰少)两类, 然后分别以时间段细分:</p>
<ul>
<li>有痰
<ul>
<li>夜间咳:
<ul>
<li>刚睡着就咳嗽,坐起来会好点: 可能是鼻涕倒流刺激咽喉, 也可能是上呼吸道感染, 比如过敏性或非过敏性鼻炎, 急慢性鼻窦炎</li>
<li>凌晨四五点咳醒:
<ul>
<li>长期如此: 考虑哮喘发作</li>
<li>短期如此: 考虑支气管炎</li>
</ul>
</li>
<li>睡到一半胸闷气短, 咳嗽甚至咳出带血丝的痰: 考虑心脏病</li>
</ul>
</li>
<li>每天不定时咳嗽: 这种情况在临床上一般是烟抽多了, 也有可能是慢性肺阻塞, 慢性支气管炎, 支气管扩张, 肺气肿引起的
<ul>
<li>带血,呼吸困难,体重下降:考虑肺结核或肺癌</li>
</ul>
</li>
<li>无特定时间咳嗽:
<ul>
<li>伴有鼻子难受, 浑身乏力等: 考虑过敏, 环境过敏,(比如像新装修的屋子里的甲醛)或者药物过敏(比如降压药)</li>
</ul>
</li>
</ul>
</li>
<li>无痰(或痰少)
<ul>
<li>白天咳: 常见于急慢性咽喉炎(一开口说话就想咳嗽, 不说话或睡大觉的时候不想咳)</li>
<li>夜间咳
<ul>
<li>睡着睡着突然猛咳, 还有胸闷胀气, 口干口苦, 声音嘶哑, 心灼热之类的症状: 一般是胃食管反流导致的</li>
<li>睡眠中咳嗽不止,像被呛到一样, 睡得浅梦还多: 考虑上呼吸道阻塞症候群, 可能与睡眠暂停呼吸症有关</li>
</ul>
</li>
<li>每天不定时咳嗽:
<ul>
<li>小孩和老人首先判断是否误吸异物</li>
<li>否则可能是咽喉神经过度敏感</li>
</ul>
</li>
<li>无特定时间咳嗽:
<ul>
<li>可能由于某些心理问题, 紧张焦虑之类的</li>
</ul>
</li>
</ul>
</li>
</ul>
<hr>
<p>接下来是一些参考的, 以食疗为主的方子, 注意, 不要随便吃药, 无论如何遵医嘱, 考虑自己身体情况, 过敏原.</p>
<ol>
<li>咳嗽多痰可以以芦根为主, 竹茹入肺, 胃两经, 可以清肺化痰
<ul>
<li>新鲜芦根100克,竹茹15克,芦根切碎和竹茹一起放水里煎一会儿,然后用汁水煮粥喝</li>
<li>配合甘草也可以平喘止咳,但注意,脾胃虚寒的话不可用: 芦根50克, 甘草5克, 加1L水煮沸十分钟,去掉渣渣喝100毫升, 喝多了也不太好.</li>
</ul>
</li>
<li>当然还有常见的冰糖雪梨啦, 经典的润肺止咳:
<ul>
<li>买个漂亮的大梨, 顶上削一块, 挖空里面, 把挖出来的果肉切块重新放进去, 再加入川贝母粉, 冰糖, 陈皮, 大火煮沸之后立刻转小火蒸三刻钟</li>
</ul>
</li>
<li>百合银耳, 这个不用多说了叭, 银耳莲子羹? 百合大枣粥?</li>
</ol>
]]></content>
<tags>
<tag>Medicine</tag>
<tag>Life</tag>
</tags>
</entry>
<entry>
<title>发热及超高热危象</title>
<url>/2023/06/17/%E5%8F%91%E7%83%AD%E5%8F%8A%E8%B6%85%E9%AB%98%E7%83%AD%E5%8D%B1%E8%B1%A1/</url>
<content><![CDATA[<p>应急诊断与处理放在开头,理论放在后面</p>
<h2 id="处理"><a class="header-anchor" href="#处理">¶</a>处理</h2>
<h3 id="支持治疗"><a class="header-anchor" href="#支持治疗">¶</a>支持治疗</h3>
<p>患者出现神志改变、呼吸窘迫、血流动力学不稳定等危及生命的症状与体征时,立即实施监护、建立静脉通路、气道管理、补液以及氧疗,必要时予以呼吸支持治疗。</p>
<h3 id="超高热危象的处理"><a class="header-anchor" href="#超高热危象的处理">¶</a>超高热危象的处理</h3>
<p>一旦出现超高热,应以最快的速度降低中心体温、代谢率,以打断超高热引起的恶性循环,同时防治各种并发症。其中,降温是抢救超高热危象的主要措施。降温速度决定预后,应在1小时内使直肠温度降至38.5℃以内。</p>
<h3 id="对症处理"><a class="header-anchor" href="#对症处理">¶</a>对症处理</h3>
<p>发热的对症治疗包括:</p>
<ol>
<li>物理降温:一般可用冷毛巾湿敷额部,每5~10分钟更换1次,或用冰袋置于额、枕后、颈、腋和腹股沟处降温,或用25%~50%乙醇擦浴。或头置冰帽、冰水灌肠、冷盐水洗胃,或将患者置于空调房内(使室温维持在27℃左右)。应根据具体条件选用。</li>
<li>药物降温:视发热程度可采用口服或肌注解热镇痛药。常用的口服解热镇痛药有:阿司匹林(0.3~0.6g/次)、对乙酰氨基酚(0.3~0.5g/次)、布洛芬(0.2~0.4g/次)、安乃近(0.25~0.5g/次)、解热止痛片(APC片,1~2片/次)、速效伤风胶囊(1~2粒/次)、复方对乙酰氨基酚片(1~2片/次)等。常用的注射用解热镇痛药有:阿司匹林精氨酸盐(0.5~1.0g/次)、阿司匹林赖氨酸盐(赖氨匹林,0.9~1.8g/次)、对乙酰氨基酚(0.15~0.25g/次)、息热痛注射液(2ml/次)、复方氨林巴比妥注射液(1支/次)等。高热者病情需要时可短期应用肾上腺皮质激素,如地塞米松5~10mg静注或肌注;或以地塞米松12~20mg/d或氢化可的松300~600mg/d静滴。</li>
</ol>
<h3 id="抗生素经验性应用"><a class="header-anchor" href="#抗生素经验性应用">¶</a>抗生素经验性应用</h3>
<p>对感染病例早期抗生素经验性应用是有益的。一般来讲,若有明确的病原菌感染,则选择覆盖特定病原菌感染的窄谱抗生素;若不明确,可选择覆盖革兰阳性和革兰阴性需氧菌、厌氧菌的广谱抗生素。</p>
<h3 id="诊断性治疗"><a class="header-anchor" href="#诊断性治疗">¶</a>诊断性治疗</h3>
<p>当发热病因一时难以查明时,在不影响进一步检查的情况下,可按可能性较大的病因进行诊断性治疗(如疑疟疾,可试用氯喹;疑阿米巴性肝脓肿,行抗阿米巴治疗;疑结核病行抗结核治疗时间以3~4周以上为宜),期望获得疗效而做出临床诊断。诊断性治疗应选用特异性强、疗效确切及安全性大的治疗药物,剂量应充足并完成整个疗程,无特殊原因不得随便更换试验药物。</p>
<h2 id="诊断"><a class="header-anchor" href="#诊断">¶</a>诊断</h2>
<h3 id="病史"><a class="header-anchor" href="#病史">¶</a>病史</h3>
<p>详细询问病史对发热原因的诊断常能提供重要线索。此外,对发热患者定期检测体温,密切观察热度的高低、时限、热型等也有重要价值。</p>
<ol>
<li>
<p>起病方式: 一般而言,急性感染性疾病起病多较急骤,常有受凉、疲劳、外伤或进食不洁食物等病史,若发热前有明显寒战者,多属化脓性细菌感染或疟疾;而一般非感染性发热,以及结核、伤寒、立克次体和病毒感染多无寒战。</p>
</li>
<li>
<p>发热的分期与分型</p>
<ol>
<li>体温上升期:表现为疲乏、不适感、肌肉酸痛、皮肤苍白、干燥、无汗、畏寒或寒战等症状。体温上升有两种形式:
<ol>
<li>骤升型:体温在数小时内达39~40℃以上,常伴有寒战</li>
<li>缓升型:体温于数日内缓慢上升达高峰。</li>
</ol>
</li>
<li>高热持续期:此时体温已达高峰,临床表现为皮肤潮红而灼热,呼吸加快加强,可有出汗。此期持续数小时、数天或数周。其热型(体温曲线)可表现为:
<ol>
<li>稽留热(continued fever):体温持续于39~40℃左右,达数天或数周,24小时波动范围不超过1℃。见于肺炎、伤寒、斑疹伤寒(早期)等。</li>
<li>弛张热(remittent fever):体温在39℃以上,但波动幅度大,24小时内体温差达2℃以上,体温最低时一般仍高于正常水平。见于脓毒血症、风湿热、重症结核、化脓性炎症如肝脓肿等</li>
<li>间歇热(intermittent fever):高热期与无热期交替地出现。体温波动幅度可达数度。无热期(间歇期)持续1天乃至数天,反复发作。见于疟疾、急性肾盂肾炎、局限性化脓性感染等。</li>
<li>回归热(recurrent fever):体温急骤升高至39℃以上,持续数天后又骤然下降至正常水平,高热期与无热期各持续若干天,即规律地互相交替一次。见于回归热、霍奇金病、周期热等。</li>
<li>波状热(undulant fever):体温逐渐升高达39℃或以上,数天后又逐渐下降至正常水平,数天后又逐渐升高,如此反复多次,常见于布鲁菌病、恶性淋巴瘤等。</li>
<li>不规则热(irregular fever):发热持续时间不定,变动无规律,可见于肺结核、感染性心内膜炎等。</li>
</ol>
</li>
<li>体温下降期: 由于机体的防御功能与适当的治疗,疾病得到控制,体温恢复正常。体温下降的方式有两种:
<ol>
<li>骤降(crisis):体温于数小时内迅速降至正常,有时可低于正常,常伴有大汗。常见于疟疾、急性肾盂肾炎、肺炎及输液反应等。</li>
<li>渐降(lysis):体温于数天内逐渐降至正常。如伤寒、风湿热等</li>
</ol>
</li>
</ol>
</li>
</ol>
<h4 id="伴随症状"><a class="header-anchor" href="#伴随症状">¶</a>伴随症状</h4>
<p>重视具有定位意义的伴发的局部症状,以便确定主要病变在哪个系统。如发热伴有鼻塞流涕、咽痛、咳嗽,而一般情况良好者多为上呼吸道感染,若有胸痛、咯铁锈色痰和呼吸困难者,则多为下呼吸道感染,如肺炎。发热伴神经系统症状,如头痛、呕吐、昏迷、惊厥、脑膜刺激征等则表示病变在中枢神经系统,应考虑各种脑膜炎、脑炎、中暑、急性脑卒中等;但儿童易有高热惊厥,不一定有严重脑部病变。发热伴有肋椎角、腰肋部疼痛及尿频、脓尿、血尿者提示多为泌尿系统感染。发热伴有明显关节痛或关节炎症状者应多考虑风湿热等结缔组织疾病。发热伴有恶心呕吐、腹痛、腹泻者,应多考虑急性胃肠道炎症。发热、黄疸伴右上腹痛应注意肝胆感染。依此类推。</p>
<h3 id="流行病学"><a class="header-anchor" href="#流行病学">¶</a>流行病学</h3>
<p>如患者来自的地区、年龄、性别、职业、发病季节、旅游史、接触感染史等,尤其是传染病的流行病学史非常重要。如布鲁菌病多见于从事畜牧业(尤其是动物接生)的人群中;同性恋者及静注毒品成瘾者的发热待查以艾滋病或合并机会性感染的可能性较大</p>
<h3 id="体态"><a class="header-anchor" href="#体态">¶</a>体态</h3>
<p>遇急重发热患者,应首先测呼吸、脉搏、血压等重要生命体征,并快速进行全面的体格检查,重点检查皮肤、黏膜有无皮疹、淤点以及肝、脾、淋巴结肿大等。发热伴有中毒性休克时,患者面色青灰,脉细速,血压下降或测不出,见于休克型肺炎、暴发性流行性脑脊髓膜炎、中毒性菌痢、脓毒症、流行性出血热等。</p>
<h4 id="面容"><a class="header-anchor" href="#面容">¶</a>面容</h4>
<p>一般急性感染多呈急热面容。伤寒、副伤寒者常表情淡漠,即所谓“伤寒面容”。斑疹伤寒、恙虫病、流行性出血热患者常呈醉酒样面容。猩红热患者见口周苍白。麻疹患者常见眼睑水肿、结膜充血、分泌物增多等。急性白血病、再生障碍性贫血和恶性组织细胞病常因贫血亦可呈面色苍白。发热伴面部蝶形红斑是播散性红斑狼疮的特殊病症。口唇疱疹可见于大叶性肺炎、间日疟、流行性脑脊髓膜炎、流行性感冒、大肠杆菌败血症等。</p>
<h4 id="皮肤特征"><a class="header-anchor" href="#皮肤特征">¶</a>皮肤特征</h4>
<p>注意有无皮疹及出血点。皮肤多汗可见于结核病、风湿热、败血症、恶性淋巴瘤。皮肤发疹可见于猩红热、麻疹、风疹、斑疹伤寒、伤寒、水痘、恙虫病、传染性单核细胞增多症、丹毒、红斑狼疮、急性皮肌炎等,根据其特征性皮疹及出疹日期可对急性发疹性传染病作出诊断:</p>
<p><img src="/img/%E5%8C%BB%E5%AD%A6/%E6%A0%B9%E6%8D%AE%E7%89%B9%E5%BE%81%E6%80%A7%E7%9A%AE%E7%96%B9%E5%8F%8A%E5%87%BA%E7%96%B9%E6%97%A5%E6%9C%9F%E5%AF%B9%E6%80%A5%E6%80%A7%E5%8F%91%E7%96%B9%E6%80%A7%E4%BC%A0%E6%9F%93%E7%97%85%E4%BD%9C%E5%87%BA%E8%AF%8A%E6%96%AD.png" alt="根据特征性皮疹及出疹日期对急性发疹性传染病作出诊断.png"></p>
<p>注意有无皮疹及出血点。皮肤多汗可见于结核病、风湿热、败血症、恶性淋巴瘤。皮肤发疹可见于猩红热、麻疹、风疹、斑疹伤寒、伤寒、水痘、恙虫病、传染性单核细胞增多症、丹毒、红斑狼疮、急性皮肌炎等,根据其特征性皮疹及出疹日期可对急性发疹性传染病作出诊断。</p>
<h4 id="淋巴结肿大"><a class="header-anchor" href="#淋巴结肿大">¶</a>淋巴结肿大</h4>
<p>注意有无皮疹及出血点。皮肤多汗可见于结核病、风湿热、败血症、恶性淋巴瘤。皮肤发疹可见于猩红热、麻疹、风疹、斑疹伤寒、伤寒、水痘、恙虫病、传染性单核细胞增多症、丹毒、红斑狼疮、急性皮肌炎等,根据其特征性皮疹及出疹日期可对急性发疹性传染病作出诊断</p>
<h4 id="脾大"><a class="header-anchor" href="#脾大">¶</a>脾大</h4>
<p>注意有无皮疹及出血点。皮肤多汗可见于结核病、风湿热、败血症、恶性淋巴瘤。皮肤发疹可见于猩红热、麻疹、风疹、斑疹伤寒、伤寒、水痘、恙虫病、传染性单核细胞增多症、丹毒、红斑狼疮、急性皮肌炎等,根据其特征性皮疹及出疹日期可对急性发疹性传染病作出诊断。</p>
<h4 id="胸部体征"><a class="header-anchor" href="#胸部体征">¶</a>胸部体征</h4>
<p>如闻及肺部干湿性啰音或实变体征等,应考虑呼吸系统感染;发热伴有栓塞、心脏杂音,尤其是原有器质性心脏病者心脏杂音发生明显改变时,应注意感染性心内膜炎;发热伴心包摩擦音或心包积液体征,常提示心包炎。而急性心肌炎常表现为发热与心率不成比例,心率增快常超过发热程度。</p>
<h4 id="肌肉与关节"><a class="header-anchor" href="#肌肉与关节">¶</a>肌肉与关节</h4>
<p>发热伴肌肉疼痛见于许多传染病,一般无特殊性诊断意义,但如腓肠肌剧烈疼痛,甚至不能站立或行走,常提示钩端螺旋体病。局部肌痛兼有发热与白细胞增多,须检查有无深部脓肿,尤其是药物肌内注射引起的臀肌无菌性脓肿。发热伴多关节肿痛,病因常为各种关节炎,如化脓性、感染中毒性与变态反应性等,而淋病性与结核性关节炎常侵犯单个的大关节。</p>
<h4 id="其他"><a class="header-anchor" href="#其他">¶</a>其他</h4>
<p>长期不明原因的发热患者尤应注意隐蔽性病灶,如肝脏、膈下、脊椎、盆腔、鼻窦、乳突等局部脓肿。肝脓肿是引起长期发热的常见病因,在早期不一定有局部症状。脊椎病变如结核或脓毒症后脊椎旁化脓性病灶在体检时易被忽略。男性患者的睾丸与附睾检查、女性患者的盆腔检查,以及所有发热待查患者的直肠指检或乙状结肠镜检查均应列为常规。眼底检查也应作为常规,粟粒性结核可有眼脉络膜结核结节,年老患者肛门指检可发现前列腺脓肿。此外,腹部与盆腔手术(包括引产)后发热可由腹腔或盆腔内隐蔽的脓肿引起。</p>
<h3 id="辅助检查"><a class="header-anchor" href="#辅助检查">¶</a>辅助检查</h3>
<p>对发热患者行辅助检查时必须掌握检查目的明确,并以简便快捷为原则。对于通过病史询问和体检能确诊者不一定均作有关检查。常用的辅助检查包括:①血、尿、粪常规检查。②血清学检查:如肥达、外斐反应、钩端螺旋体病的凝集溶解试验,乙脑的补体结合试验,系统性红斑狼疮的抗核抗体试验等。③血或骨髓培养:对伤寒、副伤寒、脓毒症、细菌性心内膜炎等疾病的病原诊断均具有决定性意义。④X线、CT与MRI检查:CT与MRI检查对诊断骨盆内、膈下与腹腔深部隐蔽性脓肿,尤其对发现腹膜后病灶如淋巴瘤、脓肿、血肿等有重要价值。⑤超声检查:对疑有急性渗出性心包炎和感染性心内膜炎患者,可行超声心动图检查。腹部超声波检查适用于疑有腹腔内占位性病变、肝脓肿、肝胆道结石以及肾脓肿、泌尿系结石等患者。⑥活体组织检查:如肝穿刺活组织检查、淋巴结以及皮损与皮下结节活体组织检查等。骨髓检查对白血病、恶性组织细胞病等具有决定性诊断价值。</p>
<h3 id="病因诊断"><a class="header-anchor" href="#病因诊断">¶</a>病因诊断</h3>
<p>发热是由于各种原因导致机体产热过多或散热减少,以及体温中枢功能障碍所致。其原因很多且复杂。在临床实践中,以发热为主诉或唯一症状就诊者有急性发热,尤其出疹性发热,原因不明发热,长期低热,超高热与反复发热。其病因特征亦各异。</p>
<h4 id="急性发热"><a class="header-anchor" href="#急性发热">¶</a>急性发热</h4>
<p>热程在2周以内的发热称为急性发热。其原因很多,绝大多数属于感染,尤以呼吸道、泌尿道和消化道感染最常见,因为这些系统与外界相通,最易遭受病原体的侵袭。在排除上述系统感染后,则要注意某些急性传染病和其他系统的感染。</p>
<h4 id="长期“不明原因”的中、高热"><a class="header-anchor" href="#长期“不明原因”的中、高热">¶</a>长期“不明原因”的中、高热</h4>
<p>系指发热持续3周以上,体温多次超过38.3℃,经过至少1周深入细致的检查仍不能确诊的一组疾病,主要有感染、恶性肿瘤与结缔组织-血管性疾病三大类</p>
<ol>
<li>感染:引起发热待查的感染性疾病中主要由细菌感染所致,而任何一种致病菌或条件致病菌,或L-型细菌性感染均可分为全身性与局部性感染。全身性感染以伤寒与副伤寒、粟粒型结核与播散性结核(包括腹膜、肠、肠系膜淋巴结、肝、肾、胸膜和肺与肺门淋巴结结核)、脓毒症与感染性心内膜炎、布鲁菌病、黑热病、急性血吸虫病、旋毛虫病等;局部性感染以肝脓肿、胆道与泌尿生殖道感染、腹腔内脓肿(包括肝下、膈下、结肠旁、阑尾周围、腹膜后、盆腔脓肿等)为常见。局部性感染易被临床忽略。</li>
<li>恶性肿瘤:也是长期发热的常见原因。最常见的为原发性肝癌、淋巴瘤、恶性组织细胞病与白血病,其次为实质性恶性肿瘤如肺癌、肾癌、甲状腺癌等。</li>
<li>结缔组织,血管性疾病:也是较常见原因之一,大多伴有关节痛、皮肤、心、肾等多系统病变引起的相应症状与体征,但少数病例在典型症状出现前数周或数月可出现发热。此类疾病以系统性红斑狼疮、成人少年型类风湿关节炎、多动脉炎、皮肌炎、混合性结缔组织病、风湿热等常见。</li>
<li>其他:肉芽肿性疾病(肉芽肿性肝炎、结节病、局限性回肠炎等)、药物热、伪装热、体腔积血如血胸、血腹、肺梗死等。</li>
</ol>
<h4 id="长期低热"><a class="header-anchor" href="#长期低热">¶</a>长期低热</h4>
<p>口腔温度在37.5℃至38.4℃,持续4周以上者。在诊断为长期低热时,必须先了解其正常体温,排除生理或功能性因素,并排除高温环境等影响,如在高温车间的纺织女工中,有长期低热者可达10%以上。长期低热由感染性疾病引起者占40%,非感染性疾病占57%,原因不明占3%。长期低热的原因可分为器质性与功能性两大类。</p>
<ol>
<li>器质性低热:
<ol>
<li>慢性感染:如结核病、肝脏疾病、慢性肾盂肾炎、慢性胆道感染以及各种病灶感染(鼻窦炎、牙根脓肿、前列腺炎、慢性盆腔炎、肛门周围脓肿等)。</li>
<li>结缔组织疾病:如风湿热、类风湿关节炎、系统性红斑狼疮等。</li>
<li>内分泌疾病:如甲亢、嗜铬细胞瘤等。</li>
<li>恶性肿瘤:早期淋巴瘤、实质性癌肿转移等。</li>
</ol>
</li>
<li>功能性低热
<ol>
<li>生理性低热:月经前期低热、妊娠期低热等。</li>
<li>神经功能性低热:多见于青年女性,长期低热可长达数月或数年。有些患者低热有季节性,出现于夏季(谓之夏季低热),且每年如此。体温在一昼夜内波动幅度较小,常不超过0.5℃,且口腔、腋窝与直肠温度差不大,甚至可出现腋温大于口温,口温大于肛温或腋温大于肛温的反常现象,两侧腋温可相差1℃以上。体温昼夜规律失常。患者常伴有脸色潮红、皮肤划痕症、心动过速等自主神经功能紊乱或神经症色彩。但患者一般情况好,体重无变化,虽经各种药物治疗无效,但不经治疗也可自行消退。神经功能性低热较常见,约占长期低热的1/3,预后良好。</li>
<li>感染后低热:急性病毒或细菌感染得到控制后,高热消退,但可出现持续较久的低热,并伴有乏力,纳差等现象。此种发热可能与体温调节中枢功能失常或自主神经功能紊乱有关。</li>
</ol>
</li>
</ol>
<h4 id="超高热危象的识别与诊断"><a class="header-anchor" href="#超高热危象的识别与诊断">¶</a>超高热危象的识别与诊断</h4>
<p>超高热系指发热超过41℃以上,主要见于体温调节中枢功能障碍,超高热(体温>41℃)是超高热危象的必有表现。</p>
<p>有以下各种原因:</p>
<ol>
<li>中暑或日射病</li>
<li>脑部疾病:如严重脑外伤、脑出血、脑炎与脑肿瘤等</li>
<li>输血、输液污染引起严重热原反应与脓毒症;</li>
<li>麻醉药引起的恶性高热</li>
<li>临终前超高热等。</li>
</ol>
<p>不论病因如何,超高热对细胞膜与细胞内结构有直接损害作用,当深部体温>41℃时细胞线粒体的氧化磷酸化出现障碍,可引起永久性脑损害;42~43℃持续数分钟细胞会陷入不可逆的损害,涉及全身各种细胞,尤以脑、心、肝、肾的变化最为突出,容易造成脑水肿颅内压升高,抽搐、昏迷,心、肝、肾、肺功能衰竭,DIC等多脏器功能衰竭。超高热危象的诊断要点是:</p>
<ol>
<li>
<p>超高热时伴有多脏器功能受损害的表现:</p>
<ol>
<li>心血管系统:低血压休克、心功能不全、心肌缺血与心律失常等。</li>
<li>中枢神经系统:体温越高对中枢神经系统损害越重,症状出现越早;包括不同程度的意识障碍如谵妄、嗜睡、昏迷、抽搐、大小便失禁、脑膜刺激征、瘫痪、病理反射阳性、脑疝、视神经乳头水肿等。</li>
<li>凝血功能障碍:早期出现凝血酶原时间延长,纤维蛋白原减少,血小板减少,出血时间、凝血时间延长;晚期常有广泛而严重的出血、DIC形成。这与过高热直接损害毛细血管、渗透性增加,肝功能受损凝血因子减少,骨髓受损血小板减少等有关。</li>
<li>肾功能损害:可有血尿、管型、少尿、无尿、血肌酐升高等肾功能不全的表现。</li>
<li>肝功能损害:肝功能异常如ALT升高、血清胆红素升高,甚至表现为急性肝功能衰竭。</li>
<li>水电解质和酸碱平衡失调。</li>
<li>其他表现:如横纹肌溶解可致血肌酸磷酸激酶(CK)增高等。</li>
</ol>
</li>
<li>
<p>原发病的表现:<br>
如中毒性菌痢的腹泻、脓血便;乙脑时的抽搐、昏迷等。</p>
</li>
</ol>
<h2 id="定义"><a class="header-anchor" href="#定义">¶</a>定义</h2>
<p>发热(fever)是指机体在致热原作用下或各种原因引起体温调节中枢的功能障碍时,体温升高超出正常范围。见于各种全身性和局部性感染以及许多非感染性疾病(如肿瘤与结缔组织病等),它是内科急诊中最常见的症状。一般而言,当腋下、口腔或直肠内温度分别超过37℃、37.3℃和37.6℃,并且24小时内温度差波动在1℃以上,可称为发热。按照发热的高低,可分为:</p>
<ol>
<li>低热:37.4~38℃</li>
<li>中度发热:38.1~39℃</li>
<li>高热:39.1~41℃</li>
<li>超高热;41℃以上。</li>
</ol>
<h2 id="后果"><a class="header-anchor" href="#后果">¶</a>后果</h2>
<p>超高热或过高热危象(extreme pyrexic crisis,EPC)是指过高热若不及时处理,使脑、心、肾等重要器官受到严重损害,出现抽搐、昏迷、休克、出血、心脏、呼吸和肾衰竭等危及生命的状态。若抢救不力,常于数小时内死亡。</p>
<h2 id="人体散热方式"><a class="header-anchor" href="#人体散热方式">¶</a>人体散热方式</h2>
<p>人体散热主要有辐射、蒸发、对流及传导物理过程,当周围温度低于皮肤温度时,热即从皮肤辐射散热;其次是体内热量传导至皮肤周围空气层,经对流散热。当周围温度超过体温时,主要依靠汗液蒸发,体热从皮肤、呼吸道和大小便3处消散,以皮肤散热最为重要。当室温在23~25℃时,体热通过皮肤辐射、对流、传导散热占70%;当室温高达31~32℃时,出汗蒸发即成为散热主要方式。皮肤血管内血流量越大,散热速度越快;</p>
<p><strong>体表温度越高,则散热也越迅速。</strong></p>
<h2 id="致热原"><a class="header-anchor" href="#致热原">¶</a>致热原</h2>
<p>致热原是一类能引起恒温动物体温异常升高的物质的总称。可分为外源性和内源性两类</p>
<ol>
<li>
<p>外源性致热原: 各种病原体如细菌、病毒、立克次体、衣原体、螺旋体、原虫和寄生虫等的毒素及其代谢产物,尤以内毒素(多属脂多糖类物质)最为重要,其次是原胆烷醇酮、多核苷酸、抗原-抗体复合物等。</p>
</li>
<li>
<p>内源性致热原: 外源性致热原一般不能直接作用于体温调节中枢引起发热,但能刺激和激活主要存在于白细胞、单核细胞和组织吞噬细胞内的内源性致热原前体,于短期内合成新的mRNA和致热原,这些具有活性的内源性致热原可能是通过某些生物活性物质如前列腺素、单胺、cAMP、钙钠比值、内啡肽等作为中介,提高体温调节中枢调定点而引起发热。</p>
</li>
</ol>
<h3 id="发生机制"><a class="header-anchor" href="#发生机制">¶</a>发生机制</h3>
<p>因产热、散热异常所致。因产热过多引起的发热不多,主要见于剧烈运动后、癫痫持续状态和甲亢危象时,一般持续不久。广泛性皮肤病、阿托品中毒时出汗功能障碍,散热减少引起发热,主要见于炎热季节。大量失水、失血常伴有发热,尤其多见于小儿,出现所谓“失水热”,是由于血容量减少、散热减少。心脏病患者也可有发热,主要由于肺部充血和肺部感染或有风湿活动或血栓形成外,在心衰阶段的发热,则与皮肤水肿引起散热减少有关。中枢神经性高温以中暑最为典型,也可由脑出血、脑炎等引起。由于中枢神经系统遭受严重伤害,下丘脑丧失调温能力而衰竭,每有骤升的超高温,达41℃或以上,同时交感神经受抑制,以皮肤干燥无汗为特征。</p>
]]></content>
<tags>
<tag>Medicine</tag>
<tag>Life</tag>
</tags>
</entry>
<entry>
<title>呋塞米</title>
<url>/2023/03/14/%E5%91%8B%E5%A1%9E%E7%B1%B3/</url>
<content><![CDATA[<p><strong>孕妇禁用,小儿慎用!!!!</strong></p>
<h2 id="简介"><a class="header-anchor" href="#简介">¶</a>简介</h2>
<p>口服吸收迅速,生物利用度约为60%,约30分钟起效,1~2小时达高峰,持续6~8小时。静脉注射5~10分钟起效,30分钟达高峰,t1/2约1小时,维持4~6小时。血浆蛋白结合率约98%。大部分以原形经近曲小管有机酸分泌系统随尿排出。反复给药不易蓄积。</p>
<p>主要作用有两个</p>
<ol>
<li>利尿:作用强大、迅速而短暂,但个体差异明显,应注意剂量个体化。而且利尿作用不受酸碱平衡失调及电解质紊乱的影响。需要注意的是,这个药容易引起低血钾、低盐综合征及低氯性碱中毒。低血钾最常见,还促进Ca2+、Mg2+排出,而抑制尿酸排出。</li>
<li>扩张血管:能扩张肾血管,增加肾血流量,改变肾皮质内血流分布,还能扩张小静脉,减少回心血量,减轻心脏负荷,降低左室充盈压,减轻肺水肿。扩血管机制可能与本药促进前列腺素酶合成,抑制其分解有关。</li>
</ol>
<p>临床上用于治疗心脏性水肿、肾性水肿、肝硬化腹水、机能障碍或血管障碍所引起的周围性水肿,并可促使上部尿道结石的排出。其利尿作用迅速、强大,多用于其它利尿药无效的严重病例。由于水、电解质丢失明显等原因,故不宜常规使用。静脉给药(20~80mg)可治疗肺水肿和脑水肿。药物中毒时可用以加速毒物的排泄。</p>
<h2 id="用药"><a class="header-anchor" href="#用药">¶</a>用药</h2>
<p>肌注或静注隔日1次,每次mg,必要时亦可1日~2次。1日量视需要可增至120mg。静注必须缓慢,不宜与其他药物混合注射。儿童用量酌减(2)口服开始时每日~40mg,以后根据需要可增至每日~120mg。当每日剂量超过40mg时,可以每4小时1次分服。儿童口服量开始按1~2mg/kg,再视情况酌增。长期(7~10日)用药后利尿作用消失,故需长期应用者,宜采取间歇疗法:给药1~3日,停药2~4日。</p>
<h2 id="不良反应"><a class="header-anchor" href="#不良反应">¶</a>不良反应</h2>
<ol>
<li>水与电解质紊乱,常为过度利尿所引起,表现为低血容量、低血钾、低血钠、低氯性碱血症,长期应用还可引起低血镁。</li>
<li>耳毒性,表现为耳鸣、听力减退或暂时性耳聋,呈剂量依赖性。耳毒性的发生机制可能与药物引起内耳淋巴液电解质成分改变有关。肾功能不全或同时使用其他耳毒性药物,如并用氨基糖苷类抗生素时较易发生耳毒性。</li>
<li>高尿酸血症,袢利尿药可能造成高尿酸血症,并引发痛风。</li>
<li>其他,可有恶心、呕吐、大剂量时尚可出现胃肠出血。</li>
</ol>
<h2 id="注意"><a class="header-anchor" href="#注意">¶</a>注意</h2>
<ol>
<li>可能出现轻微恶心、腹泻、药疹、瘙痒、视力模糊等副作用,有时可发生起立性眩晕、乏力、疲倦、肌肉痉挛、口渴,少数病例有白细胞减少,个别病例出现血小板减少、多形性红斑、直立性低血压。长期应用可致胃及十二指肠溃疡。</li>
<li>由于能减少尿酸排出,故多次应用后能产生尿酸过多症,个别病人长期应用可产生急性痛风。痛风病患者慎用。</li>
<li>糖尿病患者应用后可使血糖增高;糖尿病患者慎用。尽管其升血糖远较噻嗪类利尿药弱,但与降血糖药合并应用时,仍有使血糖增高的可能。</li>
<li>由于利尿作用迅速、强大,因此要注意掌握开始剂量,防止过度利尿,引起脱水和电解质不平衡。</li>
<li>肝炎病人服用后,因电解质(特别是K+)过度丢失易产生肝昏迷。严重肝功能不全患者慎用。</li>
<li>长期大量用药时应注意检查血中电解质浓度。顽固性水肿患者特别容易出现低钾症状,在同时使用洋地黄或排钾的甾体激素时,更应注意补充钾盐。</li>
<li>在脱水的同时,可出现可逆性血尿素氮水平的升高,如果肌酐水平不显著升高和肾功能无损害时,可继续使用本品。严重肾功能不全患者慎用。</li>
<li>使用第一个月,要定期检查血清电解质、二氧化碳和血中尿素氮水平。与其他利尿药一样,当治疗进展中的肾脏疾患而有血清尿素氮值增加和少尿现象发生时,应立即停止用药。</li>
<li>能增强降压药的作用,故合并用药时,降压药的用量应适当减少。</li>
<li>因结构上是与氯噻嗪结构相似的磺胺型化合物,能降低动脉对升压胺(如去甲肾上腺素)的反应,并能增加筒箭毒硷的肌松弛及麻痹作用,故手术前一周应停用。(11)低钾血症、超量服用洋地黄、肝昏迷患者禁用。晚期肝硬化患者慎用。</li>
<li>大剂量静注过快时,可出现听力减退或暂时性耳聋。不宜与氨基糖昔类抗生素配伍应用,因更易引起听力减退。</li>
</ol>
<h2 id="其他"><a class="header-anchor" href="#其他">¶</a>其他</h2>
<p>螺内酯为《2018版中国国家基本药物目录》在列药物,“基本药物”指的是能够满足基本医疗卫生需求,剂型适宜、保证供应、基层能够配备、国民能够公平获得的药品。</p>
<h2 id="参考"><a class="header-anchor" href="#参考">¶</a>参考</h2>
<ul>
<li><a href="https://www.yixue.com/%E5%91%8B%E5%A1%9E%E7%B1%B3">https://www.yixue.com/呋塞米</a></li>
</ul>
]]></content>
<tags>
<tag>Medicine</tag>
</tags>
</entry>
<entry>
<title>在F#中处理复杂依赖注入的实践指南</title>
<url>/2025/03/31/%E5%9C%A8F-%E4%B8%AD%E5%A4%84%E7%90%86%E5%A4%8D%E6%9D%82%E4%BE%9D%E8%B5%96%E6%B3%A8%E5%85%A5%E7%9A%84%E5%AE%9E%E8%B7%B5%E6%8C%87%E5%8D%97/</url>
<content><![CDATA[<h2 id="一、传统手艺:Partial-Application"><a class="header-anchor" href="#一、传统手艺:Partial-Application">¶</a>一、传统手艺:Partial Application</h2>
<p>在函数式编程中,Partial Application 是传递依赖的常用方式。例如:</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">let</span> foo bar baz request <span class="operator">=</span> <span class="operator">...</span></span><br><span class="line"><span class="keyword">let</span> wired <span class="operator">=</span> foo dependency1 dependency2</span><br><span class="line"><span class="keyword">let</span> response <span class="operator">=</span> wired request</span><br></pre></td></tr></table></figure>
<p>优点:</p>
<ul>
<li>无需框架或反射,直接通过函数参数传递依赖。</li>
<li>符合函数式编程的纯函数理念。</li>
</ul>
<p>缺点:</p>
<ul>
<li>参数爆炸:当功能扩展时,参数数量激增(如日志、数据库、加密等)。</li>
<li>维护困难:新增依赖需修改所有调用点的参数传递。</li>
<li>隐式依赖:难以从函数签名直接区分核心参数与辅助依赖。</li>
</ul>
<hr>
<h2 id="二、结构化方法:单一环境参数(env)"><a class="header-anchor" href="#二、结构化方法:单一环境参数(env)">¶</a>二、结构化方法:单一环境参数(<code>env</code>)</h2>
<p>为解决参数爆炸问题,可将依赖封装为单一环境对象<code>env</code>,并通过接口约束访问权限:</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="meta">[<Interface>]</span> <span class="keyword">type</span> <span class="title class_">ILog</span> <span class="operator">=</span> <span class="keyword">abstract</span> Logger<span class="operator">:</span> ILogger</span><br><span class="line"><span class="meta">[<Interface>]</span> <span class="keyword">type</span> <span class="title class_">IDb</span> <span class="operator">=</span> <span class="keyword">abstract</span> Database<span class="operator">:</span> IDatabase</span><br><span class="line"></span><br><span class="line"><span class="keyword">module</span> Log <span class="operator">=</span></span><br><span class="line"> <span class="keyword">let</span> info (env<span class="operator">:</span> #ILog) <span class="operator">=</span> env.Logger.Info(<span class="string">"Message"</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">module</span> Db <span class="operator">=</span></span><br><span class="line"> <span class="keyword">let</span> fetchUser (env<span class="operator">:</span> #IDb) <span class="operator">=</span> env.Database.Query(<span class="operator">...</span>)</span><br></pre></td></tr></table></figure>
<p>优点:</p>
<ul>
<li>显式依赖声明:函数签名仅需<code>env</code>参数,编译器验证接口实现。</li>
<li>模块化隔离:各模块仅声明所需接口(如<code>ILog</code>、<code>IDb</code>),避免全局依赖。</li>
<li>易于测试:通过模拟<code>env</code>实现单元测试,无需依赖具体实现。</li>
</ul>
<p>应用场景:</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">let</span> changePass env req <span class="operator">=</span> <span class="keyword">task</span> {</span><br><span class="line"> <span class="keyword">let!</span> user <span class="operator">=</span> Db.fetchUser env req.UserId</span><br><span class="line"> Log.info env <span class="string">"Processing user: %i"</span> user.Id</span><br><span class="line"> <span class="operator">...</span></span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<hr>
<h2 id="三、Reader-Monad"><a class="header-anchor" href="#三、Reader-Monad">¶</a>三、Reader Monad</h2>
<p>为消除显式的<code>env</code>传递,可引入 Reader Monad,将环境隐式注入计算流程:</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="meta">[<Struct>]</span> <span class="keyword">type</span> <span class="title class_">Effect</span><span class="operator"><</span><span class="symbol">'env</span>, <span class="symbol">'out</span><span class="operator">></span> <span class="operator">=</span> Effect <span class="keyword">of</span> (<span class="symbol">'env</span> <span class="operator">-></span> <span class="symbol">'out</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">module</span> Effect <span class="operator">=</span></span><br><span class="line"> <span class="keyword">let</span> run env (Effect fn) <span class="operator">=</span> fn env</span><br><span class="line"> <span class="keyword">let</span> bind f effect <span class="operator">=</span> Effect (<span class="keyword">fun</span> env <span class="operator">-></span> run env (f (run env effect)))</span><br><span class="line"></span><br><span class="line"><span class="keyword">type</span> <span class="title class_">EffectBuilder</span>() <span class="operator">=</span></span><br><span class="line"> <span class="keyword">member</span> __.Bind(e, f) <span class="operator">=</span> Effect.bind f e</span><br><span class="line"> <span class="keyword">member</span> __.Return(x) <span class="operator">=</span> Effect (<span class="keyword">fun</span> _ <span class="operator">-></span> x)</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> effect <span class="operator">=</span> EffectBuilder()</span><br></pre></td></tr></table></figure>
<p>然后:</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">let</span> changePass req <span class="operator">=</span> <span class="keyword">effect</span> {</span><br><span class="line"> <span class="keyword">let!</span> user <span class="operator">=</span> Db.fetchUser req.UserId</span><br><span class="line"> <span class="keyword">let!</span> salt <span class="operator">=</span> Random.bytes <span class="number">32</span></span><br><span class="line"> <span class="keyword">do!</span> Log.info <span class="string">"Password updated for user %i"</span> user.Id</span><br><span class="line"> <span class="keyword">return</span> <span class="literal">Ok</span>()</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>优点:</p>
<ul>
<li>隐式依赖管理:通过<code>effect</code>计算表达式自动传递<code>env</code>,减少样板代码。</li>
<li>组合性:支持与其他计算表达式(如<code>async</code>/<code>task</code>)结合,处理异步操作。</li>
</ul>
<p>缺点:</p>
<ul>
<li>性能开销:频繁的闭包创建和间接调用可能导致性能下降。</li>
<li>生态兼容性:需自定义计算表达式,与现有异步框架集成复杂。</li>
</ul>
<h2 id="Refs"><a class="header-anchor" href="#Refs">¶</a>Refs.</h2>
<ul>
<li>Spring的构造器注入</li>
<li>Blazor的DI实现</li>
</ul>
]]></content>
<tags>
<tag>Technique</tag>
</tags>
</entry>
<entry>
<title>奥司他韦</title>
<url>/2023/03/05/%E5%A5%A5%E5%8F%B8%E4%BB%96%E9%9F%A6/</url>
<content><![CDATA[<p><img src="/img/%E5%8C%BB%E5%AD%A6/%E5%A5%A5%E5%8F%B8%E4%BB%96%E9%9F%A6.jpg" alt="奥司他韦.jpg"></p>
<p>目前市场上销售的达菲为罗氏制药独家生产的抗流感药物,其通用名称为磷酸奥司他韦(Oseltamivirphosphate)。奥司他韦(Oseltamivir)于1999年在瑞士上市,2001年10月在我国上市。达菲是一种非常有效的流感治疗用药,并且可以大大减少并发症(主要是气管与支气管炎、肺炎、咽炎等)的发生和抗生素的使用,因而是目前治疗流感的最常用药物之一,也是公认的抗禽流感、甲型H1N1病毒最有效的药物之一。</p>
<h2 id="主要原理"><a class="header-anchor" href="#主要原理">¶</a>主要原理</h2>
<p>奥司他韦口服后经肝脏和肠道酯酶迅速催化转化为其活性代谢物奥司他韦羧酸,奥司他韦羧酸的构型与神经氨酸的过渡态相似,能够竞争性地与流感病毒神经氨酸酶(NA,neu-raminidase,也有称作神经氨酸苷酶)的活动位点结合,因而是一种强效的高选择性的流感病毒NA抑制剂(NAIs),它主要通过干扰病毒从被感染的宿主细胞中释放,从而减少甲型或乙型流感病毒的传播。</p>
<h2 id="特殊人群"><a class="header-anchor" href="#特殊人群">¶</a>特殊人群</h2>
<ol>
<li>
<p>对不同程度的肾功能不全患者给予100mg磷酸奥司他韦,每日两次,服用五天,显示活性代谢产物水平与降低肾功能成反比。对肌酐清除率小于30ml/min的患者建议做剂量调整。目前没有研究数据指导肾功能衰竭患者的用药(肌酐清除小于10ml/nin),所以对该人群用药时要慎重。</p>
</li>
<li>
<p>口服磷酸奥司他韦后。肝功能不全患者并没有象预期那样体内奥司他韦水平增高或其活性代谢产物水平降低。</p>
</li>
<li>
<p>给予相同剂量的磷酸奥司他韦,同年轻人相比,老年人(年龄在65—78岁间)的稳态代谢物水平比年轻人高25~35%,而两个人群药物半衰期很相似。考虑到药物暴露量和耐受力,老年人不必调整剂量。</p>
</li>
<li>
<p>对一小组5~18岁儿童给予单剂2mg/kg的粉末剂,口服建立药代曲线,数据显示儿童年龄越小,对药物前体和其活性代谢产物的清除越快,人体对每mg/kg计量单位的承受越少。比如给予5-8岁儿童2mg/kg的剂量,若要达到可比性。即相当于要给予成年人单剂75mg奥司他韦(大约为1mg/kg)。年龄相差越小,儿童与成年人对每一单位mg/kg剂量的代谢差别越小。比如大于12岁的儿童与成年人在药代动力学方面就已经很相似了。</p>
</li>
</ol>
<h2 id="不良反应"><a class="header-anchor" href="#不良反应">¶</a>不良反应</h2>
<p>在罗氏提交美国联邦食品和药品管理局的申报材料中指出,奥司他韦(达菲)主要的不良反应显示为消化道的不适,包括恶心、呕吐、腹泻、腹痛等,其次是呼吸系统的不良反应,包括支气管炎、咳嗽等,此外还有中枢神经系统的不良反应,如眩晕、头痛、失眠、疲劳等。</p>
<p>在2004年1月,FDA还发出对于奥司他韦的消费警讯,声称由于1岁以内幼儿血脑屏障发育不完全,奥司他韦应用于幼儿可能造成脑内药物浓度过高,形成潜在的安全问题。</p>
<p>2005年有日本媒体报道日本青少年服用奥斯他韦后自杀并有精神异常反应,此后日本先后报道数十例此类不良反应,此后世界各地媒体纷纷转载报道,引起公众关注。2005年11月FDA就这一反应作出报告,认为没有证据证明奥斯他韦可以导致精神异常,日本的不良反应病例系大众媒体报道后经心理暗示作用引起的群体性臆症。</p>
<h2 id="其他"><a class="header-anchor" href="#其他">¶</a>其他</h2>
<p>需要注意的是,奥司他韦是通过抑制病毒的复制而起到治疗流感作用的,如果人体没有感染病毒,吃了“达菲”也是没有任何作用的。也就是说,“达菲”是在体内存在病毒的前提下起到杀灭病毒的作用,它不可能抵抗病毒侵入人体。</p>
<h2 id="参考"><a class="header-anchor" href="#参考">¶</a>参考</h2>
<ul>
<li><a href="https://www.yixue.com/%e8%be%be%e8%8f%b2">https://www.yixue.com/达菲</a></li>
</ul>
]]></content>
<tags>
<tag>Medicine</tag>
</tags>
</entry>
<entry>
<title>如何管理用户界面中的危险操作</title>
<url>/2024/11/26/%E5%A6%82%E4%BD%95%E7%AE%A1%E7%90%86%E7%94%A8%E6%88%B7%E7%95%8C%E9%9D%A2%E4%B8%AD%E7%9A%84%E5%8D%B1%E9%99%A9%E6%93%8D%E4%BD%9C/</url>
<content><![CDATA[<blockquote>
<p>“任何可能出错的事情都会出错。”我们的目标是防止出现问题,并在出现问题时减轻后果。</p>
</blockquote>
<p>界面是用户与系统通信的中介层,界面中的交互通常需要用户执行某些操作。不同的操作可能会导致不同的结果,其中可能有一些对于双方来说都非常重要甚至危险的操作。</p>
<p>所以经常需要提供额外的保护措施来“保护”用户执行一些危险甚至无法恢复的操作。<br>
这里的“保护”并不是完全的阻止,否则这个操作也没有存在的必要。</p>
<blockquote>
<p>“良好的错误消息很重要,但最好的设计首先会小心地防止问题发生。要么消除容易出错的情况,要么检查它们并在用户承诺操作之前向他们提供确认选项。”</p>
</blockquote>
<h2 id="什么是危险行为?"><a class="header-anchor" href="#什么是危险行为?">¶</a>什么是危险行为?</h2>
<p>危险行为并不意味着要删除某些内容,具体的危险行为应该由系统的“领域”来定义,例如:</p>
<ul>
<li>进行金融交易</li>
<li>签署法律文件</li>
<li>拉黑用户</li>
<li>授予某些用户权限</li>
<li>…</li>
</ul>
<h2 id="确认危险行为的方法"><a class="header-anchor" href="#确认危险行为的方法">¶</a>确认危险行为的方法</h2>
<p>最常用的一种方法是要求用户明确确认他们的操作,这种方法也有很多实现上的细节,但无论从什么角度实现都有优劣:</p>
<h3 id="Modal-Dialog"><a class="header-anchor" href="#Modal-Dialog">¶</a>Modal Dialog</h3>
<p>首先需要明确 Modal Dialog 和 Non-modal Dialog 的区别。</p>
<blockquote>
<p>“Modal 是一种设计技术,它以一个独立的模式呈现内容,阻止用户与父视图交互,并需要明确的操作来退出。”</p>
</blockquote>
<p>所以 Modal Dialog 需要用户立即操作它。换句话说,除非以某种方式做出响应,否则用户无法继续使用系统。</p>
<p>而 Non-modal Dialog 允许用户不间断地继续使用系统。Non-modal Dialog 一个常见用法就是出现在屏幕一角的 Toast 消息。</p>
<p>所以如果使用得当,Modal Dialog 是防止意外点击危险操作的有效方法。这也是目前最流行的方法,它还可以和其他方法结合使用。</p>
<p>但首先,需要明确一个保护用户进行危险操作的 Modal Dialog 需要有哪些元素:</p>
<ol>
<li>不要含糊其辞, 如果单纯的问用户:“你确定吗?”,用户很有可能不会有任何疑问直接点确定。</li>
<li>在标题中,指定具体会发生什么或哪个实体将受到影响(例如,项目名称、用户名、金额)。</li>
<li>提供一个图标来指示该操作是危险的,这既降低了用户盲目确认它的概率,也有利于页面的无障碍性(色盲的人可以通过图标来判断)。</li>
<li>在描述中,要具体并突出显示必要的信息。</li>
<li>按钮还应包含反映操作的词语,不要用“是”或“确认”之类的词,应该用更具描述性的“支付 10 元”,“删除用户韩暮秋”之类的词语。</li>
</ol>
<p>除此之外,还有更加严格的保护,例如在某些情况下,可以让用户输入某些内容来“解锁”操作,例如 Github 在删除仓库的时候需要输入仓库名称才能删除,这能让用户明确的知道自己在做什么,在删除哪个仓库。</p>
<p>最后,根据 <a href="https://lawsofux.com/law-of-proximity/">临近法则</a> ,确认操作的按钮最好放在左侧。</p>
<h4 id="Danger-Zone"><a class="header-anchor" href="#Danger-Zone">¶</a>Danger Zone</h4>
<p>对于关键的操作,可以使用 Danger Zone,常见的实现方式是将这一类关键的操作单独放在某一个页面的同一处,例如页面的底部。如果操作比较多,可以考虑使用一个单独的页面存放。</p>
<p>使用 Danger Zone 存放关键操作组件也有一些基本要素:</p>
<ol>
<li>使用红色或其他富含警示性颜色的警告图标或边框,在视觉上将 Danger Zone 与页面的其他部分区分开来。</li>
<li>Danger Zone 中的每个操作都应该清楚地描述如果用户继续进行将会发生什么,以便用户了解潜在的后果。</li>
<li>对于一些非常关键且无法恢复的行为,可以要求用户进行额外的操作。例如要求用户重复输入密码或使用 2FA。</li>
<li>只存放真正关键的操作。避免为了拥有一个 Danger Zone 而搞出一个 Danger Zone。</li>
</ol>
<h3 id="Inline-Guard"><a class="header-anchor" href="#Inline-Guard">¶</a>Inline Guard</h3>
<p>这个方法解释起来挺简单,可以用在一些零碎的页面元素中,例如有一个删除一条消息的按钮,可以在用户单击这个按钮之后将其变为红色背景并修改按钮字体为 “确认删除”,用户再次点击就确认删除了。这用来防止误点击非常有用。</p>
<p>但是也有一些细节需要注意:</p>
<ol>
<li>这种方法对于不那么危险的动作来说很方便,注意是“不那么危险”</li>
<li>一个“不那么危险”的操作应该可以恢复,所以应该提供一个选项来撤消操作或将已删除的项目放到回收站之类的地方,这是确保用户安全操作的良好组合。</li>
</ol>
<p>Inline Guard 不能滥用,当用户非常频繁的遇到时会烦死,得权衡一下。</p>
<h3 id="其他方案"><a class="header-anchor" href="#其他方案">¶</a>其他方案</h3>
<p>还有一些方案这里简单说一下,一是 2FA,基本不用过多解释,二是双人验证甚至多人验证,就是用户发起的一个操作需要两个以上的人来验证才可以执行,例如 Github 的 Merge PR。</p>
<p>当系统要求用户进行一些额外的操作时,应该明确其最初的目的,因为:</p>
<ul>
<li>认知惯性:一个人倾向于近乎惯性地决定,即使这些决定不适合当前情况。例如,绝大多数人不阅读用户协议。他们只是同意冗长的文本,因为从法律角度来看这是必要的。(是的我就是这样)</li>
<li>人们经常根据他们容易获得或熟悉的信息做出决策,而不是动脑子想。当用户看到相同的弹出窗口时,他们可能会根据之前的经验看都不看一眼直接接受它们。</li>
<li>人类倾向于以更简单、更省力的方式思考和解决问题,而不是更复杂、更省力的方式,无论智力如何。所以许多用户只是点击“是”或“同意”而没有仔细阅读文本。</li>
</ul>
<p>所以在某些情况下,我们可以用一些更加优雅的方法:</p>
<h3 id="延迟"><a class="header-anchor" href="#延迟">¶</a>延迟</h3>
<p>上面 Inline Guard 用在删除消息的场景下,也可以点击删除按钮直接删除,但同时显示一个倒计时的 Toast 并附带一个“撤销”的按钮来提醒用户。</p>
<h2 id="撤销"><a class="header-anchor" href="#撤销">¶</a>撤销</h2>
<p>允许用户撤消刚刚执行的操作,从而提供一个安全网来减少因犯错误而产生的焦虑。<br>
与 Modal Dialog 这种中断系统并要求用户确认的模式不同,撤消允许完成操作后在需要时选择撤消操作,从而提供更流畅的体验。</p>
<p>它非常适合非破坏性、不可恢复的操作以及不会产生重大和直接后果的操作。</p>
<p>撤消选项与“软删除”的概念密切相关,“软删除” 的意思是:当用户通过 UI 删除某些内容时,<em>看起来它已被删除</em>,但在数据库中,我们保留数据但将其标记为已删除。数据不会丢失,这就是为什么可以使用撤消选项,因为我们实际上并没有删除任何内容,而是将其标记为已删除。</p>
<p>晚安。</p>
]]></content>
<tags>
<tag>UI/UX</tag>
<tag>Design</tag>
</tags>
</entry>
<entry>
<title>实时搜索中的防抖函数</title>
<url>/2025/02/10/%E5%AE%9E%E6%97%B6%E6%90%9C%E7%B4%A2%E4%B8%AD%E7%9A%84%E9%98%B2%E6%8A%96%E5%87%BD%E6%95%B0/</url>
<content><![CDATA[<p>在实现实时搜索功能时,通常会使用输入框的事件监听器来捕获用户的输入变化,并在输入变化时发送搜索请求。为了避免过多的请求导致服务器负担过重,通常会使用“防抖”(debounce)技术来控制请求的频率。</p>
<h3 id="实现步骤"><a class="header-anchor" href="#实现步骤">¶</a>实现步骤</h3>
<ol>
<li>监听输入框的变化:使用<code>input</code>事件监听器来捕获用户的输入变化。</li>
<li>防抖处理:使用防抖函数来限制请求的频率。防抖函数会在用户停止输入一段时间后才发送请求。</li>
<li>发送请求:在防抖函数中调用搜索请求。</li>
</ol>
<h3 id="防抖函数示例"><a class="header-anchor" href="#防抖函数示例">¶</a>防抖函数示例</h3>
<p>以下是一个简单的防抖函数示例:</p>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">debounce</span>(<span class="params">func, wait</span>) {</span><br><span class="line"> <span class="keyword">let</span> timeout;</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">function</span>(<span class="params">...args</span>) {</span><br><span class="line"> <span class="keyword">const</span> context = <span class="variable language_">this</span>;</span><br><span class="line"> <span class="built_in">clearTimeout</span>(timeout);</span><br><span class="line"> timeout = <span class="built_in">setTimeout</span>(<span class="function">() =></span> func.<span class="title function_">apply</span>(context, args), wait);</span><br><span class="line"> };</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h3 id="实现实时搜索"><a class="header-anchor" href="#实现实时搜索">¶</a>实现实时搜索</h3>
<p>假设你有一个输入框用于搜索患者:</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line"><script></span><br><span class="line"> import { onMount } from 'svelte';</span><br><span class="line"> import { patientsStore } from '$lib/stores/patients.svelte';</span><br><span class="line"></span><br><span class="line"> let searchTerm = '';</span><br><span class="line"></span><br><span class="line"> // 防抖函数</span><br><span class="line"> function debounce(func, wait) {</span><br><span class="line"> let timeout;</span><br><span class="line"> return function(...args) {</span><br><span class="line"> const context = this;</span><br><span class="line"> clearTimeout(timeout);</span><br><span class="line"> timeout = setTimeout(() => func.apply(context, args), wait);</span><br><span class="line"> };</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> // 搜索函数</span><br><span class="line"> const searchPatients = debounce(async (term) => {</span><br><span class="line"> if (term) {</span><br><span class="line"> // 发送搜索请求</span><br><span class="line"> const response = await fetch(`/api/search-patients?query=${term}`);</span><br><span class="line"> const data = await response.json();</span><br><span class="line"> patientsStore.mbglPatients = data;</span><br><span class="line"> } else {</span><br><span class="line"> // 清空搜索结果或恢复默认数据</span><br><span class="line"> patientsStore.mbglPatients = [];</span><br><span class="line"> }</span><br><span class="line"> }, 300); // 300ms 的防抖时间</span><br><span class="line"></span><br><span class="line"> // 监听输入框变化</span><br><span class="line"> function handleInput(event) {</span><br><span class="line"> searchTerm = event.target.value;</span><br><span class="line"> searchPatients(searchTerm);</span><br><span class="line"> }</span><br><span class="line"></script></span><br><span class="line"></span><br><span class="line"><input type="text" placeholder="搜索患者..." on:input={handleInput} bind:value={searchTerm} /></span><br></pre></td></tr></table></figure>
<h3 id="请求发送间隔"><a class="header-anchor" href="#请求发送间隔">¶</a>请求发送间隔</h3>
<ul>
<li>防抖时间:通常设置为 300ms 到 500ms 之间。这个时间足够让用户完成输入并减少不必要的请求。</li>
<li>考虑用户体验:防抖时间过短可能导致过多请求,过长则可能让用户感到延迟。300ms 是一个常用的折中值。</li>
</ul>
<p>通过这种方式,你可以实现一个高效的实时搜索功能,既能保证用户体验,又能减少服务器的负担。</p>
]]></content>
<tags>
<tag>Technique</tag>
</tags>
</entry>
<entry>
<title>急诊与灾难医学--烧伤</title>
<url>/2024/01/25/%E6%80%A5%E8%AF%8A%E4%B8%8E%E7%81%BE%E9%9A%BE%E5%8C%BB%E5%AD%A6-%E7%83%A7%E4%BC%A4/</url>
<content><![CDATA[<p>烧伤,烫伤是一个很常见的急诊意外损伤,这里科普一下相关的知识。</p>
<h2 id="临床特点"><a class="header-anchor" href="#临床特点">¶</a>临床特点</h2>
<p>烧伤组织出现变性坏死,体液渗出引起组织水肿,变性,小面积的浅度烧伤,体液渗出有限,经过代偿之后不会影响身体的有效循环血量,大面积或者深度的烧伤,会因为大量渗出,休克,感染等病理变化导致并发脓毒症和多器官功能衰竭。</p>
<h3 id="烧伤面积判断"><a class="header-anchor" href="#烧伤面积判断">¶</a>烧伤面积判断</h3>
<p>首先我们要估算烧伤的面积,也就是判断皮肤烧伤区占人体表面积的百分比,最常用的是九分法和手掌法,前者用于大面积烧伤,后者用于小面积烧伤。</p>
<ul>
<li>手掌法:病人的五个手指并拢后,手掌的面积估算为 1% 体表面积。</li>
<li>九分法:</li>
</ul>
<table>
<thead>
<tr>
<th>部位</th>
<th>成人各部位面积(%)</th>
<th>小儿各部位面积(%)</th>
</tr>
</thead>
<tbody>
<tr>
<td>头额</td>
<td><code>9x1=9 (发部3,面部3,颈部3)</code></td>
<td><code>9 + (12-年龄) </code></td>
</tr>
<tr>
<td>双上肢</td>
<td><code>9x2=18 (双手5, 双前臂6, 双上臂7)</code></td>
<td><code>9x2</code></td>
</tr>
<tr>
<td>躯干</td>
<td><code>9x3=27 (腰腹13, 背侧13,会阴1)</code></td>
<td><code>9x3</code></td>
</tr>
<tr>
<td>双下肢</td>
<td><code>9x5+1=45 (双臀5, 双大腿21, 双小腿13, 双足7)</code></td>
<td><code>46-(12-年龄)</code></td>
</tr>
</tbody>
</table>
<h3 id="烧伤深度判断"><a class="header-anchor" href="#烧伤深度判断">¶</a>烧伤深度判断</h3>
<p>三度四分法:</p>
<ul>
<li>I 度:仅伤及表皮浅层</li>
<li>浅 II 度:伤及表皮的生发层与真皮乳头层</li>
<li>深 II 度:伤及皮肤真皮乳头层及部分真皮网状层</li>
<li>III 度:全皮层烧伤甚至达到皮下,肌肉或骨骼</li>
</ul>
<table>
<thead>
<tr>
<th>分度</th>
<th>皮损性状</th>
<th>皮损状态</th>
<th>感觉</th>
<th>预后</th>
</tr>
</thead>
<tbody>
<tr>
<td>I</td>
<td>粉红或红色</td>
<td>干燥</td>
<td>疼痛</td>
<td>数天</td>
</tr>
<tr>
<td>浅 II</td>
<td>粉红或大水疱</td>
<td>潮湿</td>
<td>疼痛</td>
<td>2 ~ 3周</td>
</tr>
<tr>
<td>深 II</td>
<td>粉红或出血性水疱</td>
<td>潮湿</td>
<td>疼痛</td>
<td>数周,可能发展为 III 度,需植皮</td>
</tr>
<tr>
<td>III</td>
<td>白色或褐色</td>
<td>干燥似皮革</td>
<td>无感觉</td>
<td>需切痂皮,植皮,皮瓣移植或截肢</td>
</tr>
</tbody>
</table>
<p>根据以上的参考,可以把伤情分为四类:</p>
<ol>
<li>轻度烧伤:总面积 9% 以下的 II 度烧伤</li>
<li>中度烧伤:II 度烧伤总面积达 10%~29% ,或 III 度烧伤面积在 9% 以下。</li>
<li>重度烧伤:烧伤面积达到 30%~49%,III 度烧伤面积达到 10%~19%,或烧伤面积虽然不足 30% ,但全身情况较重或已经有休克,复合伤,呼吸道吸入性损伤或化学中毒等并发症</li>
<li>特重度烧伤:烧伤面积 50% 以上,III 度烧伤面积在 20% ,有严重并发症</li>
</ol>
<hr>
<p><strong>根据烧伤病史和临床表现,要注意包括对烧伤严重程度的判断,烧伤原因的鉴别,需要排除电和化学烧伤</strong></p>
<hr>
<h2 id="急诊处理"><a class="header-anchor" href="#急诊处理">¶</a>急诊处理</h2>
<h3 id="现场急救"><a class="header-anchor" href="#现场急救">¶</a>现场急救</h3>
<ol>
<li>迅速脱离热源,褪去衣物,不可粗暴剥脱。在现场可用干净敷料或布织物保护伤处,立即送医</li>
<li>初步判断伤情,如有大出血,窒息,开放性气胸,严重中毒,立即组织抢救
<blockquote>
<p>烧伤常伴呼吸道受烟雾,热力灼伤<br>
切记注意有无呼吸道吸入性损伤,保持呼吸通畅,<strong>必要时切开气管</strong><br>
出现心脏骤停,确认环境安全后,迅速心肺复苏</p>
</blockquote>
</li>
<li>轻度烧伤,特别是四肢烧伤,立即用冷水连续冲洗或浸泡</li>
<li>大面积烧伤,立即建立静脉通道,予以补液,抗休克治疗</li>
<li>转运伤员就近原则,严重烧伤早期,<strong>切忌长途转运</strong></li>
</ol>
<h3 id="急诊治疗"><a class="header-anchor" href="#急诊治疗">¶</a>急诊治疗</h3>
<h4 id="轻度烧伤"><a class="header-anchor" href="#轻度烧伤">¶</a>轻度烧伤</h4>
<blockquote>
<p>处理创面,<strong>小心</strong> 剔净创面周围毛发,清洁健康皮肤,去除异物。</p>
</blockquote>
<ul>
<li>I 度创面无需处理,外敷清凉药物即可</li>
<li>浅 II 度,如果水疱完整,不要动它,如果水疱很大,用消毒空针去抽水疱液,然后消毒包扎,如果水疱已经撕破,用无菌纱布,油性敷料包扎。创面无感染无需经常换药,面颈部和会阴部烧伤可以予以暴露,关节部位 II~III 度必须用夹板固定关节,按需用止痛镇静药物,酌情使用破伤风抗毒素。</li>
</ul>
<h1>中度及以上烧伤立刻运送至有烧伤专科的医院</h1>
<p>处理要点:</p>
<ol>
<li>吸氧,呼吸支持,建立输液通道,留置尿管,观察每小时尿量,比重,pH,有无血红蛋白尿,肌红蛋白尿。</li>
<li>估算烧伤面积,深度</li>
<li>液体复苏,抗休克,应用林格氏液,生理盐水,葡萄糖液,胶体溶液</li>
<li>创面处理(烧伤清除术,创面覆盖物应用,环状焦痂切开减压术,植皮术)</li>
<li>镇静,止痛</li>
<li>如果创面污染重,注射破伤风抗毒血清</li>
<li>抗感染,积极防治烧伤脓毒症</li>
<li>肠内肠外营养支持,尽量肠内营养</li>
<li>减少瘢痕,挛缩,进行功能康复</li>
</ol>
<h2 id="参考"><a class="header-anchor" href="#参考">¶</a>参考</h2>
<ul>
<li>《外科护理学【第六版】》</li>
<li>《烧伤外科学分册》</li>
<li>《急诊与灾难医学【第三版】》</li>
</ul>
]]></content>
<tags>
<tag>Medicine</tag>
</tags>
</entry>
<entry>
<title>慢性肺源性心脏病</title>
<url>/2024/01/23/%E6%85%A2%E6%80%A7%E8%82%BA%E6%BA%90%E6%80%A7%E5%BF%83%E8%84%8F%E7%97%85/</url>
<content><![CDATA[<p>肺源性心脏病(cor pulmonale)简称肺心病, 指由于支气管-肺组织, 胸廓或肺血管病变引起肺血管阻力增加, 产生肺动脉高压,继而右心室结构和/或功能改变的疾病.<br>
根据起病缓急和病程长短, 可分为急性肺心病和慢性肺心病两类, 急性肺心病常见于急性大面积肺栓塞,这里主要讲慢性肺心病.</p>
<p>慢性肺心病是常见的呼吸系统疾病.患病率存在地区差异, 北方地区高于南方地区,农村高于城市.患病率随年龄增高而增加, 吸烟者比不吸烟者患病率明显增多, 男女无明显差异.</p>
<p>冬春季节和气候骤变时, 易出现急性发作.</p>
<h2 id="病因与发病机制"><a class="header-anchor" href="#病因与发病机制">¶</a>病因与发病机制</h2>
<h3 id="病因"><a class="header-anchor" href="#病因">¶</a>病因</h3>
<ol>
<li>支气管, 肺疾病: 最多见为COPD, 占80%~90%, 其次为哮喘, 支气管扩张症, 肺结核, 间质性肺疾病等.</li>
<li>胸廓运动障碍性疾病: 较少见, 严重胸廓或脊椎畸形以及神经肌肉疾患均可引起胸廓活动受限, 肺受压, 支气管扭曲或变形, 气道引流不畅,肺部反复感染,并发肺气肿或纤维化,最终引起慢性肺心病.</li>
<li>肺血管疾病: 特发性或慢性栓塞性肺动脉高压, 肺小动脉炎均可起肺血管阻力增加, 肺动脉压升高和右心室负荷加重,发展为慢性肺心病.</li>
<li>其他: 原发性肺泡通气不足及先天性口咽畸形, 睡眠呼吸暂停低通气综合征等均可产生低氧血症, 引起肺血管收缩, 导致肺动脉高压, 发展为慢性肺心病.</li>
</ol>
<h3 id="发病机制"><a class="header-anchor" href="#发病机制">¶</a>发病机制</h3>
<blockquote>
<p>肺动脉高压的形成: 不同疾病所致肺动脉高压的机制不完全一样, 这里记录低氧性肺动脉高压, 尤其COPD所致肺动脉高压的机制.</p>
</blockquote>
<ol>
<li>肺血管阻力增加的功能性因素: 肺血管收缩在低氧性肺动脉高压的发生中起着关键作用.缺氧, 高碳酸血症和呼吸性酸中毒导致肺血管收缩, 痉挛, 其中缺氧是肺动脉高压形成最重要的因素.缺氧时收缩血管的活性物质增多,使肺血管收缩, 血管阻力增加.缺氧使肺血管平滑肌细胞膜对 <code>Ca2+</code> 的通透性增加, 直接使肺血管平滑肌收缩.另外, 高碳酸血症时,『产生增多,使血管对缺氧的敏感<br>
性增强, 致肺动脉压增高.</li>
<li>肺血管阻力增加的解剖学因素: 解剖学因素系指肺血管解剖结构的变化, 形成肺循环血流动力学障碍.主要原因有:
<ol>
<li>长期反复发作的COPD及支气管周围炎,可累及邻近肺小动脉, 引起血管炎, 使肺血管阻力增加, 产生肺动脉高压.</li>
<li>肺气肿导致肺泡内压增高, 压迫肺泡毛细血管, 造成管腔狭窄或闭塞.肺泡壁破裂造成毛细血管网的毁损, 当肺泡毛细血管床减损超过70%时肺循环阻力增大.</li>
<li>肺血管重构:慢性缺氧使肺血管收缩, 管壁张力增高.缺氧时肺内产生多种生长因子,可直接刺激管壁平滑肌细胞, 内膜弹力纤维及胶原纤维增生.</li>
<li>血栓形成:多发性肺微小动脉原位血栓形成, 引起血管阻力增加, 加重肺动脉高压.</li>
</ol>
</li>
<li>血液黏稠度增加和血容量增多:慢性缺氧导致继发性红细胞生成增多, 血液黏稠度增加.缺氧可使醛固酮分泌增加,引起水, 钠潴留;缺氧又使肾小动脉收缩, 肾血流量减少也加重水, 钠潴留, 血容量增多.血液黏稠度增加和血容量增多,可使肺动脉压升高.</li>
<li>心脏病变和心力衰竭:肺循环阻力增加导致肺动脉高压, 右心发挥代偿作用, 以克服肺动脉压升高的阻力而发生右心室肥厚.随着病情进展, 肺动脉压持续升高, 右心失代偿而致右心衰竭.</li>
<li>其他重要器官的损害:缺氧和高碳酸血症可导致重要器官如脑, 肝, 肾, 胃肠及内分泌系统, 血液系统等发生病理改变, 引起多脏器的功能损害.</li>
</ol>
<h2 id="临床表现"><a class="header-anchor" href="#临床表现">¶</a>临床表现</h2>
<h1>肺, 心功能代偿期</h1>
<ol>
<li>症状:C0PD病人可有咳嗽, 咳痰, 气促, 活动后可有心悸, 呼吸困难, 乏力和劳动耐力下降,少有胸痛或咯血.</li>
<li>体征:原发肺脏疾病体征, 可有不同程度的发维和肺气肿体征,可见肺动脉高压和右室扩大的体征.</li>
</ol>
<h2 id="肺-心功能失代偿期"><a class="header-anchor" href="#肺-心功能失代偿期">¶</a>肺, 心功能失代偿期</h2>
<ol>
<li>呼吸衰竭
<ul>
<li>症状:呼吸困难加重, 夜间为甚, 常有头痛, 失眠, 食欲下降, 白天嗜睡, 甚至出现表情淡漠, 神志恍惚, 澹妄等肺性脑病的表现.</li>
<li>体征:明显发维, 球结膜充血, 水肿, 严重时可有视网膜血管, 视盘水肿等颅内压升高的表现.腱反射减弱或消失, 出现病理反射.因高碳酸血症可出现周围血管扩张的表现, 如皮肤潮红, 多汗.</li>
</ul>
</li>
<li>右心衰竭
<ul>
<li>症状:明显气促, 心悸, 食欲缺乏, 腹胀, 恶心等.</li>
<li>体征:发绡明显, 颈静脉怒张, 心率增快, 可出现心律失常, 剑突下可闻及收缩期杂音, 甚至出现舒张期杂音.肝大并有压痛,肝颈静脉回流征阳性, 下肢水肿,重者可有腹水.</li>
</ul>
</li>
<li>并发症: 肺性脑病, 电解质及酸碱平衡紊乱, 心律失常, 休克, 消化道出血和弥散性血管内凝血等.</li>
</ol>
<h2 id="实验室及其他检查"><a class="header-anchor" href="#实验室及其他检查">¶</a>实验室及其他检查</h2>
<ol>
<li>胸部X线检查: 除肺, 胸基础疾病及急性肺部感染的特征外, 尚有肺动脉高压征象.</li>
<li>心电图检查: 心电图对慢性肺心病的诊断阳性率为60. 1%〜88. 2%.主要表现有电轴右偏,<br>
肺性P波.</li>
<li>超声心动图检查: 超声心动图诊断慢性肺心病的阳性率为60.6%〜87.0%.</li>
<li>动脉血气分析: 可出现低氧血症,甚至呼吸衰竭或合并高碳酸血症.</li>
<li>血液检查: 红细胞及血红蛋白可升高, 全血及血浆黏度可增加, 心功能不全时可伴有肾功能或肝功能异常.</li>
<li>其他: 早期或缓解期病人可行肺功能检查.痰细菌学检查可指导抗生素的选用.</li>
</ol>
<h2 id="治疗要点"><a class="header-anchor" href="#治疗要点">¶</a>治疗要点</h2>
<h3 id="肺-心功能代偿期"><a class="header-anchor" href="#肺-心功能代偿期">¶</a>肺, 心功能代偿期</h3>
<p>可采用综合治疗措施, 延缓基础疾病进展, 增强病人的免疫功能,预防感染,减少或避免急性加重, 加强康复锻炼和营养,必要时长期家庭氧疗或家庭无创呼吸机治疗等.</p>
<h3 id="肺-心功能失代偿期-v2"><a class="header-anchor" href="#肺-心功能失代偿期-v2">¶</a>肺, 心功能失代偿期</h3>
<p>治疗原则为积极控制感染, 保持呼吸道通畅,改善呼吸功能, 纠正缺氧和二氧化碳潴留, 控制呼吸衰竭和心力衰竭, 处理并发症.</p>
<ol>
<li>控制感染:参考痰培养及药敏试验选择抗生素.没有培养结果时, 根据感染的环境及痰涂片结果选用抗生素.常用青霉素类, 氨基糖甘类, 喳诺酮类或头抱菌素类药物.注意继发真菌感染的可能.</li>
<li>控制呼吸衰竭:使用支气管舒张药和祛痰药, 吸痰, 通畅呼吸道, 改善通气功能.合理氧疗.需要时给予无创正压通气或气管插管有创正压通气治疗.详见本章第十四节“呼吸衰竭”的治疗.</li>
<li>控制心力衰竭:慢性肺心病病人一般经积极控制感染, 改善呼吸功能, 纠正缺氧和二氧化碳潴留后, 心力衰竭便能得到改善,病人尿量增多, 水肿消退, 不需常规使用利尿药和正性肌力药.但病情较重或对上述治疗无效的病人, 可适当选用利尿药, 正性肌力药或扩血管药物.
<ol>
<li>利尿药:具有消除水肿, 减少血容量, 减轻右心前负荷的作用.原则上选用作用温和的利尿药, 联合保钾利尿药,宜短期, 小剂量使用.如氢氯廛嗪25mg,每天1〜3次;联用螺内酯20〜40mg,每天1〜2次.应用利尿药易出现低钾, 低氯性碱中毒, 痰液黏稠不易排痰和血液浓缩,应注意预防.</li>
<li>正性肌力药:由于慢性缺氧和感染,慢性肺心病病人对洋地黄类药物耐受性低, 容易中毒, 出现心律失常.原则上选用作用快, 排泄快的洋地黄类药物, 小剂量(常规剂量的1/2或2/3量)静脉给药.用药前注意纠正缺氧, 防治低钾血症, 以免发生药物毒性反应.</li>
</ol>
</li>
</ol>
<h2 id="健康指导"><a class="header-anchor" href="#健康指导">¶</a>健康指导</h2>
<ol>
<li>疾病预防指导: 由于慢性肺心病是各种原发肺胸疾病晚期的并发症, 应对高危人群进行宣传教育, 劝导戒烟, 积极防治COPD等慢性支气管肺疾病, 以降低发病率.</li>
<li>疾病知识指导: 使病人和家属了解疾病发生, 发展过程, 减少反复发作的次数.积极防治原发病, 避免和防治各种可能导致病情急性加重的诱因, 坚持家庭氧疗等.加强饮食营养, 以保证机体康复的需要.病情缓解期应根据肺, 心功能及体力情况进行适当的体育锻炼和呼吸功能锻炼如散步, 气功, 太极拳, 腹式呼吸, 缩唇呼吸等, 改善呼吸功能,提高机体免疫功能.</li>
<li>病情监测指导: 告知病人及家属病情变化的征象, 如体温升高, 呼吸困难加重, 咳嗽剧烈, 咳痰不畅, 尿量减少, 水肿明显或发现病人神志淡漠, 嗜睡, 躁动, 口唇发纳加重等,均提示病情变化或加重,需及时就诊.</li>
</ol>
]]></content>
<tags>
<tag>Medicine</tag>
</tags>
</entry>
<entry>
<title>替米沙坦</title>
<url>/2023/03/13/%E6%9B%BF%E7%B1%B3%E6%B2%99%E5%9D%A6/</url>
<content><![CDATA[<h2 id="名称"><a class="header-anchor" href="#名称">¶</a>名称</h2>
<p>中文名称: 替米沙坦</p>
<p>中文别名: 4-{[2-正丙基-4-甲基-6-(1-甲基苯并咪唑-2-基)苯并咪唑-1-基]甲基}联苯基-2-羧酸</p>
<p>英文名称: Telmisartan</p>
<p>英文别名: 4’[(1,4’-Dimethyl-2’-propyl[2,6’-bi-1H-benzimidazol]-1’-yl)methyl][1,1’-biphenyl]-2-carboxylic acid</p>
<h2 id="概要"><a class="header-anchor" href="#概要">¶</a>概要</h2>
<p>替米沙坦是一种新型的降血压药物,是一种特异性血管紧张素Ⅱ受体(ATⅠ型)拮抗剂。替米沙坦替代血管紧张素Ⅱ受体与ATⅠ受体亚型(已知的血管紧张素Ⅱ作用位点)高亲和性结合。替米沙坦在ATⅠ受体位点无任何部位激动剂效应,替米沙坦选择性与ATⅠ受体结合,该结合作用持久。替米沙坦对其他受体(包括AT2和其它特征更少的AT受体)无亲和力。上述其它受体的功能尚未可知,由于替米沙坦导致血管紧张素Ⅱ水平增高,从而可能引起的受体过度刺激效应亦不可知。替米沙坦不抑制人体血浆肾素,亦不阻断离子通道。替米沙坦不抑制血管紧张素转换酶Ⅱ,该酶亦可降解缓激肽作用增强导致的不良反应。在人体给予80mg替米沙坦几乎可完全抑制血管紧张素Ⅱ引起的血压升高。抑制效应持续24小时,在48小时仍可测到。首剂替米沙坦后3小时内降压效应逐渐明显。在治疗开始后4周可获得最大降压效果,并可在长期治疗中维持。替米沙坦治疗如突然中断,数天后血压逐渐恢复到治疗前水平,而不出现反弹性高血压。在直接比较两种高血压药物的临床试验研究中,替米沙坦治疗组的患者干咳发生率显著低于血管紧张素转换酶抑制剂治疗组。</p>
<p>替米沙坦降压幅度大,部分患者可能会出现低血压,这个倒是挺尴尬的。</p>
<h2 id="要点"><a class="header-anchor" href="#要点">¶</a>要点</h2>
<p>1 药代动力学显示:作用迅速(0.3h),持续时间长 (35.4h),降压时对心率的影响小</p>
<p>2 同依那普利比较:降压效果优于依那普利,两者同利尿剂合用,效果 仍为替米沙坦好,且咳嗽发生率少</p>
<p>3 同赖诺普利比较:降压(收缩压和舒张压)效果更为明显,咳嗽发生率替米沙坦组(16%)明显低于赖诺普利组(60%)</p>
<p>4 同阿替洛尔比较:降压效果相当,副作用(阳痿和疲劳)发生率低</p>
<p>5 同氨氯地平比较:替米沙坦组在服药后的四小时内和早上六点到十二点显著性地降低心率</p>
<p>总之替米沙坦与其它类抗高血压药物相比有以下特点:</p>
<p>具有受体作用的专一性</p>
<p>抗高血压作用显著</p>
<p>具有良好的利尿作用</p>
<p>能改善心肌狭窄障碍</p>
<h2 id="注意"><a class="header-anchor" href="#注意">¶</a>注意</h2>
<p>成人用药:一次mg—80mg,一日一次。服用时间不受饮食影响。 轻或中度肾功能不良的病人,以及老人服用本品不需调整剂量。轻或中度肝功能不全的病人,本品用量不应超过40mg/日。对于儿童:本品的安全性及有效性数据尚未建立。 腹泻和血管性水肿。大多为轻微的和暂时的,一般不需停止治疗。其发生与剂量无相关性。</p>
<p>注意:<br>
1.本品使用过量时若发生症状性低血压应进行支持性治疗,本品不能通过血液透析清除。<br>
2.本品可能会增加抗高血压药物的降压作用。<br>
3.与某些药物合用时应监测血清锂水平。<br>
4.轻至中度肾功能损伤患者不需调整本品剂量<br>
> 替米沙坦主要通过肝脏代谢,胆汁排泄,胆汁淤积、胆道梗阻或严重肝功能不全患者不应使用替米沙坦,用药期间定期检查肝功能,肾功能不全的患者用药期间要密切监测血钾水平。</p>
<p>5.孕妇及哺辱期妇女禁用本品。<br>
6.儿童患者使用本品的安全性尚未确立,儿童慎用。</p>
<p>特别注意的是!<br>
1.对患有胆汁梗阻性疾病和严重肝肾功能不全者禁用。<br>
2.孕妇及哺辱期妇女禁用。</p>
<h2 id="参考"><a class="header-anchor" href="#参考">¶</a>参考</h2>
<ul>
<li><a href="https://www.yixue.com/%E6%9B%BF%E7%B1%B3%E6%B2%99%E5%9D%A6">https://www.yixue.com/替米沙坦</a></li>
</ul>
]]></content>
<tags>
<tag>Medicine</tag>
</tags>
</entry>
<entry>
<title>极简主义生活的本质</title>
<url>/2025/10/19/%E6%9E%81%E7%AE%80%E4%B8%BB%E4%B9%89%E7%94%9F%E6%B4%BB%E7%9A%84%E6%9C%AC%E8%B4%A8/</url>
<content><![CDATA[<p>世界的本质是熵增的。</p>
<p>所有的一切,不论是建筑、文明,还是人的身体与精神,只要没人去维护,就会不可避免地朝着混乱、衰败的方向走去。</p>
<p>我们看到一座老厂房荒废几年就杂草丛生、窗户破碎,一座古城墙经历几百年风雨就成了残垣断壁,这些都是自然法则在起作用。</p>
<p>人类在对抗熵增,所有文明的存在,本身就是在和混乱做斗争。</p>
<p>这种规律,不仅存在于物理世界,也存在在人生中。</p>
<p>一个人若不打理生活,家里会越来越乱;若不管理身体,健康会越来越差;若不梳理思绪,精神就会陷入焦虑与疲惫。</p>
<p>人如果想让生活保持秩序与清明,就必须投入能量去维护它。而能量是有限的,当你把能量分散在太多琐事和物品上,最终什么也维持不好。</p>
<p>这就是极简主义的底层逻辑。</p>
<p>极简是一种顺应规律的生活方式。我们每个人都只有有限的时间、精力和注意力,而现代社会却不断往我们身上堆砌各种“必须拥有”的东西。</p>
<p>表面看,这些东西让生活更方便,实际上却在慢慢榨干我们的能量。因为每一样东西,都需要被管理、被维护。</p>
<p>真正的自由,不是“想买什么就买什么”,而是“不需要买什么也能过得很好”。</p>
<p>一个人若能不再被物质牵绊,他的生活简单到可以随时离开、随时出发,没有太多必须要维持的物件,也就释放出了大量的能量去维持内在秩序。</p>
<p>你不必花时间打扫塞满杂物的房间,不必焦虑哪件衣服还没穿,不必担心旧东西丢了会可惜。生活变得轻盈,心也自然平静。</p>
<p>极简主义是一种“主动熵减”的生活方式。世界的自然趋势是熵增,而极简主义者在用有限的能量对抗这股趋势。</p>
<p>打扫房间、整理衣柜、清理文件,看似小事,本质上都是在恢复秩序。</p>
<p>生活中的秩序越多,人心就越清晰。家整洁了,人也更容易集中注意力,思考变得通畅。那些东西堆满的家,其实是一个人内在混乱的外在映照。</p>
<p>极简主义不只是少买东西,而是一种能量管理的哲学。你拥有的越多,分散的能量就越多;你放下的越多,能量就越集中。</p>
<p>人的时间和注意力就像光,散开时只能照亮一小片模糊的区域,集中时才能穿透一切。</p>
<p>极简主义让人集中能量,把有限的生命力用在真正重要的地方——比如学习、思考、创造、体验。</p>
<p>有人说,极简是贫穷者的浪漫,其实恰恰相反,极简是内心丰盈者的选择。</p>
<p>因为只有不再依赖外物,你才能真正自由。什么都有的人,却都被自己的东西困住。</p>
<p>而极简的人,不被物质定义,自然也不被物质消耗。</p>
<p>归根到底,极简主义是一种逆熵的生活策略。在这个无序不断扩张的世界里,保持生活的清澈与节制,本身就是一种胜利。</p>
]]></content>
<tags>
<tag>Life</tag>
</tags>
</entry>
<entry>
<title>气胸</title>
<url>/2023/03/01/%E6%B0%94%E8%83%B8/</url>
<content><![CDATA[<p><img src="/img/%E5%8C%BB%E5%AD%A6/%E6%B0%94%E8%83%B8/%E6%B0%94%E8%83%B8.jpg" alt="气胸.jpg"></p>
<p>这个病多见于瘦高的年轻男性,一般感觉起来就是:</p>
<ol>
<li>胸腔有刺痛感,针刺的感觉,想想那种拿小针针撮你小胸胸的感觉。</li>
<li>胸闷,就像有东西压在胸口。</li>
<li>呼吸困难,就像有人捂住你的口鼻,有点窒息的感觉。</li>
</ol>
<p>通俗来讲造成这种感觉的原因是肺泡破裂,肺漏气了。然后气体在胸腔里聚集就压缩了肺。</p>
<p><img src="/img/%E5%8C%BB%E5%AD%A6/%E6%B0%94%E8%83%B8/%E6%B0%94%E8%83%B82.jpeg" alt="气胸2.jpeg"><br>
图中是一位右侧气胸患者(图中右面)的电脑断层扫描影像。在胸腔的边缘有着引流管,而图中黑色一片就是邻近于肺膜间(黑)和肋骨(白)的内腔。心脏则在图中央。医学专科胸腔医学、胸腔外科学症状胸痛、呼吸困难、疲劳常见始发于突发性肇因未知、创伤风险因子慢性阻塞性肺病(COPD)、结核病、抽烟诊断方法胸部X光、超声波、电脑断层扫描相似疾病或共病肺部大疱、血胸预防禁烟或戒烟治疗保守治疗、空针穿刺、胸管置放、肋膜黏连术盛行率约每10万人中20例外伤性气胸。</p>
<h2 id="临床表现"><a class="header-anchor" href="#临床表现">¶</a>临床表现</h2>
<p>在医学上的临床表现中,气胸具有如下症状:<br>
患者常有持重物、屏气、剧烈运动等诱发因素,但也有在睡眠中发生气胸者,病人突感一侧胸痛、气急、憋气,可有咳嗽、但痰少,小量闭合性气胸先有气急,但数小时后逐渐平稳,X线也不一定能显示肺压缩。若积气量较大者或者原来已有广泛肺部疾患,病人常不能平卧。如果侧卧,则被迫使气胸患侧在上,以减轻气急。病人呼吸困难程度与积气量的多寡以及原来肺内病变范围有关。当有胸膜粘连和肺功能减损时,即使小量局限性气胸也可能明显胸痛和气急。</p>
<h2 id="初步诊断方式"><a class="header-anchor" href="#初步诊断方式">¶</a>初步诊断方式</h2>
<p>突发一侧胸痛,伴有呼吸困难并有气胸体征,即可作出初步诊断。X线显示气胸征是确诊依据。在无条件或病情危重不允许作X线检查时,可在患侧胸腔积气体征最明确处试穿,抽气测压,若为正压且抽出气体,说明有气胸存在,即应抽出气体以缓解症状,并观察抽气后胸腔内压力的变化以判断气胸类型。在原有严重哮喘或肺气肿基础上并发气胸时,气急、胸闷等症状有时不易觉察,要与原先症状仔细比较。</p>
<h2 id="类似病症需要区别开来"><a class="header-anchor" href="#类似病症需要区别开来">¶</a>类似病症需要区别开来</h2>
<ol>
<li>支气管哮喘和阻塞性肺气肿: 有气急和呼吸困难,体征亦与自发性气胸相似,但肺气肿呼吸困难是长期缓慢加重的,支气管哮喘病人有多年哮喘反复发作史。当哮喘和肺气肿病人呼吸困难突然加重且有胸痛,应考虑并发气胸的可能,X线检查可以作出鉴别。</li>
<li>急性心肌梗塞: 病人亦有急起胸痛、胸闷、甚至呼吸困难、休克等临床表现,但常有高血压、动脉粥样硬化、冠心病史。体征、心电图和X线胸透有助于诊断。</li>
<li>肺栓塞: 有胸痛、呼吸困难和紫绀等酷似自发性气胸的临床表现,但病人往往有咯血和低热,并常有下肢或盆腔栓塞性静脉炎、骨折、严重心脏病、心房纤颤等病史,或发生在长期卧床的老年患者。体检和X线检查有助于鉴别。</li>
<li>肺大疱: 位于肺周边部位的肺大疱有时在X线下被误为气胸。肺大疱可因先天发育形成,也可因支气管内活瓣阻塞而形成张力性囊腔或巨型空腔,起病缓慢,气急不剧烈,从不同角度作胸部透视,可见肺大疱或支气管源囊肿为圆形或卵圆形透光区,在大疱的边缘看不到发线状气胸线,疱内有细小的条纹理,为肺小叶或血管的残遗物。肺大疱向周围膨胀,将肺压向肺尖区、肋膈角和心膈角,而气胸则呈胸外侧的透光带,其中无肺纹可见。肺大疱内压力与大气压相仿,抽气后,大疱容积无显著改变。</li>
<li>主动脉夹层: 夹层分离突然发生时多数患者突感胸部疼痛,向胸前及背部放射,随夹层涉及范围而可以延至腹部、下肢、臂及颈部。疼痛剧烈难以忍受,起病后即达高峰,呈刀割或撕裂样。少数起病缓慢者疼痛可以不著。必须与气胸相鉴别。</li>
<li>其他如消化性溃疡穿孔,膈疝、胸膜炎和肺癌等,有时因急起的胸痛,上腹痛和气急等,亦应注意与自发性气胸鉴别。</li>
</ol>
<p><strong>建议至医院就诊明确病因,例如是不是最常见的肺大疱引起,还是肺部其他疾病引起,病因治疗,以免延误病情。</strong></p>
<blockquote>
<p>如果是肺大疱引起: 肺大疱先天性支气管发育异常,粘膜皱襞呈瓣膜状,软骨发育不良,引起活瓣作用所致。如果有胸闷、气短的症状,而且反复发作,建议手术治疗。如果没有任何症状可以观察,内科治疗。 病人的症状主要与大疱的数目、大小以及是否伴有炎症,肺大疱是否破裂密切相关。首先、小范围的先天性肺大疱一般不会直接导致死亡。 但是、大范围的先天性肺大疱或者出现严重并发症时,有可能引起死亡:</p>
<ol>
<li>直接原因。巨大的肺大疱,因为气体交换困难,多有不同程度的呼吸困难,有的病人因而失去劳动力,甚至行动亦受到限制或者窒息可能。</li>
<li>间接原因。主要是出现并发症时,先天性肺大疱绝大多数是不感染的,但如果感冒等原因引起肺部分泌物增多,引流肺大疱的支气管堵塞,肺大疱支气管内充满炎性分泌物,患者可出现发热、咳嗽、咳痰等感染症状,严重时可以导致菌血症、败血症、脓毒血症导致生命危险。而且肺大疱引起的自发性血胸,多数由肺尖部的大疱或大疱周围的肺组织与胸顶粘连及粘连撕裂活动出血。由于肺、心脏、膈肌运动的去纤维化作用,胸腔内的血液不凝固,因此出血很难自动停止。临床症状可因出血的快慢而不同,出血缓慢时,患者可表现为逐渐加重的胸闷,呼吸困难,X线可见膈角变钝,或胸腔积液的抛物线影像。出血迅速时,短期内可以有休克表现。其次,大范围的先天性肺大疱导致机体长期处于气体交换困难,缺氧时,能导致能促使肺原性心脏病的发生,甚至多器官功能衰竭。</li>
</ol>
</blockquote>
<blockquote>
<p><strong>先天性肺大疱破裂也可以危及生命</strong>,破裂时会并发自发性气胸,有突然胸痛,呼吸困难。若肺大疱破裂后形成活瓣,吸气时胸腔负压增高,气体进入胸腔,呼气时活瓣关闭,气体不能排出,尤其是咳嗽时,声门关闭气道压力增高,气体进入胸腔,声门开放后,气道压力减低,裂口又闭合,每一次呼吸和咳嗽都使胸腔内气体量增加,就形成张力性气胸。 张力性气胸时患侧肺组织完全萎缩,纵隔被推向健侧,在健侧肺组织亦被压缩的同时心脏大血管移位,大静脉扭曲变形,影响血液回流,造成呼吸循环严重障碍。有可能出现很快的心肺功能衰竭,导致死亡。</p>
</blockquote>
<h2 id="急救"><a class="header-anchor" href="#急救">¶</a>急救</h2>
<ol>
<li>
<p>胸部损伤: 胸部如果被刺穿,需要立即覆盖伤处,并以凡士林或胶布密封,以免空气继续经伤处流入。无菌的胶布是较理想的选择, 但是所有气密的物质,例如玻璃纸和香烟盒也可以用。密封后,需要开一个小孔(振动筏)来使吸气时排出空气。胸部刺穿的患者需要密切监察,防止引发对生命有危险的张力性气胸。</p>
</li>
<li>
<p>入院前护理: 多数救护员可以进行针刺抽气, 以减低胸部的压力。 如果情况恶化,导管抽气也可能需要,包括有知觉的病人。可能的话,进行额外的治疗和即时送病人到医院治疗。没有经过适当的治疗的话,气胸患者是不能用飞机运送的。</p>
</li>
</ol>
<h2 id="流行病学"><a class="header-anchor" href="#流行病学">¶</a>流行病学</h2>
<p>经年龄调整后的年均发病率﹝AAIR﹞显示,男性患上气胸的机会较女性高出三至六倍。菲什曼在研究中提出以每十万人年计算,男女患上原发性气胸的AAIR分别为7.4和1.2。身高比平均值较高的人,他们患上气胸的AAIR也相对增加 - 至少为76英寸(1.93米)高的人,每十万人年约有200个案例。瘦削的身材也似乎增加了患上原发性气胸的风险。</p>
<p>此外,男性和女性烟民患上原发性气胸,相对于同性别的非吸烟者患上的机会高出约22倍和9倍。而个人吸烟的程度越凶,风险会有“大于线性”的效果:比方说每天吸食10支香烟的人,会比非吸烟者高出20倍患上气胸的机会;每天消耗20支香烟的,则会高出约100倍。</p>
<p>在继发自发性气胸的病例当中,男性和女性的AAIR大概为6.3和2.0,而复发的风险则取决于患者本身有否任何潜在的肺病及其严重性。一旦发生了第二次气胸,病人之后再复发的机会极高。目前来说没有周详的研究调查儿童的发病率,但估计是每年约十万份之5至10。</p>
<p>除外张力性气胸以外,因为气胸而死亡的案例是非常罕见的。据英国统计数字显示,每年每一百万人当中会有1.26位男性和0.62位女性会因为气胸死亡,其中中老年和继发性气胸的患者会有较高的死亡风险。</p>
<h2 id="引用"><a class="header-anchor" href="#引用">¶</a>引用</h2>
<ul>
<li><a href="https://zhuanlan.zhihu.com/p/140951268">https://zhuanlan.zhihu.com/p/140951268</a></li>
<li><a href="https://www.yixue.com/%E6%B0%94%E8%83%B8">https://www.yixue.com/气胸</a></li>
<li><a href="https://zhuanlan.zhihu.com/p/84718122">https://zhuanlan.zhihu.com/p/84718122</a></li>
</ul>
]]></content>
<tags>
<tag>Medicine</tag>
</tags>
</entry>
<entry>
<title>灯笼草</title>
<url>/2023/06/22/%E7%81%AF%E7%AC%BC%E8%8D%89/</url>
<content><![CDATA[<p>今天在菜园子里看到了野生的灯笼草,都结果了:</p>
<p><img src="/img/%E7%94%9F%E6%B4%BB/%E7%81%AF%E7%AC%BC%E8%8D%89.jpg" alt=""></p>
<p>灯笼果别名为小果酸浆、秘鲁苦蘵、打头泡、灯笼草等,茄科酸浆属多年生草本植物。<br>
灯笼果茎直立,叶较厚,阔卵形或心脏形,两面密生柔毛;花单独腋生,花萼阔钟状,花冠阔钟状,黄色而喉部有紫色斑纹,花丝及花药蓝紫色,花药长约3毫米。果萼薄纸质,淡绿色或淡黄色,浆果成熟时黄色,种子黄色,圆盘状,夏季开花结果。生于田间、路旁、村边。我国南北各地均有分市。</p>
<p><strong>不要与<a href="https://en.wikipedia.org/wiki/Gooseberry">醋栗</a>混淆</strong></p>
<p>醋栗长这样:</p>
<p><img src="/img/%E7%94%9F%E6%B4%BB/%E9%86%8B%E6%A0%97.jpg" alt=""></p>
<hr>
<h2 id="性味"><a class="header-anchor" href="#性味">¶</a>性味</h2>
<ul>
<li>
<p>《陆川本草》:“甘淡,微寒。”</p>
</li>
<li>
<p>《南宁市药物志》:“苦,寒,微甘。”</p>
</li>
</ul>
<h2 id="功能主治"><a class="header-anchor" href="#功能主治">¶</a>功能主治</h2>
<blockquote>
<p>清热,行气,止痛,消肿。治感冒,痄腮,喉痛,咳嗽,腹胀,疝气,天疱疮。</p>
</blockquote>
<ul>
<li>
<p>《陆川本草》:“行气,消胀,利尿。治腹胀,睾丸炎,疝气。”</p>
</li>
<li>
<p>《南宁市药物志》:“清热杀虫,止痛消肿。治热眼,喉痛,咳嗽;外敷毒疮,并熏洗阴囊肿大。”</p>
</li>
<li>
<p>《生草药手册》:“内服治伤寒或小肠疝气。外洗治天疱疮。”</p>
</li>
<li>
<p>《中国药植图鉴》:“功同酸浆。”</p>
</li>
<li>
<p>广州空军《常用中草药手册》:“治感冒发热,腮腺炎,支气管炎,疱疹,疖疮,疝气痛。”</p>
</li>
</ul>
<h2 id="用法用量"><a class="header-anchor" href="#用法用量">¶</a>用法用量</h2>
<ul>
<li>内服:煎汤,3 ~ 5钱。</li>
<li>外用:捣敷或煎水洗。</li>
</ul>
<h2 id="引用"><a class="header-anchor" href="#引用">¶</a>引用</h2>
<ul>
<li>
<p>《本草纲目》旧版草部第十六卷,草之五:燕京野果名红姑娘,外垂降囊,中含赤子如珠,酸甘可食盈盈绕砌,与翠草同芳,亦自可爱。捣计服治黄病(即黄胆性肝炎)多效,治上气咳嗽风热,明目,付小儿内辟等多种疾病。东北地区种植较广泛,其他地区种植较少,仍属稀特蔬菜。</p>
</li>
<li>
<p>《别录》:“酸浆,生荆、楚川泽及人家田园中。五月采,阴干。”陶弘景:“酸浆,处处人家多有。叶亦可食。子作房,房中有子,如梅李大,皆黄赤色。”《唐本草》:“灯笼草,所在有之。八月采。枝干高三、四尺,有花,红色,状若灯笼,内有子,红色可爱。根、茎、花、叶并入药用。”</p>
</li>
<li>
<p>《梦溪笔谈》:“苦耽,即本草酸浆也。河西番界中酸浆有盈丈者。”《本草衍义》:“酸浆,今天下皆有之。苗如天茄子,开小白花,结青壳,熟则深红,壳中子大如樱,亦红色,樱中复有细子,如落苏之子,食之有青草气。此即苦耽也。”</p>
</li>
</ul>
]]></content>
<tags>
<tag>Medicine</tag>
<tag>Life</tag>
</tags>
</entry>
<entry>
<title>直系血亲之间不能直接输血</title>
<url>/2024/08/12/%E7%9B%B4%E7%B3%BB%E8%A1%80%E4%BA%B2%E4%B9%8B%E9%97%B4%E4%B8%8D%E8%83%BD%E7%9B%B4%E6%8E%A5%E8%BE%93%E8%A1%80/</url>
<content><![CDATA[<p>现代医学证明,直系血亲间输血有时会发生一种严重的输血反应,称为输血相关移植物抗宿主病,这种输血反应尽管发病率很低,但死亡率却高达 99.9%,一旦发生几乎无法挽救。所以,很多电视剧里的那些情节都是错误的。根据中国输血协会网站上的介绍,目前虽然可以通过“血液辐照”的处理技术,把这种具有免疫活性的淋巴细胞灭活,但是临床上仍不建议直系血亲之间输血。</p>
<p>无血缘关系的人,血细胞的抗原差异很大,很容易被免疫系统识别排斥和清除;而亲属间特别是直系亲属间的细胞抗原差异小,难辨识,加上受血者本身是需要输血的病人,免疫功能低下,因此,对输入的具有免疫活性的淋巴细胞排斥弱,从而外来的淋巴细胞就在患者体内分裂、增殖,然后向皮肤、肝脏、肠道、骨髓等器官发动攻击,从而引起致命性的并发症。</p>
]]></content>
<tags>
<tag>Medicine</tag>
<tag>Life</tag>
</tags>
</entry>
<entry>
<title>内护神经系统重点考点</title>
<url>/2024/07/09/%E7%A5%9E%E7%BB%8F%E7%B3%BB%E7%BB%9F%E9%87%8D%E7%82%B9/</url>
<content><![CDATA[<ol>
<li>
<p>十二对脑神经的顺序(一嗅二视三动眼,四滑五叉六外展,七面八听九舌咽,迷副舌下神经全):嗅神经、视神经、动眼神经、滑车神经、三叉神经、展神经、面神经、位听神经、舌咽神经、迷走神经、副神经和舌下神经。</p>
</li>
<li>
<p>脑干病变的特点:交叉性瘫痪、意识障碍、去大脑僵直、定位体征、脊髓。</p>
</li>
<li>
<p>瞳孔直径约为3-4mm,一般认为瞳孔直径<2mm为瞳孔缩小,>5mm为瞳孔散大。</p>
</li>
<li>
<p>正常脑脊液压力:80-180mmH2O。</p>
</li>
<li>
<p>意识障碍包括:</p>
<ul>
<li>嗜睡:是最轻的意识障碍,是一种病理性倦睡,患者陷入持续的睡眠状态,可被唤醒,并能正确回答和做出各种反应,但当刺激去除后很快又再入睡。</li>
<li>昏睡:较嗜睡重,持续处于睡眠状态,强刺激方能唤醒,应答不切题。</li>
<li>浅昏迷:对针刺和压眶由痛苦表情及躲避反应,无语言应答,各种反射无明显改变。</li>
<li>深昏迷:意识全部丧失,强刺激也不能引起反应,肢体常呈弛缓状失。偶有深反射亢进与病理反射出现。机体仅能维持呼吸与血循环功能。</li>
<li>意识模糊:较嗜睡为深的一种意识障碍,表现为定向障碍,思维和语言不连贯,可有和幻觉、躁动不安、谵语或精神错乱。</li>
</ul>
</li>
<li>
<p>运动障碍的护理诊断:有失用综合征的危险的护理措施:</p>
<ul>
<li>早期康复干预:重视患侧刺激、保持良好的肢体位臵、体位变换(翻身)、床上运动训练(Bobath握手、桥式运动)</li>
<li>恢复期运动训练</li>
<li>综合康复治疗</li>
</ul>
<blockquote>
<ul>
<li>急性炎症性脱髓鞘性多神经根病的临床表现中感觉障碍呈手套袜子样分布。</li>
<li>重要特点是蛋白-细胞分离现象。</li>
</ul>
</blockquote>
</li>
<li>
<p>脑血管疾病的分类</p>
<ul>
<li>依据症状持续时间:短暂性脑缺血发作、脑卒中。</li>
<li>依据病理性质:缺血性卒中、出血性卒中。</li>
<li>依据发病急缓:急性脑血管疾病、慢性脑血管疾病。</li>
</ul>
</li>
<li>
<p>定义: 短暂性脑缺血发作(TIA):局造性脑缺血导致突发短暂的可逆性神经功能障碍。</p>
</li>
<li>
<p>脑血栓形成(脑血管病中最常见)</p>
<ul>
<li>病因:最常见的病因是脑动脉粥样硬化,其次为脑动脉炎。高血压、高脂血症、糖尿病是加速脑血管硬化进展的重要因素。</li>
<li>检查:头颅CT:24小时后CT可见相应部位低密度影像。</li>
<li>急性期治疗:
<ul>
<li>早期溶栓:在发病后6小时以内进行溶栓。</li>
<li>调整血压:急性期应维持病人血压于较平时稍高水平,血压过高(收缩压>220mmHg或舒张压>120mmHg)。</li>
<li>抗血小板聚集。</li>
<li>抗凝治疗:常用药物包括肝素、低分子肝素和华法林。</li>
<li>降纤治疗。</li>
<li>脑保护治疗。</li>
<li>中医中药治疗:丹参、川芎嗪、三七、葛根素、银杏叶制剂等。</li>
<li>外科治疗:开颅降压术。</li>
<li>早期康复治疗。</li>
</ul>
</li>
<li>用药护理:
<ul>
<li>溶栓和抗凝药物:严格掌握药物剂量,监测BT、PT、 APTT,观察有无黑便、牙龈出血、皮肤瘀点瘀斑等出血表现;观察有无并发颅内出血;观察有无栓子脱落所致其他部位栓塞的表现。</li>
<li>甘露醇:监测尿量及尿液颜色;有无头痛、呕吐、意识障碍等低颅压综合征的表现。</li>
</ul>
</li>
<li>饮食护理:
<ul>
<li>体位选择:能坐者坐位进食,头略前屈,不能坐起者将床头摇起30°,头下垫枕头部前屈。</li>
<li>食物选择:食物柔软、密度与性状均一;不易松散有一定黏度;能够变形;不易粘在黏膜上。</li>
<li>吞咽方法选择:空吞咽和吞咽食物交替进行;侧方吞咽:吞咽时头侧向健侧肩部;点头样吞咽。</li>
<li>不能吞咽的病人给予鼻饲饮食。
<blockquote>
<p>防止窒息:进食前应注意休息;保持进餐环境的安静、舒适;减少进餐时环境中分散注意力的干扰因素。</p>
</blockquote>
</li>
</ul>
</li>
</ul>
</li>
<li>
<p>脑栓塞的病因:根据栓子来源可分为心源性、非心源性和来源不明性。心源性为最常见的原因,其中一半以上病人有风湿性心脏病二尖瓣狭窄合并心房颤动。</p>
</li>
<li>
<p>脑出血临床特点:</p>
<ul>
<li>多见于50岁以上有高血压病史者,男性较女性多见,冬季发病率较高。</li>
<li>体力活动或情绪激动时发病,多无前驱症状。</li>
<li>起病较急,症状于数分钟至数小时达高峰。</li>
<li>有肢体瘫痪、失语等局灶定位症状和剧烈头痛、喷射性呕吐、意识障碍等全脑症状。</li>
<li>发病时血压明显升高。(“三偏征”对侧偏瘫、偏身感觉障碍和同向性偏盲)。</li>
</ul>
<blockquote>
<p>头颅CT:确诊脑出血的首选检查方法,发病后即刻出现边界清楚的高密度影像。</p>
<p>治疗要点:治疗原则是脱水降颅压、调整血压、防止继续出血、减轻血肿所致继发性损害、促进神经功> 能恢复、加强护理防治并发症。</p>
<ul>
<li>一般治疗:卧床休息,密切观察生命体征,保持呼吸道通畅,吸氧,保持肢体的功能位,鼻饲,预防感染,维持水、电解质平衡等。</li>
<li>脱水降颅压:目的是控制脑水肿,药物:20%甘露醇。</li>
<li>调控血压:血压≥200/110mmHg时,可采取降压治疗,给予硫酸镁等。</li>
<li>止血和凝血治疗。</li>
<li>外科治疗:壳核出血量>30ml,小脑或丘脑出血>10ml(6)康复治疗。</li>
<li>休息与安全:绝对卧床休息2~4周,抬高床头15~30度,减轻脑水肿。</li>
</ul>
</blockquote>
</li>
<li>
<p>帕金森病的临床表现:</p>
<ul>
<li>静止性震颤:多从一侧开始,类似“搓丸”样动作,静止时明显震颤,动作时减轻,精神紧张时加重,入睡后消失。</li>
<li>肌强直:“铅管样强直”、“齿轮样强直”。</li>
<li>运动迟缓:“小写症”“面具脸”。</li>
<li>姿势步态异常:“慌张步态”。</li>
</ul>
</li>
<li>
<p>癫痫持续状态在给氧、防护的从速制止发作,首先给地西泮10~20mg静脉注射,注射速度不超过每分钟2mg,以免抑制呼吸,在监测血压同时静脉滴入苯妥英钠以控制发作。</p>
</li>
<li>
<p>癫痫的护理诊断:</p>
<ul>
<li>防窒息</li>
<li>防受伤:活动状态时发作,陪伴者应立即将病人缓慢臵于平卧位,防止外伤,抽搐肢体不可用力按压,以免造成骨折或关节脱位。</li>
</ul>
</li>
<li>
<p>重症肌无力的临床特点:</p>
<ul>
<li>是一种与胸腺异常有关的自身免疫性疾病。</li>
<li>最先表现为一侧眼睑下垂。</li>
<li>肌无力呈进行性发展逐渐累及其他肌肉。</li>
<li>症状晨轻暮重、活动后重,休息后轻。</li>
</ul>
<blockquote>
<p>实验室检查:</p>
<ul>
<li>疲劳试验(Jolly试验):嘱病人用力眨眼30次后眼裂明显变小或两臂持续平举后出现上臂下垂。</li>
<li>新斯的明试验:新斯的明0.5-1mg肌肉注射,10-20分钟后症状明显减轻为阳性。</li>
</ul>
</blockquote>
</li>
<li>
<p>腰椎穿刺术后护理嘱病人去枕平卧4~6小时,不可抬高头部,观察有无并发症,如头痛、腰背痛、脑疝、感染。</p>
</li>
<li>
<p>意识障碍按程度可分为嗜睡、昏睡、浅昏迷、中昏迷、深昏迷。</p>
</li>
<li>
<p>脑出血病人急性期治疗的主要原则是防止再出血、控制脑水肿、减低颅内压、维持生命功能、防治并发症。</p>
</li>
<li>
<p>根据癫痫发作的临床表现和脑电图特点,可将癫痫分为部分性发作、全面性发作、不能分类的癫痫发作3大类。</p>
<blockquote>
<p>癫痫全面性强直–阵挛发作过程可分为强直期、阵挛期、痉挛后三期。</p>
</blockquote>
</li>
<li>
<p>诊断癫痫最有价值的检查是脑电图。</p>
</li>
<li>
<p>脑动脉粥样硬化是脑血栓形成最常见的病因。</p>
</li>
<li>
<p>蛛网膜下腔出血病人应绝对卧床4-6周周,避免用力排便情绪激动等。</p>
</li>
<li>
<p>三偏症指偏瘫,偏盲,偏麻(偏身感觉障碍)。</p>
</li>
<li>
<p>吉兰——巴雷的主要危险是呼吸麻痹。</p>
</li>
<li>
<p>脑血管发病最重要的危险因素是高血压,心脏病,糖尿病,TIA。</p>
</li>
<li>
<p>TIA指脑缺血症状24小时内可以完全恢复。</p>
</li>
<li>
<p>早期溶栓指发病6小时内进行溶栓处理。</p>
</li>
<li>
<p>脑组织中豆纹动脉动脉最容易出血。</p>
</li>
<li>
<p>一般20%甘露醇200ml静脉滴注30分钟分钟内滴完。输入甘露醇后4小时内尿量少于200毫升ml要慎用或停用。</p>
</li>
<li>
<p>蛛网膜下腔出血的特征性体征是脑膜刺激征,特征性实验室检查是脑脊液检查。</p>
</li>
<li>
<p>脑血栓形成常在安静时发病,脑出血常在活动及情绪激动时发病。</p>
</li>
<li>
<p>简述癫痫持续状态的护理要点:(1)迅速控制发作(安定10~20mg静脉慢推)(2)用床挡,专人守护,必要时用约束带(3)移开周围物品(4)立即取下假牙、垫牙,保护皮肤,不用力按压病人(5)解开衣领,头偏一侧,保持气道通畅(6)观察生命体征、神志。</p>
</li>
<li>
<p>名词解释:短暂性脑缺血发作、癫痫、癫痫持续状态、帕金森病。</p>
</li>
</ol>
]]></content>
<tags>
<tag>Medicine</tag>
</tags>
</entry>
<entry>
<title>纯牛奶可以加点啥</title>
<url>/2023/03/01/%E7%BA%AF%E7%89%9B%E5%A5%B6%E5%8F%AF%E4%BB%A5%E5%8A%A0%E7%82%B9%E5%95%A5/</url>
<content><![CDATA[<h2 id="糖"><a class="header-anchor" href="#糖">¶</a>糖</h2>
<p>纯牛奶加糖可以增加碳水化合物所供给的能量,建议加蔗糖,因为蔗糖在进入消化道被消化液分解后,会变成葡萄糖而被人体吸收,一般是每100毫升牛奶加5-8克糖,即5%-8%的比例。</p>
<p>牛奶中的蛋白质80%为酪蛋白,当牛奶的酸碱度在4.6以下时,大量的酪蛋白会发生凝集、沉淀,难以消化吸收,严重者还可能导致消化不良或腹泻。所以牛奶中不宜添加果汁等酸性饮料。</p>
<p>需要注意的是:牛奶中含有的赖氨酸在加热条件下能与果糖反应,生成有毒的果糖基赖氨酸,有害于人体,<strong>鲜牛奶在煮沸时不要加糖。</strong><br>
此外,红糖里的酸性物质会把牛奶凝结成块,不好吃也不利于吸收。</p>
<h2 id="蜂蜜"><a class="header-anchor" href="#蜂蜜">¶</a>蜂蜜</h2>
<p>纯牛奶和纯蜂蜜是一个非常好的搭配,牛奶和蜂蜜之间不存在任何成分反应。蜂蜜中含有丰富的葡萄糖和果糖,属于单糖,可以直接被人体吸收,不会给肠胃造成负担,纯牛奶含有丰富的蛋白质和钙,营养成分丰富,所以纯牛奶加蜂蜜,可以更全面地补充营养。</p>
<h2 id="其他"><a class="header-anchor" href="#其他">¶</a>其他</h2>
<ol>
<li>对于巧克力:牛奶含有丰富的蛋白质和钙,而巧克力含有草酸,两者同食会结合成不溶性草酸钙,极大影响钙的吸收,甚至会出现头发干枯、腹泻、生长缓慢等现象。</li>
<li>对于药物:由于牛奶容易在药物表面形成一个覆盖膜,使奶中的钙、镁等矿物质与药物发生化学反应,形成非水溶性物质,从而影响药效。</li>
</ol>
<h2 id="引用"><a class="header-anchor" href="#引用">¶</a>引用</h2>
<ul>
<li><a href="https://zhuanlan.zhihu.com/p/140951268">https://zhuanlan.zhihu.com/p/140951268</a></li>
<li><a href="https://www.zhihu.com/question/480023755">https://www.zhihu.com/question/480023755</a></li>
</ul>
]]></content>
<tags>
<tag>Life</tag>
</tags>
</entry>
<entry>
<title>什么玩意儿都是</title>
<url>/2024/09/07/%E7%BB%99%E5%AD%A9%E5%AD%90%E4%BB%AC%E7%9A%84%E8%AF%9D/</url>
<content><![CDATA[<p>那些自古流传至今的至理名言,如无证实都不可信。今天人人称道的真理,明天却有可能被证实为谬论;有些人被这种云山雾罩的谬论遮蔽了双眼,还以为那是会为他们的土地带来甘露的云雨。</p>
<p>那些古人们认为不可能办到的事,我们要敢于尝试和创新,只有这样,才能发现是否是可以做到的。古人有古人的办法,新人有新人的办法。古人只知道不断添柴就可以让火苗不灭,而新人却可以在火车锅炉中放点干柴就可以让火车绕着地球跑。</p>
<p>对于年势已高的老人家而言,不能仅凭年龄就说自己有资格成为年轻人的老师,因为他所失去的要比得到的多得多。任何人都有权怀疑,那些最聪明的人是否真正发现了生活的价值。</p>
<p>说实话,老一辈人并没有什么至关重要的忠告留给年轻人,他们的生活经验如此不完美,他们的人生因为某种个人原因过得如此悲惨失败。也许阅历中还给了他们一些有悖于那种经验的信心,可惜他们已经不再年轻。我在这世上已经活了二十年零六天,却从没在我的长辈那里得到过一个有价值的忠告或建议。他们讲不出什么有意义的事情,却总是说一些不得要领的话来教训我。这就是生活,一个在很大程度上我并未尝试过的实验;他们尝试过了,对我来说却毫无益处。</p>
]]></content>
<tags>
<tag>Life</tag>
</tags>
</entry>
<entry>
<title>给尸体上 ECMO 并维持心跳</title>
<url>/2024/05/13/%E7%BB%99%E5%B0%B8%E4%BD%93%E4%B8%8A-ECMO-%E5%B9%B6%E7%BB%B4%E6%8C%81%E5%BF%83%E8%B7%B3/</url>
<content><![CDATA[<p>没用,真实案例中,脑死亡一天后,大脑开始液化,腹腔内脏会自融,胸部肌肉皮肤会被人工呼吸机(通俗一点的叫法)捶打成肉糜一样烂,此时开颅查看大脑变成一摊灰色浆糊,脑脊液“会从鼻孔里流出来,好像清鼻涕一样。</p>
<p>植物人存活160多天生子的案例,首先植物人连大脑皮层都未必死亡(那些昏迷多年能醒的植物人,大脑额叶,大脑皮层明显没死亡),而能够存活一百多天的案例,肯定脑干功能也没有完全消失。</p>
<p>大脑和内脏快速自融的无一例外都是大脑皮层和脑干都彻底死亡,植物神经活动消失。于是大脑和内脏快速的产生自融现象。</p>
<p>像是激素调节和神经调节同时失能。比如TSH停止分泌后甲状腺停工,然后T4也没了代谢就出问题了。这是全身性的内分泌失能,失去激素调节细胞功能丧失也正常。</p>
]]></content>
<tags>
<tag>Medicine</tag>
</tags>
</entry>
<entry>
<title>肝功能检查化验单阅读指南</title>
<url>/2023/10/13/%E8%82%9D%E5%8A%9F%E8%83%BD%E6%A3%80%E6%9F%A5%E5%8C%96%E9%AA%8C%E5%8D%95/</url>
<content><![CDATA[<p><img src="/img/%E5%8C%BB%E5%AD%A6/%E8%82%9D%E5%8A%9F%E8%83%BD%E6%A3%80%E6%9F%A5%E5%8C%96%E9%AA%8C%E5%8D%95Demo.jpeg" alt=""></p>
<ul>
<li>
<p>谷丙转氨酶 (ALT)</p>
<ul>
<li>正常情况: <mjx-container class="MathJax" jax="SVG"><svg style="vertical-align: -0.566ex;" xmlns="http://www.w3.org/2000/svg" width="6.67ex" height="2.262ex" role="img" focusable="false" viewBox="0 -750 2948 1000"><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="scale(1,-1)"><g data-mml-node="math"><g data-mml-node="mn"><path data-c="34" d="M462 0Q444 3 333 3Q217 3 199 0H190V46H221Q241 46 248 46T265 48T279 53T286 61Q287 63 287 115V165H28V211L179 442Q332 674 334 675Q336 677 355 677H373L379 671V211H471V165H379V114Q379 73 379 66T385 54Q393 47 442 46H471V0H462ZM293 211V545L74 212L183 211H293Z"></path><path data-c="30" d="M96 585Q152 666 249 666Q297 666 345 640T423 548Q460 465 460 320Q460 165 417 83Q397 41 362 16T301 -15T250 -22Q224 -22 198 -16T137 16T82 83Q39 165 39 320Q39 494 96 585ZM321 597Q291 629 250 629Q208 629 178 597Q153 571 145 525T137 333Q137 175 145 125T181 46Q209 16 250 16Q290 16 318 46Q347 76 354 130T362 333Q362 478 354 524T321 597Z" transform="translate(500,0)"></path></g><g data-mml-node="mi" transform="translate(1000,0)"><path data-c="1D448" d="M107 637Q73 637 71 641Q70 643 70 649Q70 673 81 682Q83 683 98 683Q139 681 234 681Q268 681 297 681T342 682T362 682Q378 682 378 672Q378 670 376 658Q371 641 366 638H364Q362 638 359 638T352 638T343 637T334 637Q295 636 284 634T266 623Q265 621 238 518T184 302T154 169Q152 155 152 140Q152 86 183 55T269 24Q336 24 403 69T501 205L552 406Q599 598 599 606Q599 633 535 637Q511 637 511 648Q511 650 513 660Q517 676 519 679T529 683Q532 683 561 682T645 680Q696 680 723 681T752 682Q767 682 767 672Q767 650 759 642Q756 637 737 637Q666 633 648 597Q646 592 598 404Q557 235 548 205Q515 105 433 42T263 -22Q171 -22 116 34T60 167V183Q60 201 115 421Q164 622 164 628Q164 635 107 637Z"></path></g><g data-mml-node="TeXAtom" data-mjx-texclass="ORD" transform="translate(1767,0)"><g data-mml-node="mo"><path data-c="2F" d="M423 750Q432 750 438 744T444 730Q444 725 271 248T92 -240Q85 -250 75 -250Q68 -250 62 -245T56 -231Q56 -221 230 257T407 740Q411 750 423 750Z"></path></g></g><g data-mml-node="mi" transform="translate(2267,0)"><path data-c="1D43F" d="M228 637Q194 637 192 641Q191 643 191 649Q191 673 202 682Q204 683 217 683Q271 680 344 680Q485 680 506 683H518Q524 677 524 674T522 656Q517 641 513 637H475Q406 636 394 628Q387 624 380 600T313 336Q297 271 279 198T252 88L243 52Q243 48 252 48T311 46H328Q360 46 379 47T428 54T478 72T522 106T564 161Q580 191 594 228T611 270Q616 273 628 273H641Q647 264 647 262T627 203T583 83T557 9Q555 4 553 3T537 0T494 -1Q483 -1 418 -1T294 0H116Q32 0 32 10Q32 17 34 24Q39 43 44 45Q48 46 59 46H65Q92 46 125 49Q139 52 144 61Q147 65 216 339T285 628Q285 635 228 637Z"></path></g></g></g></svg></mjx-container></li>
<li>异常情况:
<ul>
<li>升高: 可见于肝胆疾病, 急慢性肝炎, 胆石症引起的胆道梗阻等,某些药物造成的肝脏损伤也可引起</li>
</ul>
</li>
</ul>
</li>
<li>
<p>谷草转氨酶 (AST)</p>
<ul>
<li>正常情况: <mjx-container class="MathJax" jax="SVG"><svg style="vertical-align: -0.566ex;" xmlns="http://www.w3.org/2000/svg" width="6.67ex" height="2.262ex" role="img" focusable="false" viewBox="0 -750 2948 1000"><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="scale(1,-1)"><g data-mml-node="math"><g data-mml-node="mn"><path data-c="34" d="M462 0Q444 3 333 3Q217 3 199 0H190V46H221Q241 46 248 46T265 48T279 53T286 61Q287 63 287 115V165H28V211L179 442Q332 674 334 675Q336 677 355 677H373L379 671V211H471V165H379V114Q379 73 379 66T385 54Q393 47 442 46H471V0H462ZM293 211V545L74 212L183 211H293Z"></path><path data-c="30" d="M96 585Q152 666 249 666Q297 666 345 640T423 548Q460 465 460 320Q460 165 417 83Q397 41 362 16T301 -15T250 -22Q224 -22 198 -16T137 16T82 83Q39 165 39 320Q39 494 96 585ZM321 597Q291 629 250 629Q208 629 178 597Q153 571 145 525T137 333Q137 175 145 125T181 46Q209 16 250 16Q290 16 318 46Q347 76 354 130T362 333Q362 478 354 524T321 597Z" transform="translate(500,0)"></path></g><g data-mml-node="mi" transform="translate(1000,0)"><path data-c="1D448" d="M107 637Q73 637 71 641Q70 643 70 649Q70 673 81 682Q83 683 98 683Q139 681 234 681Q268 681 297 681T342 682T362 682Q378 682 378 672Q378 670 376 658Q371 641 366 638H364Q362 638 359 638T352 638T343 637T334 637Q295 636 284 634T266 623Q265 621 238 518T184 302T154 169Q152 155 152 140Q152 86 183 55T269 24Q336 24 403 69T501 205L552 406Q599 598 599 606Q599 633 535 637Q511 637 511 648Q511 650 513 660Q517 676 519 679T529 683Q532 683 561 682T645 680Q696 680 723 681T752 682Q767 682 767 672Q767 650 759 642Q756 637 737 637Q666 633 648 597Q646 592 598 404Q557 235 548 205Q515 105 433 42T263 -22Q171 -22 116 34T60 167V183Q60 201 115 421Q164 622 164 628Q164 635 107 637Z"></path></g><g data-mml-node="TeXAtom" data-mjx-texclass="ORD" transform="translate(1767,0)"><g data-mml-node="mo"><path data-c="2F" d="M423 750Q432 750 438 744T444 730Q444 725 271 248T92 -240Q85 -250 75 -250Q68 -250 62 -245T56 -231Q56 -221 230 257T407 740Q411 750 423 750Z"></path></g></g><g data-mml-node="mi" transform="translate(2267,0)"><path data-c="1D43F" d="M228 637Q194 637 192 641Q191 643 191 649Q191 673 202 682Q204 683 217 683Q271 680 344 680Q485 680 506 683H518Q524 677 524 674T522 656Q517 641 513 637H475Q406 636 394 628Q387 624 380 600T313 336Q297 271 279 198T252 88L243 52Q243 48 252 48T311 46H328Q360 46 379 47T428 54T478 72T522 106T564 161Q580 191 594 228T611 270Q616 273 628 273H641Q647 264 647 262T627 203T583 83T557 9Q555 4 553 3T537 0T494 -1Q483 -1 418 -1T294 0H116Q32 0 32 10Q32 17 34 24Q39 43 44 45Q48 46 59 46H65Q92 46 125 49Q139 52 144 61Q147 65 216 339T285 628Q285 635 228 637Z"></path></g></g></g></svg></mjx-container></li>
<li>异常情况:
<ul>
<li>升高: 可见于急慢性肝炎等肝脏疾病以及某些药物的毒副作用, 与ALT主要提示肝脏疾病不同,AST还可以存在于心肌,骨骼肌,肾脏,所以AST升高还可见于心肌梗死,心肌炎,肌溶解等疾病。</li>
</ul>
</li>
</ul>
</li>
<li>
<p>血清胆红素</p>
<ul>
<li>正常情况:
<ul>
<li>总胆红素 (TBLL): <mjx-container class="MathJax" jax="SVG"><svg style="vertical-align: -0.566ex;" xmlns="http://www.w3.org/2000/svg" width="16.971ex" height="2.262ex" role="img" focusable="false" viewBox="0 -750 7501 1000"><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="scale(1,-1)"><g data-mml-node="math"><g data-mml-node="mn"><path data-c="32" d="M109 429Q82 429 66 447T50 491Q50 562 103 614T235 666Q326 666 387 610T449 465Q449 422 429 383T381 315T301 241Q265 210 201 149L142 93L218 92Q375 92 385 97Q392 99 409 186V189H449V186Q448 183 436 95T421 3V0H50V19V31Q50 38 56 46T86 81Q115 113 136 137Q145 147 170 174T204 211T233 244T261 278T284 308T305 340T320 369T333 401T340 431T343 464Q343 527 309 573T212 619Q179 619 154 602T119 569T109 550Q109 549 114 549Q132 549 151 535T170 489Q170 464 154 447T109 429Z"></path></g><g data-mml-node="mo" transform="translate(722.2,0)"><path data-c="2212" d="M84 237T84 250T98 270H679Q694 262 694 250T679 230H98Q84 237 84 250Z"></path></g><g data-mml-node="mn" transform="translate(1722.4,0)"><path data-c="31" d="M213 578L200 573Q186 568 160 563T102 556H83V602H102Q149 604 189 617T245 641T273 663Q275 666 285 666Q294 666 302 660V361L303 61Q310 54 315 52T339 48T401 46H427V0H416Q395 3 257 3Q121 3 100 0H88V46H114Q136 46 152 46T177 47T193 50T201 52T207 57T213 61V578Z"></path><path data-c="39" d="M352 287Q304 211 232 211Q154 211 104 270T44 396Q42 412 42 436V444Q42 537 111 606Q171 666 243 666Q245 666 249 666T257 665H261Q273 665 286 663T323 651T370 619T413 560Q456 472 456 334Q456 194 396 97Q361 41 312 10T208 -22Q147 -22 108 7T68 93T121 149Q143 149 158 135T173 96Q173 78 164 65T148 49T135 44L131 43Q131 41 138 37T164 27T206 22H212Q272 22 313 86Q352 142 352 280V287ZM244 248Q292 248 321 297T351 430Q351 508 343 542Q341 552 337 562T323 588T293 615T246 625Q208 625 181 598Q160 576 154 546T147 441Q147 358 152 329T172 282Q197 248 244 248Z" transform="translate(500,0)"></path></g><g data-mml-node="mi" transform="translate(2722.4,0)"><path data-c="1D707" d="M58 -216Q44 -216 34 -208T23 -186Q23 -176 96 116T173 414Q186 442 219 442Q231 441 239 435T249 423T251 413Q251 401 220 279T187 142Q185 131 185 107V99Q185 26 252 26Q261 26 270 27T287 31T302 38T315 45T327 55T338 65T348 77T356 88T365 100L372 110L408 253Q444 395 448 404Q461 431 491 431Q504 431 512 424T523 412T525 402L449 84Q448 79 448 68Q448 43 455 35T476 26Q485 27 496 35Q517 55 537 131Q543 151 547 152Q549 153 557 153H561Q580 153 580 144Q580 138 575 117T555 63T523 13Q510 0 491 -8Q483 -10 467 -10Q446 -10 429 -4T402 11T385 29T376 44T374 51L368 45Q362 39 350 30T324 12T288 -4T246 -11Q199 -11 153 12L129 -85Q108 -167 104 -180T92 -202Q76 -216 58 -216Z"></path></g><g data-mml-node="mo" transform="translate(3603.2,0)"><path data-c="3E" d="M84 520Q84 528 88 533T96 539L99 540Q106 540 253 471T544 334L687 265Q694 260 694 250T687 235Q685 233 395 96L107 -40H101Q83 -38 83 -20Q83 -19 83 -17Q82 -10 98 -1Q117 9 248 71Q326 108 378 132L626 250L378 368Q90 504 86 509Q84 513 84 520Z"></path></g><g data-mml-node="mi" transform="translate(4659,0)"><path data-c="1D45A" d="M21 287Q22 293 24 303T36 341T56 388T88 425T132 442T175 435T205 417T221 395T229 376L231 369Q231 367 232 367L243 378Q303 442 384 442Q401 442 415 440T441 433T460 423T475 411T485 398T493 385T497 373T500 364T502 357L510 367Q573 442 659 442Q713 442 746 415T780 336Q780 285 742 178T704 50Q705 36 709 31T724 26Q752 26 776 56T815 138Q818 149 821 151T837 153Q857 153 857 145Q857 144 853 130Q845 101 831 73T785 17T716 -10Q669 -10 648 17T627 73Q627 92 663 193T700 345Q700 404 656 404H651Q565 404 506 303L499 291L466 157Q433 26 428 16Q415 -11 385 -11Q372 -11 364 -4T353 8T350 18Q350 29 384 161L420 307Q423 322 423 345Q423 404 379 404H374Q288 404 229 303L222 291L189 157Q156 26 151 16Q138 -11 108 -11Q95 -11 87 -5T76 7T74 17Q74 30 112 181Q151 335 151 342Q154 357 154 369Q154 405 129 405Q107 405 92 377T69 316T57 280Q55 278 41 278H27Q21 284 21 287Z"></path></g><g data-mml-node="mi" transform="translate(5537,0)"><path data-c="1D45C" d="M201 -11Q126 -11 80 38T34 156Q34 221 64 279T146 380Q222 441 301 441Q333 441 341 440Q354 437 367 433T402 417T438 387T464 338T476 268Q476 161 390 75T201 -11ZM121 120Q121 70 147 48T206 26Q250 26 289 58T351 142Q360 163 374 216T388 308Q388 352 370 375Q346 405 306 405Q243 405 195 347Q158 303 140 230T121 120Z"></path></g><g data-mml-node="mi" transform="translate(6022,0)"><path data-c="1D459" d="M117 59Q117 26 142 26Q179 26 205 131Q211 151 215 152Q217 153 225 153H229Q238 153 241 153T246 151T248 144Q247 138 245 128T234 90T214 43T183 6T137 -11Q101 -11 70 11T38 85Q38 97 39 102L104 360Q167 615 167 623Q167 626 166 628T162 632T157 634T149 635T141 636T132 637T122 637Q112 637 109 637T101 638T95 641T94 647Q94 649 96 661Q101 680 107 682T179 688Q194 689 213 690T243 693T254 694Q266 694 266 686Q266 675 193 386T118 83Q118 81 118 75T117 65V59Z"></path></g><g data-mml-node="TeXAtom" data-mjx-texclass="ORD" transform="translate(6320,0)"><g data-mml-node="mo"><path data-c="2F" d="M423 750Q432 750 438 744T444 730Q444 725 271 248T92 -240Q85 -250 75 -250Q68 -250 62 -245T56 -231Q56 -221 230 257T407 740Q411 750 423 750Z"></path></g></g><g data-mml-node="mi" transform="translate(6820,0)"><path data-c="1D43F" d="M228 637Q194 637 192 641Q191 643 191 649Q191 673 202 682Q204 683 217 683Q271 680 344 680Q485 680 506 683H518Q524 677 524 674T522 656Q517 641 513 637H475Q406 636 394 628Q387 624 380 600T313 336Q297 271 279 198T252 88L243 52Q243 48 252 48T311 46H328Q360 46 379 47T428 54T478 72T522 106T564 161Q580 191 594 228T611 270Q616 273 628 273H641Q647 264 647 262T627 203T583 83T557 9Q555 4 553 3T537 0T494 -1Q483 -1 418 -1T294 0H116Q32 0 32 10Q32 17 34 24Q39 43 44 45Q48 46 59 46H65Q92 46 125 49Q139 52 144 61Q147 65 216 339T285 628Q285 635 228 637Z"></path></g></g></g></svg></mjx-container></li>
<li>直接胆红素 (DBIL): <mjx-container class="MathJax" jax="SVG"><svg style="vertical-align: -0.566ex;" xmlns="http://www.w3.org/2000/svg" width="19.353ex" height="2.262ex" role="img" focusable="false" viewBox="0 -750 8554 1000"><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="scale(1,-1)"><g data-mml-node="math"><g data-mml-node="mn"><path data-c="30" d="M96 585Q152 666 249 666Q297 666 345 640T423 548Q460 465 460 320Q460 165 417 83Q397 41 362 16T301 -15T250 -22Q224 -22 198 -16T137 16T82 83Q39 165 39 320Q39 494 96 585ZM321 597Q291 629 250 629Q208 629 178 597Q153 571 145 525T137 333Q137 175 145 125T181 46Q209 16 250 16Q290 16 318 46Q347 76 354 130T362 333Q362 478 354 524T321 597Z"></path></g><g data-mml-node="mo" transform="translate(722.2,0)"><path data-c="2212" d="M84 237T84 250T98 270H679Q694 262 694 250T679 230H98Q84 237 84 250Z"></path></g><g data-mml-node="mn" transform="translate(1722.4,0)"><path data-c="36" d="M42 313Q42 476 123 571T303 666Q372 666 402 630T432 550Q432 525 418 510T379 495Q356 495 341 509T326 548Q326 592 373 601Q351 623 311 626Q240 626 194 566Q147 500 147 364L148 360Q153 366 156 373Q197 433 263 433H267Q313 433 348 414Q372 400 396 374T435 317Q456 268 456 210V192Q456 169 451 149Q440 90 387 34T253 -22Q225 -22 199 -14T143 16T92 75T56 172T42 313ZM257 397Q227 397 205 380T171 335T154 278T148 216Q148 133 160 97T198 39Q222 21 251 21Q302 21 329 59Q342 77 347 104T352 209Q352 289 347 316T329 361Q302 397 257 397Z"></path><path data-c="2E" d="M78 60Q78 84 95 102T138 120Q162 120 180 104T199 61Q199 36 182 18T139 0T96 17T78 60Z" transform="translate(500,0)"></path><path data-c="38" d="M70 417T70 494T124 618T248 666Q319 666 374 624T429 515Q429 485 418 459T392 417T361 389T335 371T324 363L338 354Q352 344 366 334T382 323Q457 264 457 174Q457 95 399 37T249 -22Q159 -22 101 29T43 155Q43 263 172 335L154 348Q133 361 127 368Q70 417 70 494ZM286 386L292 390Q298 394 301 396T311 403T323 413T334 425T345 438T355 454T364 471T369 491T371 513Q371 556 342 586T275 624Q268 625 242 625Q201 625 165 599T128 534Q128 511 141 492T167 463T217 431Q224 426 228 424L286 386ZM250 21Q308 21 350 55T392 137Q392 154 387 169T375 194T353 216T330 234T301 253T274 270Q260 279 244 289T218 306L210 311Q204 311 181 294T133 239T107 157Q107 98 150 60T250 21Z" transform="translate(778,0)"></path><path data-c="34" d="M462 0Q444 3 333 3Q217 3 199 0H190V46H221Q241 46 248 46T265 48T279 53T286 61Q287 63 287 115V165H28V211L179 442Q332 674 334 675Q336 677 355 677H373L379 671V211H471V165H379V114Q379 73 379 66T385 54Q393 47 442 46H471V0H462ZM293 211V545L74 212L183 211H293Z" transform="translate(1278,0)"></path></g><g data-mml-node="mo" transform="translate(3778.2,0)"><path data-c="3E" d="M84 520Q84 528 88 533T96 539L99 540Q106 540 253 471T544 334L687 265Q694 260 694 250T687 235Q685 233 395 96L107 -40H101Q83 -38 83 -20Q83 -19 83 -17Q82 -10 98 -1Q117 9 248 71Q326 108 378 132L626 250L378 368Q90 504 86 509Q84 513 84 520Z"></path></g><g data-mml-node="mi" transform="translate(4834,0)"><path data-c="1D45A" d="M21 287Q22 293 24 303T36 341T56 388T88 425T132 442T175 435T205 417T221 395T229 376L231 369Q231 367 232 367L243 378Q303 442 384 442Q401 442 415 440T441 433T460 423T475 411T485 398T493 385T497 373T500 364T502 357L510 367Q573 442 659 442Q713 442 746 415T780 336Q780 285 742 178T704 50Q705 36 709 31T724 26Q752 26 776 56T815 138Q818 149 821 151T837 153Q857 153 857 145Q857 144 853 130Q845 101 831 73T785 17T716 -10Q669 -10 648 17T627 73Q627 92 663 193T700 345Q700 404 656 404H651Q565 404 506 303L499 291L466 157Q433 26 428 16Q415 -11 385 -11Q372 -11 364 -4T353 8T350 18Q350 29 384 161L420 307Q423 322 423 345Q423 404 379 404H374Q288 404 229 303L222 291L189 157Q156 26 151 16Q138 -11 108 -11Q95 -11 87 -5T76 7T74 17Q74 30 112 181Q151 335 151 342Q154 357 154 369Q154 405 129 405Q107 405 92 377T69 316T57 280Q55 278 41 278H27Q21 284 21 287Z"></path></g><g data-mml-node="mi" transform="translate(5712,0)"><path data-c="1D45A" d="M21 287Q22 293 24 303T36 341T56 388T88 425T132 442T175 435T205 417T221 395T229 376L231 369Q231 367 232 367L243 378Q303 442 384 442Q401 442 415 440T441 433T460 423T475 411T485 398T493 385T497 373T500 364T502 357L510 367Q573 442 659 442Q713 442 746 415T780 336Q780 285 742 178T704 50Q705 36 709 31T724 26Q752 26 776 56T815 138Q818 149 821 151T837 153Q857 153 857 145Q857 144 853 130Q845 101 831 73T785 17T716 -10Q669 -10 648 17T627 73Q627 92 663 193T700 345Q700 404 656 404H651Q565 404 506 303L499 291L466 157Q433 26 428 16Q415 -11 385 -11Q372 -11 364 -4T353 8T350 18Q350 29 384 161L420 307Q423 322 423 345Q423 404 379 404H374Q288 404 229 303L222 291L189 157Q156 26 151 16Q138 -11 108 -11Q95 -11 87 -5T76 7T74 17Q74 30 112 181Q151 335 151 342Q154 357 154 369Q154 405 129 405Q107 405 92 377T69 316T57 280Q55 278 41 278H27Q21 284 21 287Z"></path></g><g data-mml-node="mi" transform="translate(6590,0)"><path data-c="1D45C" d="M201 -11Q126 -11 80 38T34 156Q34 221 64 279T146 380Q222 441 301 441Q333 441 341 440Q354 437 367 433T402 417T438 387T464 338T476 268Q476 161 390 75T201 -11ZM121 120Q121 70 147 48T206 26Q250 26 289 58T351 142Q360 163 374 216T388 308Q388 352 370 375Q346 405 306 405Q243 405 195 347Q158 303 140 230T121 120Z"></path></g><g data-mml-node="mi" transform="translate(7075,0)"><path data-c="1D459" d="M117 59Q117 26 142 26Q179 26 205 131Q211 151 215 152Q217 153 225 153H229Q238 153 241 153T246 151T248 144Q247 138 245 128T234 90T214 43T183 6T137 -11Q101 -11 70 11T38 85Q38 97 39 102L104 360Q167 615 167 623Q167 626 166 628T162 632T157 634T149 635T141 636T132 637T122 637Q112 637 109 637T101 638T95 641T94 647Q94 649 96 661Q101 680 107 682T179 688Q194 689 213 690T243 693T254 694Q266 694 266 686Q266 675 193 386T118 83Q118 81 118 75T117 65V59Z"></path></g><g data-mml-node="TeXAtom" data-mjx-texclass="ORD" transform="translate(7373,0)"><g data-mml-node="mo"><path data-c="2F" d="M423 750Q432 750 438 744T444 730Q444 725 271 248T92 -240Q85 -250 75 -250Q68 -250 62 -245T56 -231Q56 -221 230 257T407 740Q411 750 423 750Z"></path></g></g><g data-mml-node="mi" transform="translate(7873,0)"><path data-c="1D43F" d="M228 637Q194 637 192 641Q191 643 191 649Q191 673 202 682Q204 683 217 683Q271 680 344 680Q485 680 506 683H518Q524 677 524 674T522 656Q517 641 513 637H475Q406 636 394 628Q387 624 380 600T313 336Q297 271 279 198T252 88L243 52Q243 48 252 48T311 46H328Q360 46 379 47T428 54T478 72T522 106T564 161Q580 191 594 228T611 270Q616 273 628 273H641Q647 264 647 262T627 203T583 83T557 9Q555 4 553 3T537 0T494 -1Q483 -1 418 -1T294 0H116Q32 0 32 10Q32 17 34 24Q39 43 44 45Q48 46 59 46H65Q92 46 125 49Q139 52 144 61Q147 65 216 339T285 628Q285 635 228 637Z"></path></g></g></g></svg></mjx-container></li>
</ul>
</li>
<li>异常情况:
<ul>
<li>升高: 如果伴随转氨酶升高,多见于黄疸型肝炎,或其他疾病造成的黄疸症状。如果没有伴随转氨酶升高,那么可能是先天性胆红素代谢异常或慢性溶血。</li>
</ul>
</li>
</ul>
</li>
</ul>
]]></content>
<tags>
<tag>Medicine</tag>
<tag>Life</tag>
</tags>
</entry>
<entry>
<title>肩周炎和肩袖损伤的区别</title>
<url>/2024/09/15/%E8%82%A9%E5%91%A8%E7%82%8E%E5%92%8C%E8%82%A9%E8%A2%96%E6%8D%9F%E4%BC%A4%E7%9A%84%E5%8C%BA%E5%88%AB/</url>
<content><![CDATA[<p>在肩痛患者中,只有2%~5%是肩周炎,肩袖损伤更为常见,占30%~40%。</p>
<p>肩周炎和肩袖损伤都是临床上常见的疾病。很多肩痛患者认为自己患有肩周炎,坚持做肩关节活动训练,但一直没有缓解,且逐渐加重,甚至影响了睡眠和生活,遂至医院就诊,经过影像学检查确诊为肩袖损伤。这是因为肩周炎和肩袖损伤均可引起肩痛,严重时均可出现夜间疼痛,甚至影响睡眠,症状相似,故极易混淆。</p>
<p>肩周炎是肩关节冻结、活动受限,遇风、寒冷加重,是肩周软组织(包括肩周肌、肌腱、滑囊和关节囊等)病变引起的以肩关节疼痛和功能障碍为特征的疾病。</p>
<p>肩袖损伤是由退行性病变或外力等导致肩袖的4块肌肉、肌腱发生病变,进而导致肩关节局部疼痛、活动受限的疾病。</p>
<p>许多肩袖损伤患者无明确的外伤史,而是由长期做过顶运动、提重物或上肢长期固定于一个姿势引起的。</p>
<ul>
<li>
<p>肩周炎的疼痛范围广,涉及整个肩关节,肩袖损伤引起的疼痛多出现在肩前方、外上方</p>
</li>
<li>
<p>肩周炎的压痛点多而广,肩袖损的伤压痛点多出现在肩前方、上方及肩胛骨外侧缘。</p>
</li>
<li>
<p>肩周炎患者对气候变化较为敏感,肩袖损伤患者对气候变化不敏感,对劳累、提重物及做过顶运动较为敏感。</p>
</li>
<li>
<p>肩周炎患者活动受限范围广,肩袖损伤患者多以肩关节外展活动受限为主,同时伴有外展无力。</p>
</li>
<li>
<p>肩周炎患者进行上举过顶运动训练后活动范围会好转,症状会减轻;而肩袖损伤患者进行上举过顶运动训练后疼痛加重。</p>
</li>
</ul>
]]></content>
<tags>
<tag>Medicine</tag>
<tag>Life</tag>
</tags>
</entry>
<entry>
<title>肺功能检查数值</title>
<url>/2025/03/20/%E8%82%BA%E5%8A%9F%E8%83%BD%E6%A3%80%E6%9F%A5%E6%95%B0%E5%80%BC/</url>
<content><![CDATA[<p>肺功能测试是评估患者呼吸系统健康的重要工具。各个指标的及其异常数值可能指示的潜在生理疾病:</p>
<h3 id="1-FEV1(第一秒用力呼气量)"><a class="header-anchor" href="#1-FEV1(第一秒用力呼气量)">¶</a>1. FEV1(第一秒用力呼气量)</h3>
<ul>
<li>评估:FEV1用于评估气道的通畅程度。它反映了在用力呼气的第一秒内,患者能够排出的气体量。</li>
<li>异常:FEV1降低通常提示气道阻塞,常见于慢性阻塞性肺病(COPD)、哮喘、支气管炎等疾病。</li>
</ul>
<h3 id="2-FVC(用力肺活量)"><a class="header-anchor" href="#2-FVC(用力肺活量)">¶</a>2. FVC(用力肺活量)</h3>
<ul>
<li>评估:FVC测量的是患者在一次用力呼气中能够排出的最大气体量,反映了肺的容量和扩张能力。</li>
<li>异常:FVC降低可能指示限制性肺病,如肺纤维化、胸廓畸形或神经肌肉疾病等。</li>
</ul>
<h3 id="3-FEV1-FVC比值"><a class="header-anchor" href="#3-FEV1-FVC比值">¶</a>3. FEV1/FVC比值</h3>
<ul>
<li>评估:FEV1/FVC比值用于区分阻塞性和限制性肺病。正常情况下,该比值应大于70%。</li>
<li>异常:
<ul>
<li>低于70%:提示气道阻塞,常见于COPD和哮喘。</li>
<li>正常或高于70%但FVC降低:可能提示限制性肺病。</li>
</ul>
</li>
</ul>
<h3 id="4-PEF(峰值呼气流量)"><a class="header-anchor" href="#4-PEF(峰值呼气流量)">¶</a>4. PEF(峰值呼气流量)</h3>
<ul>
<li>评估:PEF测量患者在用力呼气时达到的最大流速,常用于监测哮喘患者的病情变化。</li>
<li>异常:PEF降低可能提示气道狭窄或阻塞,常见于哮喘急性发作或COPD加重。</li>
</ul>
<h3 id="5-MVV(最大通气量)"><a class="header-anchor" href="#5-MVV(最大通气量)">¶</a>5. MVV(最大通气量)</h3>
<ul>
<li>评估:MVV测量在一定时间内(通常是12秒)能够进行的最大通气量,反映了肺部的通气能力和呼吸肌的力量。</li>
<li>异常:MVV降低可能与呼吸肌无力、气道阻塞或肺部疾病(如COPD)相关。</li>
</ul>
<h3 id="6-TLC(总肺容量)"><a class="header-anchor" href="#6-TLC(总肺容量)">¶</a>6. TLC(总肺容量)</h3>
<ul>
<li>评估:TLC测量肺部在最大吸气后所能容纳的气体总量,反映了肺的整体容量。</li>
<li>异常:
<ul>
<li>增加:可能与阻塞性肺病(如COPD)相关,因肺部过度膨胀。</li>
<li>降低:可能与限制性肺病(如肺纤维化、胸廓畸形)相关。</li>
</ul>
</li>
</ul>
<h3 id="7-RV(残气量)"><a class="header-anchor" href="#7-RV(残气量)">¶</a>7. RV(残气量)</h3>
<ul>
<li>评估:RV测量在最大呼气后,肺内仍然残留的气体量。</li>
<li>异常:
<ul>
<li>增加:常见于阻塞性肺病,因气道阻塞导致气体无法完全排出。</li>
<li>降低:可能与限制性肺病相关。</li>
</ul>
</li>
</ul>
]]></content>
<tags>
<tag>Medicine</tag>
</tags>
</entry>
<entry>
<title>肺炎支原体注意事项</title>
<url>/2023/10/25/%E8%82%BA%E7%82%8E%E6%94%AF%E5%8E%9F%E4%BD%93%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9/</url>
<content><![CDATA[<ol>
<li>什么是肺炎支原体
<ul>
<li>大小介于细菌和病毒之间的病原微生物, 缺乏细胞壁, 对作用于细胞壁的抗菌药物 (例如青霉素和头孢) 有耐药性</li>
<li>感染不分季节的, 不过一般是北方地区秋冬季流行, 南方地区夏秋季流行</li>
<li>一般多见于五岁以上的儿童, 但五岁以下也可能感染并发病</li>
</ul>
</li>
<li>肺炎支原体如何传播
<ul>
<li>主要通过飞沫和直接接触传播</li>
<li>潜伏期为一到三周</li>
<li>从潜伏期到症状缓解均有传染性</li>
</ul>
</li>
<li>肺炎支原体感染有何症状表现
<ul>
<li>主要临床表现为发热, 咳嗽, 头痛, 流涕, 咽痛, 耳痛等</li>
<li>轻者可能无症状或是上呼吸道感染</li>
<li>重者可能导致肺炎, 肺实变, 胸腔积液, 坏死性肺炎</li>
</ul>
</li>
<li>成年人是否也会感染肺炎支原体
<ul>
<li>长时间和肺炎支原体感染的患者相处很有可能会被感染</li>
<li>如果出现发热, 剧烈咳嗽, 甚至有肺部表现, 要及时去医院</li>
</ul>
</li>
<li>如何分辨肺炎支原体感染和普通感冒
<ul>
<li>初期感染上呼吸道时, 存在和普通感染类似的症状, 例如鼻塞咽痛发热等</li>
<li>可以通过血清抗体检测, 核酸检测等检出</li>
<li>医生可以通过根据患者的病史, 症状和体征结合相关的常规化检查来综合判断</li>
<li>常人可以通过判断咳嗽情况判断: 一般会出现阵发性, 成串的咳嗽, 早期是干咳, 咳嗽比较剧烈. 或有发热持续不退, 咳嗽加重等情况</li>
</ul>
</li>
<li>治疗肺炎支原体的常用药物
<ul>
<li>最佳的治疗窗口期为发热后五到十天之内</li>
<li>一线治疗药物以大环内酯类抗生素为主, 比如阿奇霉素, 红霉素, 克拉霉素</li>
<li>对于耐大环内酯类抗菌药物的患者要考虑其他药物</li>
<li>如果感染比较严重, 重症支原体感染, 则需要综合治疗, 比如糖皮质激素以及支气管镜介入治疗</li>
</ul>
</li>
<li>如何判断是否需要住院治疗
<ul>
<li>轻症经过大环内酯类药物一般均能控制病情, 但体温正常之后咳嗽可能还会持续两周左右, 胸片肺炎阴影基本均可吸收</li>
<li>重症需要住院, 重症可能会有混合病毒或者细菌感染, 要加用相应的药物治疗</li>
</ul>
</li>
<li>耐药和不耐药患者的治疗差异
<ul>
<li>并无太大差异, 主要是耐药患者退烧时间会更长, 换言之病程较长</li>
</ul>
</li>
<li>支原体肺炎是否会引发白肺
<ul>
<li>可能会, 但是跟双肺弥漫性病变的白肺是两个概念</li>
<li>通过拍背排痰, 药物祛痰, 支气管镜治疗等手段, 患者可以在较短的时间内痊愈, 基本不会引发生命危险</li>
<li>重症及难治性肺炎支原体感染患者可能会遗留肺结构或功能损害</li>
</ul>
</li>
</ol>
<h2 id="支原体肺炎-流感-新冠肺炎的区别"><a class="header-anchor" href="#支原体肺炎-流感-新冠肺炎的区别">¶</a>支原体肺炎, 流感, 新冠肺炎的区别</h2>
<table>
<thead>
<tr>
<th>区别</th>
<th>支原体肺炎</th>
<th>流感</th>
<th>新冠肺炎</th>
</tr>
</thead>
<tbody>
<tr>
<td>致病菌</td>
<td>肺炎支原体</td>
<td>流感病毒</td>
<td>新型冠状病毒</td>
</tr>
<tr>
<td>感染部位</td>
<td>肺部和上呼吸道</td>
<td>大部分也在上呼吸道</td>
<td>呼吸道和肠胃</td>
</tr>
<tr>
<td>易感人群</td>
<td>儿童及青少年</td>
<td>儿童和老年人</td>
<td>所有人</td>
</tr>
<tr>
<td>传染性</td>
<td>有, 通过飞沫传播</td>
<td>有, 通过飞沫, 气溶胶, 密接等途径传播</td>
<td>有, 主要通过飞沫和接触传播</td>
</tr>
<tr>
<td>高发季</td>
<td>秋冬</td>
<td>秋冬</td>
<td>全年</td>
</tr>
<tr>
<td>潜伏期</td>
<td>6~35天</td>
<td>1~7天</td>
<td>1~14天</td>
</tr>
<tr>
<td>症状</td>
<td>发热咳嗽为主, 伴随头痛流涕咽痛</td>
<td>常见高热畏寒, 头痛鼻塞, 肌肉关节酸痛</td>
<td>主要是咽干咽痛, 咳嗽发热, 鼻塞流涕, 乏力</td>
</tr>
<tr>
<td>病情进展</td>
<td>良性发展, 发热可能持续1~3周, 咳嗽症状长达6周</td>
<td>有一定自限性, 一般一周内好转</td>
<td>一般7~14天可好转</td>
</tr>
</tbody>
</table>
]]></content>
<tags>
<tag>Medicine</tag>
<tag>Life</tag>
</tags>
</entry>
<entry>
<title>藿香正气水和藿香正气口服液的区别</title>
<url>/2024/09/01/%E8%97%BF%E9%A6%99%E6%AD%A3%E6%B0%94%E6%B0%B4%E5%92%8C%E8%97%BF%E9%A6%99%E6%AD%A3%E6%B0%94%E5%8F%A3%E6%9C%8D%E6%B6%B2%E7%9A%84%E5%8C%BA%E5%88%AB/</url>
<content><![CDATA[<p>虽然都是口服液体制剂,且看起来单支容量都一样,但是藿香正气水里含有 40%-50% 的乙醇,而藿香正气口服液不含有乙醇。同时藿香正气水不能和头孢一起服用(因为含有酒精),而藿香正气口服液可以和头孢一起服用。在选用之前,还希望大家能仔细看药品名称。</p>
<p>正气水是酊剂,用的是生半夏,口服液用的是法半夏,比正气水多了炒的白术,生姜和大枣,两者的功效都是解表化湿,理气和中。藿香正气水由水煮及酒浸制而成,疗效最明显,但口感较差。由于霍香正气水药效比较峻猛,小儿和年老体虚者服用时应有医生指导。藿香正气口服液是藿香正气的换代产品。</p>
]]></content>
<tags>
<tag>Medicine</tag>
<tag>Life</tag>
</tags>
</entry>
<entry>
<title>螺内酯片</title>
<url>/2023/03/14/%E8%9E%BA%E5%86%85%E9%85%AF%E7%89%87/</url>
<content><![CDATA[<h2 id="简介"><a class="header-anchor" href="#简介">¶</a>简介</h2>
<p>螺内酯是保钾利尿药,用于治疗心衰、低钾血症和高血压等。但是,它也会阻断雄激素受体,从而具有抗雄激素特性(我想你应该联想到了什么);不过其效力较弱,需服用较大剂量方可充分起效(尿频等副作用也更明显)。</p>
<p>英文名称:Spironolactone Tablets.</p>
<p>需要注意的是,开始服用螺内酯前:</p>
<ol>
<li>血清钾应小于 5.0mmol/L</li>
<li>肾功能应正常,服用螺内酯后定期监测血钾和肌酐</li>
<li>避免过多食用富钾食品,如香蕉、海带、含钾代用盐等</li>
<li>不得同时服用其它利钾药物; 多喝水。</li>
</ol>
<p>对于利尿方面,螺内酯的利尿作用不强,起效慢而维时久,其利尿作用与体内醛固酮的浓度有关。仅当体内有醛固酮存在时,它才发挥作用。对切除肾上腺的动物则无利尿作用。由于其利尿作用较弱,抑制Na+再吸收量还不到3%,因此较少单用。常与噻嗪类利尿药或高效利尿药合用以增强利尿效果并减少K+的丧失。</p>
<p>如果病人出现了高钾血症,那应该立即停药。另外如果在进食的时候或者餐后用药,可以减少胃肠道反应(提高生物利用度)。</p>
<p><strong>本药起作用较慢,而维持时间较长,故首日剂量可增加至常规剂量的2-3倍,以后酌情调整剂量。与其他利尿药合用时,可先于其他利尿药2-3日服用。在已应用其他利尿药再加用本药时,其他利尿药剂量在最初2-3日可减量50%,以后酌情调整剂量。在停药时,本药应先于其他利尿药2-3日停药。</strong></p>
<h2 id="副作用"><a class="header-anchor" href="#副作用">¶</a>副作用:</h2>
<ol>
<li>高钾血症:螺内酯最严重的副作用,可导致住院甚至死亡。(高年龄(超 45 岁)、肾功能不全、与其它利钾药物合用、钾补剂的服用等)</li>
<li>尿频</li>
<li>血压过低</li>
<li>低钠血症</li>
<li>头晕</li>
<li>疲倦、困乏</li>
<li>干皮、皮疹</li>
<li>运动失调</li>
<li>皮质醇水平升高</li>
<li>胃肠道反应,包括恶心、呕吐、腹泻、胃痉挛、胃炎等</li>
</ol>
<h2 id="药理"><a class="header-anchor" href="#药理">¶</a>药理</h2>
<p>本药结构与醛固酮相似,为醛固酮的竞争性抑制剂。作用于远曲小管和集合管,阻断Na+-K+和Na+-H+交换,结果Na+、C1-和水排泄增多,K+、Mg2+和H+排泄减少,对Ca2+和P3-的作用不定。由于本药仅作用于远曲小管和集合管,对肾小管其他各段无作用,故利尿作用较弱。另外,本药对肾小管以外的醛固酮靶器官也有作用。</p>
<h2 id="代谢"><a class="header-anchor" href="#代谢">¶</a>代谢</h2>
<p>这个药口服吸收较好,生物利用度大于90%,血浆蛋白结合率在90%以上,进入体内后80%由肝脏迅速代谢为有活性的坎利酮(canrenone),口服1日左右起效,2-3日达高峰,停药后作用仍可维持2-3日。依服药方式不同T1/2有所差异,每日服药1~2次时平均19小时(13-24小时),每日服药4次时缩短为12.5小时(9-16小时)。无活性代谢产物从肾脏和胆道排泄,约有10%以原形从肾脏排泄。</p>
<h2 id="适应症"><a class="header-anchor" href="#适应症">¶</a>适应症</h2>
<ol>
<li>水肿性疾病 与其他利尿药合用,治疗充血性水肿、肝硬化腹水、肾性水肿等水肿性疾病,其目的在于纠正上述疾病时伴发的继发性醛固酮分泌增多,并对抗其他利尿药的排钾作用。也用于特发性水肿的治疗。</li>
<li>高血压 作为治疗高血压的辅助药物。</li>
<li>原发性醛固酮增多症 螺内酯可用于此病的诊断和治疗。</li>
<li>低钾血症的预防 与噻嗪类利尿药合用,增强利尿效应和预防低钾血症。</li>
</ol>
<h2 id="药物相互作用"><a class="header-anchor" href="#药物相互作用">¶</a>药物相互作用</h2>
<ol>
<li>肾上腺皮质激素尤其是具有较强盐皮质激素作用者,促肾上腺皮质激素能减弱本药的利尿作用,而拮抗本药的潴钾作用。</li>
<li>雌激素能引起水钠潴留,从而减弱本药的利尿作用。</li>
<li>非甾体类消炎镇痛药,尤其是吲哚美辛,能降低本药的利尿作用,且合用时肾毒性增加。</li>
<li>拟交感神经药物降低本药的降压作用。</li>
<li>多巴胺加强本药的利尿作用。</li>
<li>与引起血压下降的药物合用,利尿和降压效果均加强。</li>
<li>与下列药物合用时,发生高钾血症的机会增加,如含钾药物、库存血(含钾30mmol/L,如库存10日以上含钾高达65mmol/L)、血管紧张素转换酶抑制剂、血管紧张素Ⅱ受体拮抗剂和环孢素A等。</li>
<li>与葡萄糖胰岛素液、碱剂、钠型降钾交换树脂合用,发生高钾血症的机会减少。</li>
<li>本药使地高辛半衰期延长。</li>
<li>与氯化铵合用易发生代谢性酸中毒。</li>
<li>与肾毒性药物合用,肾毒性增加。</li>
<li>甘珀酸钠、甘草类制剂具有醛固酮样作用,可降低本药的利尿作用。</li>
</ol>
<h2 id="其他"><a class="header-anchor" href="#其他">¶</a>其他</h2>
<p>螺内酯为《2018版中国国家基本药物目录》在列药物,“基本药物”指的是能够满足基本医疗卫生需求,剂型适宜、保证供应、基层能够配备、国民能够公平获得的药品。</p>
<h2 id="参考"><a class="header-anchor" href="#参考">¶</a>参考</h2>
<ul>
<li><a href="https://mtf.wiki/zh-cn/docs/medicine/antiandrogen/spironolactone/">https://mtf.wiki/zh-cn/docs/medicine/antiandrogen/spironolactone/</a></li>
<li><a href="https://www.yixue.com/%E8%9E%BA%E5%86%85%E9%85%AF">https://www.yixue.com/螺内酯</a></li>
<li><a href="https://www.yixue.com/%E8%8D%AF%E7%90%86%E5%AD%A6/%E8%9E%BA%E5%86%85%E9%85%AF">https://www.yixue.com/药理学/螺内酯</a></li>
</ul>
]]></content>
<tags>
<tag>Medicine</tag>
</tags>
</entry>
<entry>
<title>血常规化验结果阅读指南</title>
<url>/2023/10/12/%E8%A1%80%E5%B8%B8%E8%A7%84%E5%8C%96%E9%AA%8C%E7%BB%93%E6%9E%9C%E9%98%85%E8%AF%BB%E6%8C%87%E5%8D%97/</url>
<content><![CDATA[<p>这里我给出一些常见的值,更详细的听医生说哦,我们以下图为例:</p>
<p><img src="/img/%E5%8C%BB%E5%AD%A6/%E8%A1%80%E5%B8%B8%E8%A7%84%E5%8C%96%E9%AA%8C%E5%8D%95Demo.jpeg" alt="血常规化验单Demo"></p>
<ul>
<li>红细胞 (RBC)
<ul>
<li>正常情况: 男性 <mjx-container class="MathJax" jax="SVG"><svg style="vertical-align: -0.566ex;" xmlns="http://www.w3.org/2000/svg" width="20.587ex" height="2.52ex" role="img" focusable="false" viewBox="0 -864 9099.6 1114"><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="scale(1,-1)"><g data-mml-node="math"><g data-mml-node="mo"><path data-c="28" d="M94 250Q94 319 104 381T127 488T164 576T202 643T244 695T277 729T302 750H315H319Q333 750 333 741Q333 738 316 720T275 667T226 581T184 443T167 250T184 58T225 -81T274 -167T316 -220T333 -241Q333 -250 318 -250H315H302L274 -226Q180 -141 137 -14T94 250Z"></path></g><g data-mml-node="mn" transform="translate(389,0)"><path data-c="34" d="M462 0Q444 3 333 3Q217 3 199 0H190V46H221Q241 46 248 46T265 48T279 53T286 61Q287 63 287 115V165H28V211L179 442Q332 674 334 675Q336 677 355 677H373L379 671V211H471V165H379V114Q379 73 379 66T385 54Q393 47 442 46H471V0H462ZM293 211V545L74 212L183 211H293Z"></path><path data-c="2E" d="M78 60Q78 84 95 102T138 120Q162 120 180 104T199 61Q199 36 182 18T139 0T96 17T78 60Z" transform="translate(500,0)"></path><path data-c="37" d="M55 458Q56 460 72 567L88 674Q88 676 108 676H128V672Q128 662 143 655T195 646T364 644H485V605L417 512Q408 500 387 472T360 435T339 403T319 367T305 330T292 284T284 230T278 162T275 80Q275 66 275 52T274 28V19Q270 2 255 -10T221 -22Q210 -22 200 -19T179 0T168 40Q168 198 265 368Q285 400 349 489L395 552H302Q128 552 119 546Q113 543 108 522T98 479L95 458V455H55V458Z" transform="translate(778,0)"></path><path data-c="35" d="M164 157Q164 133 148 117T109 101H102Q148 22 224 22Q294 22 326 82Q345 115 345 210Q345 313 318 349Q292 382 260 382H254Q176 382 136 314Q132 307 129 306T114 304Q97 304 95 310Q93 314 93 485V614Q93 664 98 664Q100 666 102 666Q103 666 123 658T178 642T253 634Q324 634 389 662Q397 666 402 666Q410 666 410 648V635Q328 538 205 538Q174 538 149 544L139 546V374Q158 388 169 396T205 412T256 420Q337 420 393 355T449 201Q449 109 385 44T229 -22Q148 -22 99 32T50 154Q50 178 61 192T84 210T107 214Q132 214 148 197T164 157Z" transform="translate(1278,0)"></path></g><g data-mml-node="mo" transform="translate(2389.2,0)"><path data-c="B1" d="M56 320T56 333T70 353H369V502Q369 651 371 655Q376 666 388 666Q402 666 405 654T409 596V500V353H707Q722 345 722 333Q722 320 707 313H409V40H707Q722 32 722 20T707 0H70Q56 7 56 20T70 40H369V313H70Q56 320 56 333Z"></path></g><g data-mml-node="mn" transform="translate(3389.4,0)"><path data-c="30" d="M96 585Q152 666 249 666Q297 666 345 640T423 548Q460 465 460 320Q460 165 417 83Q397 41 362 16T301 -15T250 -22Q224 -22 198 -16T137 16T82 83Q39 165 39 320Q39 494 96 585ZM321 597Q291 629 250 629Q208 629 178 597Q153 571 145 525T137 333Q137 175 145 125T181 46Q209 16 250 16Q290 16 318 46Q347 76 354 130T362 333Q362 478 354 524T321 597Z"></path><path data-c="2E" d="M78 60Q78 84 95 102T138 120Q162 120 180 104T199 61Q199 36 182 18T139 0T96 17T78 60Z" transform="translate(500,0)"></path><path data-c="37" d="M55 458Q56 460 72 567L88 674Q88 676 108 676H128V672Q128 662 143 655T195 646T364 644H485V605L417 512Q408 500 387 472T360 435T339 403T319 367T305 330T292 284T284 230T278 162T275 80Q275 66 275 52T274 28V19Q270 2 255 -10T221 -22Q210 -22 200 -19T179 0T168 40Q168 198 265 368Q285 400 349 489L395 552H302Q128 552 119 546Q113 543 108 522T98 479L95 458V455H55V458Z" transform="translate(778,0)"></path><path data-c="35" d="M164 157Q164 133 148 117T109 101H102Q148 22 224 22Q294 22 326 82Q345 115 345 210Q345 313 318 349Q292 382 260 382H254Q176 382 136 314Q132 307 129 306T114 304Q97 304 95 310Q93 314 93 485V614Q93 664 98 664Q100 666 102 666Q103 666 123 658T178 642T253 634Q324 634 389 662Q397 666 402 666Q410 666 410 648V635Q328 538 205 538Q174 538 149 544L139 546V374Q158 388 169 396T205 412T256 420Q337 420 393 355T449 201Q449 109 385 44T229 -22Q148 -22 99 32T50 154Q50 178 61 192T84 210T107 214Q132 214 148 197T164 157Z" transform="translate(1278,0)"></path></g><g data-mml-node="mo" transform="translate(5167.4,0)"><path data-c="29" d="M60 749L64 750Q69 750 74 750H86L114 726Q208 641 251 514T294 250Q294 182 284 119T261 12T224 -76T186 -143T145 -194T113 -227T90 -246Q87 -249 86 -250H74Q66 -250 63 -250T58 -247T55 -238Q56 -237 66 -225Q221 -64 221 250T66 725Q56 737 55 738Q55 746 60 749Z"></path></g><g data-mml-node="mi" transform="translate(5556.4,0)"><path data-c="1D465" d="M52 289Q59 331 106 386T222 442Q257 442 286 424T329 379Q371 442 430 442Q467 442 494 420T522 361Q522 332 508 314T481 292T458 288Q439 288 427 299T415 328Q415 374 465 391Q454 404 425 404Q412 404 406 402Q368 386 350 336Q290 115 290 78Q290 50 306 38T341 26Q378 26 414 59T463 140Q466 150 469 151T485 153H489Q504 153 504 145Q504 144 502 134Q486 77 440 33T333 -11Q263 -11 227 52Q186 -10 133 -10H127Q78 -10 57 16T35 71Q35 103 54 123T99 143Q142 143 142 101Q142 81 130 66T107 46T94 41L91 40Q91 39 97 36T113 29T132 26Q168 26 194 71Q203 87 217 139T245 247T261 313Q266 340 266 352Q266 380 251 392T217 404Q177 404 142 372T93 290Q91 281 88 280T72 278H58Q52 284 52 289Z"></path></g><g data-mml-node="msup" transform="translate(6128.4,0)"><g data-mml-node="mn"><path data-c="31" d="M213 578L200 573Q186 568 160 563T102 556H83V602H102Q149 604 189 617T245 641T273 663Q275 666 285 666Q294 666 302 660V361L303 61Q310 54 315 52T339 48T401 46H427V0H416Q395 3 257 3Q121 3 100 0H88V46H114Q136 46 152 46T177 47T193 50T201 52T207 57T213 61V578Z"></path><path data-c="30" d="M96 585Q152 666 249 666Q297 666 345 640T423 548Q460 465 460 320Q460 165 417 83Q397 41 362 16T301 -15T250 -22Q224 -22 198 -16T137 16T82 83Q39 165 39 320Q39 494 96 585ZM321 597Q291 629 250 629Q208 629 178 597Q153 571 145 525T137 333Q137 175 145 125T181 46Q209 16 250 16Q290 16 318 46Q347 76 354 130T362 333Q362 478 354 524T321 597Z" transform="translate(500,0)"></path></g><g data-mml-node="TeXAtom" transform="translate(1033,393.1) scale(0.707)" data-mjx-texclass="ORD"><g data-mml-node="mn"><path data-c="31" d="M213 578L200 573Q186 568 160 563T102 556H83V602H102Q149 604 189 617T245 641T273 663Q275 666 285 666Q294 666 302 660V361L303 61Q310 54 315 52T339 48T401 46H427V0H416Q395 3 257 3Q121 3 100 0H88V46H114Q136 46 152 46T177 47T193 50T201 52T207 57T213 61V578Z"></path><path data-c="32" d="M109 429Q82 429 66 447T50 491Q50 562 103 614T235 666Q326 666 387 610T449 465Q449 422 429 383T381 315T301 241Q265 210 201 149L142 93L218 92Q375 92 385 97Q392 99 409 186V189H449V186Q448 183 436 95T421 3V0H50V19V31Q50 38 56 46T86 81Q115 113 136 137Q145 147 170 174T204 211T233 244T261 278T284 308T305 340T320 369T333 401T340 431T343 464Q343 527 309 573T212 619Q179 619 154 602T119 569T109 550Q109 549 114 549Q132 549 151 535T170 489Q170 464 154 447T109 429Z" transform="translate(500,0)"></path></g></g></g><g data-mml-node="TeXAtom" data-mjx-texclass="ORD" transform="translate(7918.6,0)"><g data-mml-node="mo"><path data-c="2F" d="M423 750Q432 750 438 744T444 730Q444 725 271 248T92 -240Q85 -250 75 -250Q68 -250 62 -245T56 -231Q56 -221 230 257T407 740Q411 750 423 750Z"></path></g></g><g data-mml-node="mi" transform="translate(8418.6,0)"><path data-c="1D43F" d="M228 637Q194 637 192 641Q191 643 191 649Q191 673 202 682Q204 683 217 683Q271 680 344 680Q485 680 506 683H518Q524 677 524 674T522 656Q517 641 513 637H475Q406 636 394 628Q387 624 380 600T313 336Q297 271 279 198T252 88L243 52Q243 48 252 48T311 46H328Q360 46 379 47T428 54T478 72T522 106T564 161Q580 191 594 228T611 270Q616 273 628 273H641Q647 264 647 262T627 203T583 83T557 9Q555 4 553 3T537 0T494 -1Q483 -1 418 -1T294 0H116Q32 0 32 10Q32 17 34 24Q39 43 44 45Q48 46 59 46H65Q92 46 125 49Q139 52 144 61Q147 65 216 339T285 628Q285 635 228 637Z"></path></g></g></g></svg></mjx-container> , 女性 <mjx-container class="MathJax" jax="SVG"><svg style="vertical-align: -0.566ex;" xmlns="http://www.w3.org/2000/svg" width="20.587ex" height="2.52ex" role="img" focusable="false" viewBox="0 -864 9099.6 1114"><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="scale(1,-1)"><g data-mml-node="math"><g data-mml-node="mo"><path data-c="28" d="M94 250Q94 319 104 381T127 488T164 576T202 643T244 695T277 729T302 750H315H319Q333 750 333 741Q333 738 316 720T275 667T226 581T184 443T167 250T184 58T225 -81T274 -167T316 -220T333 -241Q333 -250 318 -250H315H302L274 -226Q180 -141 137 -14T94 250Z"></path></g><g data-mml-node="mn" transform="translate(389,0)"><path data-c="34" d="M462 0Q444 3 333 3Q217 3 199 0H190V46H221Q241 46 248 46T265 48T279 53T286 61Q287 63 287 115V165H28V211L179 442Q332 674 334 675Q336 677 355 677H373L379 671V211H471V165H379V114Q379 73 379 66T385 54Q393 47 442 46H471V0H462ZM293 211V545L74 212L183 211H293Z"></path><path data-c="2E" d="M78 60Q78 84 95 102T138 120Q162 120 180 104T199 61Q199 36 182 18T139 0T96 17T78 60Z" transform="translate(500,0)"></path><path data-c="32" d="M109 429Q82 429 66 447T50 491Q50 562 103 614T235 666Q326 666 387 610T449 465Q449 422 429 383T381 315T301 241Q265 210 201 149L142 93L218 92Q375 92 385 97Q392 99 409 186V189H449V186Q448 183 436 95T421 3V0H50V19V31Q50 38 56 46T86 81Q115 113 136 137Q145 147 170 174T204 211T233 244T261 278T284 308T305 340T320 369T333 401T340 431T343 464Q343 527 309 573T212 619Q179 619 154 602T119 569T109 550Q109 549 114 549Q132 549 151 535T170 489Q170 464 154 447T109 429Z" transform="translate(778,0)"></path><path data-c="35" d="M164 157Q164 133 148 117T109 101H102Q148 22 224 22Q294 22 326 82Q345 115 345 210Q345 313 318 349Q292 382 260 382H254Q176 382 136 314Q132 307 129 306T114 304Q97 304 95 310Q93 314 93 485V614Q93 664 98 664Q100 666 102 666Q103 666 123 658T178 642T253 634Q324 634 389 662Q397 666 402 666Q410 666 410 648V635Q328 538 205 538Q174 538 149 544L139 546V374Q158 388 169 396T205 412T256 420Q337 420 393 355T449 201Q449 109 385 44T229 -22Q148 -22 99 32T50 154Q50 178 61 192T84 210T107 214Q132 214 148 197T164 157Z" transform="translate(1278,0)"></path></g><g data-mml-node="mo" transform="translate(2389.2,0)"><path data-c="B1" d="M56 320T56 333T70 353H369V502Q369 651 371 655Q376 666 388 666Q402 666 405 654T409 596V500V353H707Q722 345 722 333Q722 320 707 313H409V40H707Q722 32 722 20T707 0H70Q56 7 56 20T70 40H369V313H70Q56 320 56 333Z"></path></g><g data-mml-node="mn" transform="translate(3389.4,0)"><path data-c="30" d="M96 585Q152 666 249 666Q297 666 345 640T423 548Q460 465 460 320Q460 165 417 83Q397 41 362 16T301 -15T250 -22Q224 -22 198 -16T137 16T82 83Q39 165 39 320Q39 494 96 585ZM321 597Q291 629 250 629Q208 629 178 597Q153 571 145 525T137 333Q137 175 145 125T181 46Q209 16 250 16Q290 16 318 46Q347 76 354 130T362 333Q362 478 354 524T321 597Z"></path><path data-c="2E" d="M78 60Q78 84 95 102T138 120Q162 120 180 104T199 61Q199 36 182 18T139 0T96 17T78 60Z" transform="translate(500,0)"></path><path data-c="37" d="M55 458Q56 460 72 567L88 674Q88 676 108 676H128V672Q128 662 143 655T195 646T364 644H485V605L417 512Q408 500 387 472T360 435T339 403T319 367T305 330T292 284T284 230T278 162T275 80Q275 66 275 52T274 28V19Q270 2 255 -10T221 -22Q210 -22 200 -19T179 0T168 40Q168 198 265 368Q285 400 349 489L395 552H302Q128 552 119 546Q113 543 108 522T98 479L95 458V455H55V458Z" transform="translate(778,0)"></path><path data-c="35" d="M164 157Q164 133 148 117T109 101H102Q148 22 224 22Q294 22 326 82Q345 115 345 210Q345 313 318 349Q292 382 260 382H254Q176 382 136 314Q132 307 129 306T114 304Q97 304 95 310Q93 314 93 485V614Q93 664 98 664Q100 666 102 666Q103 666 123 658T178 642T253 634Q324 634 389 662Q397 666 402 666Q410 666 410 648V635Q328 538 205 538Q174 538 149 544L139 546V374Q158 388 169 396T205 412T256 420Q337 420 393 355T449 201Q449 109 385 44T229 -22Q148 -22 99 32T50 154Q50 178 61 192T84 210T107 214Q132 214 148 197T164 157Z" transform="translate(1278,0)"></path></g><g data-mml-node="mo" transform="translate(5167.4,0)"><path data-c="29" d="M60 749L64 750Q69 750 74 750H86L114 726Q208 641 251 514T294 250Q294 182 284 119T261 12T224 -76T186 -143T145 -194T113 -227T90 -246Q87 -249 86 -250H74Q66 -250 63 -250T58 -247T55 -238Q56 -237 66 -225Q221 -64 221 250T66 725Q56 737 55 738Q55 746 60 749Z"></path></g><g data-mml-node="mi" transform="translate(5556.4,0)"><path data-c="1D465" d="M52 289Q59 331 106 386T222 442Q257 442 286 424T329 379Q371 442 430 442Q467 442 494 420T522 361Q522 332 508 314T481 292T458 288Q439 288 427 299T415 328Q415 374 465 391Q454 404 425 404Q412 404 406 402Q368 386 350 336Q290 115 290 78Q290 50 306 38T341 26Q378 26 414 59T463 140Q466 150 469 151T485 153H489Q504 153 504 145Q504 144 502 134Q486 77 440 33T333 -11Q263 -11 227 52Q186 -10 133 -10H127Q78 -10 57 16T35 71Q35 103 54 123T99 143Q142 143 142 101Q142 81 130 66T107 46T94 41L91 40Q91 39 97 36T113 29T132 26Q168 26 194 71Q203 87 217 139T245 247T261 313Q266 340 266 352Q266 380 251 392T217 404Q177 404 142 372T93 290Q91 281 88 280T72 278H58Q52 284 52 289Z"></path></g><g data-mml-node="msup" transform="translate(6128.4,0)"><g data-mml-node="mn"><path data-c="31" d="M213 578L200 573Q186 568 160 563T102 556H83V602H102Q149 604 189 617T245 641T273 663Q275 666 285 666Q294 666 302 660V361L303 61Q310 54 315 52T339 48T401 46H427V0H416Q395 3 257 3Q121 3 100 0H88V46H114Q136 46 152 46T177 47T193 50T201 52T207 57T213 61V578Z"></path><path data-c="30" d="M96 585Q152 666 249 666Q297 666 345 640T423 548Q460 465 460 320Q460 165 417 83Q397 41 362 16T301 -15T250 -22Q224 -22 198 -16T137 16T82 83Q39 165 39 320Q39 494 96 585ZM321 597Q291 629 250 629Q208 629 178 597Q153 571 145 525T137 333Q137 175 145 125T181 46Q209 16 250 16Q290 16 318 46Q347 76 354 130T362 333Q362 478 354 524T321 597Z" transform="translate(500,0)"></path></g><g data-mml-node="TeXAtom" transform="translate(1033,393.1) scale(0.707)" data-mjx-texclass="ORD"><g data-mml-node="mn"><path data-c="31" d="M213 578L200 573Q186 568 160 563T102 556H83V602H102Q149 604 189 617T245 641T273 663Q275 666 285 666Q294 666 302 660V361L303 61Q310 54 315 52T339 48T401 46H427V0H416Q395 3 257 3Q121 3 100 0H88V46H114Q136 46 152 46T177 47T193 50T201 52T207 57T213 61V578Z"></path><path data-c="32" d="M109 429Q82 429 66 447T50 491Q50 562 103 614T235 666Q326 666 387 610T449 465Q449 422 429 383T381 315T301 241Q265 210 201 149L142 93L218 92Q375 92 385 97Q392 99 409 186V189H449V186Q448 183 436 95T421 3V0H50V19V31Q50 38 56 46T86 81Q115 113 136 137Q145 147 170 174T204 211T233 244T261 278T284 308T305 340T320 369T333 401T340 431T343 464Q343 527 309 573T212 619Q179 619 154 602T119 569T109 550Q109 549 114 549Q132 549 151 535T170 489Q170 464 154 447T109 429Z" transform="translate(500,0)"></path></g></g></g><g data-mml-node="TeXAtom" data-mjx-texclass="ORD" transform="translate(7918.6,0)"><g data-mml-node="mo"><path data-c="2F" d="M423 750Q432 750 438 744T444 730Q444 725 271 248T92 -240Q85 -250 75 -250Q68 -250 62 -245T56 -231Q56 -221 230 257T407 740Q411 750 423 750Z"></path></g></g><g data-mml-node="mi" transform="translate(8418.6,0)"><path data-c="1D43F" d="M228 637Q194 637 192 641Q191 643 191 649Q191 673 202 682Q204 683 217 683Q271 680 344 680Q485 680 506 683H518Q524 677 524 674T522 656Q517 641 513 637H475Q406 636 394 628Q387 624 380 600T313 336Q297 271 279 198T252 88L243 52Q243 48 252 48T311 46H328Q360 46 379 47T428 54T478 72T522 106T564 161Q580 191 594 228T611 270Q616 273 628 273H641Q647 264 647 262T627 203T583 83T557 9Q555 4 553 3T537 0T494 -1Q483 -1 418 -1T294 0H116Q32 0 32 10Q32 17 34 24Q39 43 44 45Q48 46 59 46H65Q92 46 125 49Q139 52 144 61Q147 65 216 339T285 628Q285 635 228 637Z"></path></g></g></g></svg></mjx-container></li>
<li>异常情况:
<ol>
<li>升高: 真性红细胞增多症, 严重脱水, 肺源性心脏病, 先天性心脏病, 高山地区的居民和严重烧伤等</li>
<li>降低: 贫血</li>
</ol>
</li>
</ul>
</li>
<li>血红蛋白 (HGB)
<ul>
<li>正常情况: 男性 <mjx-container class="MathJax" jax="SVG"><svg style="vertical-align: -0.566ex;" xmlns="http://www.w3.org/2000/svg" width="13.933ex" height="2.262ex" role="img" focusable="false" viewBox="0 -750 6158.4 1000"><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="scale(1,-1)"><g data-mml-node="math"><g data-mml-node="mo"><path data-c="28" d="M94 250Q94 319 104 381T127 488T164 576T202 643T244 695T277 729T302 750H315H319Q333 750 333 741Q333 738 316 720T275 667T226 581T184 443T167 250T184 58T225 -81T274 -167T316 -220T333 -241Q333 -250 318 -250H315H302L274 -226Q180 -141 137 -14T94 250Z"></path></g><g data-mml-node="mn" transform="translate(389,0)"><path data-c="31" d="M213 578L200 573Q186 568 160 563T102 556H83V602H102Q149 604 189 617T245 641T273 663Q275 666 285 666Q294 666 302 660V361L303 61Q310 54 315 52T339 48T401 46H427V0H416Q395 3 257 3Q121 3 100 0H88V46H114Q136 46 152 46T177 47T193 50T201 52T207 57T213 61V578Z"></path><path data-c="34" d="M462 0Q444 3 333 3Q217 3 199 0H190V46H221Q241 46 248 46T265 48T279 53T286 61Q287 63 287 115V165H28V211L179 442Q332 674 334 675Q336 677 355 677H373L379 671V211H471V165H379V114Q379 73 379 66T385 54Q393 47 442 46H471V0H462ZM293 211V545L74 212L183 211H293Z" transform="translate(500,0)"></path><path data-c="30" d="M96 585Q152 666 249 666Q297 666 345 640T423 548Q460 465 460 320Q460 165 417 83Q397 41 362 16T301 -15T250 -22Q224 -22 198 -16T137 16T82 83Q39 165 39 320Q39 494 96 585ZM321 597Q291 629 250 629Q208 629 178 597Q153 571 145 525T137 333Q137 175 145 125T181 46Q209 16 250 16Q290 16 318 46Q347 76 354 130T362 333Q362 478 354 524T321 597Z" transform="translate(1000,0)"></path></g><g data-mml-node="mo" transform="translate(2111.2,0)"><path data-c="B1" d="M56 320T56 333T70 353H369V502Q369 651 371 655Q376 666 388 666Q402 666 405 654T409 596V500V353H707Q722 345 722 333Q722 320 707 313H409V40H707Q722 32 722 20T707 0H70Q56 7 56 20T70 40H369V313H70Q56 320 56 333Z"></path></g><g data-mml-node="mn" transform="translate(3111.4,0)"><path data-c="32" d="M109 429Q82 429 66 447T50 491Q50 562 103 614T235 666Q326 666 387 610T449 465Q449 422 429 383T381 315T301 241Q265 210 201 149L142 93L218 92Q375 92 385 97Q392 99 409 186V189H449V186Q448 183 436 95T421 3V0H50V19V31Q50 38 56 46T86 81Q115 113 136 137Q145 147 170 174T204 211T233 244T261 278T284 308T305 340T320 369T333 401T340 431T343 464Q343 527 309 573T212 619Q179 619 154 602T119 569T109 550Q109 549 114 549Q132 549 151 535T170 489Q170 464 154 447T109 429Z"></path><path data-c="30" d="M96 585Q152 666 249 666Q297 666 345 640T423 548Q460 465 460 320Q460 165 417 83Q397 41 362 16T301 -15T250 -22Q224 -22 198 -16T137 16T82 83Q39 165 39 320Q39 494 96 585ZM321 597Q291 629 250 629Q208 629 178 597Q153 571 145 525T137 333Q137 175 145 125T181 46Q209 16 250 16Q290 16 318 46Q347 76 354 130T362 333Q362 478 354 524T321 597Z" transform="translate(500,0)"></path></g><g data-mml-node="mo" transform="translate(4111.4,0)"><path data-c="29" d="M60 749L64 750Q69 750 74 750H86L114 726Q208 641 251 514T294 250Q294 182 284 119T261 12T224 -76T186 -143T145 -194T113 -227T90 -246Q87 -249 86 -250H74Q66 -250 63 -250T58 -247T55 -238Q56 -237 66 -225Q221 -64 221 250T66 725Q56 737 55 738Q55 746 60 749Z"></path></g><g data-mml-node="mi" transform="translate(4500.4,0)"><path data-c="1D454" d="M311 43Q296 30 267 15T206 0Q143 0 105 45T66 160Q66 265 143 353T314 442Q361 442 401 394L404 398Q406 401 409 404T418 412T431 419T447 422Q461 422 470 413T480 394Q480 379 423 152T363 -80Q345 -134 286 -169T151 -205Q10 -205 10 -137Q10 -111 28 -91T74 -71Q89 -71 102 -80T116 -111Q116 -121 114 -130T107 -144T99 -154T92 -162L90 -164H91Q101 -167 151 -167Q189 -167 211 -155Q234 -144 254 -122T282 -75Q288 -56 298 -13Q311 35 311 43ZM384 328L380 339Q377 350 375 354T369 368T359 382T346 393T328 402T306 405Q262 405 221 352Q191 313 171 233T151 117Q151 38 213 38Q269 38 323 108L331 118L384 328Z"></path></g><g data-mml-node="TeXAtom" data-mjx-texclass="ORD" transform="translate(4977.4,0)"><g data-mml-node="mo"><path data-c="2F" d="M423 750Q432 750 438 744T444 730Q444 725 271 248T92 -240Q85 -250 75 -250Q68 -250 62 -245T56 -231Q56 -221 230 257T407 740Q411 750 423 750Z"></path></g></g><g data-mml-node="mi" transform="translate(5477.4,0)"><path data-c="1D43F" d="M228 637Q194 637 192 641Q191 643 191 649Q191 673 202 682Q204 683 217 683Q271 680 344 680Q485 680 506 683H518Q524 677 524 674T522 656Q517 641 513 637H475Q406 636 394 628Q387 624 380 600T313 336Q297 271 279 198T252 88L243 52Q243 48 252 48T311 46H328Q360 46 379 47T428 54T478 72T522 106T564 161Q580 191 594 228T611 270Q616 273 628 273H641Q647 264 647 262T627 203T583 83T557 9Q555 4 553 3T537 0T494 -1Q483 -1 418 -1T294 0H116Q32 0 32 10Q32 17 34 24Q39 43 44 45Q48 46 59 46H65Q92 46 125 49Q139 52 144 61Q147 65 216 339T285 628Q285 635 228 637Z"></path></g></g></g></svg></mjx-container>, 女性 <mjx-container class="MathJax" jax="SVG"><svg style="vertical-align: -0.566ex;" xmlns="http://www.w3.org/2000/svg" width="13.933ex" height="2.262ex" role="img" focusable="false" viewBox="0 -750 6158.4 1000"><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="scale(1,-1)"><g data-mml-node="math"><g data-mml-node="mo"><path data-c="28" d="M94 250Q94 319 104 381T127 488T164 576T202 643T244 695T277 729T302 750H315H319Q333 750 333 741Q333 738 316 720T275 667T226 581T184 443T167 250T184 58T225 -81T274 -167T316 -220T333 -241Q333 -250 318 -250H315H302L274 -226Q180 -141 137 -14T94 250Z"></path></g><g data-mml-node="mn" transform="translate(389,0)"><path data-c="31" d="M213 578L200 573Q186 568 160 563T102 556H83V602H102Q149 604 189 617T245 641T273 663Q275 666 285 666Q294 666 302 660V361L303 61Q310 54 315 52T339 48T401 46H427V0H416Q395 3 257 3Q121 3 100 0H88V46H114Q136 46 152 46T177 47T193 50T201 52T207 57T213 61V578Z"></path><path data-c="33" d="M127 463Q100 463 85 480T69 524Q69 579 117 622T233 665Q268 665 277 664Q351 652 390 611T430 522Q430 470 396 421T302 350L299 348Q299 347 308 345T337 336T375 315Q457 262 457 175Q457 96 395 37T238 -22Q158 -22 100 21T42 130Q42 158 60 175T105 193Q133 193 151 175T169 130Q169 119 166 110T159 94T148 82T136 74T126 70T118 67L114 66Q165 21 238 21Q293 21 321 74Q338 107 338 175V195Q338 290 274 322Q259 328 213 329L171 330L168 332Q166 335 166 348Q166 366 174 366Q202 366 232 371Q266 376 294 413T322 525V533Q322 590 287 612Q265 626 240 626Q208 626 181 615T143 592T132 580H135Q138 579 143 578T153 573T165 566T175 555T183 540T186 520Q186 498 172 481T127 463Z" transform="translate(500,0)"></path><path data-c="30" d="M96 585Q152 666 249 666Q297 666 345 640T423 548Q460 465 460 320Q460 165 417 83Q397 41 362 16T301 -15T250 -22Q224 -22 198 -16T137 16T82 83Q39 165 39 320Q39 494 96 585ZM321 597Q291 629 250 629Q208 629 178 597Q153 571 145 525T137 333Q137 175 145 125T181 46Q209 16 250 16Q290 16 318 46Q347 76 354 130T362 333Q362 478 354 524T321 597Z" transform="translate(1000,0)"></path></g><g data-mml-node="mo" transform="translate(2111.2,0)"><path data-c="B1" d="M56 320T56 333T70 353H369V502Q369 651 371 655Q376 666 388 666Q402 666 405 654T409 596V500V353H707Q722 345 722 333Q722 320 707 313H409V40H707Q722 32 722 20T707 0H70Q56 7 56 20T70 40H369V313H70Q56 320 56 333Z"></path></g><g data-mml-node="mn" transform="translate(3111.4,0)"><path data-c="32" d="M109 429Q82 429 66 447T50 491Q50 562 103 614T235 666Q326 666 387 610T449 465Q449 422 429 383T381 315T301 241Q265 210 201 149L142 93L218 92Q375 92 385 97Q392 99 409 186V189H449V186Q448 183 436 95T421 3V0H50V19V31Q50 38 56 46T86 81Q115 113 136 137Q145 147 170 174T204 211T233 244T261 278T284 308T305 340T320 369T333 401T340 431T343 464Q343 527 309 573T212 619Q179 619 154 602T119 569T109 550Q109 549 114 549Q132 549 151 535T170 489Q170 464 154 447T109 429Z"></path><path data-c="30" d="M96 585Q152 666 249 666Q297 666 345 640T423 548Q460 465 460 320Q460 165 417 83Q397 41 362 16T301 -15T250 -22Q224 -22 198 -16T137 16T82 83Q39 165 39 320Q39 494 96 585ZM321 597Q291 629 250 629Q208 629 178 597Q153 571 145 525T137 333Q137 175 145 125T181 46Q209 16 250 16Q290 16 318 46Q347 76 354 130T362 333Q362 478 354 524T321 597Z" transform="translate(500,0)"></path></g><g data-mml-node="mo" transform="translate(4111.4,0)"><path data-c="29" d="M60 749L64 750Q69 750 74 750H86L114 726Q208 641 251 514T294 250Q294 182 284 119T261 12T224 -76T186 -143T145 -194T113 -227T90 -246Q87 -249 86 -250H74Q66 -250 63 -250T58 -247T55 -238Q56 -237 66 -225Q221 -64 221 250T66 725Q56 737 55 738Q55 746 60 749Z"></path></g><g data-mml-node="mi" transform="translate(4500.4,0)"><path data-c="1D454" d="M311 43Q296 30 267 15T206 0Q143 0 105 45T66 160Q66 265 143 353T314 442Q361 442 401 394L404 398Q406 401 409 404T418 412T431 419T447 422Q461 422 470 413T480 394Q480 379 423 152T363 -80Q345 -134 286 -169T151 -205Q10 -205 10 -137Q10 -111 28 -91T74 -71Q89 -71 102 -80T116 -111Q116 -121 114 -130T107 -144T99 -154T92 -162L90 -164H91Q101 -167 151 -167Q189 -167 211 -155Q234 -144 254 -122T282 -75Q288 -56 298 -13Q311 35 311 43ZM384 328L380 339Q377 350 375 354T369 368T359 382T346 393T328 402T306 405Q262 405 221 352Q191 313 171 233T151 117Q151 38 213 38Q269 38 323 108L331 118L384 328Z"></path></g><g data-mml-node="TeXAtom" data-mjx-texclass="ORD" transform="translate(4977.4,0)"><g data-mml-node="mo"><path data-c="2F" d="M423 750Q432 750 438 744T444 730Q444 725 271 248T92 -240Q85 -250 75 -250Q68 -250 62 -245T56 -231Q56 -221 230 257T407 740Q411 750 423 750Z"></path></g></g><g data-mml-node="mi" transform="translate(5477.4,0)"><path data-c="1D43F" d="M228 637Q194 637 192 641Q191 643 191 649Q191 673 202 682Q204 683 217 683Q271 680 344 680Q485 680 506 683H518Q524 677 524 674T522 656Q517 641 513 637H475Q406 636 394 628Q387 624 380 600T313 336Q297 271 279 198T252 88L243 52Q243 48 252 48T311 46H328Q360 46 379 47T428 54T478 72T522 106T564 161Q580 191 594 228T611 270Q616 273 628 273H641Q647 264 647 262T627 203T583 83T557 9Q555 4 553 3T537 0T494 -1Q483 -1 418 -1T294 0H116Q32 0 32 10Q32 17 34 24Q39 43 44 45Q48 46 59 46H65Q92 46 125 49Q139 52 144 61Q147 65 216 339T285 628Q285 635 228 637Z"></path></g></g></g></svg></mjx-container></li>
<li>异常情况
<ol>
<li>升高: 真性红细胞增多症, 严重脱水, 肺源性心脏病, 先天性心脏病, 高山地区的居民和严重烧伤等</li>
<li>降低: 贫血</li>
</ol>
</li>
</ul>
</li>
<li>白细胞 (WBC)
<ul>
<li>正常情况: 男女都是 <mjx-container class="MathJax" jax="SVG"><svg style="vertical-align: -0.566ex;" xmlns="http://www.w3.org/2000/svg" width="15.476ex" height="2.52ex" role="img" focusable="false" viewBox="0 -864 6840.4 1114"><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="scale(1,-1)"><g data-mml-node="math"><g data-mml-node="mo"><path data-c="28" d="M94 250Q94 319 104 381T127 488T164 576T202 643T244 695T277 729T302 750H315H319Q333 750 333 741Q333 738 316 720T275 667T226 581T184 443T167 250T184 58T225 -81T274 -167T316 -220T333 -241Q333 -250 318 -250H315H302L274 -226Q180 -141 137 -14T94 250Z"></path></g><g data-mml-node="mn" transform="translate(389,0)"><path data-c="37" d="M55 458Q56 460 72 567L88 674Q88 676 108 676H128V672Q128 662 143 655T195 646T364 644H485V605L417 512Q408 500 387 472T360 435T339 403T319 367T305 330T292 284T284 230T278 162T275 80Q275 66 275 52T274 28V19Q270 2 255 -10T221 -22Q210 -22 200 -19T179 0T168 40Q168 198 265 368Q285 400 349 489L395 552H302Q128 552 119 546Q113 543 108 522T98 479L95 458V455H55V458Z"></path></g><g data-mml-node="mo" transform="translate(1111.2,0)"><path data-c="B1" d="M56 320T56 333T70 353H369V502Q369 651 371 655Q376 666 388 666Q402 666 405 654T409 596V500V353H707Q722 345 722 333Q722 320 707 313H409V40H707Q722 32 722 20T707 0H70Q56 7 56 20T70 40H369V313H70Q56 320 56 333Z"></path></g><g data-mml-node="mn" transform="translate(2111.4,0)"><path data-c="33" d="M127 463Q100 463 85 480T69 524Q69 579 117 622T233 665Q268 665 277 664Q351 652 390 611T430 522Q430 470 396 421T302 350L299 348Q299 347 308 345T337 336T375 315Q457 262 457 175Q457 96 395 37T238 -22Q158 -22 100 21T42 130Q42 158 60 175T105 193Q133 193 151 175T169 130Q169 119 166 110T159 94T148 82T136 74T126 70T118 67L114 66Q165 21 238 21Q293 21 321 74Q338 107 338 175V195Q338 290 274 322Q259 328 213 329L171 330L168 332Q166 335 166 348Q166 366 174 366Q202 366 232 371Q266 376 294 413T322 525V533Q322 590 287 612Q265 626 240 626Q208 626 181 615T143 592T132 580H135Q138 579 143 578T153 573T165 566T175 555T183 540T186 520Q186 498 172 481T127 463Z"></path></g><g data-mml-node="mo" transform="translate(2611.4,0)"><path data-c="29" d="M60 749L64 750Q69 750 74 750H86L114 726Q208 641 251 514T294 250Q294 182 284 119T261 12T224 -76T186 -143T145 -194T113 -227T90 -246Q87 -249 86 -250H74Q66 -250 63 -250T58 -247T55 -238Q56 -237 66 -225Q221 -64 221 250T66 725Q56 737 55 738Q55 746 60 749Z"></path></g><g data-mml-node="mo" transform="translate(3222.7,0)"><path data-c="D7" d="M630 29Q630 9 609 9Q604 9 587 25T493 118L389 222L284 117Q178 13 175 11Q171 9 168 9Q160 9 154 15T147 29Q147 36 161 51T255 146L359 250L255 354Q174 435 161 449T147 471Q147 480 153 485T168 490Q173 490 175 489Q178 487 284 383L389 278L493 382Q570 459 587 475T609 491Q630 491 630 471Q630 464 620 453T522 355L418 250L522 145Q606 61 618 48T630 29Z"></path></g><g data-mml-node="msup" transform="translate(4222.9,0)"><g data-mml-node="mn"><path data-c="31" d="M213 578L200 573Q186 568 160 563T102 556H83V602H102Q149 604 189 617T245 641T273 663Q275 666 285 666Q294 666 302 660V361L303 61Q310 54 315 52T339 48T401 46H427V0H416Q395 3 257 3Q121 3 100 0H88V46H114Q136 46 152 46T177 47T193 50T201 52T207 57T213 61V578Z"></path><path data-c="30" d="M96 585Q152 666 249 666Q297 666 345 640T423 548Q460 465 460 320Q460 165 417 83Q397 41 362 16T301 -15T250 -22Q224 -22 198 -16T137 16T82 83Q39 165 39 320Q39 494 96 585ZM321 597Q291 629 250 629Q208 629 178 597Q153 571 145 525T137 333Q137 175 145 125T181 46Q209 16 250 16Q290 16 318 46Q347 76 354 130T362 333Q362 478 354 524T321 597Z" transform="translate(500,0)"></path></g><g data-mml-node="mn" transform="translate(1033,393.1) scale(0.707)"><path data-c="39" d="M352 287Q304 211 232 211Q154 211 104 270T44 396Q42 412 42 436V444Q42 537 111 606Q171 666 243 666Q245 666 249 666T257 665H261Q273 665 286 663T323 651T370 619T413 560Q456 472 456 334Q456 194 396 97Q361 41 312 10T208 -22Q147 -22 108 7T68 93T121 149Q143 149 158 135T173 96Q173 78 164 65T148 49T135 44L131 43Q131 41 138 37T164 27T206 22H212Q272 22 313 86Q352 142 352 280V287ZM244 248Q292 248 321 297T351 430Q351 508 343 542Q341 552 337 562T323 588T293 615T246 625Q208 625 181 598Q160 576 154 546T147 441Q147 358 152 329T172 282Q197 248 244 248Z"></path></g></g><g data-mml-node="TeXAtom" data-mjx-texclass="ORD" transform="translate(5659.4,0)"><g data-mml-node="mo"><path data-c="2F" d="M423 750Q432 750 438 744T444 730Q444 725 271 248T92 -240Q85 -250 75 -250Q68 -250 62 -245T56 -231Q56 -221 230 257T407 740Q411 750 423 750Z"></path></g></g><g data-mml-node="mi" transform="translate(6159.4,0)"><path data-c="1D43F" d="M228 637Q194 637 192 641Q191 643 191 649Q191 673 202 682Q204 683 217 683Q271 680 344 680Q485 680 506 683H518Q524 677 524 674T522 656Q517 641 513 637H475Q406 636 394 628Q387 624 380 600T313 336Q297 271 279 198T252 88L243 52Q243 48 252 48T311 46H328Q360 46 379 47T428 54T478 72T522 106T564 161Q580 191 594 228T611 270Q616 273 628 273H641Q647 264 647 262T627 203T583 83T557 9Q555 4 553 3T537 0T494 -1Q483 -1 418 -1T294 0H116Q32 0 32 10Q32 17 34 24Q39 43 44 45Q48 46 59 46H65Q92 46 125 49Q139 52 144 61Q147 65 216 339T285 628Q285 635 228 637Z"></path></g></g></g></svg></mjx-container></li>
<li>异常情况
<ol>
<li>升高: 各种细胞感染, 炎症, 严重烧伤
<ul>
<li>
<h3 id="当白细胞明显升高,-甚至于-350-pm-150-times-10-9-L-,-并伴有发热,出血,贫血等症状的时候,应当警惕白血病"><a class="header-anchor" href="#当白细胞明显升高,-甚至于-350-pm-150-times-10-9-L-,-并伴有发热,出血,贫血等症状的时候,应当警惕白血病">¶</a>当白细胞明显升高, 甚至于 <mjx-container class="MathJax" jax="SVG"><svg style="vertical-align: -0.566ex;" xmlns="http://www.w3.org/2000/svg" width="20.001ex" height="2.52ex" role="img" focusable="false" viewBox="0 -864 8840.4 1114"><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="scale(1,-1)"><g data-mml-node="math"><g data-mml-node="mo"><path data-c="28" d="M94 250Q94 319 104 381T127 488T164 576T202 643T244 695T277 729T302 750H315H319Q333 750 333 741Q333 738 316 720T275 667T226 581T184 443T167 250T184 58T225 -81T274 -167T316 -220T333 -241Q333 -250 318 -250H315H302L274 -226Q180 -141 137 -14T94 250Z"></path></g><g data-mml-node="mn" transform="translate(389,0)"><path data-c="33" d="M127 463Q100 463 85 480T69 524Q69 579 117 622T233 665Q268 665 277 664Q351 652 390 611T430 522Q430 470 396 421T302 350L299 348Q299 347 308 345T337 336T375 315Q457 262 457 175Q457 96 395 37T238 -22Q158 -22 100 21T42 130Q42 158 60 175T105 193Q133 193 151 175T169 130Q169 119 166 110T159 94T148 82T136 74T126 70T118 67L114 66Q165 21 238 21Q293 21 321 74Q338 107 338 175V195Q338 290 274 322Q259 328 213 329L171 330L168 332Q166 335 166 348Q166 366 174 366Q202 366 232 371Q266 376 294 413T322 525V533Q322 590 287 612Q265 626 240 626Q208 626 181 615T143 592T132 580H135Q138 579 143 578T153 573T165 566T175 555T183 540T186 520Q186 498 172 481T127 463Z"></path><path data-c="35" d="M164 157Q164 133 148 117T109 101H102Q148 22 224 22Q294 22 326 82Q345 115 345 210Q345 313 318 349Q292 382 260 382H254Q176 382 136 314Q132 307 129 306T114 304Q97 304 95 310Q93 314 93 485V614Q93 664 98 664Q100 666 102 666Q103 666 123 658T178 642T253 634Q324 634 389 662Q397 666 402 666Q410 666 410 648V635Q328 538 205 538Q174 538 149 544L139 546V374Q158 388 169 396T205 412T256 420Q337 420 393 355T449 201Q449 109 385 44T229 -22Q148 -22 99 32T50 154Q50 178 61 192T84 210T107 214Q132 214 148 197T164 157Z" transform="translate(500,0)"></path><path data-c="30" d="M96 585Q152 666 249 666Q297 666 345 640T423 548Q460 465 460 320Q460 165 417 83Q397 41 362 16T301 -15T250 -22Q224 -22 198 -16T137 16T82 83Q39 165 39 320Q39 494 96 585ZM321 597Q291 629 250 629Q208 629 178 597Q153 571 145 525T137 333Q137 175 145 125T181 46Q209 16 250 16Q290 16 318 46Q347 76 354 130T362 333Q362 478 354 524T321 597Z" transform="translate(1000,0)"></path></g><g data-mml-node="mo" transform="translate(2111.2,0)"><path data-c="B1" d="M56 320T56 333T70 353H369V502Q369 651 371 655Q376 666 388 666Q402 666 405 654T409 596V500V353H707Q722 345 722 333Q722 320 707 313H409V40H707Q722 32 722 20T707 0H70Q56 7 56 20T70 40H369V313H70Q56 320 56 333Z"></path></g><g data-mml-node="mn" transform="translate(3111.4,0)"><path data-c="31" d="M213 578L200 573Q186 568 160 563T102 556H83V602H102Q149 604 189 617T245 641T273 663Q275 666 285 666Q294 666 302 660V361L303 61Q310 54 315 52T339 48T401 46H427V0H416Q395 3 257 3Q121 3 100 0H88V46H114Q136 46 152 46T177 47T193 50T201 52T207 57T213 61V578Z"></path><path data-c="35" d="M164 157Q164 133 148 117T109 101H102Q148 22 224 22Q294 22 326 82Q345 115 345 210Q345 313 318 349Q292 382 260 382H254Q176 382 136 314Q132 307 129 306T114 304Q97 304 95 310Q93 314 93 485V614Q93 664 98 664Q100 666 102 666Q103 666 123 658T178 642T253 634Q324 634 389 662Q397 666 402 666Q410 666 410 648V635Q328 538 205 538Q174 538 149 544L139 546V374Q158 388 169 396T205 412T256 420Q337 420 393 355T449 201Q449 109 385 44T229 -22Q148 -22 99 32T50 154Q50 178 61 192T84 210T107 214Q132 214 148 197T164 157Z" transform="translate(500,0)"></path><path data-c="30" d="M96 585Q152 666 249 666Q297 666 345 640T423 548Q460 465 460 320Q460 165 417 83Q397 41 362 16T301 -15T250 -22Q224 -22 198 -16T137 16T82 83Q39 165 39 320Q39 494 96 585ZM321 597Q291 629 250 629Q208 629 178 597Q153 571 145 525T137 333Q137 175 145 125T181 46Q209 16 250 16Q290 16 318 46Q347 76 354 130T362 333Q362 478 354 524T321 597Z" transform="translate(1000,0)"></path></g><g data-mml-node="mo" transform="translate(4611.4,0)"><path data-c="29" d="M60 749L64 750Q69 750 74 750H86L114 726Q208 641 251 514T294 250Q294 182 284 119T261 12T224 -76T186 -143T145 -194T113 -227T90 -246Q87 -249 86 -250H74Q66 -250 63 -250T58 -247T55 -238Q56 -237 66 -225Q221 -64 221 250T66 725Q56 737 55 738Q55 746 60 749Z"></path></g><g data-mml-node="mo" transform="translate(5222.7,0)"><path data-c="D7" d="M630 29Q630 9 609 9Q604 9 587 25T493 118L389 222L284 117Q178 13 175 11Q171 9 168 9Q160 9 154 15T147 29Q147 36 161 51T255 146L359 250L255 354Q174 435 161 449T147 471Q147 480 153 485T168 490Q173 490 175 489Q178 487 284 383L389 278L493 382Q570 459 587 475T609 491Q630 491 630 471Q630 464 620 453T522 355L418 250L522 145Q606 61 618 48T630 29Z"></path></g><g data-mml-node="msup" transform="translate(6222.9,0)"><g data-mml-node="mn"><path data-c="31" d="M213 578L200 573Q186 568 160 563T102 556H83V602H102Q149 604 189 617T245 641T273 663Q275 666 285 666Q294 666 302 660V361L303 61Q310 54 315 52T339 48T401 46H427V0H416Q395 3 257 3Q121 3 100 0H88V46H114Q136 46 152 46T177 47T193 50T201 52T207 57T213 61V578Z"></path><path data-c="30" d="M96 585Q152 666 249 666Q297 666 345 640T423 548Q460 465 460 320Q460 165 417 83Q397 41 362 16T301 -15T250 -22Q224 -22 198 -16T137 16T82 83Q39 165 39 320Q39 494 96 585ZM321 597Q291 629 250 629Q208 629 178 597Q153 571 145 525T137 333Q137 175 145 125T181 46Q209 16 250 16Q290 16 318 46Q347 76 354 130T362 333Q362 478 354 524T321 597Z" transform="translate(500,0)"></path></g><g data-mml-node="mn" transform="translate(1033,393.1) scale(0.707)"><path data-c="39" d="M352 287Q304 211 232 211Q154 211 104 270T44 396Q42 412 42 436V444Q42 537 111 606Q171 666 243 666Q245 666 249 666T257 665H261Q273 665 286 663T323 651T370 619T413 560Q456 472 456 334Q456 194 396 97Q361 41 312 10T208 -22Q147 -22 108 7T68 93T121 149Q143 149 158 135T173 96Q173 78 164 65T148 49T135 44L131 43Q131 41 138 37T164 27T206 22H212Q272 22 313 86Q352 142 352 280V287ZM244 248Q292 248 321 297T351 430Q351 508 343 542Q341 552 337 562T323 588T293 615T246 625Q208 625 181 598Q160 576 154 546T147 441Q147 358 152 329T172 282Q197 248 244 248Z"></path></g></g><g data-mml-node="TeXAtom" data-mjx-texclass="ORD" transform="translate(7659.4,0)"><g data-mml-node="mo"><path data-c="2F" d="M423 750Q432 750 438 744T444 730Q444 725 271 248T92 -240Q85 -250 75 -250Q68 -250 62 -245T56 -231Q56 -221 230 257T407 740Q411 750 423 750Z"></path></g></g><g data-mml-node="mi" transform="translate(8159.4,0)"><path data-c="1D43F" d="M228 637Q194 637 192 641Q191 643 191 649Q191 673 202 682Q204 683 217 683Q271 680 344 680Q485 680 506 683H518Q524 677 524 674T522 656Q517 641 513 637H475Q406 636 394 628Q387 624 380 600T313 336Q297 271 279 198T252 88L243 52Q243 48 252 48T311 46H328Q360 46 379 47T428 54T478 72T522 106T564 161Q580 191 594 228T611 270Q616 273 628 273H641Q647 264 647 262T627 203T583 83T557 9Q555 4 553 3T537 0T494 -1Q483 -1 418 -1T294 0H116Q32 0 32 10Q32 17 34 24Q39 43 44 45Q48 46 59 46H65Q92 46 125 49Q139 52 144 61Q147 65 216 339T285 628Q285 635 228 637Z"></path></g></g></g></svg></mjx-container> , 并伴有发热,出血,贫血等症状的时候,应当警惕白血病</h3>
</li>
</ul>
</li>
<li>降低: 白细胞减少症, 脾功能亢进, 造血功能障碍等</li>
</ol>
</li>
</ul>
</li>
<li>中性粒细胞比率 (NEUTP)
<ul>
<li>正常情况: 男女都是 <mjx-container class="MathJax" jax="SVG"><svg style="vertical-align: -0.566ex;" xmlns="http://www.w3.org/2000/svg" width="9.051ex" height="2.262ex" role="img" focusable="false" viewBox="0 -750 4000.4 1000"><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="scale(1,-1)"><g data-mml-node="math"><g data-mml-node="mo"><path data-c="28" d="M94 250Q94 319 104 381T127 488T164 576T202 643T244 695T277 729T302 750H315H319Q333 750 333 741Q333 738 316 720T275 667T226 581T184 443T167 250T184 58T225 -81T274 -167T316 -220T333 -241Q333 -250 318 -250H315H302L274 -226Q180 -141 137 -14T94 250Z"></path></g><g data-mml-node="mn" transform="translate(389,0)"><path data-c="36" d="M42 313Q42 476 123 571T303 666Q372 666 402 630T432 550Q432 525 418 510T379 495Q356 495 341 509T326 548Q326 592 373 601Q351 623 311 626Q240 626 194 566Q147 500 147 364L148 360Q153 366 156 373Q197 433 263 433H267Q313 433 348 414Q372 400 396 374T435 317Q456 268 456 210V192Q456 169 451 149Q440 90 387 34T253 -22Q225 -22 199 -14T143 16T92 75T56 172T42 313ZM257 397Q227 397 205 380T171 335T154 278T148 216Q148 133 160 97T198 39Q222 21 251 21Q302 21 329 59Q342 77 347 104T352 209Q352 289 347 316T329 361Q302 397 257 397Z"></path><path data-c="30" d="M96 585Q152 666 249 666Q297 666 345 640T423 548Q460 465 460 320Q460 165 417 83Q397 41 362 16T301 -15T250 -22Q224 -22 198 -16T137 16T82 83Q39 165 39 320Q39 494 96 585ZM321 597Q291 629 250 629Q208 629 178 597Q153 571 145 525T137 333Q137 175 145 125T181 46Q209 16 250 16Q290 16 318 46Q347 76 354 130T362 333Q362 478 354 524T321 597Z" transform="translate(500,0)"></path></g><g data-mml-node="mo" transform="translate(1611.2,0)"><path data-c="B1" d="M56 320T56 333T70 353H369V502Q369 651 371 655Q376 666 388 666Q402 666 405 654T409 596V500V353H707Q722 345 722 333Q722 320 707 313H409V40H707Q722 32 722 20T707 0H70Q56 7 56 20T70 40H369V313H70Q56 320 56 333Z"></path></g><g data-mml-node="mn" transform="translate(2611.4,0)"><path data-c="31" d="M213 578L200 573Q186 568 160 563T102 556H83V602H102Q149 604 189 617T245 641T273 663Q275 666 285 666Q294 666 302 660V361L303 61Q310 54 315 52T339 48T401 46H427V0H416Q395 3 257 3Q121 3 100 0H88V46H114Q136 46 152 46T177 47T193 50T201 52T207 57T213 61V578Z"></path><path data-c="30" d="M96 585Q152 666 249 666Q297 666 345 640T423 548Q460 465 460 320Q460 165 417 83Q397 41 362 16T301 -15T250 -22Q224 -22 198 -16T137 16T82 83Q39 165 39 320Q39 494 96 585ZM321 597Q291 629 250 629Q208 629 178 597Q153 571 145 525T137 333Q137 175 145 125T181 46Q209 16 250 16Q290 16 318 46Q347 76 354 130T362 333Q362 478 354 524T321 597Z" transform="translate(500,0)"></path></g><g data-mml-node="mo" transform="translate(3611.4,0)"><path data-c="29" d="M60 749L64 750Q69 750 74 750H86L114 726Q208 641 251 514T294 250Q294 182 284 119T261 12T224 -76T186 -143T145 -194T113 -227T90 -246Q87 -249 86 -250H74Q66 -250 63 -250T58 -247T55 -238Q56 -237 66 -225Q221 -64 221 250T66 725Q56 737 55 738Q55 746 60 749Z"></path></g></g></g></svg></mjx-container></li>
<li>异常情况
<ul>
<li>升高:炎症, 各种感染 (细菌病毒都有)</li>
<li>降低: 各种感染 (病毒感染多见), 中性粒细胞减少症</li>
</ul>
</li>
</ul>
</li>
<li>淋巴细胞数 (LYM)
<ul>
<li>正常情况: 男女都是 <mjx-container class="MathJax" jax="SVG"><svg style="vertical-align: -0.566ex;" xmlns="http://www.w3.org/2000/svg" width="18.996ex" height="2.52ex" role="img" focusable="false" viewBox="0 -864 8396.4 1114"><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="scale(1,-1)"><g data-mml-node="math"><g data-mml-node="mo"><path data-c="28" d="M94 250Q94 319 104 381T127 488T164 576T202 643T244 695T277 729T302 750H315H319Q333 750 333 741Q333 738 316 720T275 667T226 581T184 443T167 250T184 58T225 -81T274 -167T316 -220T333 -241Q333 -250 318 -250H315H302L274 -226Q180 -141 137 -14T94 250Z"></path></g><g data-mml-node="mn" transform="translate(389,0)"><path data-c="32" d="M109 429Q82 429 66 447T50 491Q50 562 103 614T235 666Q326 666 387 610T449 465Q449 422 429 383T381 315T301 241Q265 210 201 149L142 93L218 92Q375 92 385 97Q392 99 409 186V189H449V186Q448 183 436 95T421 3V0H50V19V31Q50 38 56 46T86 81Q115 113 136 137Q145 147 170 174T204 211T233 244T261 278T284 308T305 340T320 369T333 401T340 431T343 464Q343 527 309 573T212 619Q179 619 154 602T119 569T109 550Q109 549 114 549Q132 549 151 535T170 489Q170 464 154 447T109 429Z"></path><path data-c="2E" d="M78 60Q78 84 95 102T138 120Q162 120 180 104T199 61Q199 36 182 18T139 0T96 17T78 60Z" transform="translate(500,0)"></path><path data-c="34" d="M462 0Q444 3 333 3Q217 3 199 0H190V46H221Q241 46 248 46T265 48T279 53T286 61Q287 63 287 115V165H28V211L179 442Q332 674 334 675Q336 677 355 677H373L379 671V211H471V165H379V114Q379 73 379 66T385 54Q393 47 442 46H471V0H462ZM293 211V545L74 212L183 211H293Z" transform="translate(778,0)"></path></g><g data-mml-node="mo" transform="translate(1889.2,0)"><path data-c="B1" d="M56 320T56 333T70 353H369V502Q369 651 371 655Q376 666 388 666Q402 666 405 654T409 596V500V353H707Q722 345 722 333Q722 320 707 313H409V40H707Q722 32 722 20T707 0H70Q56 7 56 20T70 40H369V313H70Q56 320 56 333Z"></path></g><g data-mml-node="mn" transform="translate(2889.4,0)"><path data-c="31" d="M213 578L200 573Q186 568 160 563T102 556H83V602H102Q149 604 189 617T245 641T273 663Q275 666 285 666Q294 666 302 660V361L303 61Q310 54 315 52T339 48T401 46H427V0H416Q395 3 257 3Q121 3 100 0H88V46H114Q136 46 152 46T177 47T193 50T201 52T207 57T213 61V578Z"></path><path data-c="2E" d="M78 60Q78 84 95 102T138 120Q162 120 180 104T199 61Q199 36 182 18T139 0T96 17T78 60Z" transform="translate(500,0)"></path><path data-c="36" d="M42 313Q42 476 123 571T303 666Q372 666 402 630T432 550Q432 525 418 510T379 495Q356 495 341 509T326 548Q326 592 373 601Q351 623 311 626Q240 626 194 566Q147 500 147 364L148 360Q153 366 156 373Q197 433 263 433H267Q313 433 348 414Q372 400 396 374T435 317Q456 268 456 210V192Q456 169 451 149Q440 90 387 34T253 -22Q225 -22 199 -14T143 16T92 75T56 172T42 313ZM257 397Q227 397 205 380T171 335T154 278T148 216Q148 133 160 97T198 39Q222 21 251 21Q302 21 329 59Q342 77 347 104T352 209Q352 289 347 316T329 361Q302 397 257 397Z" transform="translate(778,0)"></path></g><g data-mml-node="mo" transform="translate(4167.4,0)"><path data-c="29" d="M60 749L64 750Q69 750 74 750H86L114 726Q208 641 251 514T294 250Q294 182 284 119T261 12T224 -76T186 -143T145 -194T113 -227T90 -246Q87 -249 86 -250H74Q66 -250 63 -250T58 -247T55 -238Q56 -237 66 -225Q221 -64 221 250T66 725Q56 737 55 738Q55 746 60 749Z"></path></g><g data-mml-node="mo" transform="translate(4778.7,0)"><path data-c="D7" d="M630 29Q630 9 609 9Q604 9 587 25T493 118L389 222L284 117Q178 13 175 11Q171 9 168 9Q160 9 154 15T147 29Q147 36 161 51T255 146L359 250L255 354Q174 435 161 449T147 471Q147 480 153 485T168 490Q173 490 175 489Q178 487 284 383L389 278L493 382Q570 459 587 475T609 491Q630 491 630 471Q630 464 620 453T522 355L418 250L522 145Q606 61 618 48T630 29Z"></path></g><g data-mml-node="msup" transform="translate(5778.9,0)"><g data-mml-node="mn"><path data-c="31" d="M213 578L200 573Q186 568 160 563T102 556H83V602H102Q149 604 189 617T245 641T273 663Q275 666 285 666Q294 666 302 660V361L303 61Q310 54 315 52T339 48T401 46H427V0H416Q395 3 257 3Q121 3 100 0H88V46H114Q136 46 152 46T177 47T193 50T201 52T207 57T213 61V578Z"></path><path data-c="30" d="M96 585Q152 666 249 666Q297 666 345 640T423 548Q460 465 460 320Q460 165 417 83Q397 41 362 16T301 -15T250 -22Q224 -22 198 -16T137 16T82 83Q39 165 39 320Q39 494 96 585ZM321 597Q291 629 250 629Q208 629 178 597Q153 571 145 525T137 333Q137 175 145 125T181 46Q209 16 250 16Q290 16 318 46Q347 76 354 130T362 333Q362 478 354 524T321 597Z" transform="translate(500,0)"></path></g><g data-mml-node="mn" transform="translate(1033,393.1) scale(0.707)"><path data-c="39" d="M352 287Q304 211 232 211Q154 211 104 270T44 396Q42 412 42 436V444Q42 537 111 606Q171 666 243 666Q245 666 249 666T257 665H261Q273 665 286 663T323 651T370 619T413 560Q456 472 456 334Q456 194 396 97Q361 41 312 10T208 -22Q147 -22 108 7T68 93T121 149Q143 149 158 135T173 96Q173 78 164 65T148 49T135 44L131 43Q131 41 138 37T164 27T206 22H212Q272 22 313 86Q352 142 352 280V287ZM244 248Q292 248 321 297T351 430Q351 508 343 542Q341 552 337 562T323 588T293 615T246 625Q208 625 181 598Q160 576 154 546T147 441Q147 358 152 329T172 282Q197 248 244 248Z"></path></g></g><g data-mml-node="TeXAtom" data-mjx-texclass="ORD" transform="translate(7215.4,0)"><g data-mml-node="mo"><path data-c="2F" d="M423 750Q432 750 438 744T444 730Q444 725 271 248T92 -240Q85 -250 75 -250Q68 -250 62 -245T56 -231Q56 -221 230 257T407 740Q411 750 423 750Z"></path></g></g><g data-mml-node="mi" transform="translate(7715.4,0)"><path data-c="1D43F" d="M228 637Q194 637 192 641Q191 643 191 649Q191 673 202 682Q204 683 217 683Q271 680 344 680Q485 680 506 683H518Q524 677 524 674T522 656Q517 641 513 637H475Q406 636 394 628Q387 624 380 600T313 336Q297 271 279 198T252 88L243 52Q243 48 252 48T311 46H328Q360 46 379 47T428 54T478 72T522 106T564 161Q580 191 594 228T611 270Q616 273 628 273H641Q647 264 647 262T627 203T583 83T557 9Q555 4 553 3T537 0T494 -1Q483 -1 418 -1T294 0H116Q32 0 32 10Q32 17 34 24Q39 43 44 45Q48 46 59 46H65Q92 46 125 49Q139 52 144 61Q147 65 216 339T285 628Q285 635 228 637Z"></path></g></g></g></svg></mjx-container></li>
<li>异常情况:
<ul>
<li>升高: 百日咳, 传染性单核细胞增多症, 急性传染性淋巴细胞增多症, 淋巴细胞性白血病等</li>
<li>降低: 免疫缺陷等</li>
</ul>
</li>
</ul>
</li>
<li>血小板 (PLT)
<ul>
<li>正常情况: 男女都是 <mjx-container class="MathJax" jax="SVG"><svg style="vertical-align: -0.566ex;" xmlns="http://www.w3.org/2000/svg" width="20.001ex" height="2.52ex" role="img" focusable="false" viewBox="0 -864 8840.4 1114"><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="scale(1,-1)"><g data-mml-node="math"><g data-mml-node="mo"><path data-c="28" d="M94 250Q94 319 104 381T127 488T164 576T202 643T244 695T277 729T302 750H315H319Q333 750 333 741Q333 738 316 720T275 667T226 581T184 443T167 250T184 58T225 -81T274 -167T316 -220T333 -241Q333 -250 318 -250H315H302L274 -226Q180 -141 137 -14T94 250Z"></path></g><g data-mml-node="mn" transform="translate(389,0)"><path data-c="32" d="M109 429Q82 429 66 447T50 491Q50 562 103 614T235 666Q326 666 387 610T449 465Q449 422 429 383T381 315T301 241Q265 210 201 149L142 93L218 92Q375 92 385 97Q392 99 409 186V189H449V186Q448 183 436 95T421 3V0H50V19V31Q50 38 56 46T86 81Q115 113 136 137Q145 147 170 174T204 211T233 244T261 278T284 308T305 340T320 369T333 401T340 431T343 464Q343 527 309 573T212 619Q179 619 154 602T119 569T109 550Q109 549 114 549Q132 549 151 535T170 489Q170 464 154 447T109 429Z"></path><path data-c="31" d="M213 578L200 573Q186 568 160 563T102 556H83V602H102Q149 604 189 617T245 641T273 663Q275 666 285 666Q294 666 302 660V361L303 61Q310 54 315 52T339 48T401 46H427V0H416Q395 3 257 3Q121 3 100 0H88V46H114Q136 46 152 46T177 47T193 50T201 52T207 57T213 61V578Z" transform="translate(500,0)"></path><path data-c="30" d="M96 585Q152 666 249 666Q297 666 345 640T423 548Q460 465 460 320Q460 165 417 83Q397 41 362 16T301 -15T250 -22Q224 -22 198 -16T137 16T82 83Q39 165 39 320Q39 494 96 585ZM321 597Q291 629 250 629Q208 629 178 597Q153 571 145 525T137 333Q137 175 145 125T181 46Q209 16 250 16Q290 16 318 46Q347 76 354 130T362 333Q362 478 354 524T321 597Z" transform="translate(1000,0)"></path></g><g data-mml-node="mo" transform="translate(2111.2,0)"><path data-c="B1" d="M56 320T56 333T70 353H369V502Q369 651 371 655Q376 666 388 666Q402 666 405 654T409 596V500V353H707Q722 345 722 333Q722 320 707 313H409V40H707Q722 32 722 20T707 0H70Q56 7 56 20T70 40H369V313H70Q56 320 56 333Z"></path></g><g data-mml-node="mn" transform="translate(3111.4,0)"><path data-c="31" d="M213 578L200 573Q186 568 160 563T102 556H83V602H102Q149 604 189 617T245 641T273 663Q275 666 285 666Q294 666 302 660V361L303 61Q310 54 315 52T339 48T401 46H427V0H416Q395 3 257 3Q121 3 100 0H88V46H114Q136 46 152 46T177 47T193 50T201 52T207 57T213 61V578Z"></path><path data-c="31" d="M213 578L200 573Q186 568 160 563T102 556H83V602H102Q149 604 189 617T245 641T273 663Q275 666 285 666Q294 666 302 660V361L303 61Q310 54 315 52T339 48T401 46H427V0H416Q395 3 257 3Q121 3 100 0H88V46H114Q136 46 152 46T177 47T193 50T201 52T207 57T213 61V578Z" transform="translate(500,0)"></path><path data-c="30" d="M96 585Q152 666 249 666Q297 666 345 640T423 548Q460 465 460 320Q460 165 417 83Q397 41 362 16T301 -15T250 -22Q224 -22 198 -16T137 16T82 83Q39 165 39 320Q39 494 96 585ZM321 597Q291 629 250 629Q208 629 178 597Q153 571 145 525T137 333Q137 175 145 125T181 46Q209 16 250 16Q290 16 318 46Q347 76 354 130T362 333Q362 478 354 524T321 597Z" transform="translate(1000,0)"></path></g><g data-mml-node="mo" transform="translate(4611.4,0)"><path data-c="29" d="M60 749L64 750Q69 750 74 750H86L114 726Q208 641 251 514T294 250Q294 182 284 119T261 12T224 -76T186 -143T145 -194T113 -227T90 -246Q87 -249 86 -250H74Q66 -250 63 -250T58 -247T55 -238Q56 -237 66 -225Q221 -64 221 250T66 725Q56 737 55 738Q55 746 60 749Z"></path></g><g data-mml-node="mo" transform="translate(5222.7,0)"><path data-c="D7" d="M630 29Q630 9 609 9Q604 9 587 25T493 118L389 222L284 117Q178 13 175 11Q171 9 168 9Q160 9 154 15T147 29Q147 36 161 51T255 146L359 250L255 354Q174 435 161 449T147 471Q147 480 153 485T168 490Q173 490 175 489Q178 487 284 383L389 278L493 382Q570 459 587 475T609 491Q630 491 630 471Q630 464 620 453T522 355L418 250L522 145Q606 61 618 48T630 29Z"></path></g><g data-mml-node="msup" transform="translate(6222.9,0)"><g data-mml-node="mn"><path data-c="31" d="M213 578L200 573Q186 568 160 563T102 556H83V602H102Q149 604 189 617T245 641T273 663Q275 666 285 666Q294 666 302 660V361L303 61Q310 54 315 52T339 48T401 46H427V0H416Q395 3 257 3Q121 3 100 0H88V46H114Q136 46 152 46T177 47T193 50T201 52T207 57T213 61V578Z"></path><path data-c="30" d="M96 585Q152 666 249 666Q297 666 345 640T423 548Q460 465 460 320Q460 165 417 83Q397 41 362 16T301 -15T250 -22Q224 -22 198 -16T137 16T82 83Q39 165 39 320Q39 494 96 585ZM321 597Q291 629 250 629Q208 629 178 597Q153 571 145 525T137 333Q137 175 145 125T181 46Q209 16 250 16Q290 16 318 46Q347 76 354 130T362 333Q362 478 354 524T321 597Z" transform="translate(500,0)"></path></g><g data-mml-node="mn" transform="translate(1033,393.1) scale(0.707)"><path data-c="39" d="M352 287Q304 211 232 211Q154 211 104 270T44 396Q42 412 42 436V444Q42 537 111 606Q171 666 243 666Q245 666 249 666T257 665H261Q273 665 286 663T323 651T370 619T413 560Q456 472 456 334Q456 194 396 97Q361 41 312 10T208 -22Q147 -22 108 7T68 93T121 149Q143 149 158 135T173 96Q173 78 164 65T148 49T135 44L131 43Q131 41 138 37T164 27T206 22H212Q272 22 313 86Q352 142 352 280V287ZM244 248Q292 248 321 297T351 430Q351 508 343 542Q341 552 337 562T323 588T293 615T246 625Q208 625 181 598Q160 576 154 546T147 441Q147 358 152 329T172 282Q197 248 244 248Z"></path></g></g><g data-mml-node="TeXAtom" data-mjx-texclass="ORD" transform="translate(7659.4,0)"><g data-mml-node="mo"><path data-c="2F" d="M423 750Q432 750 438 744T444 730Q444 725 271 248T92 -240Q85 -250 75 -250Q68 -250 62 -245T56 -231Q56 -221 230 257T407 740Q411 750 423 750Z"></path></g></g><g data-mml-node="mi" transform="translate(8159.4,0)"><path data-c="1D43F" d="M228 637Q194 637 192 641Q191 643 191 649Q191 673 202 682Q204 683 217 683Q271 680 344 680Q485 680 506 683H518Q524 677 524 674T522 656Q517 641 513 637H475Q406 636 394 628Q387 624 380 600T313 336Q297 271 279 198T252 88L243 52Q243 48 252 48T311 46H328Q360 46 379 47T428 54T478 72T522 106T564 161Q580 191 594 228T611 270Q616 273 628 273H641Q647 264 647 262T627 203T583 83T557 9Q555 4 553 3T537 0T494 -1Q483 -1 418 -1T294 0H116Q32 0 32 10Q32 17 34 24Q39 43 44 45Q48 46 59 46H65Q92 46 125 49Q139 52 144 61Q147 65 216 339T285 628Q285 635 228 637Z"></path></g></g></g></svg></mjx-container></li>
<li>异常情况:
<ul>
<li>升高: 原发性血小板增多症, 真性红细胞增多症, 慢性白血病, 症状性血小板增多症, 感染, 炎症, 恶性肿瘤, 缺铁性贫血, 外伤, 手术, 出血等</li>
<li>降低: 原发性血小板减少性紫癜, 播散性红斑狼疮, 药物过敏性血小板减少症, 弥漫性血管内凝血, 血小板破坏增多, 血小板生成减少等</li>
</ul>
</li>
</ul>
</li>
</ul>
]]></content>
<tags>
<tag>Medicine</tag>
<tag>Life</tag>
</tags>
</entry>
<entry>
<title>西柚对药物的影响</title>
<url>/2024/11/15/%E8%A5%BF%E6%9F%9A%E5%AF%B9%E8%8D%AF%E7%89%A9%E7%9A%84%E5%BD%B1%E5%93%8D/</url>
<content><![CDATA[<p>西柚,橙子之类中的呋喃香豆素会抑制肝脏中 CYP3A4 酶的活性,经肝脏代谢的药物,正常是肝脏中的酶在发挥作用,CYP3A4 酶就是其中一种,吃药的同时又吃了西柚相当于无形中增加了药物剂量,曾经有人吃硝苯地平的同时,喝了大概 500mL 的西柚汁,结果血压迅速下降导致了危险。<br>
除此之外这种酶参与了现今使用近半数药物的代谢,总体来说,呋喃香豆素可以使药物以 2~10 倍的量进入血液,目前已知的,有超过 85 种药物会受到西柚的影响,但药物种类实在太多了,很难找到会受西柚影响的完整药物名单。最稳妥的方案还是吃药的时候别他妈吃西柚了</p>
]]></content>
<tags>
<tag>Medicine</tag>
<tag>Life</tag>
</tags>
</entry>
<entry>
<title>身体往前倾,灵魂向后摇</title>
<url>/2025/01/19/%E8%BA%AB%E4%BD%93%E5%BE%80%E5%89%8D%E5%80%BE%EF%BC%8C%E7%81%B5%E9%AD%82%E5%90%91%E5%90%8E%E6%91%87/</url>
<content><![CDATA[<p>后摇( Post-Rock )是用摇滚乐器作非摇滚的用途,吉他对音色和结构的推动作用超过了Riff和主旋律。后摇的关键问题除了电子的启用之外,还有多层次打击乐器的出现。</p>
<p>现代社会的后摇创作者们不必为任何形式和任何器物所限制,庞大的音乐历史使得任何形式都展现为能为现代人所应用的形式,现代技术所提供的音乐制作功能则使得作为乐器的器物在后摇中不必再被特地强调。</p>
<p>而对于后摇的受众即聆听者而言,这一现代性的音乐则在正真意义上解放了听众,使得听众成为肆意解读的读者。后摇的解放意义完成了作品自创作完成后从作者向听众的让渡,作品的拥有者不是作者本人,而恰恰是一群无从计数的听众, 作为独特个体而非集体的听众。</p>
<p>因此,后摇的出现意味着创作者与受众的双向失责。两者在彼此自私自取的艺术行为中消弭了主体与客体这一古老的二分法。</p>
<p>极致的喧闹与绝对的安静并无区别。</p>
]]></content>
<tags>
<tag>Life</tag>
<tag>Music</tag>
</tags>
</entry>
<entry>
<title>过度疲劳的六种表现</title>
<url>/2023/10/12/%E8%BF%87%E5%BA%A6%E7%96%B2%E5%8A%B3%E7%9A%84%E5%85%AD%E7%A7%8D%E8%A1%A8%E7%8E%B0/</url>
<content><![CDATA[<ol>
<li>不知不觉就睡着了, 比如在地铁, 公交车这一类很吵闹的地方无意识睡着了。</li>
<li>刚起床就感觉很累,怎么睡都睡不够,醒来就很累,很疲惫,身心俱疲的无力感</li>
<li>长溃疡,结节甚至是囊肿,免疫力下降,反复溃疡,年纪轻轻身上一堆结节和囊肿</li>
<li>比较严重的体臭,口臭,头发油臭,和正常的味道不同,肝功能下降,氨随汗液排出,这种“疲劳臭”很难闻。</li>
<li>脱发长痘痘,痘痘反复发作,年纪轻轻头发大把大把掉,梳头一地头发。</li>
<li>对任何事物都感到厌烦,对任何事物都提不起兴趣,第一反应是很烦,做什么都烦</li>
</ol>
<p>身体不舒服应该第一时间去医院,如果你打算明天去医院看看的话,那今天就不要剧烈运动了,也不要再过度劳累,好今晚好好泡一个热水澡早点睡觉噢。</p>
]]></content>
<tags>
<tag>Life</tag>
</tags>
</entry>
<entry>
<title>金匮要略</title>
<url>/2023/03/04/%E9%87%91%E5%8C%AE%E8%A6%81%E7%95%A5/</url>
<content><![CDATA[<p>臟腑經絡先後病脈證第一問曰:上工治未病,何也?師曰:夫治未病者,見肝之病,知肝傳脾,當先實脾,四季脾王不受邪,即勿補之;中工不曉相傳,見肝之病,不解實脾,惟治肝也。</p>
<p>夫肝之病,補用酸,助用焦苦,益用甘味之藥調之。酸入肝,焦苦入心,甘入脾。脾能傷腎,腎氣微弱,則水不行;水不行,則心火氣盛;心火氣盛,則傷肺,肺被傷,則金氣不行;金氣不行,則肝氣盛。故實脾,則肝自愈。此治肝補脾之要妙也。肝虛則用此法,實則不在用之。</p>
<p>經曰:「虛虛實實,補不足,損有餘」,是其義也。餘臟準此。</p>
<p>夫人稟五常,因風氣而生長,風氣雖能生萬物,亦能害萬物,如水能浮舟,亦能覆舟。若五臟元真通暢,人即安和。客氣邪風,中人多死。千般疢難,不越三條:一者,經絡受邪,入臟腑,為內所因也;二者,四肢九竅,血脈相傳,壅塞不通,為外皮膚所中也;三者,房室、金刃、蟲獸所傷。以此詳之,病由都盡。</p>
<p>若人能養慎,不令邪風干忤經絡;適中經絡,未流傳臟腑,即醫治之。四肢才覺重滯,即導引、吐納、針灸、膏摩,勿令九竅閉塞;更能無犯王法、禽獸災傷,房室勿令竭乏,服食節其冷、熱、苦、酸、辛、甘,不遺形體有衰,病則無由入其腠理。腠者,是三焦通會元真之處,為血氣所注;理者,是皮膚臟腑之文理也。</p>
<p>問曰:病人有氣色見於面部,願聞其說。師曰:鼻頭色青,腹中痛,苦冷者死;一云腹中冷,苦痛者死。鼻頭色微黑者,有水氣;色黃者,胸上有寒;色白者,亡血也,設微赤非時者死;其目正圓者痙,不治。又色青為痛,色黑為勞,色赤為風,色黃者便難,色鮮明者有留飲。</p>
<p>師曰:病人語聲寂然喜驚呼者,骨節間病;語聲喑喑然不澈者,心膈間病;語聲啾啾然細而長者,頭中病。一作痛。</p>
<p>師曰:息搖肩者,心中堅;息引胸中上氣者,咳;息張口短氣者,肺痿唾沫。</p>
<p>師曰:吸而微數,其病在中焦,實也,當下之即愈;虛者不治。在上焦者,其吸促,在下焦者,其吸遠,此皆難治。呼吸動搖振振者,不治。</p>
<p>師曰:寸口脈動者,因其旺時而動,假令肝旺色青,四時各隨其色。肝色青而反色白,非其時色脈,皆當病。</p>
<p>問曰:有未至而至,有至而不至,有至而不去,有至而太過,何謂也?師曰:冬至之後,甲子夜半少陽起,少陽之時,陽始生,天得溫和。以未得甲子,天因溫和,此為未至而至也;以得甲子,而天未溫和,為至而不至也;以得甲子,而天大寒不解,此為至而不去也;以得甲子,而天溫如盛夏五六月時,此為至而太過也。</p>
<p>師曰:病人脈浮者在前,其病在表;浮者在後,其病在裡,腰痛背強不能行,必短氣而極也。</p>
<p>問曰:經云:「厥陽獨行」,何謂也?師曰:此為有陽無陰,故稱厥陽。</p>
<p>問曰:寸脈沉大而滑,沉則為實,滑則為氣,實氣相搏,血氣入臟即死,入腑即愈,此為卒厥,何謂也?師曰:唇口青,身冷,為入臟即死;如身和,汗自出,為入腑即愈。</p>
<p>問曰:脈脫入臟即死,入腑即愈,何謂也?師曰:非為一病,百病皆然。譬如浸淫瘡,從口起流向四肢者可治,從四肢流來入口者不可治;病在外者可治,入裡者即死。</p>
<p>問曰:陽病十八,何謂也?師曰:頭痛、項、腰、脊、臂、腳掣痛。陰病十八,何謂也?師曰:咳、上氣、喘、噦、咽、腸鳴、脹滿、心痛、拘急。五臟病各有十八,合為九十病,人又有六微,微有十八病,合為一百八病,五勞、七傷、六極、婦人三十六病,不在其中。</p>
<p>清邪居上,濁邪居下,大邪中表,小邪中裡,●飪之邪,從口入者,宿食也。五邪中人,各有法度,風中於前,寒中於暮,濕傷於下,霧傷於上,風令脈浮,寒令脈急,霧傷皮腠,濕流關節,食傷脾胃,極寒傷經,極熱傷絡。</p>
<p>問曰:病有急當救裡救表者,何謂也?師曰:病,醫下之,續得下利清穀不止,身體疼痛者,急當救裡;後身體疼痛,清便自調者,急當救表也。</p>
<p>夫病痼疾加以卒病,當先治其卒病,後乃治其痼疾也。</p>
<p>師曰:五臟病各有所得者愈,五臟病各有所惡,各隨其所不喜者為病。病者素不應食,而反暴思之,必發熱也。</p>
<p>夫諸病在臟,欲攻之,當隨其所得而攻之,如渴者,與豬苓湯。餘皆仿此。</p>
<p>痙濕暍病脈證治第二太陽病,發熱無汗,反惡寒者,名曰剛痙。</p>
<p>太陽病,發熱汗出,而不惡寒,名曰柔痙。</p>
<p>太陽病,發熱,脈沉而細者,名曰痙,為難治。</p>
<p>太陽病,發汗太多,因致痙。</p>
<p>夫風病,下之則痙,復發汗,必拘急。</p>
<p>瘡家雖身疼痛,不可發汗,汗出則痙。</p>
<p>病者身熱足寒,頸項強急,惡寒,時頭熱,面赤,目赤,獨頭動搖,卒口噤,背反張者,痙病也。若發其汗者,寒濕相得,其表益虛,即惡寒甚。發其汗已,其脈如蛇。一云其脈??。</p>
<p>暴腹脹大者,為欲解。脈如故,反伏弦者,痙。</p>
<p>夫痙脈,按之緊如弦,直上下行。一作●●而弦,《脈經》云:痙家,其脈伏堅,直上下。</p>
<p>痙病有灸瘡,難治。</p>
<p>太陽病,其證備,身體強,几几然,脈反沉遲,此為痙,栝蔞桂枝湯主之。</p>
<p>栝蔞桂枝湯方:</p>
<p>栝蔞根二兩桂枝三兩芍藥三兩甘草二兩生薑三兩大棗十二枚上六味,以水九升,煮取三升,分溫三服,取微汗。汗不出,食頃,啜熱粥發之。</p>
<p>太陽病,無汗而小便反少,氣上衝胸,口噤不得語,欲作剛痙,葛根湯主之。</p>
<p>葛根湯方:</p>
<p>葛根四兩麻黃三兩(去節)</p>
<p>桂枝二兩(去皮)</p>
<p>芍藥二兩甘草二兩(炙)</p>
<p>生薑三兩(切)</p>
<p>大棗十二枚(擘)</p>
<p>上七味,●咀,以水一斗,先煮麻黃、葛根,減二升,去沫,內諸藥,煮取三升,去滓,溫服一升,覆取微似汗,不須啜粥,餘如桂枝湯法將息及禁忌。</p>
<p>痙為病,一本痙上有剛字。胸滿口噤,臥不著席,腳攣急,必齘齒,可與大承氣湯。</p>
<p>大承氣湯方:</p>
<p>大黃四兩(酒洗)</p>
<p>厚朴半斤(炙去皮)</p>
<p>枳實五枚芒硝三合上四味,以水一斗,先煮枳朴,取五升,去滓,內大黃,煮取二升,去滓,內芒硝,更上微火一二沸,分溫再服,得下止服。</p>
<p>太陽病,關節疼痛而煩,脈沉而細一作緩。者,此名濕痹。《玉函》云中濕,濕痹之候,小便不利,大便反快,但當利其小便。</p>
<p>濕家之為病,一身盡疼,一云疼煩。發熱,身色如熏黃也。</p>
<p>濕家,其人但頭汗出,背強,欲得被覆向火。若下之早則噦,或胸滿,小便不利,一云利。舌上如胎者,以丹田有熱,胸上有寒,渴欲得飲而不能,則口燥煩也。</p>
<p>濕家下之,額上汗出,微喘,小便利一云不利者死;若下利不止者,亦死。</p>
<p>風濕相搏,一身盡疼痛,法當汗出而解;值天陰雨不止,醫云此可發汗,汗之病不愈者,何也?蓋發其汗,汗大出者,但風氣去,濕氣在,是故不愈也。若治風濕者,發其汗,但微微似欲汗出者,風濕俱去也。</p>
<p>濕家病,身疼發熱,面黃而喘,頭痛鼻塞而煩,其脈大,自能飲食,腹中和無病,病在頭中寒濕,故鼻塞,內藥鼻中則愈。《脈經》云:病人喘,而無濕家病以下至而喘十一字。</p>
<p>濕家身煩疼,可與麻黃加朮湯發其汗為宜,慎不可以火攻之。</p>
<p>麻黃加朮湯方:</p>
<p>麻黃三兩(去節)</p>
<p>桂枝二兩(去皮)</p>
<p>甘草二兩(炙)杏仁七十個(去皮尖)</p>
<p>白朮四兩上五味,以水九升,先煮麻黃,減二升,去上沫,內諸藥,煮取二升半,去滓,溫服八合,覆取微似汗。</p>
<p>病者一身盡疼,發熱,日晡所劇者,名風濕。此病傷於汗出當風,或久傷取冷所致也。可與麻黃杏仁薏苡甘草湯。</p>
<p>麻黃杏仁薏苡甘草湯方:</p>
<p>麻黃(去節)半兩(湯泡)</p>
<p>甘草一兩(炙)</p>
<p>薏苡仁半兩杏仁十個(去皮尖,炒)</p>
<p>上銼麻豆大,每服四錢匕,水盞半,煮八分,去滓,溫服,有微汗,避風。</p>
<p>風濕,脈浮身重,汗出惡風者,防己黃耆湯主之。</p>
<p>防己黃耆湯方:</p>
<p>防己一兩,甘草半兩(炒),白朮七錢半,黃耆一兩一分(去蘆)</p>
<p>上銼麻豆大,每抄五錢匕,生薑四片,大棗一枚,水盞半,煎八分,去滓,溫服,良久再服。喘者加麻黃半兩,胃中不和者加芍藥三分,氣上衝者加桂枝三分,下有陳寒者加細辛三分。服後當如蟲行皮中,從腰下如冰,後坐被上,又以一被繞腰以下,溫令微汗,差。</p>
<p>傷寒八九日,風濕相搏,身體疼煩,不能自轉側,不嘔不渴,脈浮虛而澀者,桂枝附子湯主之;若大便堅,小便自利者,去桂加白朮湯主之。</p>
<p>桂枝附子湯方:</p>
<p>桂枝四兩(去皮)</p>
<p>生薑三兩(切)附子三枚(炮去皮,破八片)</p>
<p>甘草二兩(炙)</p>
<p>大棗十二枚(擘)</p>
<p>上五味,以水六升,煮取二升,去滓,分溫三服。</p>
<p>白朮附子湯方:</p>
<p>白朮二兩附子一枚半(炮,去皮)</p>
<p>甘草一兩(炙)</p>
<p>生薑一兩半(切)</p>
<p>大棗六枚(擘)</p>
<p>上五味,以水三升,煮取一升,去滓,分溫三服。一服覺身痹,半日許,再服,三服都盡,其人如冒狀,勿怪,即是朮附並走皮中,逐水氣未得除故耳。</p>
<p>風濕相搏,骨節疼煩,掣痛不得屈伸,近之則痛劇,汗出短氣,小便不利,惡風不欲去衣,或身微腫者,甘草附子湯主之。</p>
<p>甘草附子湯方:</p>
<p>甘草二兩(炙)</p>
<p>白朮二兩附子二枚(炮去皮)</p>
<p>桂枝四兩(去皮)</p>
<p>上四味,以水六升,煮取三升,去滓。溫服一升,日三服,初服得微汗則解,能食,汗出復煩者,服五合。恐一升多者,服六、七合為妙。</p>
<p>太陽中暍,發熱惡寒,身重而疼痛,其脈弦細芤遲。小便已,洒洒然毛聳,手足逆冷,小有勞,身即熱,口開,前板齒燥。若發其汗,則惡寒甚;加溫針,則發熱甚;數下之,則淋甚。</p>
<p>太陽中熱者,暍是也。汗出惡寒,身熱而渴,白虎加人參湯主之。</p>
<p>白虎加人參湯方:</p>
<p>知母六兩石膏一斤(碎)</p>
<p>甘草二兩粳米六合人參三兩上五味,以水一斗,煮米熟,湯成,去滓,溫服一升,日三服。</p>
<p>太陽中暍,身熱疼重,而脈微弱,此以夏月傷冷水,水行皮中所致也,一物瓜蒂湯主之。</p>
<p>一物瓜蒂湯方:</p>
<p>瓜蒂二十個上銼,以水一升,煮取五合,去滓,頓服。</p>
<p>百合狐惑陰陽毒病脈證治第三論曰:百合病者,百脈一宗,悉致其病也。意欲食復不能食,常默默,欲臥不能臥,欲行不能行,欲飲食,或有美時,或有不用聞食臭時,如寒無寒,如熱無熱,口苦,小便赤,諸藥不能治,得藥則劇吐利,如有神靈者,身形如和,其脈微數。</p>
<p>每溺時頭痛者,六十日乃愈;若溺時頭不痛,淅然者,四十日愈;若溺快然,但頭眩者,二十日愈。</p>
<p>其證或未病而預見,或病四、五日而出,或病二十日或一月微見者,各隨證治之。</p>
<p>百合病發汗後者,百合知母湯主之。</p>
<p>百合知母湯方:</p>
<p>百合七枚(擘)</p>
<p>知母三兩(切)</p>
<p>上先以水洗百合,漬一宿,當白沫出,去其水,更以泉水二升,煎取一升,去滓;別以泉水二升煎知母,取一升,去滓;後合和,煎取一升五合,分溫再服。</p>
<p>百合病下之後者,滑石代赭湯主之。</p>
<p>滑石代赭湯方:</p>
<p>百合七枚(擘)</p>
<p>滑石三兩(碎,綿裹)</p>
<p>代赭石如彈丸大一枚(碎,綿裹)</p>
<p>上先以水洗百合,漬一宿,當白沫出,去其水,更以泉水二升,煎取一升,去滓;別以泉水二升煎滑石、代赭,取一升,去滓;後合和重煎,取一升五合,分溫服。</p>
<p>百合病,吐之後者,用後方主之。</p>
<p>百合雞子湯方:</p>
<p>百合七枚(擘)</p>
<p>雞子黃一枚上先以水洗百合,漬一宿,當白沫出,去其水,更以泉水二升,煎取一升,去滓,內雞子黃,攪勻,煎五分,溫服。</p>
<p>百合病,不經吐、下、發汗,病形如初者,百合地黃湯主之。</p>
<p>百合地黃湯方:</p>
<p>百合七枚(擘)</p>
<p>生地黃汁一升上以水洗百合,漬一宿,當白沫出,去其水,更以泉水二升,煎取一升,去滓,內地黃汁,煎取一升五合,分溫再服。中病,勿更服。大便當如漆。</p>
<p>百合病一月不解,變成渴者,百合洗方主之。</p>
<p>百合洗方:</p>
<p>上以百合一升,以水一斗,漬之一宿,以洗身。洗已,食煮餅,勿以鹽豉也。</p>
<p>百合病,渴不差者,用後方主之。</p>
<p>栝蔞牡蠣散方:</p>
<p>栝蔞根牡蠣熬等分上為細末,飲服方寸匕,日三服。</p>
<p>百合病變發熱者,一作發寒熱。百合滑石散主之。</p>
<p>百合滑石散方:</p>
<p>百合一兩(炙)</p>
<p>滑石三兩上為散,飲服方寸匕,日三服。當微利者,止服,熱則除。</p>
<p>百合病見於陰者,以陽法救之;見於陽者,以陰法救之。見陽攻陰,復發其汗,此為逆;見陰攻陽,乃復下之,此亦為逆。</p>
<p>狐蜮之為病,狀如傷寒,默默欲眠,目不得閉,臥起不安,蝕於喉為蜮,蝕於陰為狐,不欲飲食,惡聞食臭,其面目乍赤、乍黑、乍白。蝕於上部則聲喝,〔一作嗄〕,甘草瀉心湯主之。</p>
<p>甘草瀉心湯方:</p>
<p>甘草四兩黃芩三兩人參三兩乾薑三兩黃連一兩大棗十二枚半夏半升上七味,水一斗,煮取六升,去滓再煎,取三升,溫服一升,日三服。</p>
<p>蝕於下部則咽乾,苦參湯洗之。</p>
<p>蝕於肛者,雄黃熏之。</p>
<p>雄黃上一味為末,筒瓦二枚合之,燒,向肛熏之。(《脈經》云:病人或從呼吸上蝕其咽,或從下焦蝕其肛陰,蝕上為惑,蝕下為狐,狐惑病者,豬苓散主之)。</p>
<p>病者脈數,無熱,微煩,默默但欲臥,汗出,初得之三、四日,目赤如鳩眼;七、八日,目四眥(一本此有黃字)黑。若能食者,膿已成也,赤豆當歸散主之。</p>
<p>赤豆當歸散方:</p>
<p>赤小豆三升(浸,令芽出,曝乾)當歸上二味,杵為散,漿水服方寸匕,日三服。</p>
<p>陽毒之為病,面赤斑斑如錦紋,咽喉痛,唾膿血,五日可治,七日不可治,升麻鱉甲湯主之。</p>
<p>陰毒之為病,面目青,身痛如被杖,咽喉痛。五日可治,七日不可治,升麻鱉甲湯去雄黃、蜀椒主之。</p>
<p>升麻鱉甲湯方:</p>
<p>升麻二兩當歸一兩蜀椒(炒去汗)一兩甘草二兩雄黃半兩(研)</p>
<p>鱉甲手指大一片(炙)</p>
<p>上六味,以水四升,煮取一升,頓服之,老小再服,取汗。(《肘後》、《千金方》:陽毒用升麻湯,無鱉甲,有桂:陰毒用甘草湯,無雄黃。)</p>
<p>瘧病脈證并治第四師曰:瘧脈自弦,弦數者多熱,弦遲者多寒,弦小緊者下之差,弦遲者可溫之,弦緊者可發汗、針灸也,浮大者可吐之,弦數者風發也,以飲食消息止之。</p>
<p>病瘧,以月一日發,當以十五日愈;設不差,當月盡解;如其不差,當云何?師曰:此結為癥瘕,名曰瘧母,急治之,宜鱉甲煎丸。</p>
<p>鱉甲煎丸方:</p>
<p>鱉甲十二分(炙)</p>
<p>烏扇三分(燒)</p>
<p>黃芩三分柴胡六分鼠婦三分(熬)</p>
<p>乾薑三分大黃三分芍藥五分桂枝三分葶藶一分(熬)</p>
<p>石葦三分(去毛)</p>
<p>厚朴三分牡丹五分(去心)</p>
<p>瞿麥二分紫葳三分半夏一分人參一分●蟲五分(熬)</p>
<p>阿膠三分(炙)</p>
<p>蜂窩四分(炙)</p>
<p>赤硝十二分蜣螂六分(熬)</p>
<p>桃仁二分上二十三味,為末,取鍛灶下灰一斗,清酒一斛五斗,浸灰,候酒盡一半,著鱉甲於中,煮令泛爛如膠漆,絞取汁,內諸藥,煎為丸,如梧子大,空心服七丸,日三服。</p>
<p>師曰:陰氣孤絕,陽氣獨發,則熱而少氣煩冤,手足熱而欲嘔,名曰癉瘧,若但熱不寒者,邪氣內藏於心,外舍分肉之間,令人消鑠脫肉。</p>
<p>溫瘧者,其脈如平,身無寒但熱,骨節疼煩,時嘔,白虎加桂枝湯主之。</p>
<p>白虎加桂枝湯方:</p>
<p>知母六兩甘草(炙)二兩石膏一斤粳米二合桂枝(去皮)三兩上銼,每五錢,水一盞半,煎至八分,去滓,溫服,汗出愈。</p>
<p>瘧多寒者,名曰牝瘧,蜀漆散主之。</p>
<p>蜀漆散方:</p>
<p>蜀漆(洗去腥)</p>
<p>雲母(燒二日夜)</p>
<p>龍骨等分上三味,杵為散,未發前以漿水服半錢匕。溫瘧加蜀漆半分,臨發時服一錢匕。</p>
<p>附注:《外臺秘要》方牡蠣湯:治牝瘧牡蠣四兩麻黃四兩(去節)</p>
<p>甘草二兩蜀漆三兩上四味以水八升,先煮蜀漆、麻黃,去上沫,得六升,內諸藥,煮取二升,溫服一升,若吐則勿更服。</p>
<p>柴胡去半夏加栝蔞湯,治瘧病以發渴者,亦治勞瘧。</p>
<p>柴胡八兩人參黃芩甘草各三兩栝蔞根四兩生薑二兩大棗十二枚上七味,以水一斗二升,煮取六升,去渣,再煎,取三升,溫服一升,日二服。</p>
<p>柴胡桂薑湯:治瘧寒多微有熱,或但寒不熱。(服一劑如神)</p>
<p>柴胡半斤桂枝三兩(去皮)</p>
<p>乾薑二兩栝蔞根四兩黃芩三兩牡蠣三兩熬甘草三兩(炙)</p>
<p>上七味,以水一斗二升,煮取六升,去渣,再煎,取三升,溫服一升,日三服,初服微煩,復服汗出便愈。</p>
<p>中風歷節病脈證并治第五夫風之為病,當半身不遂,或但臂不遂者,此為痹。脈微而數,中風使然。</p>
<p>寸口脈浮而緊,緊則為寒,浮則為虛;寒虛相搏,邪在皮膚;浮者血虛,絡脈空虛;賊邪不瀉,或左或右;邪氣反緩;正氣即急,正氣引邪,喎僻不遂。</p>
<p>邪在於絡,肌膚不仁;邪在於經,即重不勝;邪入於腑,即不識人;邪入於臟,舌即難言,口吐涎。</p>
<p>侯氏黑散:治大風四肢煩重,心中惡寒不足者。《外臺》治風癲。</p>
<p>菊花四十分白朮十分細辛三分茯苓三分牡蠣三分桔梗八分防風十分人參三分礬石三分黃芩五分當歸三分乾薑三分芎藭三分桂枝三分上十四味,杵為散,酒服方寸匕,日一服,初服二十日,溫酒調服,禁一切魚肉大蒜,常宜冷食,六十日止,即藥積在腹中不下也。熱食即下矣,冷食自能助藥力。</p>
<p>寸口脈遲而緩,遲則為寒,緩則為虛;營緩則為亡血,衛緩則為中風。邪氣中經則身癢而癮疹;心氣不足,邪氣入中,則胸滿而短氣。</p>
<p>風引湯:除熱癱癇大黃乾薑龍骨各四兩桂枝三兩甘草牡蠣各二兩寒水石滑石赤石脂白石脂紫石英石膏各六兩上十二味,杵,粗篩,以韋囊盛之,取三指撮,井花水三升,煮三沸,溫服一升。治大人風引,少小驚癇瘛瘲,日數十發,醫所不療,除熱方。巢氏云:腳氣宜風引湯。</p>
<p>防己地黃湯:治病如狂狀,妄行,獨語不休,無寒熱,其脈浮。</p>
<p>防己一錢桂枝三錢防風三錢甘草二錢上四味,以酒一杯,浸之一宿,絞取汁,生地黃二斤,●咀,蒸之如斗米飯久,以銅器盛其汁,更絞地黃汁,和,分再服。</p>
<p>頭風摩散方:</p>
<p>大附子一枚(炮)</p>
<p>鹽等分上二味為散,沐了,以方寸匕,已摩疢上,令藥力行。</p>
<p>寸口脈沉而弱,沉即主骨,弱即主筋,沉即為腎,弱即為肝。汗出入水中,如水傷心,歷節黃汗出,故曰歷節。</p>
<p>跗陽脈浮而滑,滑則穀氣實,浮則汗自出。</p>
<p>少陰脈浮而弱,弱則血不足,浮則為風,風血相搏,即疼痛如掣。</p>
<p>盛人脈澀小,短氣,自汗出,歷節痛,不可屈伸,此皆飲酒汗出當風所致。</p>
<p>諸肢節疼痛,身體●羸,腳腫如脫,頭眩短氣,溫溫欲吐,桂枝芍藥知母湯主之。</p>
<p>桂枝芍藥知母湯方:</p>
<p>桂枝四兩芍藥三兩甘草二兩麻黃二兩生薑五兩白朮五兩知母四兩防風四兩附子二枚(炮)</p>
<p>上九味,以水七升,煮取二升,溫服七合,日三服。</p>
<p>味酸則傷筋,筋傷則緩,名曰泄。鹹則傷骨,骨傷則痿,名曰枯。枯泄相搏,名曰斷泄。營氣不通,衛不獨行,營衛俱微,三焦無所御,四屬斷絕,身體羸瘦,獨足腫大,黃汗出,脛冷。假令發熱,便為歷節也。</p>
<p>病歷節不可屈伸,疼痛,烏頭湯主之。</p>
<p>烏頭湯方:治腳氣疼痛,不可屈伸。</p>
<p>麻黃芍藥黃耆各三兩甘草三兩(炙)</p>
<p>川烏五枚(●咀,以蜜二升,煎取一升,即出烏頭)</p>
<p>上五味,●咀四味,以水三升,煮取一升,去滓,內蜜煎中,更煎之,服七合。不知,盡服之。</p>
<p>礬石湯:治腳氣衝心礬石二兩上一味,以漿水一斗五升,煎三五沸,浸腳良。</p>
<p>〔附方〕</p>
<p>《古今錄驗》續命湯:治中風痱,身體不能自收持,口不能言,冒昧不知痛處,或拘急不得轉側。姚云:與大續命同,兼治婦人產後出血者及老人小兒。</p>
<p>麻黃桂枝當歸人參石膏乾薑甘草各三兩芎藭一兩杏仁四十枚上九味,以水一斗,煮取四升,溫服一升,當小汗,薄覆脊,憑几坐,汗出則癒;不汗,更服。無所禁,勿當風。並治但伏不得臥,咳逆上氣,面目浮腫。</p>
<p>《千金》三黃湯:治中風手足拘急,百節疼痛,煩熱心亂,惡寒,經日不欲飲食。</p>
<p>麻黃五分獨活四分細辛二分黃耆二分黃芩三分上五味,以水六升,煮取二升,分溫三服,一服小汗,二服大汗。心熱加大黃二分,腹滿加枳實一枚,氣逆加人參三分,悸加牡蠣三分,渴加栝蔞根三分,先有寒加附子一枚。</p>
<p>《近效方》朮附湯:治風虛頭重眩,苦極,不知食味,暖肌補中,益精氣。</p>
<p>白朮二兩甘草一兩(炙)</p>
<p>附子一枚半(炮去皮)</p>
<p>上三味,剉,每五錢匕,薑五片,棗一枚。水盞半,煎七成,去滓,溫服。</p>
<p>崔氏八味丸:治腳氣上入,少腹不仁。</p>
<p>乾地黃八兩山茱萸四兩薯蕷四兩澤瀉茯苓牡丹皮各三兩桂枝一兩附子一兩(炮)</p>
<p>上八味,末之,煉蜜和丸,梧子大。酒下十五丸,日再服。</p>
<p>《千金方》越婢加朮湯:治肉極,熱則身體津脫,腠理開,汗大泄,厲風氣,下焦腳弱。</p>
<p>麻黃六兩石膏半斤生薑二兩甘草二兩白朮四兩大棗十五枚上六味,以水六升,先煮麻黃去沫,內諸藥,煮取三升,分溫三服。惡風加附子一枚,炮。</p>
<p>血痹虛勞病脈證并治第六問曰:血痹病從何得之?師曰:夫尊榮人骨弱肌膚盛,重困疲勞汗出,臥不時動搖,加被微風,遂得之。但以脈自微澀,在寸口,關上小緊,宜針引陽氣,令脈和緊去則愈。</p>
<p>血痹陰陽俱微,寸口關上微,尺中小緊,外證身體不仁,如風痹狀,黃耆桂枝五物湯主之。</p>
<p>黃耆桂枝五物湯方:</p>
<p>黃耆三兩芍藥三兩桂枝三兩生薑六兩大棗十二枚上五味,以水六升,煮取二升,溫服七合,日三服,一方有人參。</p>
<p>夫男子平人,脈大為勞,極虛亦為勞。</p>
<p>男子面色薄者,主渴及亡血,卒喘悸,脈浮者,裡虛也。</p>
<p>男子脈虛沉弦,無寒熱,短氣裡急,小便不利,面色白,時目瞑,兼衄,少腹滿,此為勞使之然。</p>
<p>勞之為病,其脈浮大,手足煩,春夏劇,秋冬瘥,陰寒精自出,痠削不能行。</p>
<p>男子脈浮弱而澀,為無子,精氣清冷。一作冷。</p>
<p>夫失精家少腹弦急,陰頭寒,目眩一作目眶痛,髮落,脈極虛芤遲,為清穀亡血,失精。脈得諸芤動微緊,男子失精,女子夢交,桂枝加龍骨牡蠣湯主之。</p>
<p>桂枝加龍骨牡蠣湯方:《小品》云:虛弱浮熱汗出者,除桂,加白薇、附子各三分,故曰二加龍骨湯。</p>
<p>桂枝芍藥生薑各三兩甘草二兩大棗十二枚龍骨牡蠣各三兩上七味,以水七升,煮取三升,分溫三服。</p>
<p>天雄散方:</p>
<p>天雄三兩(炮)</p>
<p>白朮八兩桂枝六兩龍骨三兩上四味、杵為散,酒服半錢匕,日三服,不知,稍增之。</p>
<p>男子平人,脈虛弱細微者,喜盜汗也。</p>
<p>人年五六十,其病脈大者,痹俠背行,若腸鳴,馬刀俠癭者,皆為勞得之。</p>
<p>脈沉小遲,名脫氣,其人疾行則喘喝手足逆寒,腹滿,甚則溏泄、食不消化也。</p>
<p>脈弦而大,弦則為減,大則為芤,減則為寒,芤則為虛,虛寒相搏,此名為革。婦人則半產漏下,男子則亡血失精。</p>
<p>虛勞裡急,悸、衄,腹中痛,夢失精,四肢痠疼,手足煩熱,咽乾口燥,小建中湯主之。</p>
<p>小建中湯方:</p>
<p>桂枝三兩(去皮)</p>
<p>甘草三兩(炙)大棗十二枚芍藥六兩生薑三兩膠飴一升上六味,以水七升,煮取三升,去滓,內膠飴,更上微火消解,溫服一升,日三服。嘔家不可用建中湯,以甜故也。</p>
<p>虛勞裡急、諸不足,黃耆建中湯主之。於小建中湯內加黃耆一兩半,餘依上法,氣短胸滿者加生薑;腹滿者去棗,加茯苓一兩半;及療肺虛損不足,補氣加半夏三兩。</p>
<p>虛勞腰痛,少腹拘急,小便不利者,八味腎氣丸主之。方見腳氣中。</p>
<p>腎氣丸方:</p>
<p>乾地黃八兩山藥山茱萸各四兩澤瀉牡丹皮茯苓各三兩桂枝附子(炮)各一兩上八味末之,煉蜜和丸梧子大,酒下十五丸,加至二十五丸,日再服。</p>
<p>虛勞諸不足,風氣百疾,薯蕷丸主之。</p>
<p>薯蕷丸方:</p>
<p>薯蕷三十分當歸桂枝?乾地黃豆黃卷各十分甘草二十八分人參七分芎藭芍藥白朮麥門冬杏仁各六分柴胡桔梗茯苓各五分阿膠七分乾薑三分白斂二分防風六分大棗百枚為膏上二十一味,末之,煉蜜和丸,如彈子大,空腹酒服一丸,一百丸為劑。</p>
<p>虛勞虛煩不得眠,酸棗仁湯主之。</p>
<p>酸棗仁湯方:</p>
<p>酸棗仁二升甘草一兩知母二兩茯苓二兩芎藭二兩深師有生薑二兩上五味,以水八升,煮酸棗仁,得六升,內諸藥,煮取三升,分溫三服。</p>
<p>五勞虛極羸瘦,腹滿不能飲食,食傷、憂傷、飲傷、房室傷、飢傷、勞傷、經絡營衛氣傷,內有乾血,肌膚甲錯,兩目黯黑,緩中補虛,大黃●蟲丸主之。</p>
<p>大黃●蟲丸方:</p>
<p>大黃十分(蒸)</p>
<p>黃芩二兩甘草三兩桃仁一升杏仁一升芍藥四兩乾地黃十兩乾漆一兩虻蟲一升水蛭百枚蠐螬一升●蟲半升上十二味,末之,煉蜜和丸小豆大,酒飲服五丸,日三服。</p>
<p>〔附方〕</p>
<p>《千金翼》炙甘草湯一云復脈湯:治虛勞不足、汗出而悶,脈結悸,行動如常,不出百日,危急者十一日死。</p>
<p>甘草四兩(炙)</p>
<p>桂枝生薑各三兩麥門冬半升麻仁半升人參阿膠各二兩大棗三十枚生地黃一斤上九味,以酒七升,水八升,先煮八味,取三升,去滓,內膠消盡,溫服一升,日三服。</p>
<p>《肘後》獺肝散:治冷勞,又主鬼疰一門相染。</p>
<p>獺肝一具炙乾末之,水服方寸匕,日三服。</p>
<p>肺痿肺癰咳嗽上氣病脈證治第七問曰:熱在上焦者,因咳為肺痿。肺痿之病,從何得之?師曰:或從汗出,或從嘔吐,或從消渴,小便利數,或從便難,又被快藥下利,重亡津液,故得之。</p>
<p>曰:寸口脈數,其人咳,口中反有濁唾涎沫者何?師曰:為肺痿之病。若口中辟辟燥,咳即胸中隱隱痛,脈反滑數,此為肺癰,咳唾膿血。</p>
<p>脈數虛者為肺痿,數實者為肺癰。</p>
<p>問曰:病咳逆,脈之何以知此為肺癰,當有膿血,吐之則死,其脈何類?師曰:寸口脈微而數,微則為風,數則為熱;微則汗出,數則惡寒。風中於衛,呼氣不入;熱過於營,吸而不出。風傷皮毛,熱傷血脈。風舍於肺,其人則咳,口乾喘滿,咽燥不渴,多唾濁沫,時時振寒,熱之所過,血為之凝滯,蓄結癰膿,吐如米粥。始萌可救,膿成則死。</p>
<p>上氣面浮腫,肩息,其脈浮大,不治,又加利尤甚。</p>
<p>上氣喘而躁者,屬肺脹,欲作風水,發汗則愈。</p>
<p>肺痿吐涎沫而不咳者,其人不渴,必遺尿,小便數。所以然者,以上虛不能制下故也。此為肺中冷,必眩,多涎唾,甘草乾薑湯以溫之。若服湯已渴者,屬消渴。</p>
<p>甘草乾薑湯方:</p>
<p>甘草四兩(炙)</p>
<p>乾薑二兩(炮)</p>
<p>上●咀,以水三升,煮取一升五合,去滓,分溫再服。</p>
<p>咳而上氣,喉中水雞聲,射干麻黃湯主之。</p>
<p>射干麻黃湯方:</p>
<p>射干十三枚(一法三兩)</p>
<p>麻黃四兩生薑四兩細辛三兩紫菀三兩款冬花三兩五味子半升大棗七枚半夏大者洗八枚(一法半升)</p>
<p>上九味,以水一斗二升,先煮麻黃兩沸,去上沫,內諸藥,煮取三升,分溫三服。</p>
<p>咳逆上氣,時時吐濁,但坐不得眠,皂莢丸主之。</p>
<p>皂莢八兩(刮去皮,用酥炙)</p>
<p>上一味,末之,蜜丸梧子大,以棗膏和湯服三丸,日三夜一服。</p>
<p>咳而脈浮者,厚朴麻黃湯主之。</p>
<p>厚朴麻黃湯方:</p>
<p>厚朴五兩麻黃四兩石膏如雞子大杏仁半升半夏半升乾薑二兩細辛二兩五味子半升小麥一升上九味,以水一斗二升,先煮小麥熟,去滓,內諸藥,煮取三升,溫服一升,日三服。</p>
<p>脈沈者,澤漆湯主之。</p>
<p>澤漆湯方:</p>
<p>半夏半升紫參五兩一作紫菀澤漆三斤(以東流水五斗,煮取一斗五升)生薑五兩白前五兩甘草黃芩人參桂枝各三兩上九味,●咀,內澤漆汁中,煮取五升,溫服五合,至夜盡。</p>
<p>大逆上氣,咽喉不利,止逆下氣者,麥門冬湯主之。</p>
<p>麥門冬湯方:</p>
<p>麥門冬七升半夏一升人參三兩甘草二兩粳米三合大棗十二枚右六味,以水一斗二升,煮取六升,溫服一升,日三夜一服。</p>
<p>肺癰,喘不得臥,葶藶大棗瀉肺湯主之。</p>
<p>葶藶大棗瀉肺湯方:</p>
<p>葶藶熬令黃色,搗丸如彈子大大棗十二枚上先以水三升,煮棗取二升,去棗,內葶藶,煮取一升,頓服。</p>
<p>咳而胸滿,振寒脈數,咽乾不渴,時出濁唾腥臭,久久吐膿如米粥者,為肺癰,桔梗湯主之。</p>
<p>桔梗湯方:亦治血痹。</p>
<p>桔梗一兩甘草二兩上二味,以水三升,煮取一升,分溫再服,則吐膿血也。</p>
<p>咳而上氣,此為肺脹,其人喘,目如脫狀,脈浮大者,越婢加半夏湯主之。</p>
<p>越婢加半夏湯方:</p>
<p>麻黃六兩石膏半斤生薑三兩大棗十五枚甘草二兩半夏半升上六味,以水六升,先煮麻黃,去上沫,內諸藥,煮取三升,分溫三服。</p>
<p>肺脹,咳而上氣,煩躁而喘,脈浮者,心下有水,小青龍加石膏湯主之。《千金》證治同,外更加脅下痛引缺盆。</p>
<p>小青龍加石膏湯方:</p>
<p>麻黃芍藥桂枝細辛甘草乾薑各三兩五味子半夏各半升石膏二兩上九味,以水一斗,先煮麻黃,去上沫,內諸藥,煮取三升,強人服一升,羸者減之,日三服,小兒服四合。</p>
<p>〔附方〕</p>
<p>《外臺》炙甘草湯:治肺痿涎唾多,心中溫溫液液者。方見虛勞中。</p>
<p>《千金》甘草湯:</p>
<p>甘草上一味,以水三升,煮減半,分溫三服。</p>
<p>《千金》生薑甘草湯:治肺痿,咳唾涎沫不止,咽燥而渴。</p>
<p>生薑五兩人參三兩甘草四兩大棗十五枚上四味,以水七升,煮三升,分溫三服。</p>
<p>《千金》桂枝去芍藥加皂莢湯:治肺痿吐涎沫。</p>
<p>桂枝三兩生薑三兩甘草二兩大棗十枚皂莢一枚去皮子炙焦上五味,以水七升,微微火煮取三升,分溫三服。</p>
<p>《外臺》桔梗白散:治咳而胸滿,振寒脈數,咽乾不渴,時出濁唾腥臭,久久吐膿如米粥者,為肺癰。</p>
<p>桔梗貝母各三分巴豆一分(去皮,熬,研如脂)</p>
<p>上三味,為散,強人飲服半錢匕,羸者減之。病在膈上者吐膿血,在膈下者瀉出,若下多不止,飲冷水一杯則定。</p>
<p>《千金》葦莖湯:治咳有微熱,煩滿,胸中甲錯,是為肺癰。</p>
<p>葦莖二升薏苡仁半升桃仁五十枚瓜瓣半升上四味,以水一斗,先煮葦莖,得五升,去滓,內諸藥,煮取二升,服一升,再服,當吐如膿。</p>
<p>肺癰胸脹滿,一身面目浮腫,鼻塞清涕出,不聞香臭酸辛,咳逆上氣,喘鳴迫塞,葶藶大棗瀉肺湯主之。方見上,三日一劑,可至三四劑,此先服小青龍湯一劑乃進。小青龍湯方見咳嗽門中。</p>
<p>奔豚氣病脈證治第八師曰:病有奔豚,有吐膿,有驚怖,有火邪,此四部病,皆從驚發得之。師曰:奔豚病,從少腹起,上衝咽喉,發作欲死,復還止,皆從驚恐得之。</p>
<p>奔豚氣上衝胸,腹痛,往來寒熱,奔豚湯主之。</p>
<p>奔豚湯方:</p>
<p>甘草芎藭當歸各二兩半夏四兩黃芩二兩生葛五兩芍藥二兩生薑四兩甘李根白皮一升。</p>
<p>上九味,以水二斗,煮取五升,溫服一升,日三夜一服。</p>
<p>發汗後,燒針令其汗,針處被寒,核起而赤者,必發奔豚,氣從少腹上至心,灸其核上各一壯,與桂枝加桂湯主之。</p>
<p>桂枝加桂湯方:</p>
<p>桂枝五兩芍藥三兩甘草二兩(炙)</p>
<p>生薑三兩大棗十二枚上五味,以水七升,微火煮取三升,去滓,溫服一升。</p>
<p>發汗後,臍下悸者,欲作奔豚,茯苓桂枝甘草大棗湯主之。</p>
<p>茯苓桂枝甘草大棗湯方:</p>
<p>茯苓半斤甘草二兩(炙)</p>
<p>大棗十五枚桂枝四兩上四味,以甘瀾水一斗,先煮茯苓、減二升,內諸藥,煮取三升,去滓,溫服一升,日三服。甘瀾水法:取水二斗,置大盆內,以杓揚之,水上有珠子五、六千顆相逐,取用之。</p>
<p>胸痹心痛短氣病脈證治第九師曰:夫脈當取太過不及,陽微陰弦,即胸痹而痛,所以然者,責其極虛也。今陽虛知在上焦,所以胸痹、心痛者,以其陰弦故也。</p>
<p>平人無寒熱,短氣不足以息者,實也。</p>
<p>胸痹之病,喘息咳唾,胸背痛,短氣,寸口脈沉而遲,關上小緊數,栝蔞薤白白酒湯主之。</p>
<p>栝蔞薤白白酒湯方:</p>
<p>栝蔞實一枚(搗)</p>
<p>薤白半斤白酒七升上三味,同煮,取二升,分溫再服。</p>
<p>胸痹不得臥,心痛徹背者,栝蔞薤白半夏湯主之。</p>
<p>栝蔞薤白半夏湯方:</p>
<p>栝蔞實一枚(搗)</p>
<p>薤白三兩半夏半升白酒一斗上四味,同煮,取四升,溫服一升,日三服。</p>
<p>胸痹心中痞,留氣結在胸,胸滿,脅下逆搶心,枳實薤白桂枝湯主之;人參湯亦主之。</p>
<p>枳實薤白桂枝湯方:</p>
<p>枳實四枚厚朴四兩薤白半斤桂枝一兩栝蔞一枚(搗)</p>
<p>上五味,以水五升,先煮枳實、厚朴,取二升,去滓,內諸藥,煮數沸,分溫三服。</p>
<p>人參湯方:</p>
<p>人參甘草乾薑白朮各三兩上四味,以水八升,煮取三升,溫服一升,日三服。</p>
<p>胸痹,胸中氣塞,短氣,茯苓杏仁甘草湯主之;橘枳薑湯亦主之。</p>
<p>茯苓杏仁甘草湯方:</p>
<p>茯苓三兩杏仁五十個甘草一兩上三味,以水一斗,煮取五升,溫服一升,日三服。不差,更服。</p>
<p>橘枳薑湯方:</p>
<p>橘皮一斤枳實三兩生薑半斤上三味,以水五升,煮取二升,分溫再服。《肘後)《千金》云:治胸痹,胸中愊愊如滿,噎塞習習如癢,喉中澀燥,唾沫。</p>
<p>胸痹緩急者,薏苡附子散主之。</p>
<p>薏苡附子散方:</p>
<p>薏苡仁十五兩大附子十枚(炮)</p>
<p>上二味,杵為散,服方寸匕,日三服。</p>
<p>心中痞,諸逆,心懸痛,桂枝生薑枳實湯主之。</p>
<p>桂枝生薑枳實湯方:</p>
<p>桂枝生薑各三兩枳實五枚上三味,以水六升,煮取三升,分溫三服。</p>
<p>心痛徹背,背痛徹心,烏頭赤石脂丸主之。</p>
<p>烏頭赤石脂丸方:</p>
<p>蜀椒一兩一法二分烏頭一分(炮)</p>
<p>附子半兩(炮)</p>
<p>一法一分乾薑一兩一法一分赤石脂一兩一法二分上五味,末之,蜜丸如桐子大,先食服一丸,日三服,不知,稍加服。</p>
<p>〔附方〕</p>
<p>九痛丸:治九種心痛附子三兩(炮)</p>
<p>生狼牙一兩(炙香)</p>
<p>巴豆一兩(去皮心,熬,研如脂)</p>
<p>人參乾薑吳茱萸各一兩上六味,末之,煉蜜丸如桐子大,酒下。強人初服三丸,日三服,弱者二丸。兼治卒中惡,腹脹痛,口不能言;又治連年積冷,流注心胸痛,並冷衝上氣,落馬墜車血疾等皆主之。忌口如常法。</p>
<p>腹滿寒疝宿食病脈證治第十跗陽脈微弦,法當腹滿,不滿者必便難,兩胠疼痛,此虛寒從下上也,當與溫藥服之。</p>
<p>病者腹滿,按之不痛為虛,痛者為實,可下之;舌黃未下者,下之黃自去。</p>
<p>腹滿時減,復如故,此為寒,當與溫藥。</p>
<p>病者痿黃,躁而不渴,胸中寒實,而利不止者,死。</p>
<p>寸口脈弦者,即脅下拘急而痛,其人嗇嗇惡寒也。</p>
<p>夫中寒家,喜欠,其人清涕出,發熱色和者,善嚏。</p>
<p>中寒,其人下利,以裡虛也,欲嚏不能,此人肚中寒。一云痛。</p>
<p>夫瘦人繞臍痛,必有風冷,穀氣不行,而反下之,其氣必衝;不衝者,心下則痞。</p>
<p>病腹滿,發熱十日,脈浮而數,飲食如故,厚朴七物湯主之。</p>
<p>厚朴七物湯方:</p>
<p>厚朴半斤甘草大黃各三兩大棗十枚枳實五枚桂枝二兩生薑五兩上七味,以水一斗,煮取四升,溫服八合,日三服。嘔者加半夏五合,下利去大黃,寒多者加生薑至半斤。</p>
<p>腹中寒氣,雷鳴切痛,胸脅逆滿,嘔吐,附子粳米湯主之。</p>
<p>附子粳米湯方:</p>
<p>附子一枚(炮)</p>
<p>半夏半升甘草一兩大棗十枚粳米半升上五味,以水八升,煮米熟,湯成,去滓,溫服一升,日三服。</p>
<p>痛而閉者,厚朴三物湯主之。</p>
<p>厚朴三物湯方:</p>
<p>厚朴八兩大黃四兩枳實五枚上三味,以水一斗二升,先煮二味,取五升,內大黃,煮取三升,溫服一升。以利為度。</p>
<p>按之心下滿痛者,此為實也,當下之,宜大柴胡湯。</p>
<p>大柴胡湯方:</p>
<p>柴胡半斤黃芩三兩芍藥三兩半夏半升(洗)</p>
<p>枳實四枚(炙)</p>
<p>大黃二兩大棗十二枚生薑五兩上八味:以水一斗二升,煮取六升,去滓,再煎,溫服一升,日三服。</p>
<p>腹滿不減,減不足言,當須下之,宜大承氣湯。</p>
<p>大承氣湯方:見前痙病中心胸中大寒痛,嘔不能飲食,腹中滿,上衝皮起,出見有頭足,上下痛而不可觸近,大建中湯主之。</p>
<p>大建中湯方:</p>
<p>蜀椒二合(去汗)</p>
<p>乾薑四兩人參二兩上三味,以水四升,煮取三升,去滓,內膠飴一升,微火煎取一升半,分溫再服;如一炊頃,可飲粥二升,後更服,當一日食糜,溫覆之。</p>
<p>脅下偏痛,發熱,其脈緊弦,此寒也,以溫藥下之,宜大黃附子湯。</p>
<p>大黃附子湯方:</p>
<p>大黃三兩附子三枚(炮)</p>
<p>細辛二兩右三味,以水五升,煮取二升,分溫三服;若強人煮取二升半,分溫三服,服後如人行四、五里,進一服。</p>
<p>寒氣厥逆,赤丸主之。</p>
<p>赤丸方:</p>
<p>茯苓四兩烏頭二兩(炮)</p>
<p>半夏四兩(洗)一方用桂細辛一兩《千金》作人參上四味,末之,內真朱為色,煉蜜丸如麻子大,先食酒飲下三丸,日再夜一服;不知,稍增之,以知為度。</p>
<p>腹痛,脈弦而緊,弦則衛氣不行,即惡寒,緊則不欲食,邪正相搏,即為寒疝。</p>
<p>寒疝繞臍痛,若發則白汗出,手足厥冷,其脈沉緊者,大烏頭煎主之。</p>
<p>烏頭煎方:</p>
<p>烏頭大者五枚(熬,去皮,不●咀)</p>
<p>上以水三升,煮取一升,去滓,內蜜二升,煎令水氣盡,取二升,強人服七合,弱人服五合。不差,明日更服,不可一日再服。</p>
<p>寒疝腹中痛,及脅痛裡急者,當歸生薑羊肉湯主之。</p>
<p>當歸生薑羊肉湯方:</p>
<p>當歸三兩生薑五兩羊肉一斤上三味,以水八升,煮取三升,溫服七合,日三服。若寒多者,加生薑成一斤;痛多而嘔者,加橘皮二兩,白朮一兩。加生薑者,亦加水五升,煮取三升二合,服之。</p>
<p>寒疝腹中痛,逆冷,手足不仁,若身疼痛,灸刺諸藥不能治,抵當烏頭桂枝湯主之。</p>
<p>烏頭桂枝湯方:</p>
<p>烏頭上一味,以蜜二斤,煎減半,去滓,以桂枝湯五合解之,得一升後,初服二合,不知,即服三合;又不知,復加至五合。其知者,如醉狀,得吐者,為中病。</p>
<p>桂枝湯方:</p>
<p>桂枝三兩(去皮)</p>
<p>芍藥三兩甘草二兩(炙)</p>
<p>生薑三兩大棗十二枚上五味,銼,以水七升,微火煮取三升,去滓。</p>
<p>其脈數而緊,乃弦,狀如弓弦,按之不移。脈數弦者,當下其寒;脈緊大而遲者,必心下堅;脈大而緊者,陽中有陰,可下之。</p>
<p>〔附方〕</p>
<p>《外臺?卷七》烏頭湯:治寒疝腹中絞痛,賊風入攻五臟,拘急不得轉側,發作有時,使人陰縮,手足厥逆。方見上。</p>
<p>《外臺》柴胡桂枝湯:治心腹卒中痛者。</p>
<p>柴胡四兩黃芩人參芍藥桂枝生薑各一兩半甘草一兩半夏二合半大棗六枚上九味,以水六升,煮取三升,溫服一升,日三服。</p>
<p>《外臺》走馬湯:治中惡心痛腹脹,大便不通。</p>
<p>杏仁二枚巴豆二枚(去皮心,熬)</p>
<p>上二味,以綿纏捶令碎,熱湯二合,捻取白汁飲之,當下。老小量之。通治飛尸鬼擊病。</p>
<p>問曰:人病有宿食,何以別之?師曰:寸口脈浮而大,按之反澀,尺中亦微而澀,故知有宿食,大承氣湯主之。</p>
<p>脈數而滑者,實也,此有宿食,下之愈,宜大承氣湯。</p>
<p>下利不欲食者,有宿食也,當下之,宜大承氣湯。大承氣湯方:見前痙病中。</p>
<p>宿食在上脘,當吐之,宜瓜蒂散。</p>
<p>瓜蒂散方:</p>
<p>瓜蒂一分(熬黃)</p>
<p>赤小豆一分(煮)</p>
<p>上二味,杵為散,以香豉七合煮取汁,和散一錢匕,溫服之,不吐者少加之,以快吐為度而止。亡血及虛者,不可與之。</p>
<p>脈緊如轉索無常者,有宿食也。</p>
<p>脈緊,頭痛風寒,腹中有宿食不化也。一云寸口脈緊。</p>
<p>五臟風寒積聚病脈證并治第十一肺中風者,口燥而喘,身運而重,冒而腫脹。</p>
<p>肺中寒,吐濁涕。</p>
<p>肺死臟,浮之虛,按之弱如蔥葉,下無根者死。</p>
<p>肝中風者,頭目?,兩脅痛,行常傴,令人嗜甘。</p>
<p>肝中寒者,兩臂不舉,舌本燥,喜太息,胸中痛,不得轉側,食則吐而汗出也。《脈經》、《千金》云:時盜汗、咳,食已吐其汁。</p>
<p>肝死臟,浮之弱,按之如索不來,或曲如蛇行者,死。</p>
<p>肝著,其人常欲蹈其胸上,先未苦時,但欲飲熱,旋覆花湯主之。臣億等校諸本旋覆花湯方皆同。</p>
<p>旋覆花湯方:</p>
<p>旋覆花三兩蔥十四莖新絳少許上三味,以水三升,煮取一升,頓服之。</p>
<p>心中風者,翕翕發熱,不能起,心中飢,食即嘔吐。</p>
<p>心中寒者,其人苦病心如噉蒜狀,劇者心痛徹背,背痛徹心,譬如蠱注。其脈浮者,自吐乃愈。</p>
<p>心傷者,其人勞倦,即頭面赤而下重,心中痛而自煩,發熱,當臍跳,其脈弦,此為心臟傷所致也。</p>
<p>心死臟,浮之實如麻豆,按之益躁急者,死。</p>
<p>邪哭使魂魄不安者,血氣少也;血氣少者屬於心,心氣虛者,其人則畏,合目欲眠,夢遠行而精神離散,魂魄妄行。陰氣衰者為癲,陽氣衰者為狂。</p>
<p>脾中風者,翕翕發熱,形如醉人,腹中煩重,皮目??而短氣。</p>
<p>脾死臟,浮之大堅,按之如覆盃潔潔,狀如搖者,死。臣億等詳五臟各有中風、中寒,今脾只載中風,腎中風、中寒俱不載者以古文簡亂極多,去古既遠,無文可補綴也。</p>
<p>跗陽脈浮而澀,浮則胃氣強,澀則小便數,浮澀相搏,大便則堅,其脾為約,麻子仁丸主之。</p>
<p>麻子仁丸方:</p>
<p>麻子仁二升芍藥半斤枳實一斤大黃一斤厚朴一尺杏仁一升上六味,末之,煉蜜和丸,梧子大,飲服十丸,日三,以知為度。</p>
<p>腎著之病,其人身體重,腰中冷,如坐水中,形如水狀,反不渴,小便自利,飲食如故,病屬下焦,身勞汗出,衣裡冷濕,久久得之,腰以下冷痛,腹重如帶五千錢,甘薑苓朮湯主之。</p>
<p>甘草乾薑苓朮湯方:</p>
<p>甘草白朮各二兩乾薑茯苓各四兩上四味,以水四升,煮取三升,分溫三服,腰中即溫。</p>
<p>腎死臟,浮之堅,按之亂如轉丸,益下入尺中者,死。</p>
<p>問曰:三焦竭部,上焦竭善噫,何謂也?師曰:上焦受中焦氣未和,不能消穀,故能噫耳。下焦竭,即遺溺失便,其氣不和,不能自禁制,不須治,久則愈。</p>
<p>師曰:熱在上焦者,因咳為肺痿;熱在中焦者,則為堅;熱在下焦者,則尿血,亦令淋秘不通,大腸有寒者,多鶩溏;有熱者,便腸垢。小腸有寒者,其人下重便血,有熱者,必痔。</p>
<p>問曰:病有積、有聚、有●氣,何謂也?師曰:積者,臟病也,終不移;聚者,腑病也,發作有時,展轉痛移,為可治,●氣者,脅下痛,按之則愈,復發為●氣。</p>
<p>諸積大法,脈來細而附骨者,乃積也。寸口,積在胸中;微出寸口,積在喉中;關應上,積在臍旁;上關上,積在心下;微下關,積在少腹;尺中,積在氣衝。脈出左,積在左;脈出右,積在右;脈兩出,積在中央。各以其部處之。</p>
<p>痰飲咳嗽病脈證并治第十二問曰:夫飲有四,何謂也?師曰:有痰飲,有懸飲,有溢飲,有支飲。</p>
<p>問曰:四飲何以為異?師曰:其人素盛今瘦,水走腸間,瀝瀝有聲謂之痰飲,飲後水流在脅下,咳唾引痛,謂之懸飲。飲水流行,歸於四肢,當汗出而不汗出,身體疼重,謂之溢飲。咳逆倚息,短氣不得臥,其形如腫,謂之支飲。</p>
<p>水在心,心下堅築,短氣,惡水不欲飲。</p>
<p>水在肺,吐涎沫,欲飲水。</p>
<p>水在脾,少氣身重。</p>
<p>水在肝,脅下支滿,嚏而痛。</p>
<p>水在腎,心下悸。</p>
<p>夫心下有留飲,其人背寒冷如手大。</p>
<p>留飲者,脅下痛引缺盆,咳嗽則輒已,一作轉甚。</p>
<p>胸中有留飲,其人短氣而渴;四肢歷節痛,脈沉者,有留飲。</p>
<p>膈上病痰,滿喘咳吐,發則寒熱,背痛腰疼,目泣自出,其人振振身?劇,必有伏飲。</p>
<p>夫病人飲水多,必暴喘滿。凡食少飲多,水停心下。甚者則悸,微者短氣。</p>
<p>脈雙弦者寒也,皆大下後善虛。脈偏弦者飲也。</p>
<p>肺飲不弦,但苦喘短氣。</p>
<p>支飲亦喘而不能臥,加短氣。其脈平也。</p>
<p>病痰飲者,當以溫藥和之。</p>
<p>心下有痰飲,胸脅支滿,目眩。苓桂朮甘湯主之。</p>
<p>苓桂朮甘湯方:</p>
<p>茯苓四兩桂枝白朮各三兩甘草二兩上四味,以水六升,煮取三升,分溫三服,小便則利。</p>
<p>夫短氣有微飲,當從小便去之,苓桂朮甘湯主之;方見上。腎氣丸亦主之。方見腳氣中。</p>
<p>病者脈伏,其人欲自利,利反快,雖利,心下續堅滿,此為留飲欲去故也,甘遂半夏湯主之。</p>
<p>甘遂半夏湯方:</p>
<p>甘遂大者三枚半夏十二枚(以水一升,煮取半升,去滓)芍藥五枚甘草如指大一枚(炙)一本作無。</p>
<p>上四味,以水二升煮取半升,去滓,以蜜半升,和藥汁煎取八合。頓服之。</p>
<p>脈浮而細滑,傷飲。</p>
<p>脈弦數,有寒飲,冬夏難治。</p>
<p>脈沉而弦者,懸飲內痛。</p>
<p>病懸飲者,十棗湯主之。</p>
<p>十棗湯方:</p>
<p>芫花(熬)</p>
<p>甘遂大戟各等分上三味,搗篩,以水一升五合,先煮肥大棗十枚。取六合,去滓,內藥末,強人服一錢七分,羸人服半錢,平旦溫服之;不下者,明日更加半錢。得快下後,糜粥自養。</p>
<p>病溢飲者,當發其汗,大青龍湯主之;小青龍湯亦主之。</p>
<p>大青龍湯方:</p>
<p>麻黃六兩(去節)</p>
<p>桂枝三兩(去皮)</p>
<p>甘草二兩(炙)杏仁四十個(去皮尖)</p>
<p>生薑三兩(切)</p>
<p>大棗十二枚石膏如雞子大(碎)</p>
<p>上七味,以水九升,先煮麻黃,減二升,去上沫,內諸藥,煮取三升,去滓,溫服一升,取微似汗,汗多者,溫粉粉之。</p>
<p>小青龍湯方:</p>
<p>麻黃三兩(去節)</p>
<p>芍藥三兩五味子半升乾薑三兩甘草三兩(炙)細辛三兩桂枝三兩(去皮)</p>
<p>半夏半升(洗)</p>
<p>上八味,以水一斗,先煮麻黃,減二升,去上沫,內諸藥,煮取三升,去滓,溫服一升。</p>
<p>膈間支飲,其人喘滿,心下痞堅,面色黧黑,其脈沉緊,得之數十日,醫吐下之不愈,木防己湯主之。虛者即愈,實者三日復發,復與不愈者,宜木防己湯去石膏加茯苓芒硝湯主之。</p>
<p>木防己湯方:</p>
<p>木防己三兩石膏十二枚雞子大桂枝三兩人參四兩上四味,以水六升,煮取二升,分溫再服。</p>
<p>木防己去石膏加茯苓芒硝湯方:</p>
<p>木防己桂枝各二兩人參四兩芒硝三合茯苓四兩上五味,以水六升,煮取二升,去滓,內芒硝,再微煎,分溫再服,微利則愈。</p>
<p>心下有支飲,其人苦冒眩,澤瀉湯主之。</p>
<p>澤瀉湯方:</p>
<p>澤瀉五兩白朮二兩上二味,以水二升,煮取一升,分溫再服。</p>
<p>支飲胸滿者,厚朴大黃湯主之。</p>
<p>厚朴大黃湯方:</p>
<p>厚朴一尺大黃六兩枳實四枚上三味,以水五升,煮取二升,分溫再服。</p>
<p>支飲不得息,葶藶大棗瀉肺湯主之。方見肺癰中。</p>
<p>嘔家本渴,渴者為欲解,今反不渴,心下有支飲故也,小半夏湯主之。《千金》云:「小半夏加茯苓湯」。</p>
<p>小半夏湯方:</p>
<p>半夏一升生薑半斤上二味,以水七升,煮取一升半,分溫再服。</p>
<p>腹滿,口舌乾燥,此腸間有水氣,己椒藶黃丸主之。</p>
<p>己椒藶黃丸方:</p>
<p>防己椒目葶藶(熬)</p>
<p>大黃各一兩上四味,末之,蜜丸如梧子大,先食飲服一丸,日三服,稍增,口中有津液,渴者加芒硝半兩。</p>
<p>卒嘔吐,心下痞,膈間有水,眩悸者,小半夏加茯苓湯主之。</p>
<p>小半夏加茯苓湯方:</p>
<p>半夏一升生薑半斤茯苓三兩一法四兩。</p>
<p>上三味,以水七升,煮取一升五合,分溫再服。</p>
<p>假令瘦人臍下有悸,吐涎沫而癲眩,此水也,五苓散主之。</p>
<p>五苓散方:澤瀉一兩一分豬苓三分(去皮)</p>
<p>茯苓三分白朮三分桂枝二分(去皮)</p>
<p>上五味,為末,白飲服方寸匕,日三服,多飲暖水,汗出愈。</p>
<p>〔附方〕</p>
<p>《外臺》茯苓飲:治心胸中有停痰宿水,自吐出水後,心胸間虛,氣滿,不能食,消痰氣,令能食。</p>
<p>茯苓人參白朮各三兩枳實二兩橘皮二兩半生薑四兩上六味,水六升,煮取一升八合,分溫三服,如人行八九里進之。</p>
<p>咳家其脈弦,為有水。十棗湯主之。方見上。</p>
<p>夫有支飲家,咳煩胸中痛者,不卒死,至一百日或一歲,宜十棗湯。方見上。</p>
<p>久咳數歲,其脈弱者可治;實大數者死;其脈虛者必苦冒。其人本有支飲在胸中故也。治屬飲家。</p>
<p>咳逆倚息不得臥,小青龍湯主之。方見上。</p>
<p>青龍湯下已,多唾口燥,寸脈沉,尺脈微,手足厥逆,氣從小腹上衝胸咽,手足痹,其面翕熱如醉狀,因復下流陰股,小便難,時復冒者,與茯苓桂枝五味甘草湯,治其氣衝。</p>
<p>桂苓五味甘草湯方:</p>
<p>茯苓四兩桂枝四兩(去皮)</p>
<p>甘草三兩(炙)</p>
<p>五味子半升上四味,以水八升,煮取三升,去滓,分溫三服。</p>
<p>衝氣即低,而反更咳、胸滿者,用桂苓五味甘草湯去桂加乾薑、細辛,以治其咳滿。</p>
<p>苓甘五味薑辛湯方:</p>
<p>茯苓四兩甘草乾薑細辛各三兩五味子半升上五味,以水八升,煮取三升去滓,溫服半升,日三服。</p>
<p>咳滿即止,而更復渴,衝氣復發者,以細辛、乾薑為熱藥也,服之當遂渴,而渴反止者,為支飲也。支飲者法當冒,冒者必嘔,嘔者復內半夏以去其水。</p>
<p>桂苓五味甘草去桂加薑辛夏湯方:</p>
<p>茯苓四兩甘草細辛乾薑各二兩五味子半夏各半升上六味,以水八升,煮取三升,去滓,溫服半升,日三。</p>
<p>水去嘔止,其人形腫者,加杏仁主之。其證應內麻黃,以其人遂痹,故不內之。若逆而內之者,必厥,所以然者,以其人血虛,麻黃發其陽故也。</p>
<p>苓甘五味加薑辛半夏杏仁湯方:</p>
<p>茯苓四兩甘草三兩五味半升乾薑三兩細辛三兩半夏半升杏仁半升(去皮尖)</p>
<p>上七味,以水一斗,煮取三升,去滓,溫服半升,日三服。</p>
<p>若面熱如醉,此為胃熱上衝熏其面,加大黃以利之。</p>
<p>苓甘五味加薑辛半杏大黃湯方:</p>
<p>茯苓四兩甘草三兩五味半升乾薑三兩細辛三兩半夏半升杏仁半升大黃三兩上八味,以水一斗,煮取三升,去滓,溫服半升,日三服。</p>
<p>先渴後嘔,為水停心下,此屬飲家,小半夏加茯苓湯主之。方見上。</p>
<p>消渴小便利淋病脈證并治第十三厥陰之為病,消渴氣上衝心,心中疼熱,飢而不欲食,食即吐,下之不肯止。</p>
<p>寸口脈浮而遲,浮即為虛,遲即為勞;虛則衛氣不足,勞則營氣竭。</p>
<p>跗陽脈浮而數,浮即為氣,數即消穀而大堅一作緊,氣盛則溲數,溲數即堅,堅數相搏,即為消渴。</p>
<p>男子消渴,小便反多,以飲一斗,小便一斗,腎氣丸主之。方見腳氣中。</p>
<p>脈浮,小便不利,微熱消渴者,宜利小便發汗,五苓散主之。方見上。</p>
<p>渴欲飲水,水入則吐者,名曰水逆,五苓散主之。方見上渴欲飲水不止者,文蛤散主之。</p>
<p>文蛤散方:</p>
<p>文蛤五兩上一味,杵為散,以沸湯五合,和服方寸匕。</p>
<p>淋之為病,小便如粟狀,小腹弦急,痛引臍中。</p>
<p>跗陽脈數,胃中有熱,即消穀引食,大便必堅,小便即數。</p>
<p>淋家不可發汗,發汗則必便血。</p>
<p>小便不利者,有水氣,其人若渴,栝蔞瞿麥丸主之。</p>
<p>栝蔞瞿麥丸方:</p>
<p>栝蔞根二兩茯苓三兩薯蕷三兩附子一枚(炮)</p>
<p>瞿麥一兩上五味,末之,煉蜜丸梧子大,飲服三丸,日三服;不知。增至七八丸,以小便利,腹中溫為知。</p>
<p>小便不利,蒲灰散主之;滑石白魚散,茯苓戎鹽湯并主之。</p>
<p>蒲灰散方:</p>
<p>蒲灰七分滑石三分上二味,杵為散,飲服方寸匕,日三服。</p>
<p>滑石白魚散方:</p>
<p>滑石二分亂髮二分〔燒〕</p>
<p>白魚三分上三味,杵為散,飲服方寸匕,日三服。</p>
<p>茯苓戎鹽湯方:</p>
<p>茯苓半斤白朮二兩戎鹽彈丸大一枚上三味渴欲飲水,口乾舌燥者,白虎加人參湯主之。方見中暍中。</p>
<p>脈浮發熱,渴欲飲水,小便不利者,豬苓湯主之。</p>
<p>豬苓湯方:</p>
<p>豬苓(去皮)</p>
<p>茯苓阿膠滑石澤瀉各一兩上五味,以水四升,先煮四味,取二升,去滓,內膠烊消,溫服七合,日三服。</p>
<p>水氣病脈證并治第十四師曰:病有風水、有皮水、有正水、有石水、有黃汗。風水其脈自浮,外證骨節疼痛,惡風;皮水其脈亦浮,外證胕腫,按之沒指,不惡風,其腹如鼓,不渴,當發其汗。正水其脈沉遲,外證自喘;石水其脈自沉,外證腹滿不喘。黃汗其脈沉遲,身發熱,胸滿,四肢頭面腫,久不愈,必致癰膿。</p>
<p>脈浮而洪,浮則為風,洪則為氣,風氣相搏,風強則為隱疹,身體為癢,癢為泄風,久為痂癩;氣強則為水,難以俯仰,風氣相擊,身體洪腫,汗出乃愈,惡風則虛,此為風水;不惡風者,小便通利,上焦有寒,其口多涎,此為黃汗。</p>
<p>寸口脈沉滑者,中有水氣,面目腫大有熱,名曰風水。視人之目窠上微擁,如蠶新臥起狀,其頸脈動,時時咳,按其手足上,陷而不起者,風水。</p>
<p>太陽病,脈浮而緊,法當骨節疼痛,反不痛,身體反重而痠,其人不渴,汗出即愈,此為風水。惡寒者,此為極虛發汗得之。</p>
<p>渴而不惡寒者,此為皮水。</p>
<p>身腫而冷,狀如周痹,胸中窒,不能食,反聚痛,暮躁不得眠,此為黃汗。痛在骨節。</p>
<p>咳而喘,不渴者,此為脾脹,其狀如腫,發汗即愈。</p>
<p>然諸病此者,渴而下利,小便數者,皆不可發汗。</p>
<p>裡水者,一身面目黃腫,其脈沉,小便不利,故令病水。假如小便自利,此亡津液,故令渴也。越婢加朮湯主之。方見下。</p>
<p>跗陽脈當伏,今反緊,本自有寒,疝瘕,腹中痛,醫反下之,下之即胸滿短氣。</p>
<p>跗陽脈當伏,今反數,本自有熱,消穀,小便數,今反不利,此欲作水。</p>
<p>寸口脈浮而遲,浮脈則熱,遲脈則潛,熱潛相搏,名曰沉。跗陽脈浮而數,浮脈即熱,數脈即止,熱止相搏,名曰伏。沉伏相搏,名曰水。沉則脈絡虛,伏則小便難,虛難相搏,水走皮膚,即為水矣。</p>
<p>寸口脈弦而緊,弦則衛氣不行,即惡寒,水不沾流,走於腸間。</p>
<p>少陰脈緊而沉,緊則為痛,沉則為水,小便即難。</p>
<p>脈得諸沉,當責有水,身體腫重,水病脈出者,死。</p>
<p>夫水病人,目下有臥蠶,面目鮮澤,脈伏,其人消渴。病水腹大,小便不利,其脈沉絕者,有水,可下之。</p>
<p>問曰:病下利後,渴飲水,小便不利,腹滿因腫者,何也?答曰:此法當病水,若小便自利及汗出者,自當愈。</p>
<p>心水者,其身重而少氣,不得臥,煩而躁,其人陰腫。</p>
<p>肝水者,其腹大,不能自轉側,脅下腹痛,時時津液微生,小便續通。</p>
<p>肺水者,其身腫,小便難,時時鴨溏。</p>
<p>脾水者,其腹大,四肢苦重,津液不生,但苦少氣,小便難。</p>
<p>腎水者,其腹大,臍腫腰痛,不得溺,陰下濕如牛鼻上汗,其足逆冷,面反瘦。</p>
<p>師曰:諸有水者,腰以下腫,當利小便;腰以上腫,當發汗乃愈。</p>
<p>師曰:寸口脈沉而遲,沉則為水,遲則為寒,寒水相搏。跗陽脈伏,水穀不化,脾氣衰則鶩溏,胃氣衰則身腫。少陽脈卑,少陰脈細,男子則小便不利,婦人則經水不通;經為血,血不利則為水,名曰血分。</p>
<p>問曰:病有血分水分,何也?師曰:經水前斷,後病水,名曰血分,此病難治;先病水,後經水斷,名曰水分,此病易治。何以故?去水,其經自下。</p>
<p>問曰:病者苦水,面目身體四肢皆腫,小便不利,脈之,不言水,反言胸中痛,氣上衝咽,狀如炙肉,當微咳喘,審如師言,其脈何類?</p>
<p>師曰:寸口脈沉而緊,沉為水,緊為寒,沉緊相搏,結在關元,始時尚微,年盛不覺,陽衰之後,營衛相干,陽損陰盛,結寒微動,腎氣上衝,喉咽塞噎,脅下急痛。醫以為留飲而大下之,氣擊不去,其病不除。後重吐之,胃家虛煩,咽燥欲飲水,小便不利,水穀不化,面目手足浮腫。又與葶藶丸下水,當時如小差,食飲過度,腫復如前,胸脅苦痛,象若奔豚,其水揚溢,則浮咳喘逆。當先攻擊衝氣,令止,乃治咳;咳止,其喘自差。先治新病,病當在後。</p>
<p>風水,脈浮身重,汗出惡風者,防己黃耆湯主之。腹痛者加芍藥。</p>
<p>防己黃耆湯方:方見濕病中。</p>
<p>風水惡風,一身悉腫,脈浮不渴,續自汗出,無大熱,越婢湯主之。</p>
<p>越婢湯方:</p>
<p>麻黃六兩石膏半斤生薑三兩甘草二兩大棗十五枚上五味,以水六升,先煮麻黃,去上沫,內諸藥,煮取三升,分溫三服。惡風者加附子一枚炮。風水加朮四兩。(《古今錄驗》)</p>
<p>皮水為病,四肢腫,水氣在皮膚中,四肢聶聶動者,防己茯苓湯主之。</p>
<p>防己茯苓湯方:</p>
<p>防己三兩黃耆三兩桂枝三兩茯苓六兩甘草二兩上五味,以水六升,煮取二升,分溫三服。</p>
<p>裡水,越婢加朮湯主之,甘草麻黃湯亦主之。</p>
<p>越婢加朮湯方:方見上。於內加白朮四兩。又見腳氣中。</p>
<p>甘草麻黃湯方:</p>
<p>甘草二兩麻黃四兩上二味,以水五升,先煮麻黃,去上沫,內甘草,煮取三升,溫服一升,重覆汗出,不汗,再服。慎風寒。</p>
<p>水之為病,其脈沉小,屬少陰;浮者為風,無水虛脹者,為氣。水,發其汗即已。脈沉者宜麻黃附子湯;浮者宜杏子湯。</p>
<p>麻黃附子湯方:</p>
<p>麻黃三兩甘草二兩附子一枚(炮)</p>
<p>上三味,以水七升,先煮麻黃,去上沫,內諸藥,煮取二升半,溫服八分,日三服。</p>
<p>杏子湯方:未見,恐是麻黃杏仁甘草石膏湯。</p>
<p>厥而皮水者,蒲灰散主之。方見消渴中。</p>
<p>問曰:黃汗之為病,身體腫,一作重。發熱汗出而渴,狀如風水,汗沾衣,色正黃如柏汁,脈自沉,何從得之?師曰:以汗入水中浴,水從汗孔入得之,宜耆芍桂酒湯主之。</p>
<p>黃耆芍藥桂枝苦酒湯方:</p>
<p>黃耆五兩芍藥三兩桂枝三兩上三味,以苦酒一升,水七升,相和,煮取三升,溫服一升,當心煩,服至六七日乃解。若心煩不止者,以苦酒阻故也。一方用美酒醯代苦酒。</p>
<p>黃汗之病,兩脛自冷;假令發熱,此屬歷節。食已汗出,又身常暮臥盜汗出者,此勞氣也。若汗出已反發熱者,久久其身必甲錯;發熱不止者,必生惡瘡。</p>
<p>若身重,汗出已輒輕者,久久必身?,?即胸中痛,又從腰以上必汗出,下無汗,腰髖弛痛,如有物在皮中狀,劇者不能食,身疼重,煩躁,小便不利,此為黃汗,桂枝加黃耆湯主之。</p>
<p>桂枝加黃耆湯方:</p>
<p>桂枝三兩芍藥三兩生薑三兩大棗十二枚甘草黃耆各二兩上六味,以水八升,煮取三升,溫服一升,須臾飲熱稀粥一升餘,以助藥力,溫服取微汗;若不汗,更服。</p>
<p>師曰:寸口脈遲而澀,遲則為寒,澀為血不足。跗陽脈微而遲,微則為氣,遲則為寒。寒氣不足,則手足逆冷;手足逆冷,則營衛不利;營衛不利,則腹滿脅鳴相逐;氣轉膀胱,營衛俱勞;陽氣不通即身冷,陰氣不通即骨疼;陽前通則惡寒,陰前通則痹不仁;陰陽相得,其氣乃行,大氣一轉,其氣乃散;實則失氣,虛則遺尿,名曰氣分。</p>
<p>氣分,心下堅,大如盤,邊如旋杯,水飲所作,桂枝去芍藥加麻辛附子湯主之。</p>
<p>桂枝去芍藥加麻黃細辛附子湯方:</p>
<p>桂枝三兩生薑三兩甘草二兩大棗十二枚麻黃二兩細辛二兩附子一枚(炮)</p>
<p>上七味,以水七升,煮麻黃,去上沫,內諸藥,煮取二升,分溫三服,當汗出,如蟲行皮中,即愈。</p>
<p>心下堅,大如盤,邊如旋盤,水飲所作,枳朮湯主之。</p>
<p>枳朮湯方:</p>
<p>枳實七枚白朮二兩上二味,以水五升,煮取三升,分溫三服,腹中軟即當散也。</p>
<p>〔附方〕</p>
<p>《外臺》防己黃耆湯:治風水,脈浮為在表,其人或頭汗,表無他病,病者但下重,從腰以上為和,腰以下當腫及陰,難以屈伸。方見風濕中。</p>
<p>黃疸病脈證并治第十五寸口脈浮而緩,浮則為風,緩則為痹。痹非中風。四肢苦煩、脾色必黃,瘀熱以行。</p>
<p>跗陽脈緊而數,數則為熱,熱則消穀,緊則為寒,食即為滿。尺脈浮為傷腎,跗陽脈緊為傷脾。風寒相搏,食穀即眩,穀氣不消,胃中苦濁,濁氣下流,小便不通,陰被其寒,熱流膀胱,身體盡黃,名曰穀疸。</p>
<p>額上黑,微汗出,手足中熱,薄暮即發,膀胱急,小便自利,名曰女勞疸;腹如水狀不治。</p>
<p>心中懊?而熱,不能食,時欲吐,名曰酒疸。</p>
<p>陽明病,脈遲者,食難用飽,飽則發煩頭眩,小便必難。此欲作穀疸。雖下之,腹滿如故,所以然者,脈遲故也。</p>
<p>夫病酒黃疸,必小便不利,其候心中熱,足下熱,是其證也。</p>
<p>酒黃疸者,或無熱,靖言了了,腹滿欲吐,鼻燥;其脈浮者先吐之,沉弦者先下之。</p>
<p>酒疸,心中熱,欲嘔者,吐之愈。</p>
<p>酒疸下之,久久為黑疸,目青面黑,心中如噉蒜虀狀,大便正黑,皮膚爪之不仁,其脈浮弱,雖黑微黃,故知之。</p>
<p>師曰:病黃疸,發熱煩喘,胸滿口燥者,以病發時火劫其汗,兩熱所得。然黃家所得,從濕得之。一身盡發熱而黃,肚熱,熱在裡,當下之。</p>
<p>脈沉,渴欲飲水,小便不利者,皆發黃。</p>
<p>腹滿,舌痿黃,燥不得睡,屬黃家。舌痿疑作身痿。</p>
<p>黃疸之病,當以十八日為期,治之十日以上瘥,反劇為難治。</p>
<p>疸而渴者,其疸難治;疸而不渴者,其疸可治。發於陰部,其人必嘔;陽部,其人振寒而發熱也。</p>
<p>穀疸之為病,寒熱不食,食即頭眩,心胸不安,久久發黃為穀疸,茵陳蒿湯主之。</p>
<p>茵陳蒿湯方:</p>
<p>茵陳蒿六兩梔子十四枚大黃二兩上三味,以水一斗,先煮茵陳,減六升,內二味,煮取三升,去滓,分溫三服。小便當利,尿如皂角汁狀,色正赤,一宿腹減,黃從小便去也。</p>
<p>黃家日晡所發熱,而反惡寒,此為女勞得之;膀胱急,少腹滿、身盡黃,額上黑,足下熱,因作黑疸,其腹脹如水狀,大便必黑,時溏,此女勞之病,非水也。腹滿者難治。硝石礬石散主之。</p>
<p>硝石礬石散方:</p>
<p>硝石、礬石(燒)等分上二味為散,以大麥粥汁和服方寸匕,日三服。病隨大小便去、小便正黃,大便正黑,是候也。</p>
<p>酒黃疸,心中懊?或熱痛,梔子大黃湯主之。</p>
<p>梔子大黃湯方:</p>
<p>梔子十四枚大黃一兩枳實五枚豉一升上四味,以水六升,煮取二升,分溫三服。</p>
<p>諸病黃家,但利其小便;假令脈浮,當以汗解之,宜桂枝加黃耆湯主之。方見水氣病中。</p>
<p>諸黃,豬膏髮煎主之。</p>
<p>豬膏髮煎方:</p>
<p>豬膏半斤亂髮如雞子大三枚上二味,和膏中煎之,髮消藥成,分再服。病從小便出。</p>
<p>黃疸病,茵陳五苓散主之。一本云茵陳湯及五苓散并主之。</p>
<p>茵陳五苓散方:</p>
<p>茵陳蒿末十分五苓散五分方見痰飲中。</p>
<p>上二物和,先食飲方寸匕,日三服。</p>
<p>黃疸腹滿,小便不利而赤,自汗出,此為表和裡實,當下之,宜大黃硝石湯。</p>
<p>大黃硝石湯方:</p>
<p>大黃黃柏硝石各四兩梔子十五枚上四味,以水六升,煮取二升,去滓,內硝,更煮取一升,頓服。</p>
<p>黃疸病,小便色不變,欲自利,腹滿而喘,不可除熱,熱除必噦。噦者,小半夏湯主之。方見痰飲中。</p>
<p>諸黃,腹痛而嘔者,宜柴胡湯。必小柴胡湯,方見嘔吐中。</p>
<p>男子黃,小便自利,當與虛勞小建中湯。方見虛勞中。</p>
<p>〔附方〕</p>
<p>瓜蒂湯:治諸黃。方見暍病中。</p>
<p>《千金》麻黃醇酒湯:治黃疸。</p>
<p>麻黃三兩上一味,以美清酒五升,煮去二升半,頓服盡。冬月用酒,春月用水煮之。</p>
<p>驚悸吐衄下血胸滿瘀血病脈證治第十六寸口脈動而弱,動即為驚,弱則為悸。</p>
<p>師曰:夫脈浮,目睛暈黃,衄未止。暈黃去,目睛慧了,知衄今止。</p>
<p>又曰:從春至夏衄者太陽,從秋至冬衄者陽明。</p>
<p>衄家不可汗,汗出必額上陷,脈緊急,直視不能眴,不得眠。</p>
<p>病人面無色,無寒熱。脈沉弦者,衄;浮弱,手按之絕者,下血,煩咳者,必吐血。</p>
<p>夫吐血,咳逆上氣,其脈數而有熱,不得臥者,死。</p>
<p>夫酒客咳者,必致吐血,此因極飲過度所致也。</p>
<p>寸口脈弦而大,弦則為減,大則為芤,減則為寒,芤則為虛,寒虛相擊,此名曰革,婦人則半產漏下,男子則亡血。</p>
<p>亡血不可發其表,汗出即寒栗而振。</p>
<p>病人胸滿,唇痿舌青,口燥,但欲漱水不欲嚥,無寒熱,脈微大來遲,腹不滿,其人言我滿,為有瘀血。</p>
<p>病者如熱狀,煩滿,口乾燥而渴,其脈反無熱,此為陰伏,是瘀血也,當下之。</p>
<p>火邪者,桂枝去芍藥加蜀漆牡蠣龍骨救逆湯主之。</p>
<p>桂枝救逆湯方:</p>
<p>桂枝三兩(去皮)</p>
<p>甘草二兩(炙)生薑三兩牡蠣五兩(熬)</p>
<p>龍骨四兩大棗十二枚蜀漆三兩(洗去腥)</p>
<p>上為末,以水一斗二升,先煮蜀漆,減二升,內諸藥,煮取三升,去滓,溫服一升。</p>
<p>心下悸者,半夏麻黃丸主之。</p>
<p>半夏麻黃丸方:</p>
<p>半夏麻黃等分上二味,末之,煉蜜和丸小豆大,飲服三丸,日三服。</p>
<p>吐血不止者,柏葉湯主之。</p>
<p>柏葉湯方:</p>
<p>柏葉乾薑各三兩艾三把上三味,以水五升,取馬通汁一升,合煮,取一升,分溫再服。</p>
<p>下血,先便後血,此遠血也,黃土湯主之。</p>
<p>黃土湯方:亦主吐血衄血。</p>
<p>甘草乾地黃白朮附子(炮)</p>
<p>阿膠黃芩各三兩灶中黃土半斤上七味,以水八升,煮取三升,分溫二服。</p>
<p>下血,先血後便,此近血也,赤小豆當歸散主之。方見狐惑中。</p>
<p>心氣不足,吐血、衄血,瀉心湯主之。</p>
<p>瀉心湯方:亦治霍亂。</p>
<p>大黃二兩黃連黃芩各一兩上三味,以水三升,煮取一升,頓服之。</p>
<p>嘔吐噦下利病脈證治第十七夫嘔家有癰膿,不可治嘔,膿盡自愈。</p>
<p>嘔家本渴,今反不渴者,以心下有支飲故也,此屬支飲。</p>
<p>病人脈數,數為熱,當消穀引食,而反吐者,何也?師曰:以發其汗,令陽微,膈氣虛,脈乃數,數為客熱,不能消穀,胃中虛冷故也。</p>
<p>脈弦者,虛也,胃氣無餘,朝食暮吐,變為胃反。寒在於上,醫反下之,今脈反弦,故名曰虛。</p>
<p>寸口脈微數,微則無氣,無氣則營虛,營虛則血不足,血不足則胸中冷。</p>
<p>跗陽脈浮而澀、浮則為虛,澀則傷脾、脾傷則不磨,朝食暮吐、暮食朝吐、宿穀不化、名曰胃反。脈緊而澀、其病難治。</p>
<p>病人欲吐者,不可下之。</p>
<p>噦而腹滿,視其前後,知何部不利,利之即愈。</p>
<p>嘔而胸滿者,茱萸湯主之。</p>
<p>茱萸湯方:</p>
<p>吳茱萸一升,人參三兩生薑六兩大棗十二枚。</p>
<p>上四味,以水五升,煮取三升,溫服七合,日三服。</p>
<p>乾嘔,吐涎沫,頭痛者,茱萸湯主之。方見上。</p>
<p>嘔而腸鳴,心下痞者,半夏瀉心湯主之。</p>
<p>半夏瀉心湯:</p>
<p>半夏半升(洗)</p>
<p>黃芩三兩乾薑三兩人參三兩黃連一兩大棗十二枚甘草三兩(炙)</p>
<p>上七味,以水一斗,煮取六升,去滓,再取三升,溫服一升,日三服。</p>
<p>乾嘔而利者,黃芩加半夏生薑湯主之。</p>
<p>黃芩加半夏生薑湯方:</p>
<p>黃芩三兩甘草二兩(炙)</p>
<p>芍藥二兩半夏半升,生薑三兩大棗十二枚上六味,以水一斗,煮取三升,去滓、溫服一升,日再夜一服。</p>
<p>諸嘔吐,穀不得下者,小半夏湯主之。方見痰飲中。</p>
<p>嘔吐而病在膈上,後思水者,解,急與之。思水者,豬苓散主之。</p>
<p>豬苓散方:</p>
<p>豬苓茯苓白朮各等分上三味,杵為散,飲服方寸匕,日三服。</p>
<p>嘔而脈弱,小便後利,身有微熱,見厥者,難治,四逆湯主之。</p>
<p>四逆湯方:</p>
<p>附子(生用)一枚乾薑一兩半甘草二兩(炙)</p>
<p>上三味,以水三升,煮取一升二合,去滓,分溫再服。強人可大附子一枚,乾薑三兩。</p>
<p>嘔而發熱者,小柴胡湯主之。</p>
<p>小柴胡湯方:</p>
<p>柴胡半斤黃芩三兩人參三兩甘草三兩半夏半斤生薑三兩大棗十二枚上七味,以水一斗二升,煮取六升,去滓,再煎取三升,溫服一升,日三服。</p>
<p>胃反嘔吐者,大半夏湯主之。《千金》云:治胃反不受食,食入即吐。《外臺》云:治嘔,心下痞硬者。</p>
<p>大半夏湯方:</p>
<p>半夏二升(洗完用)</p>
<p>人參三兩白蜜一升上三味,以水一斗二升、和蜜揚之二百四十遍,煮取二升半,溫服一升,餘分再服。</p>
<p>食已即吐者,大黃甘草湯主之。《外臺》方,又治吐水。</p>
<p>大黃甘草湯方:</p>
<p>大黃四兩甘草一兩上二味,以水三升,煮取一升,分溫再服。</p>
<p>胃反,吐而渴欲飲水者,茯苓澤瀉湯主之。</p>
<p>茯苓澤瀉湯方:《外臺》云:治消渴脈絕,胃反吐食之,有小麥一升。</p>
<p>茯苓半斤澤瀉四兩甘草二兩桂枝二兩白朮三兩生薑四兩上六味,以水一斗,煮取三升,內澤瀉、再煮取二升半,溫服八合,日三服。</p>
<p>吐後、渴欲得水而貪飲者,文蛤湯主之。兼主微風、脈緊、頭痛。</p>
<p>文蛤湯方:</p>
<p>文蛤五兩麻黃三兩甘草三兩生薑三兩石膏五兩杏仁五十枚大棗十二枚上七味,以水六升,煮取二升,溫服一升,汗出即愈。</p>
<p>乾嘔,吐逆,吐涎沫,半夏乾薑散主之。</p>
<p>半夏乾薑散方:</p>
<p>半夏乾薑等分上二味,杵為散,取方寸匕,漿水一升半,煎取七合,頓服之。</p>
<p>病人胸中似喘不喘,似嘔不嘔,似噦不噦,徹心中憒憒然無奈者,生薑半夏湯主之。</p>
<p>生薑半夏湯方:</p>
<p>半夏半升,生薑汁一升上二味,以水三升,煮半夏取二升,內生薑汁,煮取一升半,小冷,分四服,日三夜一服,嘔止、停後服。</p>
<p>乾嘔噦,若手足厥者,橘皮湯主之。</p>
<p>橘皮湯方:</p>
<p>橘皮四兩生薑半斤上二味,以水七升,煮取三升,溫服一升,下咽即愈。</p>
<p>噦逆者,橘皮竹茹湯主之。</p>
<p>橘皮竹茹湯方:</p>
<p>橘皮二升,竹茹二升,大棗三十枚,人參一兩,生薑半斤,甘草五兩。</p>
<p>上六味,以水一斗,煮取三升、溫服一升,日三服。</p>
<p>夫六腑氣絕於外者,手足寒、上氣、腳縮;五臟氣絕於內者、利不禁、下甚者、手足不仁。</p>
<p>下利脈沉弦者、下重;脈大者、為未止;脈微弱數者、為欲自止,雖發熱不死。</p>
<p>下利,手足厥冷,無脈者,灸之不溫。若脈不還,反微喘者、死。少陰負跗陽者,為順也。</p>
<p>下利有微熱而渴、脈弱者,今自愈。</p>
<p>下利脈數、有微熱汗出、今自愈;設脈緊為未解。</p>
<p>下利脈數而渴者,今自愈;設不差,必圊膿血,以有熱故也。</p>
<p>下利脈反弦,發熱身汗者,自愈。</p>
<p>下利氣者,當利其小便。</p>
<p>下利、寸脈反浮數、尺中自澀者、必圊膿血。</p>
<p>下利清穀,不可攻其表,汗出必脹滿。</p>
<p>下利,脈沉而遲,其人面少赤,身有微熱,下利清穀者,必鬱冒汗出而解,其人必微厥,所以然者,其面戴陽,下虛故也。</p>
<p>下利後脈絕,手足厥冷,晬時脈還,手足溫者生,脈不還者死。</p>
<p>下利腹脹滿,身體疼痛者,先溫其裡,乃攻其表,溫裡宜四逆湯,攻表宜桂枝湯。</p>
<p>四逆湯方:方見上。</p>
<p>桂枝湯方:</p>
<p>桂枝三兩(去皮)</p>
<p>芍藥三兩甘草二兩(炙)</p>
<p>生薑三兩大棗十二枚上五味,●咀,以水七升,微火煮取三升,去滓,適寒溫服一升,服已須臾,啜稀粥一升,以助藥力,溫覆令一時許,遍身●●微似有汗者、益佳,不可令如水淋漓,若一服汗出病瘥,停後服。</p>
<p>下利,三部脈皆平,按之心下堅者,急下之,宜大承氣湯。</p>
<p>下利脈遲而滑者、實也、利未欲止,急下之,宜大承氣湯。</p>
<p>下利脈反滑者,當有所去,下乃愈,宜大承氣湯。下利已差,至其年月日時復發者,以病不盡故也。當下之,宜大承氣湯。</p>
<p>下利譫語者,有燥屎也,小承氣湯主之。</p>
<p>小承氣湯方:</p>
<p>大黃四兩厚朴二兩(炙)</p>
<p>枳實大者三枚(炙)</p>
<p>上三味,以水四升,煮取一升二合,去滓,分溫二服,得利則止。</p>
<p>下利便膿血者、桃花湯主之。</p>
<p>桃花湯方:</p>
<p>赤石脂一升(一半挫,一半篩末)</p>
<p>乾薑一兩粳米一升上三味,以水七升,煮令米熟,去滓,溫服七合,內赤石脂末方寸匕,日三服。若一服愈,餘勿服。</p>
<p>熱利下重者,白頭翁湯主之。</p>
<p>白頭翁湯方:</p>
<p>白頭翁二兩黃連三兩,黃柏三兩秦皮三兩上四味,以水七升,煮取二升、去滓、溫服一升、不愈,更服。</p>
<p>下利後更煩,按之心下濡者,為虛煩也,梔子豉湯主之。</p>
<p>梔子豉湯方:</p>
<p>梔子十四枚、香豉四合(綿裹)</p>
<p>上二味,以水四升,先煮梔子得二升半、內豉,煮取一升半、去滓、分二服,溫進一服,得吐則止。</p>
<p>下利清穀,裡寒外熱、汗出而厥者,通脈四逆湯主之。</p>
<p>通脈四逆湯方:</p>
<p>用大附子一枚(生用)</p>
<p>乾薑三兩(強人可四兩)</p>
<p>甘草二兩(炙)</p>
<p>上三味,以水三升、煮取一升二合,去滓,分溫再服。</p>
<p>下利肺痛,紫參湯主之。</p>
<p>紫參湯方:</p>
<p>紫參半斤甘草三兩上二味,以水五升,先煮紫參,取二升,內甘草,煮取一升半,分溫三服。(疑非仲景方)</p>
<p>氣利,訶黎勒散主之。</p>
<p>訶黎勒散方:</p>
<p>訶黎勒十枚(煨)</p>
<p>上一味,為散,粥飲和,頓服。疑非仲景方。</p>
<p>〔附方〕</p>
<p>《千金翼》小承氣湯:治大便不通,噦數譫語。</p>
<p>《外臺》黃芩湯:治乾嘔下利。</p>
<p>黃芩三兩人參三兩乾薑三兩桂枝一兩大棗十二枚,半夏半斤。</p>
<p>上六味,以水七升,煮取三升,溫分三服。</p>
<p>瘡癰腸癰浸淫病脈證并治第十八諸浮數脈,應當發熱,而反洒淅惡寒,若有痛處,當發其癰。</p>
<p>師曰:諸癰腫,欲知有膿無膿,以手掩腫上,熱者為有膿,不熱者為無膿。</p>
<p>腸癰之為病,其身甲錯,腹皮急,按之濡,如腫狀,腹無積聚,身無熱,脈數,此為腹內有癰膿,薏苡附子敗醬散主之。</p>
<p>薏苡附子敗醬散方:</p>
<p>薏苡仁十分附子二分敗醬五分上三味,杵為末,取方寸匕,以水二升,煎減半,頓服,小便當下。</p>
<p>腸癰者,少腹腫痞,按之即痛如淋,小便自調,時時發熱,自汗出,復惡寒,其脈遲緊者,膿未成,可下之,當有血。脈洪數者,膿已成,不可下也,大黃牡丹湯主之。</p>
<p>大黃牡丹湯方:</p>
<p>大黃四兩牡丹一兩桃仁五十枚瓜子半升芒硝三合上五味,以水六升,煮取一升,去滓,內芒硝,再煎沸,頓服之,有膿當下,如無膿,當下血。</p>
<p>問曰:寸口脈浮微而澀,法當亡血,若汗出,設不汗者云何?答曰:若身有瘡,被刀斧所傷,亡血故也。</p>
<p>病金瘡,王不留行散主之。</p>
<p>王不留行散方:</p>
<p>王不留行十分(八月八日採)</p>
<p>蒴藋細葉十分(七月七日採)</p>
<p>桑東南根白皮十分(三月三日採)</p>
<p>甘草十八分川椒三分(除目及閉口,去汗)黃芩二分乾薑二分厚朴二分芍藥二分上九味,桑根皮以上三味燒灰存性,勿令灰過,各別杵篩,合治之為散,服方寸匕。小瘡即粉之,大瘡但服之,產後亦可服。如風寒,桑東根勿取之;前三物,皆陰乾百日。</p>
<p>排膿散方:</p>
<p>枳實十六枚芍藥六分桔梗二分上三味,杵為散,取雞子黃一枚,以藥散與雞黃相等,揉和令相得,飲和服之,日一服。</p>
<p>排膿湯方:</p>
<p>甘草二兩桔梗三兩生薑一兩大棗十枚上四味,以水三升,煮取一升,溫服五合,日再服。</p>
<p>浸淫瘡,從口流向四肢者,可治;從四肢流來入口者,不可治。</p>
<p>浸淫瘡,黃連粉主之。方未見。</p>
<p>跗蹶手指臂腫轉筋陰狐疝蚘蟲病脈證治第十九師曰:病跗蹶,其人但能前,不能卻,刺●入二寸,此太陽經傷也。</p>
<p>病人常以手指臂腫動,此人身體??者,藜蘆甘草湯主之。</p>
<p>藜蘆甘草湯方:未見轉筋之為病,其人臂腳直,脈上下行,微弦。轉筋入腹者,雞矢白散主之。</p>
<p>雞矢白散方:</p>
<p>雞矢白上一味,為散,取方寸匕,以水六合,和,溫服。</p>
<p>陰狐疝氣者,偏有大小,時時上下,蜘蛛散主之。</p>
<p>蜘蛛散方:</p>
<p>蜘蛛十四枚(熬焦)</p>
<p>桂枝半兩上二味,為散,取八分一匕,飲和服,日再服。蜜丸亦可。</p>
<p>問曰:病腹痛有蟲,其脈何以別之?師曰:腹中痛,其脈當沉,若弦,反洪大,故有蛔蟲。</p>
<p>蛔蟲之為病,令人吐涎,心痛,發作有時,毒藥不止,甘草粉蜜湯主之。</p>
<p>甘草粉蜜湯方:</p>
<p>甘草二兩粉一兩蜜四兩上三味,以水三升,先煮甘草,取二升,去滓,內粉、蜜,攪令和,煎如薄粥,溫服一升,差即止。</p>
<p>蛔厥者,當吐蛔,令病者靜而復時煩,此為臟寒,蛔上入膈,故煩,須臾復止,得食而嘔,又煩者,蛔聞食臭出,其人當自吐蛔。</p>
<p>蛔厥者,烏梅丸主之。</p>
<p>烏梅丸方:</p>
<p>烏梅三百個細辛六兩乾薑十兩黃連一斤當歸四兩附子六兩(炮)</p>
<p>川椒四兩(去汗)</p>
<p>桂枝六兩人參六兩黃柏六兩上十味,異搗篩,合治之,以苦酒漬烏梅一宿,去核,蒸之五升米下,飯熟搗成泥,和藥令相得,內臼中,與蜜杵二千下,丸如梧子大。先食飲服十丸,日三服,稍加至二十丸。禁生冷滑臭等食。</p>
<p>婦人妊娠病脈證并治第二十師曰:婦人得平脈,陰脈小弱,其人渴,不能食,無寒熱,名妊娠,桂枝湯主之。方見下利中。於法六十日當有此證;設有醫治逆者,卻一月加吐下者,則絕之。</p>
<p>婦人宿有癥病,經斷未及三月,而得漏下不止,胎動在臍上者,為癥痼害。妊娠六月動者,前三月經水利時,胎也。下血者,後斷三月衃也。所以血不止者,其癥不去故也,當下其癥,桂枝茯苓丸主之。</p>
<p>桂枝茯苓丸方:</p>
<p>桂枝茯苓牡丹(去心)</p>
<p>芍藥桃仁(去皮尖熬)各等分上五味,末之,煉蜜和丸,如兔屎大,每日食前服一丸,不知,加至三丸。</p>
<p>婦人懷娠六七月,脈弦發熱,其胎愈脹,腹痛惡寒者,少腹如扇,所以然者,子臟開故也,當以附子湯溫其臟。方未見。</p>
<p>師曰:婦人有漏下者,有半產後因續下血都不絕者,有妊娠下血者,假令妊娠腹中痛,為胞阻,膠艾湯主之。</p>
<p>芎歸膠艾湯方:一方加乾薑一兩。胡氏治婦人胞動,無乾薑。</p>
<p>芎藭阿膠甘草各二兩艾葉當歸各三兩芍藥四兩乾地黃上七味,以水五升,清酒三升,合煮取三升,去滓,內膠,令消盡,溫服一升,日三服,不差,更作。</p>
<p>婦人懷妊,腹中●痛,當歸芍藥散主之。</p>
<p>當歸芍藥散方:</p>
<p>當歸三兩芍藥一斤(一作六兩)</p>
<p>茯苓四兩白朮四兩澤瀉半斤芎藭半斤(作三兩)</p>
<p>上六味,杵為散,取方寸匕,酒和,日三服。</p>
<p>妊娠嘔吐不止,乾薑人參半夏丸主之。</p>
<p>乾薑人參半夏丸方:</p>
<p>乾薑人參各一兩半夏二兩上三味,末之,以生薑汁糊為丸,如梧桐子大,飲服十丸,日三次。</p>
<p>妊娠,小便難,飲食如故,當歸貝母苦參丸主之。</p>
<p>當歸貝母苦參丸方:男子加滑石半兩。</p>
<p>當歸四兩貝母四兩苦參四兩上三味,末之,煉蜜丸如小豆大,飲服三丸,加至十丸。</p>
<p>妊娠有水氣,身重、小便不利,洒淅惡寒,起即頭眩,葵子茯苓散主之。</p>
<p>葵子茯苓散方:</p>
<p>葵子一斤茯苓三兩上二味,杵為散,飲服方寸匕,日三服,小便利則愈。</p>
<p>婦人妊娠,宜常服當歸散主之。</p>
<p>當歸散方:</p>
<p>當歸黃芩芍藥芎藭各一斤白朮半斤上五味,杵為散,酒服方寸匕,日再服,妊娠常服即易產,胎無苦疾,產後百病悉主之。</p>
<p>妊娠養胎,白朮散主之。</p>
<p>白朮散方:見《外臺》。</p>
<p>白朮芎藭蜀椒三分去汗牡蠣上四味,杵為散,酒服一錢匕,日三服,夜一服。但苦痛,加芍藥;心下毒痛,倍加芎藭;心煩吐痛,不能食飲,加細辛一兩,半夏大者二十枚。服之後,更以醋漿水服之。若嘔,以醋漿水服之;復不解者,小麥汁服之。已後渴者,大麥粥服之,病雖愈,服之勿置。</p>
<p>婦人傷胎,懷身腹滿,不得小便,從腰以下重,如有水氣狀。懷身七月,太陰當養不養,此心氣實,當刺瀉勞宮及關元,小便微利則愈。見玉函。</p>
<p>婦人產後病脈證治第二十一問曰:新產婦人有三病,一者病痙,二者病鬱冒,三者大便難,何謂也。師曰:新產血虛,多汗出,喜中風,故令病痙;亡血復汗,寒多,故令鬱冒;亡津液,胃燥,故大便難。</p>
<p>產婦鬱冒,其脈微弱,嘔不能食,大便反堅,但頭汗出。所以然者,血虛而厥,厥而必冒。冒家欲解,必大汗出。以血虛下厥,孤陽上出,故頭汗出,所以產婦喜汗出者,亡陰血虛,陽氣獨盛,故當汗出,陰陽乃復。大便堅,嘔不能食,小柴胡湯主之。方見嘔吐中。</p>
<p>病解能食,七八日更發熱者,此為胃實,大承氣湯主之。方在痙病中。</p>
<p>產後腹中●痛,當歸生薑羊肉湯主之,并治腹中寒疝,虛勞不足。</p>
<p>當歸生薑羊肉湯方:見寒疝中。</p>
<p>產後腹痛,煩滿不得臥,枳實芍藥散主之。</p>
<p>枳實芍藥散方:</p>
<p>枳實(燒令黑,勿太過)芍藥等分。</p>
<p>上二味,杵為散,服方寸匕,日三服,並主癰膿,以麥粥下之。</p>
<p>產婦腹痛、法當以枳實芍藥散,假令不愈者,此為腹中有瘀血著臍下,宜下瘀血湯主之,亦主經水不利。</p>
<p>下瘀血湯方:</p>
<p>大黃二兩桃仁二十枚●蟲二十枚(熬、去足)</p>
<p>上三味、末之,煉蜜和為四丸,以酒一升,煎一丸,取八合頓服之,新血下如豚肝。</p>
<p>產後七八日,無太陽證,少腹堅痼,此惡露不盡,不大便,煩躁發熱、切脈微實,更倍發熱,日晡時煩躁者,不食,食則譫語,至夜即愈,宜大承氣湯主之,熱在裡,結在膀胱也。方見痙病中。</p>
<p>產後風、續之數十日不解、頭微痛,惡寒,時時有熱、心下悶、乾嘔、汗出、雖久、陽旦證續在耳,可與陽旦湯。即桂枝湯。方見下利。</p>
<p>產後中風、發熱正面赤、喘而頭痛,竹葉湯主之。</p>
<p>竹葉湯方:</p>
<p>竹葉一把葛根三兩防風一兩桔梗一兩桂枝一兩人參一兩甘草一兩附子一枚(炮)</p>
<p>大棗十五枚生薑五兩上十味,以水一斗,煮取二升半,分溫三服,溫覆使汗出,頸項強,用大附子一枚,破之如豆大,煎藥揚去沫。嘔者加半夏半升洗。</p>
<p>婦人乳中虛,煩亂嘔逆,安中益氣,竹皮大丸主之。</p>
<p>竹皮大丸方:</p>
<p>生竹茹二分石膏二分桂枝一分甘草七分白薇一分上五味,末之,棗肉和丸彈子大,以飲服一丸、日三夜二服。有熱者倍白薇,煩喘者加柏實一分。</p>
<p>產後下利虛極,白頭翁加甘草阿膠湯主之。</p>
<p>白頭翁加甘草阿膠湯方:</p>
<p>白頭翁二兩甘草二兩阿膠二兩秦皮三兩黃連三兩柏皮三兩上六味,以水七升、煮取二升半,內膠令消盡,分溫三服。</p>
<p>〔附方〕</p>
<p>《千金》三物黃芩湯,治婦人在草蓐,自發露得風,四肢苦煩熱,頭痛者與小柴胡湯,頭不痛但煩者,此湯主之。</p>
<p>黃芩一兩苦參二兩乾地黃四兩上三味,以水八升,煮取二升,溫服一升,多吐下蟲。</p>
<p>《千金》內補當歸建中湯:治婦人產後虛羸不足,腹中刺痛不止,吸吸少氣,或苦少腹急,摩痛引腰背,不能食飲,產後一月,日得服四、五劑為善,令人強壯宜。</p>
<p>當歸四兩桂枝三兩芍藥六兩生薑三兩甘草一兩大棗十二枚上六味,以水一斗,煮取三升,分溫三服,一日令盡。若大虛加飴糖六兩,湯成內之,於火上暖令飴消,若去血過多,崩傷內衄不止,加地黃六兩,阿膠二兩,合成八味,湯成內阿膠,若無當歸,加芎藭代之,若無生薑,以乾薑代之。</p>
<p>婦人雜病脈證并治第二十二婦人中風,七八日續來寒熱,發作有時,經水適斷,此為熱入血室,其血必結,故使如瘧狀,發作有時,小柴胡湯主之。方見嘔吐中。</p>
<p>婦人傷寒發熱,經水適來,晝日明了暮則譫語,如見鬼狀者,此為熱入血室,治之無犯胃氣及上二焦,必自愈。</p>
<p>婦人中風,發熱惡寒,經水適來,得之七八日,熱除脈遲,身涼和,胸脅滿,如結胸狀,●語者,此為熱入血室也,當刺期門,隨其實而取之。</p>
<p>陽明病、下血●語者,此為熱入血室,但頭汗出,當刺期門,隨其實而瀉之,濈然汗出者愈。</p>
<p>婦人咽中如有炙臠,半夏厚朴湯主之。</p>
<p>半夏厚朴湯方:《千金》作胸滿,心下堅,咽中帖帖,如有炙肉,吐之不出,吞之不下。</p>
<p>半夏一升厚朴三兩茯苓四兩生薑五兩乾蘇葉二兩上五味,以水七升,煮取四升,分服四服,日三夜一服。</p>
<p>婦人臟躁,喜悲傷欲哭,象如神靈所作,數欠伸,甘麥大棗湯主之。</p>
<p>甘麥大棗湯方:</p>
<p>甘草三兩小麥一升大棗十枚上三味,以水六升,煮取三升,溫分三服,亦補脾氣。</p>
<p>婦人吐涎沫,醫反下之,心下即痞,當先治其吐涎沫,小青龍湯主之,涎沫止、乃治痞,瀉心湯主之。</p>
<p>小青龍湯方:見痰飲中。</p>
<p>瀉心湯方:見驚悸中。</p>
<p>婦人之病,因虛,積冷,結氣,為諸經水斷絕,至有歷年,血寒積結,胞門寒傷,經絡凝堅。</p>
<p>在上嘔吐涎唾,久成肺癰,形體損分。在中盤結,繞臍寒疝;或兩脅疼痛,與臟相連;或結熱中,痛在關元,脈數無瘡,肌若魚鱗時著男子,非止女身。在下未多,經候不勻,令陰掣痛,少腹惡寒;或引腰脊,下根氣街,氣衝急痛,膝脛疼煩。奄忽眩冒,狀如厥癲;或有憂慘,悲傷多嗔,此皆帶下,非有鬼神。</p>
<p>久則羸瘦,脈虛多寒;三十六病,千變萬端;審脈陰陽,虛實緊弦;行其針藥,治危得安;其雖同病,脈各異源,子當辨記,勿謂不然。</p>
<p>婦人年五十所,病下利數十日不止,暮即發熱,少腹裡急,腹滿,手掌煩熱,唇口乾燥,何也?師曰:此病屬帶下,何以故?曾經半產,瘀血在少腹不去。何以知之?其證唇口乾燥,故知之。當以溫經湯主之。</p>
<p>溫經湯方:</p>
<p>吳茱萸三兩當歸二兩芎藭二兩芍藥二兩人參二兩桂枝二兩阿膠二兩生薑二兩牡丹皮(去心)二兩甘草二兩半夏半升麥門冬一升(去心)</p>
<p>上十二味,以水一斗,煮取三升,分溫三服,亦主婦人少腹寒,久不受胎;兼取崩中去血,或月水來過多,及至期不來。</p>
<p>帶下經水不利,少腹滿痛,經一月再見者,土瓜根散主之。</p>
<p>土瓜根散方:陰?腫亦主之。</p>
<p>土瓜根三兩芍藥三兩桂枝三兩●蟲三兩上四味,杵為散,酒服方寸匕,日三服。</p>
<p>寸口脈弦而大,弦則為減,大則為芤,減則為寒,芤則為虛,寒虛相搏,此名曰革,婦人則半產漏下,旋覆花湯主之。</p>
<p>旋覆花湯方:見五藏風寒積聚篇。</p>
<p>婦人陷經,漏下黑不解,膠薑湯主之。臣億等校諸本無膠薑湯方,想是前妊娠中膠艾湯。</p>
<p>婦人少腹滿如敦狀,小便微難而不渴,生後者,此為水與血俱結血室也,大黃甘遂湯主之。</p>
<p>大黃甘遂湯方:</p>
<p>大黃四兩甘遂二兩阿膠二兩上三味,以水三升,煮取一升,頓服之,其血當下。</p>
<p>婦人經水不利下,抵當湯主之。(亦治膀胱滿急有瘀血者。)</p>
<p>抵當湯方:</p>
<p>水蛭二十個(熬)</p>
<p>?蟲三十枚(熬、去翅足)</p>
<p>桃仁二十個(去皮尖)大黃三兩(酒浸)</p>
<p>上四味,為末,以水五升,煮取三升,去滓,溫服一升。</p>
<p>婦人經水閉不利,臟堅癖不止,中有乾血,下白物,礬石丸主之。</p>
<p>礬石丸方:</p>
<p>礬石三分(燒)</p>
<p>杏仁一分上二味,末之,煉蜜和丸,棗核大,內臟中,劇者再內之。</p>
<p>婦人六十二種風,及腹中血氣刺痛,紅藍花酒主之。</p>
<p>紅藍花酒方:疑非仲景方。</p>
<p>紅藍花一兩上一味,以酒一大升,煎減半,頓服一半,未止再服。</p>
<p>婦人腹中諸疾痛,當歸芍藥散主之。</p>
<p>當歸芍藥散方:見前妊娠中婦人腹中痛,小建中湯主之。</p>
<p>小建中湯方:見虛勞中。</p>
<p>問曰:婦人病飲食如故,煩熱不得臥,而反倚息者,何也?師曰:此名轉胞,不得溺也。以胞系了戾,故致此病。但利小便則愈,宜腎氣丸主之。方見虛勞中。</p>
<p>蛇床子散方,溫陰中坐藥。</p>
<p>蛇床子散方:</p>
<p>蛇床子仁上一味,末之,以白粉少許,和合相得,如棗大,棉裹內之,自然溫。</p>
<p>少陰脈滑而數者,陰中即生瘡,陰中蝕瘡爛者,狼牙湯洗之。</p>
<p>狼牙湯方:</p>
<p>狼牙三兩上一味,以水四升,煮取半升,以綿纏筋如繭浸湯瀝陰中,日四遍。</p>
<p>胃氣下泄,陰吹而正暄,此穀氣之實也,膏髮煎導之。</p>
<p>膏髮煎方:(見黃疸中)</p>
<p>小兒疳蟲蝕齒方:疑非仲景方。</p>
<p>雄黃葶藶上二味,末之,取臘月豬脂溶,以槐枝綿裹頭四五枚,點藥烙之。</p>
<p>雜療方第二十三退五臟虛熱,四時加減柴胡飲子方:</p>
<p>冬三月加柴胡八分白朮八分陳皮五分大腹檳榔四枚並皮子用生薑五分桔梗七分春三月加枳實減白朮共六味夏三月加生薑三分枳實五分甘草三分共八味秋三月加陳皮三分共六味上各●咀,分為三貼,一貼以水三升,煮取二升,分溫三服;如人行四五里進一服,如四體壅,添甘草少許,每貼分作三小貼,每小貼以水一升,煮取七合,溫服,再合滓為一服。重煮,都成四服。疑非仲景方。</p>
<p>長服訶黎勒丸方:疑非仲景方。</p>
<p>訶黎勒煨陳皮厚朴各三兩上三味,末之,煉蜜丸如梧子大,酒飲服二十丸,加至三十丸。</p>
<p>三物備急丸方:見《千金》司空裴秀為散用亦可。先和成汁,乃傾口中,令從齒間得入,至良驗。</p>
<p>大黃一兩乾薑一兩巴豆一兩去皮心熬,外研如脂上藥各須精新,先搗大黃、乾薑為末,研巴豆內中,合治一千杵,用為散,蜜和丸亦佳,密器中貯之,莫令歇。主心腹諸卒暴百病,若中惡客忤,心腹脹滿,卒痛如錐刺,氣急口噤,停尸卒死者,以煖水苦酒服大豆許三四丸,或不下,捧頭起,灌令下咽,須臾當差,如未差,更與三丸,當腹中鳴,即吐下便差。若口噤,亦須折齒灌之。</p>
<p>治傷寒令愈不復,紫石寒食散方:見《千金翼》。</p>
<p>紫石英白石英赤石脂鐘乳研煉栝蔞根防風桔梗文蛤鬼臼各十分太乙餘糧十分燒乾薑附子炮去皮桂枝去皮各四分上十三味,杵為散,酒服方寸匕。</p>
<p>救卒死方:</p>
<p>薤搗汁,灌鼻中。</p>
<p>又方:</p>
<p>雄雞冠,割取血,管吹內鼻中。</p>
<p>豬脂如雞子大,苦酒一升,煮沸灌喉中。</p>
<p>雞肝及血,塗面上,以灰圍四旁,立起。</p>
<p>大豆二七粒,以雞子白并酒和,盡以吞之。</p>
<p>救卒死而壯熱者方:</p>
<p>礬石半斤,以水一斗半煮消,以漬腳,令沒踝。</p>
<p>救卒死而目閉者方:</p>
<p>騎牛臨面,搗薤汁灌耳中,吹皂莢末鼻中,立效。</p>
<p>救卒死而張口反折者方:</p>
<p>灸手足兩爪後十四壯了,飲以五毒諸膏散。有巴豆者。</p>
<p>救卒死而四肢不收,失便者方:</p>
<p>馬屎一升,水三斗,煮取二斗以洗之,又取牛洞稀糞也一升,溫酒灌口中。灸心下一寸,臍上三寸,臍下四寸,各一百壯,差。</p>
<p>救小兒卒死而吐利,不知是何病方:</p>
<p>狗屎一丸,絞取汁以灌之;無濕者,水煮乾者,取汁。</p>
<p>尸蹶,脈動而無氣,氣閉不通,故靜而死也,治方:脈證見上卷。菖蒲屑,內鼻兩孔中吹之,令人以桂屑著舌下。</p>
<p>又方:</p>
<p>剔取左角髮方寸,燒末,酒和,灌令入喉立起。</p>
<p>救卒死,客忤死,還魂湯主之方。《千金方》云:主卒忤鬼擊飛尸,諸奄忽氣絕,無復覺,或已無脈,口噤拗不開,去齒下湯。湯下口不下者,分病人髮左右,捉肩引之。藥下復增取一升,須臾立蘇。</p>
<p>麻黃三兩去節。一方四兩杏仁去皮尖,七十個甘草一兩炙《千金》用桂心二兩。</p>
<p>上三味,以水八升,煮取三升,去滓,分令嚥之,通治諸感忤。</p>
<p>又方:</p>
<p>韭根一把烏梅二七個吳茱萸半升,炒上三味,以水一斗煮之,以病人櫛內中,三沸,櫛浮者生,沉者死,煮取三升,去滓分飲之。</p>
<p>救自縊死,旦至暮,雖已冷,必可治;暮至旦,小難也,恐此當言陰氣盛故也。然夏時夜短於晝,又熱,猶應可治。又云:心下若微溫者,一日以上,猶可治之方。</p>
<p>徐徐抱解,不得截繩,上下安被臥之,一人以腳踏其兩肩,手少挽其髮,常弦弦勿縱之;一人以手按據胸上,數動之;一人摩捋臂脛,屈伸之。若已僵,但漸漸強屈之,并按其腹,如此一炊頃,氣從口出,呼吸眼開,而猶引按莫置,亦勿苦勞之,須臾,可少與桂枝湯及粥清,含與之,令濡喉,漸漸能嚥,及稍止,若向令兩人以管吹其兩耳,好,此法最善,無不活也。</p>
<p>凡中暍死,不可使得冷,得冷便死,療之方:</p>
<p>屈草帶,繞暍人臍,使三兩人溺其中,令溫。亦可用熱泥和屈草,亦可扣瓦碗底,按及車缸,以著暍人,取令溺須得流去,此謂道路窮,卒無湯當令溺其中,欲使多人溺,取令溫,若湯,便可與之,不可泥及車缸,恐此物冷,暍既在夏月,得熱泥土,暖車缸,亦可用也。</p>
<p>救溺死方:</p>
<p>取灶中灰兩石餘,以埋人,從頭至足,水出七孔,即活。</p>
<p>上療自縊溺暍之法并出自張仲景為之,其意殊絕,殆非常情所及,本草所能關,實救人之大術矣,傷寒家數有暍病,非此遇熱之暍。見《外臺》《肘後》目。</p>
<p>治馬墜及一切筋骨損方:見《肘後方》。</p>
<p>大黃一兩,切浸湯成下緋帛如手大燒灰亂髮如雞子大燒灰用久用炊單布一尺,燒灰敗蒲一握三寸桃仁四十九枚,去皮尖熬甘草如中指節,炙剉。</p>
<p>上七味,以童子小便,量多少,煎成湯,內酒一大盞,次下大黃,去滓,分溫三服,先剉敗蒲席半領,煎湯浴,衣被蓋覆,斯須,通利數行,痛楚立差,利及浴水赤,勿怪,即瘀血也。</p>
<p>禽獸魚蟲禁忌并治第二十四凡飲食滋味以養於生,食之有妨,反能為害,自非服藥煉液、焉能不飲食乎?切見時人,不閑調攝,疾疢競起;若不因食而生,苟全其生,須知切忌者矣。所食之味,有與病相宜,有與身為害,若得宜則益體,害則成疾,以此致危,例皆難療。凡煮藥飲汁以解毒者,雖云救急,不可熱飲,諸毒病,得熱更甚,宜冷飲之。</p>
<p>肝病禁辛,心病禁鹹,脾病禁酸,肺病禁苦,腎病禁甘。春不食肝,夏不食心,秋不食肺,冬不食腎,四季不食脾。辯曰:春不食肝者,為肝氣王,脾氣敗,若食肝,則又補肝,脾氣敗尤甚,不可救,又肝王之時,不可以死氣入肝,恐傷魂也,若非王時即虛,以肝補之佳,餘臟準此。</p>
<p>凡肝臟,自不可輕噉,自死者彌甚。</p>
<p>凡心皆為神識所舍,勿食之,使人來生復其報對矣。</p>
<p>凡肉及肝,落地不著塵土者,不可食之。</p>
<p>豬肉落水浮者,不可食。</p>
<p>諸肉及魚,若狗不食,鳥不啄者,不可食。</p>
<p>諸肉不乾,火灸不動,見水自動者,不可食之。</p>
<p>肉中有朱點者,不可食之。</p>
<p>六畜肉,熱血不斷者,不可食之。</p>
<p>父母及身本命肉,食之令人神魂不安。</p>
<p>食肥肉及熱羹,不得飲冷水。</p>
<p>諸五臟及魚,投地塵土不污者,不可食之。</p>
<p>穢飯,餒肉,臭魚,食之皆傷人。</p>
<p>自死肉口閉者,不可食之。</p>
<p>六畜自死,皆疫死,則有毒,不可食之。</p>
<p>獸自死,北首及伏地者,食之殺人。</p>
<p>食生肉,飽飲乳,變成白蟲。一作血蠱。</p>
<p>疫死牛肉,食之令病洞下,亦致堅積,宜利藥下之。</p>
<p>脯藏米甕中有毒,及經夏食之,發腎病。</p>
<p>治自死六畜肉中毒方:</p>
<p>黃蘗屑,搗服方寸匕。</p>
<p>治食鬱肉漏脯中毒方:鬱肉,密器蓋之,隔宿者是也。漏脯,茅屋漏下,沾著者是也。</p>
<p>燒犬屎,酒服方寸匕,每服人乳汁亦良。飲生韭汁三升,亦得。</p>
<p>治黍米中藏乾脯,食之中毒方:</p>
<p>大豆濃煮汁,飲數升即解,亦治狸肉漏脯等毒。</p>
<p>治食生肉中毒方:</p>
<p>掘地深三尺,取其下土三升,以水五升,煮數沸,澄清汁,飲一升即愈。</p>
<p>治六畜鳥獸肝中毒方:</p>
<p>水浸豆豉,絞取汁,服數升愈。</p>
<p>馬腳無夜眼者,不可食之。</p>
<p>食酸馬肉,不飲酒,則殺人。</p>
<p>馬肉不可熱食,傷人心。</p>
<p>馬鞍下肉,食之殺人。</p>
<p>白馬黑頭者,不可食之。</p>
<p>白馬青蹄者,不可食之。</p>
<p>馬肉?肉共食飽,醉臥大忌。</p>
<p>驢、馬肉,合豬肉食之,成霍亂。</p>
<p>馬肝及毛不可妄食,中毒害人。</p>
<p>食馬肝中毒,人未死方:</p>
<p>雄鼠屎二七粒,末之,水和服,日再服。屎尖者是。</p>
<p>又方:人垢取方寸匕,服之佳。</p>
<p>治食馬肉中毒欲死方:</p>
<p>香豉二兩杏仁三兩上二味,蒸一食頃,熟杵之服,日再服。</p>
<p>又方:煮蘆根汁,飲之良。</p>
<p>疫死牛,或目赤,或黃,食之大忌。</p>
<p>牛肉共豬肉食之,必作寸白蟲。</p>
<p>青牛腸,不可合犬肉食之。</p>
<p>牛肺從三月至五月,其中有蟲如馬尾,割去勿食,食則損人。</p>
<p>牛羊豬肉,皆不得以楮木桑木蒸炙,食之令人腹內生蟲。</p>
<p>噉蛇牛肉殺人,何以知之?噉蛇者,毛髮向後順者,是也。</p>
<p>治噉蛇牛肉,食之欲死方:</p>
<p>飲人乳汁一升,立愈。</p>
<p>又方:</p>
<p>以泔洗頭,飲一升,愈。</p>
<p>牛肚細切,以水一斗,煮取一升,暖飲之,大汗出者愈。</p>
<p>治食牛肉中毒方:</p>
<p>甘草煮汁,飲之即解羊肉其有宿熱者,不可食之。</p>
<p>羊肉不可共生魚酪食之,害人。</p>
<p>羊蹄甲中有珠子白者,名羊懸筋,食之令人癲。</p>
<p>白羊黑頭,食其腦,作腸癰。</p>
<p>羊肝共生椒食之,破人五臟。</p>
<p>豬肉共羊肝和食之,令人心悶。</p>
<p>豬肉以生胡荽同食,爛人臍。</p>
<p>豬脂不可合梅子食之。</p>
<p>豬肉和葵食之,少氣。</p>
<p>鹿肉不可和蒲白作羹,食之發惡瘡。</p>
<p>麋脂及梅李子,若妊婦食之,令子青盲,男子傷精。</p>
<p>?肉不可合蝦及生菜,梅李果食之,皆病人。</p>
<p>痼疾人不可食熊肉,令終身不愈。</p>
<p>白犬自死,不出舌者,食之害人。</p>
<p>食狗鼠餘,令人發?瘡。</p>
<p>治食犬肉不消,心下堅或腹脹,口乾大渴,心急發熱,妄語如狂,或洞下方:</p>
<p>杏仁一升,合皮熟研用上一味,以沸湯三升和取汁,分三服,利下肉方,大驗。</p>
<p>婦人妊娠,不可食兔肉、山羊肉及鱉、雞、鴨,令子無聲音。</p>
<p>兔肉不可合白雞肉食之,令人面發黃。</p>
<p>兔肉著乾薑食之,成霍亂。</p>
<p>凡鳥自死,口不閉,翅不合者,不可食之。</p>
<p>諸禽肉肝青者,食之殺人。</p>
<p>雞有六翮四距者,不可食之。</p>
<p>烏雞白首者,不可食之。</p>
<p>雞不可共葫蒜食之,滯氣。一云?子。</p>
<p>山雞不可合鳥獸肉食之。</p>
<p>雉肉久食之,令人瘦。</p>
<p>鴨卵不可合鱉肉食之。</p>
<p>婦人妊娠,食雀肉,令子淫亂無恥。</p>
<p>雀肉不可合李子食之。</p>
<p>燕肉勿食,入水為蛟龍所噉。</p>
<p>鳥獸有中毒箭死者,其肉有毒,解之方:</p>
<p>大豆煮汁,及鹽汁,服之解。</p>
<p>魚頭正白,如連珠至脊上,食之殺人。</p>
<p>魚頭中無鰓者,不可食之,殺人。</p>
<p>魚無腸膽者,不可食之,三年陰不起,女子絕生。</p>
<p>魚頭似有角者,不可食之。</p>
<p>魚目合者,不可食之。</p>
<p>六甲日,勿食鱗甲之物。</p>
<p>魚不可合雞肉食之。</p>
<p>魚不得和鸕肉食之。</p>
<p>鯉魚鮓不可合小豆藿食之,其子不可合豬肝食之,害人。</p>
<p>鯉魚不可合犬肉食之。</p>
<p>鯽魚不可合猴雉肉食之。一云不可合豬肝食。</p>
<p>鯷魚合鹿肉生食,令人筋甲縮。</p>
<p>青魚鮓不可合生胡荽,及生葵,并麥中食之。</p>
<p>?鱔不可合白犬血食之。</p>
<p>龜肉不可合酒果子食之。</p>
<p>鱉目凹陷者,及壓下有王字形者,不可食之。</p>
<p>其肉不得合雞鴨子食之。</p>
<p>龜鱉肉不可合莧菜食之。</p>
<p>蝦無鬚及腹下通黑,煮之反白者,不可食之。</p>
<p>食膾,飲乳酪,令人腹中生蟲,為瘕。</p>
<p>鱠食之,在心胸間不化,吐復不出,速下除之,久成癥病,治之方:</p>
<p>橘皮一兩大黃二兩朴硝二兩上三味,以水一大升,煮至小升,頓服即消。</p>
<p>食鱠多,不消,結為癥病,治之方:</p>
<p>馬鞭草上一味,搗汁飲之,或以薑葉汁飲之一升,亦消。又可服吐藥吐之。</p>
<p>食魚後中毒,兩種煩亂,治之方:</p>
<p>橘皮濃煎汁,服之即解。</p>
<p>食鯸?魚中毒方:</p>
<p>蘆根煮汁,服之即解。</p>
<p>蟹目相向,足斑目赤者,不可食之。</p>
<p>食蟹中毒,治之方:</p>
<p>紫蘇煮汁,飲之三升。紫蘇子搗汁,飲之亦良。</p>
<p>又方:</p>
<p>冬瓜汁,飲二升,食冬瓜亦可。</p>
<p>凡蟹未遇霜,多毒,其熟者,乃可食之。</p>
<p>蜘蛛落食中,有毒,勿食之。</p>
<p>凡蜂蠅蟲蟻等,多集食上,食之致?。</p>
<p>果實菜穀禁忌并治第二十五果子生食生瘡。</p>
<p>果子落地經宿,蟲蟻食之者,人大忌食之。</p>
<p>生米停留多日,有損處,食之傷人。</p>
<p>桃子多食令人熱,仍不得入水浴,今人病淋瀝寒熱病。</p>
<p>杏酪不熟,傷人。</p>
<p>梅多食,壞人齒。</p>
<p>李不可多食,令人臚脹。</p>
<p>林檎不可多食,令人百脈弱。</p>
<p>橘柚多食,令人口爽,不知五味。</p>
<p>梨不可多食,令人寒中,金瘡、產婦,亦不宜食。</p>
<p>櫻桃杏多食,傷筋骨。</p>
<p>安石榴不可多食,損人肺。</p>
<p>胡桃不可多食,令人動痰飲。</p>
<p>生棗多食,令人熱渴,氣脹。寒熱羸瘦者,彌不可食,傷人。</p>
<p>食諸果中毒,治之方:</p>
<p>豬骨燒灰上一味,末之,水服方寸匕。亦治馬肝漏脯等毒。</p>
<p>木耳赤色,及仰生者,勿食。菌仰卷及赤色者不可食。</p>
<p>食諸菌中毒,悶亂欲死,治之方:</p>
<p>人糞汁飲一升,土漿飲一二升,大豆濃煎汁飲之。服諸吐利藥,并解。</p>
<p>食楓柱菌而哭不止,治之以前方。</p>
<p>誤食野芋,煩亂欲死,治之以前方。其野芋根,山東人名魁芋,人種芋,三年不收,亦成野芋,并殺人。</p>
<p>蜀椒閉口者有毒,誤食之戟人咽喉,氣病欲絕。或吐下白沫,身體痹冷,急治之方。</p>
<p>肉桂,煎汁飲之,飲冷水一二升。</p>
<p>或食蒜,或飲地漿。</p>
<p>或濃煮豉汁飲之。並解。</p>
<p>正月勿食生蔥,令人面生游風。</p>
<p>二月勿食蓼,傷人腎。</p>
<p>三月勿食小蒜,傷人志性。</p>
<p>四月、八月勿食胡荽,傷人神。</p>
<p>五月勿食韭,令人乏氣力。</p>
<p>五月五日勿食生菜,發百病。</p>
<p>六月、七月勿食茱萸,傷神氣。</p>
<p>八月、九月勿食薑,傷人神。</p>
<p>十月勿食椒,損人心,傷心脈。</p>
<p>十一月、十二月勿食薤,令人多涕唾。</p>
<p>四季勿食生葵,令人飲食不化,發百病,非但食中,藥中皆不可用,深宜慎之。</p>
<p>時病差未健,食生菜,手足必腫。</p>
<p>夜食生菜,不利人。</p>
<p>十月勿食被霜生菜,令人面無光,目澀心痛,腰疼,或發心瘧,瘧發時手足十指爪皆青,困萎。</p>
<p>蔥韭初生芽者,食之傷人心氣。</p>
<p>飲白酒食生韭,令人病增。</p>
<p>生蔥不可共蜜,食之殺人,獨顆蒜彌忌。</p>
<p>棗和生蔥食之,令人病。</p>
<p>生蔥和雄雞、雉、白犬肉食之,令人七竅經年流血。</p>
<p>食糖蜜後,四日內食生蔥蒜,令人心痛。</p>
<p>夜食諸薑蒜蔥等,傷人心。</p>
<p>蕪菁根多食之,令人氣脹。</p>
<p>薤不可共牛肉作羹食之,成瘕病,韭亦然。</p>
<p>蓴多病,動痔疾。</p>
<p>野苣不可同蜜食之,作內痔。</p>
<p>白苣不可共酪同食,作●蟲。</p>
<p>黃瓜食之,發熱病。</p>
<p>葵心不可食,傷人;葉尤冷,黃背赤莖者勿食之。</p>
<p>胡荽久食之,令人多忘。</p>
<p>病人不可食胡荽及黃花菜。</p>
<p>芋不可多食,動病。</p>
<p>妊婦食薑,令子餘指。</p>
<p>蓼多食,發心痛。</p>
<p>蓼和生魚食之,令人奪氣,陰咳疼痛。</p>
<p>芥菜不可共兔肉食之,成惡邪病。</p>
<p>小蒜多食,傷人心力。</p>
<p>食躁式躁方:</p>
<p>豉濃煮汁飲之。</p>
<p>鉤吻與芹菜相似,誤食之,殺人,解之方:《肘後》云,與茱萸黃食芥相似。</p>
<p>薺苨八兩上一味,水六升,煮取二升,分溫二服。鉤吻生地傍無他草,其莖有毛者,以此別之。</p>
<p>菜中有水莨菪,葉圓而光,有毒,誤食之,令人狂亂,狀如中風,或吐血,治之方:</p>
<p>甘草煮汁,服之即解。</p>
<p>春秋二時,龍帶精入芹菜中,人偶食之為病,發時手青腹滿,痛不可忍,名蛟龍病,治之方:</p>
<p>硬糖二、三升上一味,日兩度,服之,吐出如蜥蜴三五枚,差。</p>
<p>食苦瓠中毒,治之方:</p>
<p>黎穰煮汁,數服之解。</p>
<p>扁豆,寒熱者,不可食之。</p>
<p>久食小豆,令人枯燥。</p>
<p>食大豆等,忌啖豬肉。</p>
<p>大麥久食,令人作●。</p>
<p>白黍米不可同飴蜜食,亦不可合葵食之。</p>
<p>荍麥麵,多食令人髮落。</p>
<p>鹽多食,傷人肺。</p>
<p>食冷物,冰人齒。食熱物,勿飲冷水。</p>
<p>飲酒,食生蒼耳,令人心痛。</p>
<p>夏月大醉汗流,不得冷水洗著身,及使扇,即成病。</p>
<p>飲酒大忌灸腹背,令人腸結。</p>
<p>醉後勿飽食,發寒熱。</p>
<p>飲酒食豬肉,臥秫稻穰中則發黃。</p>
<p>食飴多飲酒,大忌。</p>
<p>凡水及酒,照見人影動者,不可飲之。</p>
<p>醋合酪食之,令人血瘕。</p>
<p>食白米粥勿食生蒼耳,成走疰。</p>
<p>食甜粥已,食鹽即吐。</p>
<p>犀角?攪飲食,沫出,及澆地墳起者,食之殺人。</p>
<p>飲食中毒煩滿,治之方:</p>
<p>苦參三兩苦酒一升半上二味,煮三沸,三上三下,服之,吐食出即差,或以水煮亦得。</p>
<p>又方:</p>
<p>犀角湯亦佳。</p>
<p>貪食、食多不消,心腹堅滿痛治之方:</p>
<p>鹽一升水三升上二味,煮令鹽消,分三服,當吐出食,便差。</p>
<p>礬石生入腹,破人心肝,亦禁水。</p>
<p>商陸,以水服,殺人。</p>
<p>葶藶子,傅頭瘡,藥成入腦,殺人。</p>
<p>水銀入人耳及六畜等,皆死。以金銀著耳邊,水銀則吐。</p>
<p>苦練無子者殺人。</p>
<p>凡諸毒,多是假毒以投,無知時宜煮甘草薺苨汁飲之,通除諸毒藥。</p>
]]></content>
<tags>
<tag>Medicine</tag>
</tags>
</entry>
<entry>
<title>隐藏一些OCaml Effect的机制,让其语法在精神上更接近delimcc</title>
<url>/2023/06/28/%E9%9A%90%E8%97%8F%E4%B8%80%E4%BA%9BOCaml-Effect%E7%9A%84%E6%9C%BA%E5%88%B6%EF%BC%8C%E8%AE%A9%E5%85%B6%E8%AF%AD%E6%B3%95%E5%9C%A8%E7%B2%BE%E7%A5%9E%E4%B8%8A%E6%9B%B4%E6%8E%A5%E8%BF%91delimcc/</url>
<content><![CDATA[<p><a href="https://github.com/kayceesrk/delimcc_of_fxhandler">delimcc_of_fxhandler这个库</a>在OCaml5的effect handlers上实现了一些delimcc原语(shift/reset, control/prompt这些):</p>
<figure class="highlight ocaml"><table><tr><td class="code"><pre><span class="line"><span class="keyword">let</span> p = new_prompt <span class="literal">()</span> <span class="keyword">in</span></span><br><span class="line"><span class="keyword">assert</span> (<span class="literal">[]</span> = push_prompt p (<span class="keyword">fun</span> <span class="literal">()</span> -></span><br><span class="line"> <span class="number">1</span>::<span class="number">2</span>::take_subcont p (<span class="keyword">fun</span> _k -> <span class="literal">[]</span>)));</span><br><span class="line"><span class="keyword">assert</span> ([<span class="number">1</span>;<span class="number">2</span>] = push_prompt p (<span class="keyword">fun</span> <span class="literal">()</span> -></span><br><span class="line"> <span class="number">1</span>::<span class="number">2</span>::take_subcont p (<span class="keyword">fun</span> k -> push_subcont k <span class="literal">[]</span>)));</span><br><span class="line"><span class="keyword">assert</span> (<span class="number">135</span> =</span><br><span class="line"> <span class="keyword">let</span> p1 = new_prompt <span class="literal">()</span> <span class="keyword">in</span></span><br><span class="line"> <span class="keyword">let</span> p2 = new_prompt <span class="literal">()</span> <span class="keyword">in</span></span><br><span class="line"> <span class="keyword">let</span> p3 = new_prompt <span class="literal">()</span> <span class="keyword">in</span></span><br><span class="line"> <span class="keyword">let</span> pushtwice sk =</span><br><span class="line"> sk (<span class="keyword">fun</span> <span class="literal">()</span> -></span><br><span class="line"> sk (<span class="keyword">fun</span> <span class="literal">()</span> -></span><br><span class="line"> shift0 p2 (<span class="keyword">fun</span> sk2 -> sk2 (<span class="keyword">fun</span> <span class="literal">()</span> -></span><br><span class="line"> sk2 (<span class="keyword">fun</span> <span class="literal">()</span> -> <span class="number">3</span>))) <span class="literal">()</span>))</span><br><span class="line"> <span class="keyword">in</span></span><br><span class="line"> push_prompt p1 (<span class="keyword">fun</span> <span class="literal">()</span> -></span><br><span class="line"> push_prompt p2 (<span class="keyword">fun</span> <span class="literal">()</span> -></span><br><span class="line"> push_prompt p3 (<span class="keyword">fun</span> <span class="literal">()</span> -> shift0 p1 pushtwice <span class="literal">()</span>) + <span class="number">10</span>) + <span class="number">1</span>) + <span class="number">100</span>);</span><br><span class="line"></span><br><span class="line">print_endline <span class="string">"Success!"</span></span><br></pre></td></tr></table></figure>
<p>另外, <a href="https://github.com/avsm/ocaml/commits/effect-syntax">avsm这里</a>可以看到一些OCaml的Effect Syntax进展。</p>
<p>还有 <a href="https://github.com/dhil/ocaml-multicont">multi-shot continuations in OCaml</a>,在这个仓库里面还讨论了一些有趣的问题,例如,OCaml 编译器和runtime会做出一些假设从而进行一些优化,这些优化在使用multi-shot continutation时是不可取的(或完全错误的)。编译器优化导致错误的一个例子是堆到栈的转换,例如:</p>
<figure class="highlight ocaml"><table><tr><td class="code"><pre><span class="line"><span class="comment">(* An illustration of how the heap to stack optimisation is broken.</span></span><br><span class="line"><span class="comment"> * This example is adapted from de Vilhena and Pottier (2021).</span></span><br><span class="line"><span class="comment"> * file: heap2stack.ml</span></span><br><span class="line"><span class="comment"> * compile: ocamlopt -I $(opam var lib)/multicont multicont.cmxa heap2stack.ml</span></span><br><span class="line"><span class="comment"> * run: ./a.out *)</span></span><br><span class="line"></span><br><span class="line"><span class="comment">(* We first require a little bit of setup. The following declares an</span></span><br><span class="line"><span class="comment"> operation `Twice' which we use to implement multiple returns. *)</span></span><br><span class="line"><span class="keyword">type</span> _ <span class="type">Effect</span>.t += <span class="type">Twice</span> : <span class="built_in">unit</span> <span class="type">Effect</span>.t</span><br><span class="line"></span><br><span class="line"><span class="comment">(* The handler `htwice' interprets `Twice' by simply invoking its</span></span><br><span class="line"><span class="comment"> continuation twice. *)</span></span><br><span class="line"><span class="keyword">let</span> htwice : (<span class="built_in">unit</span>, <span class="built_in">unit</span>) <span class="type">Effect</span>.<span class="type">Deep</span>.handler</span><br><span class="line"> = { retc = (<span class="keyword">fun</span> x -> x)</span><br><span class="line"> ; exnc = (<span class="keyword">fun</span> e -> raise e)</span><br><span class="line"> ; effc = (<span class="keyword">fun</span> (<span class="keyword">type</span> a) (eff : a <span class="type">Effect</span>.t) -></span><br><span class="line"> <span class="keyword">let</span> <span class="keyword">open</span> <span class="type">Effect</span>.<span class="type">Deep</span> <span class="keyword">in</span></span><br><span class="line"> <span class="keyword">match</span> eff <span class="keyword">with</span></span><br><span class="line"> | <span class="type">Twice</span> -> <span class="type">Some</span> (<span class="keyword">fun</span> (k : (a, _) continuation) -></span><br><span class="line"> continue (<span class="type">Multicont</span>.<span class="type">Deep</span>.clone_continuation k) <span class="literal">()</span>;</span><br><span class="line"> continue k <span class="literal">()</span>)</span><br><span class="line"> | _ -> <span class="type">None</span>) }</span><br><span class="line"></span><br><span class="line"><span class="comment">(* Now for the interesting stuff. In the code below, the compiler will</span></span><br><span class="line"><span class="comment"> perform an escape analysis on the reference `i' and deduce that it</span></span><br><span class="line"><span class="comment"> does not escape the local scope, because it is unaware of the</span></span><br><span class="line"><span class="comment"> semantics of `perform Twice', hence the optimiser will transform</span></span><br><span class="line"><span class="comment"> `i' into an immediate on the stack to save a heap allocation. As a</span></span><br><span class="line"><span class="comment"> consequence, the assertion `(!i = 1)' will succeed twice, whereas</span></span><br><span class="line"><span class="comment"> it should fail after the second return of `perform Twice'. *)</span></span><br><span class="line"><span class="keyword">let</span> heap2stack <span class="literal">()</span> =</span><br><span class="line"> <span class="type">Effect</span>.<span class="type">Deep</span>.match_with</span><br><span class="line"> (<span class="keyword">fun</span> <span class="literal">()</span> -></span><br><span class="line"> <span class="keyword">let</span> i = <span class="built_in">ref</span> <span class="number">0</span> <span class="keyword">in</span></span><br><span class="line"> <span class="type">Effect</span>.perform <span class="type">Twice</span>;</span><br><span class="line"> i := !i + <span class="number">1</span>;</span><br><span class="line"> <span class="type">Printf</span>.printf <span class="string">"i = %d\n%!"</span> !i;</span><br><span class="line"> <span class="keyword">assert</span> (!i = <span class="number">1</span>))</span><br><span class="line"> <span class="literal">()</span> htwice</span><br><span class="line"></span><br><span class="line"><span class="comment">(* The following does not trigger an assertion failure. *)</span></span><br><span class="line"><span class="keyword">let</span> _ = heap2stack <span class="literal">()</span></span><br><span class="line"></span><br><span class="line"><span class="comment">(* To fix this issue, we can wrap reference allocations in an instance</span></span><br><span class="line"><span class="comment"> of `Sys.opaque_identity'. However, this is not really a viable fix</span></span><br><span class="line"><span class="comment"> in general, as we may not have access to the client code that</span></span><br><span class="line"><span class="comment"> allocates the reference! *)</span></span><br><span class="line"><span class="keyword">let</span> heap2stack' <span class="literal">()</span> =</span><br><span class="line"> <span class="type">Effect</span>.<span class="type">Deep</span>.match_with</span><br><span class="line"> (<span class="keyword">fun</span> <span class="literal">()</span> -></span><br><span class="line"> <span class="keyword">let</span> i = <span class="type">Sys</span>.opaque_identity (<span class="built_in">ref</span> <span class="number">0</span>) <span class="keyword">in</span></span><br><span class="line"> <span class="type">Effect</span>.perform <span class="type">Twice</span>;</span><br><span class="line"> i := !i + <span class="number">1</span>;</span><br><span class="line"> <span class="type">Printf</span>.printf <span class="string">"i = %d\n%!"</span> !i;</span><br><span class="line"> <span class="keyword">assert</span> (!i = <span class="number">1</span>))</span><br><span class="line"> <span class="literal">()</span> htwice</span><br><span class="line"></span><br><span class="line"><span class="comment">(* The following triggers an assertion failure. *)</span></span><br><span class="line"><span class="keyword">let</span> _ = heap2stack' <span class="literal">()</span></span><br></pre></td></tr></table></figure>
]]></content>
<tags>
<tag>Technique</tag>
</tags>
</entry>
<entry>
<title>领域驱动设计中的“聚合根”</title>
<url>/2025/03/13/%E9%A2%86%E5%9F%9F%E9%A9%B1%E5%8A%A8%E8%AE%BE%E8%AE%A1%E4%B8%AD%E7%9A%84%E2%80%9C%E8%81%9A%E5%90%88%E6%A0%B9%E2%80%9D/</url>
<content><![CDATA[<p>在领域驱动设计(Domain-Driven Design,简称DDD)中,聚合根(Aggregate Root)是聚合(Aggregate)中的核心实体,是一个聚合的入口点和控制者,负责维护聚合内部的一致性和不变性条件。聚合是一组紧密相关的领域对象的集合,这些对象通过一定的业务规则绑定在一起,并被视为一个单元。</p>
<p>主要的作用如下:</p>
<ul>
<li>维护不变性:聚合根确保聚合内所有对象的一致性和不变性条件不被破坏。它负责封装与聚合相关的业务逻辑,保证聚合内的对象符合业务规则。</li>
<li>管理生命周期:聚合根负责管理其内部对象的创建、修改和删除。它控制着聚合内部成员的生命周期,包括它们的创建、更新和删除。</li>
<li>处理业务逻辑:聚合根负责处理与聚合相关的业务逻辑和操作,外部系统通过调用聚合根的方法来执行这些操作。它不仅是数据的容器,还负责封装与聚合相关的业务逻辑。</li>
</ul>
<p>其具有以下特性:</p>
<ul>
<li>唯一入口:聚合根是聚合内部对象的唯一入口,外部系统只能与聚合根交互,而无法直接访问聚合内部的其他对象。这样可以避免外部系统直接修改聚合内的实体,确保聚合的一致性和业务逻辑的完整性。</li>
<li>标识唯一性:每个聚合根都有一个全局唯一的标识符(ID),用以区分不同的聚合实例。</li>
<li>事务边界:聚合根常常作为事务的边界,确保事务内的所有操作要么全部成功,要么全部失败,以此来维护数据的完整性。</li>
</ul>
<p>用 F# 来描述,以订单管理为例,大概写一下:</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">type</span> <span class="title class_">OrderStatus</span> <span class="operator">=</span> </span><br><span class="line"> <span class="operator">|</span> New</span><br><span class="line"> <span class="operator">|</span> Shipped</span><br><span class="line"> <span class="operator">|</span> Delivered</span><br><span class="line"> <span class="operator">|</span> Cancelled</span><br><span class="line"></span><br><span class="line"><span class="keyword">type</span> <span class="title class_">OrderItem</span> (productName<span class="operator">:</span> <span class="type">string</span>, price<span class="operator">:</span> <span class="type">float</span>, quantity<span class="operator">:</span> <span class="type">int</span>) <span class="operator">=</span> </span><br><span class="line"> <span class="keyword">do</span></span><br><span class="line"> <span class="keyword">if</span> quantity <span class="operator"><=</span> <span class="number">0</span> <span class="keyword">then</span></span><br><span class="line"> <span class="built_in">failwith</span> <span class="string">"Quantity must be positive"</span></span><br><span class="line"> </span><br><span class="line"> <span class="keyword">member</span> <span class="keyword">public</span> this.ProductName <span class="operator">=</span> productName</span><br><span class="line"> <span class="keyword">member</span> <span class="keyword">public</span> this.Price <span class="operator">=</span> price</span><br><span class="line"> <span class="keyword">member</span> <span class="keyword">public</span> this.Quantity <span class="operator">=</span> quantity</span><br><span class="line"> <span class="keyword">member</span> <span class="keyword">public</span> this.TotalPrice () <span class="operator">=</span> price <span class="operator">*</span> quantity</span><br><span class="line"></span><br><span class="line"><span class="keyword">type</span> <span class="title class_">Order</span> (<span class="built_in">id</span><span class="operator">:</span> <span class="type">int</span>, customerName<span class="operator">:</span> <span class="type">string</span>) <span class="operator">=</span> </span><br><span class="line"> <span class="keyword">let</span> <span class="keyword">mutable</span> status <span class="operator">=</span> OrderStatus.New</span><br><span class="line"> <span class="keyword">let</span> <span class="keyword">mutable</span> orderItems <span class="operator">=</span> []</span><br><span class="line"></span><br><span class="line"> <span class="keyword">member</span> <span class="keyword">public</span> this.Id <span class="operator">=</span> <span class="built_in">id</span></span><br><span class="line"> <span class="keyword">member</span> <span class="keyword">public</span> this.CustomerName <span class="operator">=</span> customerName</span><br><span class="line"> <span class="keyword">member</span> <span class="keyword">public</span> this.Status <span class="operator">=</span> status</span><br><span class="line"> <span class="keyword">member</span> <span class="keyword">public</span> this.OrderItems <span class="operator">=</span> orderItems</span><br><span class="line"></span><br><span class="line"> <span class="keyword">member</span> <span class="keyword">public</span> this.AddItem (item<span class="operator">:</span> OrderItem, price<span class="operator">:</span> <span class="type">float</span>, quantity<span class="operator">:</span> <span class="type">int</span>) <span class="operator">=</span></span><br><span class="line"> <span class="keyword">if</span> quantity <span class="operator"><=</span> <span class="number">0</span> <span class="keyword">then</span></span><br><span class="line"> <span class="built_in">failwith</span> <span class="string">"Quantity must be positive"</span></span><br><span class="line"> </span><br><span class="line"> orderItems <span class="operator"><-</span> orderItems <span class="operator">@</span> [OrderItem(item.ProductName, price, quantity)]</span><br><span class="line"></span><br><span class="line"> <span class="keyword">member</span> <span class="keyword">public</span> this.ChangeStatus (status<span class="operator">:</span> OrderStatus) <span class="operator">=</span></span><br><span class="line"> this.Status <span class="operator"><-</span> status</span><br><span class="line"></span><br><span class="line"> <span class="keyword">member</span> <span class="keyword">public</span> this.TotalPrice () <span class="operator">=</span></span><br><span class="line"> orderItems <span class="operator">|></span> List.sumBy (<span class="keyword">fun</span> item <span class="operator">-></span> item.TotalPrice())</span><br><span class="line"></span><br><span class="line"> <span class="keyword">member</span> <span class="keyword">public</span> this.GetTotalPrice () <span class="operator">=</span></span><br><span class="line"> orderItems <span class="operator">|></span> List.sumBy (<span class="keyword">fun</span> item <span class="operator">-></span> item.TotalPrice())</span><br></pre></td></tr></table></figure>
<p>在这个例子中,<code>Order</code> 是聚合根,它通过 <code>AddItem</code> 方法来添加订单项,保证每个订单项符合业务规则。同时,聚合根 <code>Order</code> 还负责订单状态的管理,例如通过 <code>ChangeStatus</code> 方法来更新订单状态。<code>OrderItem</code> 是聚合内的一个实体,表示订单项,它通过 <code>GetTotalPrice</code> 方法来计算每个订单项的总价。外部系统只能通过 <code>Order</code> 聚合根来访问和操作订单项,而不能直接访问或修改 <code>OrderItem</code></p>
]]></content>
<tags>
<tag>Technique</tag>
</tags>
</entry>
<entry>
<title>领域驱动设计中聚合根持久化和事件发布可能导致数据不一致问题</title>
<url>/2025/03/19/%E9%A2%86%E5%9F%9F%E9%A9%B1%E5%8A%A8%E8%AE%BE%E8%AE%A1%E4%B8%AD%E8%81%9A%E5%90%88%E6%A0%B9%E6%8C%81%E4%B9%85%E5%8C%96%E5%92%8C%E4%BA%8B%E4%BB%B6%E5%8F%91%E5%B8%83%E5%8F%AF%E8%83%BD%E5%AF%BC%E8%87%B4%E6%95%B0%E6%8D%AE%E4%B8%8D%E4%B8%80%E8%87%B4%E9%97%AE%E9%A2%98/</url>
<content><![CDATA[<p>使用领域事件的一种直接做法是:在 应用服务 (Application Service) 中产生事件并发布出去。例如,对于“用户昵称更新”的场景来讲,对应的应用服务 <code>UserCommandService</code> 实现如下:</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">member</span> <span class="keyword">public</span> this.UpdateMyName (command<span class="operator">:</span> UpdateUsernameCommand) (user<span class="operator">:</span> User) <span class="operator">=</span></span><br><span class="line"> <span class="keyword">let</span> user <span class="operator">=</span> userRepository.GetById user.Id</span><br><span class="line"> <span class="keyword">let</span> oldName <span class="operator">=</span> user.Username</span><br><span class="line"> <span class="keyword">let</span> newName <span class="operator">=</span> command.Username</span><br><span class="line"></span><br><span class="line"> user.UpdateUsername newName</span><br><span class="line"> <span class="operator">|></span> userRepository.Save</span><br><span class="line"> </span><br><span class="line"> UsernameChangeEvent (user.Id, newName, oldName) </span><br><span class="line"> <span class="operator">|></span> eventPublisher.Publish</span><br></pre></td></tr></table></figure>
<p>这里,在更新了用户姓名之后,即刻调用事件发布器 <code>eventPublisher.Publish</code> 将事件发送到消息队列中。虽然这种方式比较流行,但它至少存在两个问题:</p>
<ol>
<li>领域事件本应属于领域模型的一部分,也即应该从领域模型中产生,而这里却在应用服务中产生</li>
<li>对聚合根(本例中的 <code>User</code> )的持久化和对事件的发布可能导致数据不一致问题。</li>
</ol>
<p>对于第1个问题,可以采用“从领域模型中返回领域事件”的方式:</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">member</span> <span class="keyword">public</span> this.UpdateMyName (command<span class="operator">:</span> UpdateUsernameCommand) (user<span class="operator">:</span> User) <span class="operator">=</span></span><br><span class="line"> <span class="keyword">let</span> user <span class="operator">=</span> userRepository.GetById user.Id</span><br><span class="line"> <span class="keyword">let</span> oldName <span class="operator">=</span> user.Username</span><br><span class="line"> <span class="keyword">let</span> newName <span class="operator">=</span> command.Username</span><br><span class="line"></span><br><span class="line"> user.UpdateUsername newName <span class="comment">// UpdateUsername 中构建 UsernameChangeEvent</span></span><br><span class="line"> <span class="operator">|></span> <span class="keyword">fun</span> user event <span class="operator">-></span> </span><br><span class="line"> userRepository.Save user</span><br><span class="line"> eventPublisher.Publish event</span><br></pre></td></tr></table></figure>
<p>这种方式保证了领域事件是从领域模型中产生,但仍然存在第二个问题。</p>
<p>第二个问题中所谓的“数据一致性”,表示的是将聚合根保存到数据库和将领域事件发布到消息队列之间的一致性。由于数据库和消息队列属于异构的数据源,要保证他们之间的数据一致性需要引入分布式事务。</p>
<p>但是分布式事务通常是比较重量级的,再加上当下的诸多常见消息队列均不支持分布式事务(比如Kafka),因此并不建议使用分布式事务来解决这个问题。</p>
<p><a href="https://microservices.io/patterns/data/transactional-outbox.html">Transactional Outbox</a> 便是一种方案,概括来说,这种方式将一个分布式事务的问题拆解为多个本地事务,并采用“至少一次投递(At Least Once Delivery)”原则保证消息的发布。具体来讲,发布方在与业务数据相同的数据库中为领域事件创建相应的事件发布表(Outbox table),然后在保存业务数据的同时将所产生的事件保存到事件发布表中,由于此时二者都属于同一个数据库的本地事务所管辖,因此保证了“业务操作”与“事件产生”之间的一致性。此时的代码变成了:</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">member</span> <span class="keyword">public</span> this.UpdateMyName (command<span class="operator">:</span> UpdateUsernameCommand) (user<span class="operator">:</span> User) <span class="operator">=</span></span><br><span class="line"> <span class="keyword">let</span> user <span class="operator">=</span> userRepository.GetById user.Id</span><br><span class="line"> <span class="keyword">let</span> oldName <span class="operator">=</span> user.Username</span><br><span class="line"> <span class="keyword">let</span> newName <span class="operator">=</span> command.Username</span><br><span class="line"></span><br><span class="line"> user.UpdateUsername newName <span class="comment">// UpdateUsername 中构建 UsernameChangeEvent</span></span><br><span class="line"> <span class="operator">|></span> <span class="keyword">fun</span> user event <span class="operator">-></span> </span><br><span class="line"> userRepository.Save user</span><br><span class="line"> eventStore.Save event <span class="comment">// 这儿用 eventStore 代替 eventPublisher 啦</span></span><br></pre></td></tr></table></figure>
<p>应用服务不再将事件直接发布出去,而是将事件保存到数据库中,之后,另一个模块将从数据库中读取事件并发布。</p>
<p>然而,这种方式依然有个缺点:每个需要产生领域事件的场景都需要应用服务先后调用<code>repository.Save()</code>和<code>eventStore.Save()</code>,导致了代码重复。解决方法也很简单——在聚合根中临时保存领域事件,然后在资源库中同时保存聚合根和领域事件到数据库。</p>
<p>在这种方式下,首先需要在聚合根的基类中完成与领域事件相关的各种设施,包括创建临时性的事件容器<code>events</code>以及通用的事件产生方法<code>RaiseEvent()</code>:</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="meta">[<AbstractClass>]</span></span><br><span class="line"><span class="keyword">type</span> <span class="title class_">IAggregateRoot</span> <span class="operator">=</span> </span><br><span class="line"> <span class="operator">...</span></span><br><span class="line"> <span class="keyword">let</span> events <span class="operator">=</span> Collections.Generic.List<span class="operator"><</span>DomainEvent<span class="operator">></span> ()</span><br><span class="line"></span><br><span class="line"> <span class="keyword">member</span> <span class="keyword">private</span> this.RaiseEvent (event<span class="operator">:</span> DomainEvent) <span class="operator">=</span></span><br><span class="line"> events.Add event</span><br><span class="line"> </span><br><span class="line"> <span class="operator">...</span></span><br></pre></td></tr></table></figure>
<p>在聚合根基类<code>AggregateRoot</code>中,<code>events</code>字段用于临时保存聚合根中所产生的所有事件,各实际的聚合根类通过调用<code>RaiseEvent()</code>向<code>events</code>中添加事件。比如,对于“用户修改昵称”而言,<code>User</code>实现如下:</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">member</span> <span class="keyword">public</span> this.UpdateUsername (name<span class="operator">:</span> <span class="type">string</span>, user<span class="operator">:</span> User) <span class="operator">=</span></span><br><span class="line"> <span class="keyword">if</span> this.Username <span class="operator">=</span> name <span class="keyword">then</span></span><br><span class="line"> ()</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> <span class="keyword">let</span> oldName <span class="operator">=</span> this.Username</span><br><span class="line"> this.Username <span class="operator"><-</span> name</span><br><span class="line"> UsernameChangeEvent (user.Id, name, oldName) </span><br><span class="line"> <span class="operator">|></span> this.RaiseEvent</span><br></pre></td></tr></table></figure>
<p>这里,聚合根 <code>User</code> 不再返回领域事件,而是将领域事件通过<code>AggregateRoot.RaiseEvent()</code>暂时性地保存到自身的<code>events</code>中。之后在保存<code>User</code>时,资源库的公共基类<code>BaseRepository</code>的<code>Save()</code>方法同时完成对聚合根和领域事件的持久化:</p>
<figure class="highlight fsharp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">member</span> <span class="keyword">public</span> this.Save<span class="operator"><</span>AR<span class="operator">:</span> <span class="keyword">when</span> AR <span class="operator">:></span> AggrateRoot<span class="operator">></span> (it<span class="operator">:</span> AR) <span class="operator">=</span></span><br><span class="line"> <span class="keyword">match</span> it <span class="keyword">with</span></span><br><span class="line"> <span class="operator">|</span> <span class="literal">null</span> <span class="operator">-></span> <span class="built_in">failwith</span> <span class="string">"..."</span></span><br><span class="line"> <span class="operator">|</span> it <span class="keyword">when</span> it.Events <span class="operator">|></span> isEmpty <span class="operator">|></span> <span class="built_in">not</span> <span class="operator">-></span> </span><br><span class="line"> this.SaveEvents it.Events</span><br><span class="line"> this.CleanEvents ()</span><br><span class="line"> <span class="operator">|</span> _ <span class="operator">-></span> ()</span><br><span class="line"></span><br><span class="line"> db.Save it</span><br></pre></td></tr></table></figure>
<p>在<code>Save()</code>方法中,首先获取到聚合根中的所有领域事件,然后通过<code>SaveEvents()</code>方法将它们保存到发布事件表中,最后通过<code>db.Save it</code>保存聚合根。需要注意的是,在这种方式下,<code>AggregateRoot</code>中的<code>events</code>字段是不能被持久化的,因为需要保证每次从数据库中加载出聚合根时<code>events</code>都是空的,为此在<code>SaveEvents()</code>保存了领域事件后,立即调用<code>it.clearEvents()</code>将所有的领域事件清空掉,以免领域事件随着聚合根一道被持久化到数据库中。</p>
<p>到目前为止,对领域事件的处理都还没有涉及到与任何消息中间件相关的内容,也即事件的产生是一个完全独立于消息队列的关注点,此时不用关心领域事件之后将以何种形式发布出去,Kafka 也好,RabbitMQ 也罢。除了关注点分离的好处外,这种解耦也使得系统在有可能切换消息中间件时更加的简单。</p>
<p>对于“在应用服务中通过<code>eventPublisher.Publish()</code>直接发布事件”而言,事件的产生和发布是同时完成的;但是对于“在聚合根中临时性保存领域事件”的方式来说,它只解决了事件的产生问题,并未解决事件的发布问题,事件的发布方应该采用“发射后不管(Fire And Forget)”的原则,即发布方无需了解消费方是如何处理领域事件的,甚至都不需要知道事件被哪些消费方所消费。</p>
<p>但是因为发送事件需要操作消息中间件,而更新事件状态需要操作数据库。在不使用分布式事务的情况下,此时的代码对于“事件发布成功 + 数据库落库成功”来讲是皆大欢喜的,但是依然无法排除有很小的概率导致事件发送成功了但是状态却为得到更新的情况。要解决这个问题,有一个选择是做妥协,即事件发布方无法保证事件的“<a href="https://www.cloudcomputingpatterns.org/exactly_once_delivery/">精确一次性投递(Exactly Once)</a>”,而是保证“<a href="https://www.cloudcomputingpatterns.org/at_least_once_delivery/">至少一次投递(At Least Once)</a>”。假设在事件发布成功之后,由于种种原因导致事件的状态未得到更新,即依然为<code>CREATED</code>状态,那么稍后,当事件兜底机制启动时,它将加载系统中尚未发布的事件进行发布,其中就包含状态为<code>CREATED</code>的事件,进而导致事件的重复投递。</p>
<p>“至少一次投递”将更多的负担转嫁给了事件的消费方,使得事件发送方得以全身而退。</p>
<p>事件消费的重点在于如何解决发布方的“至少一次投递”问题。举个例子,假设在电商系统中,订单子系统发布了“订单已成交”(<code>OrderPlacedEvent</code>)事件,积分子系统消费这个事件时会给用户新增与订单价格等额的积分,但是对事件的“至少一次投递”有可能导致该事件被重复投递进而导致重复给用户积分的情况产生。解决这个问题通常有2种方式:</p>
<ol>
<li>将消费方自身的处理逻辑设计为幂等的,即多次执行和一次执行的结果是相同的</li>
<li>消费方在数据库中建立一个事件消费表,用于跟踪已经被消费的事件</li>
</ol>
<p>第一种方式是最理想的,消费方不用引入额外的支撑性机制,但是这种方式对消费方的要求太高,并不是所有场景都能将消费方本身的处理逻辑设计为幂等。因此,实践中主要采用第二种方式。</p>
]]></content>
<tags>
<tag>Technique</tag>
</tags>
</entry>
<entry>
<title>关于我</title>
<url>/about.html</url>
<content><![CDATA[<h2 id="韩暮秋"><a class="header-anchor" href="#韩暮秋">¶</a>韩暮秋</h2>
<ul>
<li>Github: <a href="https://github.com/muqiuhan">https://github.com/muqiuhan</a></li>
<li>QQ群: <a href="http://qm.qq.com/cgi-bin/qm/qr?_wv%253D1027%2526k%253DAdCEKMDgAUg2ppuYqPt-OUeXBUG_jFN0%2526authKey%253D4%252FlDBj2jxzc32hN756pW2wlDkaNR%252FqrstHr6hsIy%252B6pfeRnvPvS7QBMUgeQfkp2V%2526noverify%253D0%2526group_code%253D780653172">780653172</a></li>
<li>邮箱: <a href="[email protected]">[email protected]</a></li>
</ul>
]]></content>
</entry>
<entry>
<title>'tag'</title>
<url>/tag/index.html</url>
<content><![CDATA[]]></content>
</entry>
</search>
|