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 7434 7435 7436 7437 7438 7439 7440 7441 7442 7443 7444 7445 7446 7447 7448 7449 7450 7451 7452 7453 7454 7455 7456 7457 7458 7459 7460 7461 7462 7463 7464 7465 7466 7467 7468 7469 7470 7471 7472 7473 7474 7475 7476 7477 7478 7479 7480 7481 7482 7483 7484 7485 7486 7487 7488 7489 7490 7491 7492 7493 7494 7495 7496 7497 7498 7499 7500 7501 7502 7503 7504 7505 7506 7507 7508 7509 7510 7511 7512 7513 7514 7515 7516 7517 7518 7519 7520 7521 7522 7523 7524 7525 7526 7527 7528 7529 7530 7531 7532 7533 7534 7535 7536 7537 7538 7539 7540 7541 7542 7543 7544 7545 7546 7547 7548 7549 7550 7551 7552 7553 7554 7555 7556 7557 7558 7559 7560 7561 7562 7563 7564 7565 7566 7567 7568 7569 7570 7571 7572 7573 7574 7575 7576 7577 7578 7579 7580 7581 7582 7583 7584 7585 7586 7587 7588 7589 7590 7591 7592 7593 7594 7595 7596 7597 7598 7599 7600 7601 7602 7603 7604 7605 7606 7607 7608 7609 7610 7611 7612 7613 7614 7615 7616 7617 7618 7619 7620 7621 7622 7623 7624 7625 7626 7627 7628 7629 7630 7631 7632 7633 7634 7635 7636 7637 7638 7639 7640 7641 7642 7643 7644 7645 7646 7647 7648 7649 7650 7651 7652 7653 7654 7655 7656 7657 7658 7659 7660 7661 7662 7663 7664 7665 7666 7667 7668 7669 7670 7671 7672 7673 7674 7675 7676 7677 7678 7679 7680 7681 7682 7683 7684 7685 7686 7687 7688 7689 7690 7691 7692 7693 7694 7695 7696 7697 7698 7699 7700 7701 7702 7703 7704 7705 7706 7707 7708 7709 7710 7711 7712 7713 7714 7715 7716 7717 7718 7719 7720 7721 7722 7723 7724 7725 7726 7727 7728 7729 7730 7731 7732 7733 7734 7735 7736 7737 7738 7739 7740 7741 7742 7743 7744 7745 7746 7747 7748 7749 7750 7751 7752 7753 7754 7755 7756 7757 7758 7759 7760 7761 7762 7763 7764 7765 7766 7767 7768 7769 7770 7771 7772 7773 7774 7775 7776 7777 7778 7779 7780 7781 7782 7783 7784 7785 7786 7787 7788 7789 7790 7791 7792 7793 7794 7795 7796 7797 7798 7799 7800 7801 7802 7803 7804 7805 7806 7807 7808 7809 7810 7811 7812 7813 7814 7815 7816 7817 7818 7819 7820 7821 7822 7823 7824 7825 7826 7827 7828 7829 7830 7831 7832 7833 7834 7835 7836 7837 7838 7839 7840 7841 7842 7843 7844 7845 7846 7847 7848 7849 7850 7851 7852 7853 7854 7855 7856 7857 7858 7859 7860 7861 7862 7863 7864 7865 7866 7867 7868 7869 7870 7871 7872 7873 7874 7875 7876 7877 7878 7879 7880 7881 7882 7883 7884 7885 7886 7887 7888 7889 7890 7891 7892 7893 7894 7895 7896 7897 7898 7899 7900 7901 7902 7903 7904 7905 7906 7907 7908 7909 7910 7911 7912 7913 7914 7915 7916 7917 7918 7919 7920 7921 7922 7923 7924 7925 7926 7927 7928 7929 7930 7931 7932 7933 7934 7935 7936 7937 7938 7939 7940 7941 7942 7943 7944 7945 7946 7947 7948 7949 7950 7951 7952 7953 7954 7955 7956 7957 7958 7959 7960 7961 7962 7963 7964 7965 7966 7967 7968 7969 7970 7971 7972 7973 7974 7975 7976 7977 7978 7979 7980 7981 7982 7983 7984 7985 7986 7987 7988 7989 7990 7991 7992 7993 7994 7995 7996 7997 7998 7999 8000 8001 8002 8003 8004 8005 8006 8007 8008 8009 8010 8011 8012 8013 8014 8015 8016 8017 8018 8019 8020 8021 8022 8023 8024 8025 8026 8027 8028 8029 8030 8031 8032 8033 8034 8035 8036 8037 8038 8039 8040 8041 8042 8043 8044 8045 8046 8047 8048 8049 8050 8051 8052 8053 8054 8055 8056 8057 8058 8059 8060 8061 8062 8063 8064 8065 8066 8067 8068 8069 8070 8071 8072 8073 8074 8075 8076 8077 8078 8079 8080 8081 8082 8083 8084 8085 8086 8087 8088 8089 8090 8091 8092 8093 8094 8095 8096 8097 8098 8099 8100 8101 8102 8103 8104 8105 8106 8107 8108 8109 8110 8111 8112 8113 8114 8115 8116 8117 8118 8119 8120 8121 8122 8123 8124 8125 8126 8127 8128 8129 8130 8131 8132 8133 8134 8135 8136 8137 8138 8139 8140 8141 8142 8143 8144 8145 8146 8147 8148 8149 8150 8151 8152 8153 8154 8155 8156 8157 8158 8159 8160 8161 8162 8163 8164 8165 8166 8167 8168 8169 8170 8171 8172 8173 8174 8175 8176 8177 8178 8179 8180 8181 8182 8183 8184 8185 8186 8187 8188 8189 8190 8191 8192 8193 8194 8195 8196 8197 8198 8199 8200 8201 8202 8203 8204 8205 8206 8207 8208 8209 8210 8211 8212 8213 8214 8215 8216 8217 8218 8219 8220 8221 8222 8223 8224 8225 8226 8227 8228 8229 8230 8231 8232 8233 8234 8235 8236 8237 8238 8239 8240 8241 8242 8243 8244 8245 8246 8247 8248 8249 8250 8251 8252 8253 8254 8255 8256 8257 8258 8259 8260 8261 8262 8263 8264 8265 8266 8267 8268 8269 8270 8271 8272 8273 8274 8275 8276 8277 8278 8279 8280 8281 8282 8283 8284 8285 8286 8287 8288 8289 8290 8291 8292 8293 8294 8295 8296 8297 8298 8299 8300 8301 8302 8303 8304 8305 8306 8307 8308 8309 8310 8311 8312 8313 8314 8315 8316 8317 8318 8319 8320 8321 8322 8323 8324 8325 8326 8327 8328 8329 8330 8331 8332 8333 8334 8335 8336 8337 8338 8339 8340 8341 8342 8343 8344 8345 8346 8347 8348 8349 8350 8351 8352 8353 8354 8355 8356 8357 8358 8359 8360 8361 8362 8363 8364 8365 8366 8367 8368 8369 8370 8371 8372 8373 8374 8375 8376 8377 8378 8379 8380 8381 8382 8383 8384 8385 8386 8387 8388 8389 8390 8391 8392 8393 8394 8395 8396 8397 8398 8399 8400 8401 8402 8403 8404 8405 8406 8407 8408 8409 8410 8411 8412 8413 8414 8415 8416 8417 8418 8419 8420 8421 8422 8423 8424 8425 8426 8427 8428 8429 8430 8431 8432 8433 8434 8435 8436 8437 8438 8439 8440 8441 8442 8443 8444 8445 8446 8447 8448 8449 8450 8451 8452 8453 8454 8455 8456 8457 8458 8459 8460 8461 8462 8463 8464 8465 8466 8467 8468 8469 8470 8471 8472 8473 8474 8475 8476 8477 8478 8479 8480 8481 8482 8483 8484 8485 8486 8487 8488 8489 8490 8491 8492 8493 8494 8495 8496 8497 8498 8499 8500 8501 8502 8503 8504 8505 8506 8507 8508 8509 8510 8511 8512 8513 8514 8515 8516 8517 8518 8519 8520 8521 8522 8523 8524 8525 8526 8527 8528 8529 8530 8531 8532 8533 8534 8535 8536 8537 8538 8539 8540 8541 8542 8543 8544 8545 8546 8547 8548 8549 8550 8551 8552 8553 8554 8555 8556 8557 8558 8559 8560 8561 8562 8563 8564 8565 8566 8567 8568 8569 8570 8571 8572 8573 8574 8575 8576 8577 8578 8579 8580 8581 8582 8583 8584 8585 8586 8587 8588 8589 8590 8591 8592 8593 8594 8595 8596 8597 8598 8599 8600 8601 8602 8603 8604 8605 8606 8607 8608 8609 8610 8611 8612 8613 8614 8615 8616 8617 8618 8619 8620 8621 8622 8623 8624 8625 8626 8627 8628 8629 8630 8631 8632 8633 8634 8635 8636 8637 8638 8639 8640 8641 8642 8643 8644 8645 8646 8647 8648 8649 8650 8651 8652 8653 8654 8655 8656 8657 8658 8659 8660 8661 8662 8663 8664 8665 8666 8667 8668 8669 8670 8671 8672 8673 8674 8675 8676 8677 8678 8679 8680 8681 8682 8683 8684 8685 8686 8687 8688 8689 8690 8691 8692 8693 8694 8695 8696 8697 8698 8699 8700 8701 8702 8703 8704 8705 8706 8707 8708 8709 8710 8711 8712 8713 8714 8715 8716 8717 8718 8719 8720 8721 8722 8723 8724 8725 8726 8727 8728 8729 8730 8731 8732 8733 8734 8735 8736 8737 8738 8739 8740 8741 8742 8743 8744 8745 8746 8747 8748 8749 8750 8751 8752 8753 8754 8755 8756 8757 8758 8759 8760 8761 8762 8763 8764 8765 8766 8767 8768 8769 8770 8771 8772 8773 8774 8775 8776 8777 8778 8779 8780 8781 8782 8783 8784 8785 8786 8787 8788 8789 8790 8791 8792 8793 8794 8795 8796 8797 8798 8799 8800 8801 8802 8803 8804 8805 8806 8807 8808 8809 8810 8811 8812 8813 8814 8815 8816 8817 8818 8819 8820 8821 8822 8823 8824 8825 8826 8827 8828 8829 8830 8831 8832 8833 8834 8835 8836 8837 8838 8839 8840 8841 8842 8843 8844 8845 8846 8847 8848 8849 8850 8851 8852 8853 8854 8855 8856 8857 8858 8859 8860 8861 8862 8863 8864 8865 8866 8867 8868 8869 8870 8871 8872 8873 8874 8875 8876 8877 8878 8879 8880 8881 8882 8883 8884 8885 8886 8887 8888 8889 8890 8891 8892 8893 8894 8895 8896 8897 8898 8899 8900 8901 8902 8903 8904 8905 8906 8907 8908 8909 8910 8911 8912 8913 8914 8915 8916 8917 8918 8919 8920 8921 8922 8923 8924 8925 8926 8927 8928 8929 8930 8931 8932 8933 8934 8935 8936 8937 8938 8939 8940 8941 8942 8943 8944 8945 8946 8947 8948 8949 8950 8951 8952 8953 8954 8955 8956 8957 8958 8959 8960 8961 8962 8963 8964 8965 8966 8967 8968 8969 8970 8971 8972 8973 8974 8975 8976 8977 8978 8979 8980 8981 8982 8983 8984 8985 8986 8987 8988 8989 8990 8991 8992 8993 8994 8995 8996 8997 8998 8999 9000 9001 9002 9003 9004 9005 9006 9007 9008 9009 9010 9011 9012 9013 9014 9015 9016 9017 9018 9019 9020 9021 9022 9023 9024 9025 9026 9027 9028 9029 9030 9031 9032 9033 9034 9035 9036 9037 9038 9039 9040 9041 9042 9043 9044 9045 9046 9047 9048 9049 9050 9051 9052 9053 9054 9055 9056 9057 9058 9059 9060 9061 9062 9063 9064 9065 9066 9067 9068 9069 9070 9071 9072 9073 9074 9075 9076 9077 9078 9079 9080 9081 9082 9083 9084 9085 9086 9087 9088 9089 9090 9091 9092 9093 9094 9095 9096 9097 9098 9099 9100 9101 9102 9103 9104 9105 9106 9107 9108 9109 9110 9111 9112 9113 9114 9115 9116 9117 9118 9119 9120 9121 9122 9123 9124 9125 9126 9127 9128 9129 9130 9131 9132 9133 9134 9135 9136 9137 9138 9139 9140 9141 9142 9143 9144 9145 9146 9147 9148 9149 9150 9151 9152 9153 9154 9155 9156 9157 9158 9159 9160 9161 9162 9163 9164 9165 9166 9167 9168 9169 9170 9171 9172 9173 9174 9175 9176 9177 9178 9179 9180 9181 9182 9183 9184 9185 9186 9187 9188 9189 9190 9191 9192 9193 9194 9195 9196 9197 9198 9199 9200 9201 9202 9203 9204 9205 9206 9207 9208 9209 9210 9211 9212 9213 9214 9215 9216 9217 9218 9219 9220 9221 9222 9223 9224 9225 9226 9227 9228 9229 9230 9231 9232 9233 9234 9235 9236 9237 9238 9239 9240 9241 9242 9243 9244 9245 9246 9247 9248 9249 9250 9251 9252 9253 9254 9255 9256 9257 9258 9259 9260 9261 9262 9263 9264 9265 9266 9267 9268 9269 9270 9271 9272 9273 9274 9275 9276 9277 9278 9279 9280 9281 9282 9283 9284 9285 9286 9287 9288 9289 9290 9291 9292 9293 9294 9295 9296 9297 9298 9299 9300 9301 9302 9303 9304 9305 9306 9307 9308 9309 9310 9311 9312 9313 9314 9315 9316 9317 9318 9319 9320 9321 9322 9323 9324 9325 9326 9327 9328 9329 9330 9331 9332 9333 9334 9335 9336 9337 9338 9339 9340 9341 9342 9343 9344 9345 9346 9347 9348 9349 9350 9351 9352 9353 9354 9355 9356 9357 9358 9359 9360 9361 9362 9363 9364 9365 9366 9367 9368 9369 9370 9371 9372 9373 9374 9375 9376 9377 9378 9379 9380 9381 9382 9383 9384 9385 9386 9387 9388 9389 9390 9391 9392 9393 9394 9395 9396 9397 9398 9399 9400 9401 9402 9403 9404 9405 9406 9407 9408 9409 9410 9411 9412 9413 9414 9415 9416 9417 9418 9419 9420 9421 9422 9423 9424 9425 9426 9427 9428 9429 9430 9431 9432 9433 9434 9435 9436 9437 9438 9439 9440 9441 9442 9443 9444 9445 9446 9447 9448 9449 9450 9451 9452 9453 9454 9455 9456 9457 9458 9459 9460 9461 9462 9463 9464 9465 9466 9467 9468 9469 9470 9471 9472 9473 9474 9475 9476 9477 9478 9479 9480 9481 9482 9483 9484 9485 9486 9487 9488 9489 9490 9491 9492 9493 9494 9495 9496 9497 9498 9499 9500 9501 9502 9503 9504 9505 9506 9507 9508 9509 9510 9511 9512 9513 9514 9515 9516 9517 9518 9519 9520 9521 9522 9523 9524 9525 9526 9527 9528 9529 9530 9531 9532 9533 9534 9535 9536 9537 9538 9539 9540 9541 9542 9543 9544 9545 9546 9547 9548 9549 9550 9551 9552 9553 9554 9555 9556 9557 9558 9559 9560 9561 9562 9563 9564 9565 9566 9567 9568 9569 9570 9571 9572 9573 9574 9575 9576 9577 9578 9579 9580 9581 9582 9583 9584 9585 9586 9587 9588 9589 9590 9591 9592 9593 9594 9595 9596 9597 9598 9599 9600 9601 9602 9603 9604 9605 9606 9607 9608 9609 9610 9611 9612 9613 9614 9615 9616 9617 9618 9619 9620 9621 9622 9623 9624 9625 9626 9627 9628 9629 9630 9631 9632 9633 9634 9635 9636 9637 9638 9639 9640 9641 9642 9643 9644 9645 9646 9647 9648 9649 9650 9651 9652 9653 9654 9655 9656 9657 9658 9659 9660 9661 9662 9663 9664 9665 9666 9667 9668 9669 9670 9671 9672 9673 9674 9675 9676 9677 9678 9679 9680 9681 9682 9683 9684 9685 9686 9687 9688 9689 9690 9691 9692 9693 9694 9695 9696 9697 9698 9699 9700 9701 9702 9703 9704 9705 9706 9707 9708 9709 9710 9711 9712 9713 9714 9715 9716 9717 9718 9719 9720 9721 9722 9723 9724 9725 9726 9727 9728 9729 9730 9731 9732 9733 9734 9735 9736 9737 9738 9739 9740 9741 9742 9743 9744 9745 9746 9747 9748 9749 9750 9751 9752 9753 9754 9755 9756 9757 9758 9759 9760 9761 9762 9763 9764 9765 9766 9767 9768 9769 9770 9771 9772 9773 9774 9775 9776 9777 9778 9779 9780 9781 9782 9783 9784 9785 9786 9787 9788 9789 9790 9791 9792 9793 9794 9795 9796 9797 9798 9799 9800 9801 9802 9803 9804 9805 9806 9807 9808 9809 9810 9811 9812 9813 9814 9815 9816 9817 9818 9819 9820 9821 9822 9823 9824 9825 9826 9827 9828 9829 9830 9831 9832 9833 9834 9835 9836 9837 9838 9839 9840 9841 9842 9843 9844 9845 9846 9847 9848 9849 9850 9851 9852 9853 9854 9855 9856 9857 9858 9859 9860 9861 9862 9863 9864 9865 9866 9867 9868 9869 9870 9871 9872 9873 9874 9875 9876 9877 9878 9879 9880 9881 9882 9883 9884 9885 9886 9887 9888 9889 9890 9891 9892 9893 9894 9895 9896 9897 9898 9899 9900 9901 9902 9903 9904 9905 9906 9907 9908 9909 9910 9911 9912 9913 9914 9915 9916 9917 9918 9919 9920 9921 9922 9923 9924 9925 9926 9927 9928 9929 9930 9931 9932 9933 9934 9935 9936 9937 9938 9939 9940 9941 9942 9943 9944 9945 9946 9947 9948 9949 9950 9951 9952 9953 9954 9955 9956 9957 9958 9959 9960 9961 9962 9963 9964 9965 9966 9967 9968 9969 9970 9971 9972 9973 9974 9975 9976 9977 9978 9979 9980 9981 9982 9983 9984 9985 9986 9987 9988 9989 9990 9991 9992 9993 9994 9995 9996 9997 9998 9999 10000 10001 10002 10003 10004 10005 10006 10007 10008 10009 10010 10011 10012 10013 10014 10015 10016 10017 10018 10019 10020 10021 10022 10023 10024 10025 10026 10027 10028 10029 10030 10031 10032 10033 10034 10035 10036 10037 10038 10039 10040 10041 10042 10043 10044 10045 10046 10047 10048 10049 10050 10051 10052 10053 10054 10055 10056 10057 10058 10059 10060 10061 10062 10063 10064 10065 10066 10067 10068 10069 10070 10071 10072 10073 10074 10075 10076 10077 10078 10079 10080 10081 10082 10083 10084 10085 10086 10087 10088 10089 10090 10091 10092 10093 10094 10095 10096 10097 10098 10099 10100 10101 10102 10103 10104 10105 10106 10107 10108 10109 10110 10111 10112 10113 10114 10115 10116 10117 10118 10119 10120 10121 10122 10123 10124 10125 10126 10127 10128 10129 10130 10131 10132 10133 10134 10135 10136 10137 10138 10139 10140 10141 10142 10143 10144 10145 10146 10147 10148 10149 10150 10151 10152 10153 10154 10155 10156 10157 10158 10159 10160 10161 10162 10163 10164 10165 10166 10167 10168 10169 10170 10171 10172 10173 10174 10175 10176 10177 10178 10179 10180 10181 10182 10183 10184 10185 10186 10187 10188 10189 10190 10191 10192 10193 10194 10195 10196 10197 10198 10199 10200 10201 10202 10203 10204 10205 10206 10207 10208 10209 10210 10211 10212 10213 10214 10215 10216 10217 10218 10219 10220 10221 10222 10223 10224 10225 10226 10227 10228 10229 10230 10231 10232 10233 10234 10235 10236 10237 10238 10239 10240 10241 10242 10243 10244 10245 10246 10247 10248 10249 10250 10251 10252 10253 10254 10255 10256 10257 10258 10259 10260 10261 10262 10263 10264 10265 10266 10267 10268 10269 10270 10271 10272 10273 10274 10275 10276 10277 10278 10279 10280 10281 10282 10283 10284 10285 10286 10287 10288 10289 10290 10291 10292 10293 10294 10295 10296 10297 10298 10299 10300 10301 10302 10303 10304 10305 10306 10307 10308 10309 10310 10311 10312 10313 10314 10315 10316 10317 10318 10319 10320 10321 10322 10323 10324 10325 10326 10327 10328 10329 10330 10331 10332 10333 10334 10335 10336 10337 10338 10339 10340 10341 10342 10343 10344 10345 10346 10347 10348 10349 10350 10351 10352 10353 10354 10355 10356 10357 10358 10359 10360 10361 10362 10363 10364 10365 10366 10367 10368 10369 10370 10371 10372 10373 10374 10375 10376 10377 10378 10379 10380 10381 10382 10383 10384 10385 10386 10387 10388 10389 10390 10391 10392 10393 10394 10395 10396 10397 10398 10399 10400 10401 10402 10403 10404 10405 10406 10407 10408 10409 10410 10411 10412 10413 10414 10415 10416 10417 10418 10419 10420 10421 10422 10423 10424 10425 10426 10427 10428 10429 10430 10431 10432 10433 10434 10435 10436 10437 10438 10439 10440 10441 10442 10443 10444 10445 10446 10447 10448 10449 10450 10451 10452 10453 10454 10455 10456 10457 10458 10459 10460 10461 10462 10463 10464 10465 10466 10467 10468 10469 10470 10471 10472 10473 10474 10475 10476 10477 10478 10479 10480 10481 10482 10483 10484 10485 10486 10487 10488 10489 10490 10491 10492 10493 10494 10495 10496 10497 10498 10499 10500 10501 10502 10503 10504 10505 10506 10507 10508 10509 10510 10511 10512 10513 10514 10515 10516 10517 10518 10519 10520 10521 10522 10523 10524 10525 10526 10527 10528 10529 10530 10531 10532 10533 10534 10535 10536 10537 10538 10539 10540 10541 10542 10543 10544 10545 10546 10547 10548 10549 10550 10551 10552 10553 10554 10555 10556 10557 10558 10559 10560 10561 10562 10563 10564 10565 10566 10567 10568 10569 10570 10571 10572 10573 10574 10575 10576 10577 10578 10579 10580 10581 10582 10583 10584 10585 10586 10587 10588 10589 10590 10591 10592 10593 10594 10595 10596 10597 10598 10599 10600 10601 10602 10603 10604 10605 10606 10607 10608 10609 10610 10611 10612 10613 10614 10615 10616 10617 10618 10619 10620 10621 10622 10623 10624 10625 10626 10627 10628 10629 10630 10631 10632 10633 10634 10635 10636 10637 10638 10639 10640 10641 10642 10643 10644 10645 10646 10647 10648 10649 10650 10651 10652 10653 10654 10655 10656 10657 10658 10659 10660 10661 10662 10663 10664 10665 10666 10667 10668 10669 10670 10671 10672 10673 10674 10675 10676 10677 10678 10679 10680 10681 10682 10683 10684 10685 10686 10687 10688 10689 10690 10691 10692 10693 10694 10695 10696 10697 10698 10699 10700 10701 10702 10703 10704 10705 10706 10707 10708 10709 10710 10711 10712 10713 10714 10715 10716 10717 10718 10719 10720 10721 10722 10723 10724 10725 10726 10727 10728 10729 10730 10731 10732 10733 10734 10735 10736 10737 10738 10739 10740 10741 10742 10743 10744 10745 10746 10747 10748 10749 10750 10751 10752 10753 10754 10755 10756 10757 10758 10759 10760 10761 10762 10763 10764 10765 10766 10767 10768 10769 10770 10771 10772 10773 10774 10775 10776 10777 10778 10779 10780 10781 10782 10783 10784 10785 10786 10787 10788 10789 10790 10791 10792 10793 10794 10795 10796 10797 10798 10799 10800 10801 10802 10803 10804 10805 10806 10807 10808 10809 10810 10811 10812 10813 10814 10815 10816 10817 10818 10819 10820 10821 10822 10823 10824 10825 10826 10827 10828 10829 10830 10831 10832 10833 10834 10835 10836 10837 10838 10839 10840 10841 10842 10843 10844 10845 10846 10847 10848 10849 10850 10851 10852 10853 10854 10855 10856 10857 10858 10859 10860 10861 10862 10863 10864 10865 10866 10867 10868 10869 10870 10871 10872 10873 10874 10875 10876 10877 10878 10879 10880 10881 10882 10883 10884 10885 10886 10887 10888 10889 10890 10891 10892 10893 10894 10895 10896 10897 10898 10899 10900 10901 10902 10903 10904 10905 10906 10907 10908 10909 10910 10911 10912 10913 10914 10915 10916 10917 10918 10919 10920 10921 10922 10923 10924 10925 10926 10927 10928 10929 10930 10931 10932 10933 10934 10935 10936 10937 10938 10939 10940 10941 10942 10943 10944 10945 10946 10947 10948 10949 10950 10951 10952 10953 10954 10955 10956 10957 10958 10959 10960 10961 10962 10963 10964 10965 10966 10967 10968 10969 10970 10971 10972 10973 10974 10975 10976 10977 10978 10979 10980 10981 10982 10983 10984 10985 10986 10987 10988 10989 10990 10991 10992 10993 10994 10995 10996 10997 10998 10999 11000 11001 11002 11003 11004 11005 11006 11007 11008 11009 11010 11011 11012 11013 11014 11015 11016 11017 11018 11019 11020 11021 11022 11023 11024 11025 11026 11027 11028 11029 11030 11031 11032 11033 11034 11035 11036 11037 11038 11039 11040 11041 11042 11043 11044 11045 11046 11047 11048 11049 11050 11051 11052 11053 11054 11055 11056 11057 11058 11059 11060 11061 11062 11063 11064 11065 11066 11067 11068 11069 11070 11071 11072 11073 11074 11075 11076 11077 11078 11079 11080 11081 11082 11083 11084 11085 11086 11087 11088 11089 11090 11091 11092 11093 11094 11095 11096 11097 11098 11099 11100 11101 11102 11103 11104 11105 11106 11107 11108 11109 11110 11111 11112 11113 11114 11115 11116 11117 11118 11119 11120 11121 11122 11123 11124 11125 11126 11127 11128 11129 11130 11131 11132 11133 11134 11135 11136 11137 11138 11139 11140 11141 11142 11143 11144 11145 11146 11147 11148 11149 11150 11151 11152 11153 11154 11155 11156 11157 11158 11159 11160 11161 11162 11163 11164 11165 11166 11167 11168 11169 11170 11171 11172 11173 11174 11175 11176 11177 11178 11179 11180 11181 11182 11183 11184 11185 11186 11187 11188 11189 11190 11191 11192 11193 11194 11195 11196 11197 11198 11199 11200 11201 11202 11203 11204 11205 11206 11207 11208 11209 11210 11211 11212 11213 11214 11215 11216 11217 11218 11219 11220 11221 11222 11223 11224 11225 11226 11227 11228 11229 11230 11231 11232 11233 11234 11235 11236 11237 11238 11239 11240 11241 11242 11243 11244 11245 11246 11247 11248 11249 11250 11251 11252 11253 11254 11255 11256 11257 11258 11259 11260 11261 11262 11263 11264 11265 11266 11267 11268 11269 11270 11271 11272 11273 11274 11275 11276 11277 11278 11279 11280 11281 11282 11283 11284 11285 11286 11287 11288 11289 11290 11291 11292 11293 11294 11295 11296 11297 11298 11299 11300 11301 11302 11303 11304 11305 11306 11307 11308 11309 11310 11311 11312 11313 11314 11315 11316 11317 11318 11319 11320 11321 11322 11323 11324 11325 11326 11327 11328 11329 11330 11331 11332 11333 11334 11335 11336 11337 11338 11339 11340 11341 11342 11343 11344 11345 11346 11347 11348 11349 11350 11351 11352 11353 11354 11355 11356 11357 11358 11359 11360 11361 11362 11363 11364 11365 11366 11367 11368 11369 11370 11371 11372 11373 11374 11375 11376 11377 11378 11379 11380 11381 11382 11383 11384 11385 11386 11387 11388 11389 11390 11391 11392 11393 11394 11395 11396 11397 11398 11399 11400 11401 11402 11403 11404 11405 11406 11407 11408 11409 11410 11411 11412 11413 11414 11415 11416 11417 11418 11419 11420 11421 11422 11423 11424 11425 11426 11427 11428 11429 11430 11431 11432 11433 11434 11435 11436 11437 11438 11439 11440 11441 11442 11443 11444 11445 11446 11447 11448 11449 11450 11451 11452 11453 11454 11455 11456 11457 11458 11459 11460 11461 11462 11463 11464 11465 11466 11467 11468 11469 11470 11471 11472 11473 11474 11475 11476 11477 11478 11479 11480 11481 11482 11483 11484 11485 11486 11487 11488 11489 11490 11491 11492 11493 11494 11495 11496 11497 11498 11499 11500 11501 11502 11503 11504 11505 11506 11507 11508 11509 11510 11511 11512 11513 11514 11515 11516 11517 11518 11519 11520 11521 11522 11523 11524 11525 11526 11527 11528 11529 11530 11531 11532 11533 11534 11535 11536 11537 11538 11539 11540 11541 11542 11543 11544 11545 11546 11547 11548 11549 11550 11551 11552 11553 11554 11555 11556 11557 11558 11559 11560 11561 11562 11563 11564 11565 11566 11567 11568 11569 11570 11571 11572 11573 11574 11575 11576 11577 11578 11579 11580 11581 11582 11583 11584 11585 11586 11587 11588 11589 11590 11591 11592 11593 11594 11595 11596 11597 11598 11599 11600 11601 11602 11603 11604 11605 11606 11607 11608 11609 11610 11611 11612 11613 11614 11615 11616 11617 11618 11619 11620 11621 11622 11623 11624 11625 11626 11627 11628 11629 11630 11631 11632 11633 11634 11635 11636 11637 11638 11639 11640 11641 11642 11643 11644 11645 11646 11647 11648 11649 11650 11651 11652 11653 11654 11655 11656 11657 11658 11659 11660 11661 11662 11663 11664 11665 11666 11667 11668 11669 11670 11671 11672 11673 11674 11675 11676 11677 11678 11679 11680 11681 11682 11683 11684 11685 11686 11687 11688 11689 11690 11691 11692 11693 11694 11695 11696 11697 11698 11699 11700 11701 11702 11703 11704 11705 11706 11707 11708 11709 11710 11711 11712 11713 11714 11715 11716 11717 11718 11719 11720 11721 11722 11723 11724 11725 11726 11727 11728 11729 11730 11731 11732 11733 11734 11735 11736 11737 11738 11739 11740 11741 11742 11743 11744 11745 11746 11747 11748 11749 11750 11751 11752 11753 11754 11755 11756 11757 11758 11759 11760 11761 11762 11763 11764 11765 11766 11767 11768 11769 11770 11771 11772 11773 11774 11775 11776 11777 11778 11779 11780 11781 11782 11783 11784 11785 11786 11787 11788 11789 11790 11791 11792 11793 11794 11795 11796 11797 11798 11799 11800 11801 11802 11803 11804 11805 11806 11807 11808 11809 11810 11811 11812 11813 11814 11815 11816 11817 11818 11819 11820 11821 11822 11823 11824 11825 11826 11827 11828 11829 11830 11831 11832 11833 11834 11835 11836 11837 11838 11839 11840 11841 11842 11843 11844 11845 11846 11847 11848 11849 11850 11851 11852 11853 11854 11855 11856 11857 11858 11859 11860 11861 11862 11863 11864 11865 11866 11867 11868 11869 11870 11871 11872 11873 11874 11875 11876 11877 11878 11879 11880 11881 11882 11883 11884 11885 11886 11887 11888 11889 11890 11891 11892 11893 11894 11895 11896 11897 11898 11899 11900 11901 11902 11903 11904 11905 11906 11907 11908 11909 11910 11911 11912 11913 11914 11915 11916 11917 11918 11919 11920 11921 11922 11923 11924 11925 11926 11927 11928 11929 11930 11931 11932 11933 11934 11935 11936 11937 11938 11939 11940 11941 11942 11943 11944 11945 11946 11947 11948 11949 11950 11951 11952 11953 11954 11955 11956 11957 11958 11959 11960 11961 11962 11963 11964 11965 11966 11967 11968 11969 11970 11971 11972 11973 11974 11975 11976 11977 11978 11979 11980 11981 11982 11983 11984 11985 11986 11987 11988 11989 11990 11991 11992 11993 11994 11995 11996 11997 11998 11999 12000 12001 12002 12003 12004 12005 12006 12007 12008 12009 12010 12011 12012 12013 12014 12015 12016 12017 12018 12019 12020 12021 12022 12023 12024 12025 12026 12027 12028 12029 12030 12031 12032 12033 12034 12035 12036 12037 12038 12039 12040 12041 12042 12043 12044 12045 12046 12047 12048 12049 12050 12051 12052 12053 12054 12055 12056 12057 12058 12059 12060 12061 12062 12063 12064 12065 12066 12067 12068 12069 12070 12071 12072 12073 12074 12075 12076 12077 12078 12079 12080 12081 12082 12083 12084 12085 12086 12087 12088 12089 12090 12091 12092 12093 12094 12095 12096 12097 12098 12099 12100 12101 12102 12103 12104 12105 12106 12107 12108 12109 12110 12111 12112 12113 12114 12115 12116 12117 12118 12119 12120 12121 12122 12123 12124 12125 12126 12127 12128 12129 12130 12131 12132 12133 12134 12135 12136 12137 12138 12139 12140 12141 12142 12143 12144 12145 12146 12147 12148 12149 12150 12151 12152 12153 12154 12155 12156 12157 12158 12159 12160 12161 12162 12163 12164 12165 12166 12167 12168 12169 12170 12171 12172 12173 12174 12175 12176 12177 12178 12179 12180 12181 12182 12183 12184 12185 12186 12187 12188 12189 12190 12191 12192 12193 12194 12195 12196 12197 12198 12199 12200 12201 12202 12203 12204 12205 12206 12207 12208 12209 12210 12211 12212 12213 12214 12215 12216 12217 12218 12219 12220 12221 12222 12223 12224 12225 12226 12227 12228 12229 12230 12231 12232 12233 12234 12235 12236 12237 12238 12239 12240 12241 12242 12243 12244 12245 12246 12247 12248 12249 12250 12251 12252 12253 12254 12255 12256 12257 12258 12259 12260 12261 12262 12263 12264 12265 12266 12267 12268 12269 12270 12271 12272 12273 12274 12275 12276 12277 12278 12279 12280 12281 12282 12283 12284 12285 12286 12287 12288 12289 12290 12291 12292 12293 12294 12295 12296 12297 12298 12299 12300 12301 12302 12303 12304 12305 12306 12307 12308 12309 12310 12311 12312 12313 12314 12315 12316
// Copyright (c) 2020-2022 Weird Constructor <weirdconstructor@gmail.com>
// This is a part of WLambda. See README.md and COPYING for details.
/*!
This module defines some default functions and operations
available in the WLambda language.
You there are two WLambda modules provided by this module:
- [core_symbol_table()](fn.core_symbol_table.html)
- [std_symbol_table()](fn.std_symbol_table.html)
[]: ---- REFERENCE DOC START ----
# WLambda Language Reference
WLambda is a functional programming language. The main goal of this
implementation is the extension of Rust applications with dynamic scripting.
The syntax gravitates around the concept that everything is callable like a
function. There is special syntax for composing arguments of functions, to give
the programmer the ability to express his thoughts as they see fit.
You can use this document as reference or as cover to cover lecture. It starts
out with functions and the base data types of WLambda, where I also explain
some semantics of the language.
Please note: I expect you to know how to program and be familiar with at least
one other dynamic language like _JavaScript_, _Perl_ or at least _Python_. The
syntax and semantics of WLambda are different from what you might know. Think
of it more like a LISP without parenthesis. The syntax is loosely inspired from
Smalltalk, LISP and Perl.
-----
**Table Of Contents:**
- [1](#1-variable-definition-and-assignment) Variable Definition and Assignment
- [1.1](#11-destructuring-to-variables) Destructuring to Variables
- [1.2](#12-global-variables) Global Variables
- [1.3](#13-constants) Constants
- [2](#2-functions-part-12) Functions (part 1/2)
- [2.1](#21-closures) Closures
- [2.1.1](#211-object-oriented-programming-with-closures) Object Oriented Programming with Closures
- [2.2](#22-function-calling) Function calling
- [2.3](#23-function-arity-checks) Function arity checks
- [2.3.1](#231-stdtonoarity-function) std:to\_no\_arity _function_
- [2.4](#24-calling-fields--method-calling) Calling fields / Method calling
- [2.4.1](#241-the-self-and-data-special-variables) The $self and $data special variables
- [2.4.2](#242-object-oriented-programming-with-prototypes) Object Oriented Programming with Prototypes
- [2.4.3](#243-object-oriented-with-prototypes-and-inheritance) Object Oriented with Prototypes and Inheritance
- [2.4.4](#244-object-oriented-with-prototypes-and-self-references-and-data-references) Object Oriented with Prototypes and $self References and $data References
- [2.5](#25-function-call-composition) Function call composition
- [2.5.1](#251--tail-argument-function-chaninig) '|' Tail Argument Function Chaninig
- [2.5.2](#252--left-hand-function-chaining) '|>' Left Hand Function Chaining
- [2.5.3](#253-forward-argument-pipe-arg--fun) Forward Argument Pipe `arg &> fun`
- [2.5.4](#254-forward-argument-apply-pipe-list--fun) Forward Argument Apply Pipe `list &@> fun`
- [2.5.5](#255-reverse-argument-pipe-fun--arg) Reverse Argument Pipe `fun <& arg`
- [2.5.6](#256-reverse-argument-apply-pipe-list--fun) Reverse Argument Apply Pipe `list &@> fun`
- [2.6](#26-control-flow---returning) Control Flow - Returning
- [2.6.1](#261-return-label-value) return [_label_] _value_
- [2.6.2](#262-block-label-function) block [label] _function_
- [2.6.3](#263-stdtodrop-function-or-raii-destructors-or-drop-functions) std:to\_drop _function_ (or RAII, Destructors or Drop Functions)
- [2.6.4](#264-stdtimenow-unit) std:time:now [_unit_]
- [2.6.5](#265-stdsrand-seed) std:srand [_seed_]
- [2.6.6](#266-stdrand-max-or-mode) std:rand [_max-or-mode_]
- [2.7](#27-function-utilities) Function utilities
- [2.7.1](#271-isfun-value) is\_fun _value_
- [3](#3-data-types) Data Types
- [3.1](#31-none-sentinel-value-n-or-none) None sentinel value: `$n` or `$none`
- [3.1.1](#311-isnone-value) is\_none _value_
- [3.1.2](#312-issome-value) is\_some _value_
- [3.2](#32-optional-values-o-and-o) Optional values `$o()` and `$o(...)`
- [3.2.1](#321-isoptional-value) is\_optional _value_
- [3.2.2](#322-unwrapping-optionals) Unwrapping optionals
- [3.3](#33-error-values-e-expr-or-error-expr) Error values: `$e expr` or `$error expr`
- [3.3.1](#331--label-value) _? [_label_] _value_
- [3.3.2](#332-unwrap-value) unwrap _value_
- [3.3.3](#333-unwraperr-error-value) unwrap\_err _error-value_
- [3.3.4](#334-onerror-handler-maybe-error-value) on\_error _handler_ _maybe-error-value_
- [3.3.5](#335-iserr-value) is\_err _value_
- [3.3.6](#336-stderrortostr-value) std:error\_to\_str _value_
- [3.4](#34-booleans) Booleans
- [3.4.1](#341-isbool-any-value) is\_bool _any-value_
- [3.4.2](#342-bool-any-value) bool _any-value_
- [3.4.3](#343-not-value) not _value_
- [3.4.4](#344-boolean-list-indexing) Boolean List Indexing
- [3.5](#35-64-bit-integers) 64-Bit Integers
- [3.5.1](#351-int-value) int _value_
- [3.5.2](#352-isint-value) is\_int _value_
- [3.5.3](#353-stdnegi64-integer) std:neg\_i64 _integer_
- [3.5.4](#354-stdnoti64-integer) std:not\_i64 _integer_
- [3.5.5](#355-stdnegu32-integer) std:neg\_u32 _integer_
- [3.5.6](#356-stdnotu32-integer) std:not\_u32 _integer_
- [3.6](#36-64-bit-floats) 64-Bit Floats
- [3.6.1](#361-float-value) float _value_
- [3.6.2](#362-isfloat-value) is\_float _value_
- [3.6.3](#363-stdnumacos-float) std:num:acos _float_
- [3.6.4](#364-stdnumacosh-float) std:num:acosh _float_
- [3.6.5](#365-stdnumasin-float) std:num:asin _float_
- [3.6.6](#366-stdnumasinh-float) std:num:asinh _float_
- [3.6.7](#367-stdnumatan-float) std:num:atan _float_
- [3.6.8](#368-stdnumatan2-y-x) std:num:atan2 _y_ _x_
- [3.6.9](#369-stdnumatanh-float) std:num:atanh _float_
- [3.6.10](#3610-stdnumcbrt-float) std:num:cbrt _float_
- [3.6.11](#3611-stdnumceil-float) std:num:ceil _float_
- [3.6.12](#3612-stdnumcos-float) std:num:cos _float_
- [3.6.13](#3613-stdnumcosh-float) std:num:cosh _float_
- [3.6.14](#3614-stdnumexp-float) std:num:exp _float_
- [3.6.15](#3615-stdnumexp2-float) std:num:exp2 _float_
- [3.6.16](#3616-stdnumexpm1-float) std:num:exp\_m1 _float_
- [3.6.17](#3617-stdnumfloor-float) std:num:floor _float_
- [3.6.18](#3618-stdnumhypot-y-x) std:num:hypot _y_ _x_
- [3.6.19](#3619-stdnumln-float) std:num:ln _float_
- [3.6.20](#3620-stdnumlog-float) std:num:log _float_
- [3.6.21](#3621-stdnumlog10-float) std:num:log10 _float_
- [3.6.22](#3622-stdnumlog2-float) std:num:log2 _float_
- [3.6.23](#3623-stdnumpow-float) std:num:pow _float_
- [3.6.24](#3624-stdnumrecip-float) std:num:recip _float_
- [3.6.25](#3625-stdnumround-float) std:num:round _float_
- [3.6.26](#3626-stdnumsin-float) std:num:sin _float_
- [3.6.27](#3627-stdnumsinh-float) std:num:sinh _float_
- [3.6.28](#3628-stdnumsqrt-float) std:num:sqrt _float_
- [3.6.29](#3629-stdnumtan-float) std:num:tan _float_
- [3.6.30](#3630-stdnumtanh-float) std:num:tanh _float_
- [3.6.31](#3631-stdnumtodegrees-float) std:num:to\_degrees _float_
- [3.6.32](#3632-stdnumtoradians-float) std:num:to\_radians _float_
- [3.6.33](#3633-stdnumtrunc-float) std:num:trunc _float_
- [3.6.34](#3634-stdnumlerp-a-b-x) std:num:lerp _a_ _b_ _x_
- [3.6.35](#3635-stdnumsmoothstep-a-b-x) std:num:smoothstep _a_ _b_ _x_
- [3.6.36](#3636-stdnumfract-float) std:num:fract _float_
- [3.7](#37-numeric-functions) Numeric Functions
- [3.7.1](#371-stdnumabs-number) std:num:abs _number_
- [3.7.2](#372-stdnumsignum-number) std:num:signum _number_
- [3.7.3](#373-stdnuminttoclosedopen01-integer) std:num:int\_to\_closed\_open01 _integer_
- [3.7.4](#374-stdnuminttoopen01-integer) std:num:int\_to\_open01 _integer_
- [3.7.5](#375-stdnuminttoopenclosed01-integer) std:num:int\_to\_open\_closed01 _integer_
- [3.8](#38-numerical-mathematical-vectors) Numerical Mathematical Vectors
- [3.8.1](#381-vector-conversions) Vector Conversions
- [3.8.2](#382-vector-component-access) Vector Component Access
- [3.8.3](#383-named-field-access-and-swizzling) Named Field Access and Swizzling
- [3.8.4](#384-euler-additionsubtraction) Euler Addition/Subtraction
- [3.8.5](#385-scalar-multiplicationdivision) Scalar Multiplication/Division
- [3.8.6](#386-unary-vector-operations) Unary Vector Operations
- [3.8.7](#387-isfvec-value) is\_fvec _value_
- [3.8.8](#388-isivec-value) is\_ivec _value_
- [3.8.9](#389-isnvec-value) is\_nvec _value_
- [3.8.10](#3810-nveclen-value) nvec\_len _value_
- [3.8.11](#3811-fvec-value) fvec _value_
- [3.8.12](#3812-fvec2-value) fvec2 _value_
- [3.8.13](#3813-fvec3-value) fvec3 _value_
- [3.8.14](#3814-fvec4-value) fvec4 _value_
- [3.8.15](#3815-ivec-value) ivec _value_
- [3.8.16](#3816-ivec2-value) ivec2 _value_
- [3.8.17](#3817-ivec3-value) ivec3 _value_
- [3.8.18](#3818-ivec4-value) ivec4 _value_
- [3.8.19](#3819-stdvdims-vec) std:v:dims _vec_
- [3.8.20](#3820-stdvmag2-vec) std:v:mag2 _vec_
- [3.8.21](#3821-stdvmag-vec) std:v:mag _vec_
- [3.8.22](#3822-stdvnorm-vec) std:v:norm _vec_
- [3.8.23](#3823-stdvdot-vec1-vec2) std:v:dot _vec1_ _vec2_
- [3.8.24](#3824-stdvcross-vec1-vec2) std:v:cross _vec1_ _vec2_
- [3.8.25](#3825-stdvlerp-vec1-vec2-t) std:v:lerp _vec1_ _vec2_ _t_
- [3.8.26](#3826-stdvslerp-vec1-vec2-t) std:v:slerp _vec1_ _vec2_ _t_
- [3.8.27](#3827-stdvvec2rad-vec) std:v:vec2rad _vec_
- [3.8.28](#3828-stdvrad2vec-radians) std:v:rad2vec _radians_
- [3.9](#39-characters-and-bytes) Characters and Bytes
- [3.9.1](#391-byte-value) byte _value_
- [3.9.2](#392-char-value) char _value_
- [3.9.3](#393-isbyte-value) is\_byte _value_
- [3.9.4](#394-ischar-value) is\_char _value_
- [3.9.5](#395-stdchartolowercase-value) std:char:to\_lowercase _value_
- [3.9.6](#396-stdchartouppercase-value) std:char:to\_uppercase _value_
- [3.10](#310-strings) Strings
- [3.10.1](#3101-string-literal-syntaxes) String Literal Syntaxes
- [3.10.2](#3102-str-value) str _value_
- [3.10.3](#3103-stdwritestr-value) std:write\_str _value_
- [3.10.4](#3104-isstr-value) is\_str _value_
- [3.10.5](#3105-stdstrcat-a-b-) std:str:cat _a_ _b_ ...
- [3.10.6](#3106-stdstrjoin-sep-vector) std:str:join _sep_ _vector_
- [3.10.7](#3107-stdstrlen-value) std:str:len _value_
- [3.10.8](#3108-stdstrfind-pattern-string-offset) std:str:find _pattern_ _string_ [_offset_]
- [3.10.9](#3109-stdstrreplace-pattern-replacement-string) std:str:replace _pattern_ _replacement_ _string_
- [3.10.10](#31010-stdstrreplacen-pattern-replacement-count-string) std:str:replace\_n _pattern_ _replacement_ _count_ _string_
- [3.10.11](#31011-stdstrtrim-value) std:str:trim _value_
- [3.10.12](#31012-stdstrtrimstart-value) std:str:trim\_start _value_
- [3.10.13](#31013-stdstrtrimend-value) std:str:trim\_end _value_
- [3.10.14](#31014-stdstrpadstart-len-pad-str-value) std:str:pad\_start _len_ _pad-str_ _value_
- [3.10.15](#31015-stdstrpadend-len-pad-str-value) std:str:pad\_end _len_ _pad-str_ _value_
- [3.10.16](#31016-stdstrtobytes-string) std:str:to\_bytes _string_
- [3.10.17](#31017-stdstrtobyteslatin1-string) std:str:to\_bytes\_latin1 _string_
- [3.10.18](#31018-stdstrfromlatin1-byte-vector) std:str:from\_latin1 _byte-vector_
- [3.10.19](#31019-stdstrfromutf8-byte-vector) std:str:from\_utf8 _byte-vector_
- [3.10.20](#31020-stdstrfromutf8lossy-byte-vector) std:str:from\_utf8\_lossy _byte-vector_
- [3.10.21](#31021-stdstrtocharvec-string) std:str:to\_char\_vec _string_
- [3.10.22](#31022-stdstrfromcharvec-vector) std:str:from\_char\_vec _vector_
- [3.10.23](#31023-stdstrtolowercase-string) std:str:to\_lowercase _string_
- [3.10.24](#31024-stdstrtouppercase-string) std:str:to\_uppercase _string_
- [3.10.25](#31025-stdstreditdistance-str-a-strb) std:str:edit\_distance _str-a_ _str\_b
- [3.11](#311-byte-vectors) Byte Vectors
- [3.11.1](#3111-call-properties-of-bytes) Call Properties of Bytes
- [3.11.2](#3112-byte-conversion-functions) Byte Conversion Functions
- [3.11.3](#3113-isbytes-value) is\_bytes _value_
- [3.11.4](#3114-stdbytesfind-pattern-string-offset) std:bytes:find _pattern_ _string_ [_offset_]
- [3.11.5](#3115-stdbytesreplace-byte-vector-pattern-replacement) std:bytes:replace _byte-vector_ _pattern_ _replacement_
- [3.11.6](#3116-stdbytesfromhex-string-with-hex-chars) std:bytes:from\_hex _string-with-hex-chars_
- [3.11.7](#3117-stdbytesfromvec-vector-of-ints) std:bytes:from\_vec _vector-of-ints_
- [3.11.8](#3118-stdbytestohex-byte-vector) std:bytes:to\_hex _byte-vector_
- [3.11.9](#3119-stdbytestobase64-byte-vector-config) std:bytes:to\_base64 _byte-vector_ [_config_]
- [3.11.10](#31110-stdbytesfrombase64-byte-vector-config) std:bytes:from\_base64 _byte-vector_ [_config_]
- [3.11.11](#31111-stdbytestovec-byte-vector) std:bytes:to\_vec _byte-vector_
- [3.11.12](#31112-stdbytespack-pack-format-string-list-of-values) std:bytes:pack _pack-format-string_ _list-of-values_
- [3.11.13](#31113-stdbytesunpack-pack-format-string-byte-vector) std:bytes:unpack _pack-format-string_ _byte-vector_
- [3.12](#312-symbols) Symbols
- [3.12.1](#3121-sym-value) sym _value_
- [3.12.2](#3122-issym-value) is\_sym _value_
- [3.12.3](#3123-stdsymbolscollect) std:symbols:collect
- [3.13](#313-syntax-block) Syntax `$%:Block`
- [3.13.1](#3131-stdsynpos-syntax) std:syn:pos _syntax_
- [3.13.2](#3132-stdsyntype-syntax) std:syn:type _syntax_
- [3.14](#314-pairs-pa-b) Pairs `$p(a, b)`
- [3.14.1](#3141-pair-operator-a--b) Pair Operator `a => b`
- [3.14.2](#3142-cons-a-b) cons _a_ _b_
- [3.14.3](#3143-pair-stringbyte-vector-operations) Pair string/byte vector operations
- [3.14.3.1](#31431-p-from--count--string-or-byte-vec) $p( _from_ , _count_ ) _string-or-byte-vec_
- [3.14.3.2](#31432-p-pattern--replacement--string-or-byte-vec) $p( _pattern_ , _replacement_ ) _string-or-byte-vec_
- [3.14.3.3](#31433-p-split-pattern--max--string-or-byte-vec) $p( _split-pattern_ , _max_ ) _string-or-byte-vec_
- [3.14.4](#3144-pair-to-iterator) Pair to Iterator
- [3.14.4.1](#31441-iter---range) Iter - Range
- [3.14.4.2](#31442-iter---enumerate) Iter - Enumerate
- [3.14.4.3](#31443-iter---values) Iter - Values
- [3.14.4.4](#31444-iter---keys) Iter - Keys
- [3.14.5](#3145-ispair-value) is\_pair _value_
- [3.15](#315-vectors-or-lists) Vectors (or Lists)
- [3.15.1](#3151-stdpush-vector-item) std:push _vector_ _item_
- [3.15.2](#3152-stdpop-vector) std:pop _vector_
- [3.15.3](#3153-stdunshift-vector-item) std:unshift _vector_ _item_
- [3.15.4](#3154-isvec-value) is\_vec _value_
- [3.15.5](#3155-vector-splicing) Vector Splicing
- [3.15.6](#3156-stdappend-vec-a-value-or-vec-) std:append _vec-a_ _value-or-vec_ ...
- [3.15.7](#3157-stdprepend-vec-a-value-or-vec-) std:prepend _vec-a_ _value-or-vec_ ...
- [3.15.8](#3158-stdtake-count-vector) std:take _count_ _vector_
- [3.15.9](#3159-stddrop-count-vector) std:drop _count_ _vector_
- [3.16](#316-associative-maps-or-string-to-value-mappings) Associative Maps (or String to Value mappings)
- [3.16.1](#3161-map-splicing) Map Splicing
- [3.16.2](#3162-ismap-value) is\_map _value_
- [3.17](#317-references) References
- [3.17.1](#3171-stdtoref-value) std:to\_ref _value_
- [3.17.2](#3172-stdrefweaken-ref) std:ref:weaken _ref_
- [3.17.3](#3173-stdrefhide-value) std:ref:hide _value_
- [3.17.4](#3174-isref-value) is\_ref _value_
- [3.17.5](#3175-iswref-value) is\_wref _value_
- [3.17.6](#3176-stdrefstrengthen-ref) std:ref:strengthen _ref_
- [3.17.7](#3177-stdrefset-ref-value) std:ref:set _ref_ _value_
- [3.18](#318-iterators-iter-expression) Iterators $iter _expression_
- [3.18.1](#3181-iterator-kinds) Iterator Kinds
- [3.18.2](#3182-iterators-on-mutated-data) Iterators on mutated data
- [3.18.3](#3183-splicing-an-iterator) Splicing an Iterator
- [3.18.4](#3184-calling-an-iterator-with-a-function) Calling an Iterator with a Function
- [3.18.5](#3185-zip-iterators) Zip Iterators
- [3.18.6](#3186-isiter-value) is\_iter _value_
- [3.19](#319-calling-semantics-of-data-types) Calling Semantics of Data Types
- [4](#4-conditional-execution---if--then--else) Conditional Execution - if / then / else
- [4.1](#41-if-condition-then-expr-else-expr) if/? _condition_ _then-expr_ [_else-expr_]
- [4.2](#42-using-booleans-for-conditional-execution) Using Booleans for Conditional Execution
- [4.2.1](#421-pick-bool-a--b-) pick _bool_ _a_ -b-
- [4.2.2](#422-indexing-by-booleans) Indexing by Booleans
- [4.3](#43-value-matching-with---match-value-expr-) Value matching with - match _value-expr_ ...
- [5](#5-loops-and-iteration) Loops And Iteration
- [5.1](#51-control-flow) Control Flow
- [5.1.1](#511-while-predicate-body) while _predicate_ _body_
- [5.1.2](#512-iter-var-iterable-body) iter _var_ _iterable_ _body_
- [5.1.2.1](#5121-counting-loop-with-iter) Counting loop with _iter_
- [5.1.2.2](#5122-vector-iteration-with-iter) Vector iteration with _iter_
- [5.1.2.3](#5123-map-iteration-with-iter) Map iteration with _iter_
- [5.1.2.4](#5124-closures-and-iter-iter-i-) Closures and _iter_ `iter i ...`
- [5.1.3](#513-range-start-end-step-fun) range _start_ _end_ _step_ _fun_
- [5.1.4](#514-break-value) break _value_
- [5.1.5](#515-next) next
- [5.1.6](#516-jump-index-val-branch1--last-branch) jump _index-val_ _branch1_ ... _last-branch_
- [5.2](#52-collection-iteration) Collection Iteration
- [5.2.1](#521-iteration-over-vectors) Iteration over vectors
- [5.2.2](#522-iteration-over-maps) Iteration over maps
- [5.2.3](#523-for-iteratable-value-function) for _iteratable-value_ _function_
- [5.2.4](#524-map-function-iterable) map _function_ _iterable_
- [5.2.5](#525-filter-function-iterable) filter _function_ _iterable_
- [5.3](#53-accumulation-and-collection) Accumulation and Collection
- [5.3.1](#531-transforming-a-vector) Transforming a vector
- [5.3.2](#532-example-of-) Example of `$@@`
- [5.3.3](#533-transforming-a-vector-to-a-map) Transforming a vector to a map
- [5.3.4](#534-iteratively-concatenating-strings) Iteratively concatenating strings
- [5.3.5](#535-accumulating-sums) Accumulating sums
- [5.4](#54-utilities) Utilities
- [5.4.1](#541-stdaccum-collection-a-b-) std:accum _collection_ _a_ _b_ ...
- [5.4.2](#542-stdzip-vector-map-fn) std:zip _vector_ _map-fn_
- [5.4.3](#543-stdfold-accumulator-func-iteratable) std:fold _accumulator_ _func_ _iteratable_
- [5.4.4](#544-stdenumerate-map-fn) std:enumerate _map-fn_
- [6](#6-operators) Operators
- [6.1](#61-operator-assignment) Operator Assignment
- [6.2](#62-arithmetic) Arithmetic
- [6.2.1](#621--operand-1-operand-2-) + _operand-1_ _operand-2_ ...
- [6.2.2](#622---operand-1-operand-2-) - _operand-1_ _operand-2_ ...
- [6.2.3](#623--op-a-op-b) * _op-a_ _op-b_
- [6.2.4](#624--op-a-op-b) / _op-a_ _op-b_
- [6.2.5](#625--op-a-op-b) % _op-a_ _op-b_
- [6.2.6](#626--op-a-op-b) ^ _op-a_ _op-b_
- [6.3](#63-comparison) Comparison
- [6.3.1](#631--op-a-op-b) == _op-a_ _op-b_
- [6.3.2](#632--op-a-op-b) != _op-a_ _op-b_
- [6.3.3](#633--op-a-op-b) < _op-a_ _op-b_
- [6.3.4](#634--op-a-op-b) <= _op-a_ _op-b_
- [6.3.5](#635--op-a-op-b) > _op-a_ _op-b_
- [6.3.6](#636--op-a-op-b) >= _op-a_ _op-b_
- [6.4](#64-bit-operations) Bit Operations
- [6.4.1](#641--op-a-op-b) & _op-a_ _op-b_
- [6.4.2](#642--op-a-op-b) &^ _op-a_ _op-b_
- [6.4.3](#643--op-a-op-b) &| _op-a_ _op-b_
- [6.4.4](#644--op-a-op-b) << _op-a_ _op-b_
- [6.4.5](#645--op-a-op-b) >> _op-a_ _op-b_
- [6.5](#65-collection-addition-operators--and-) Collection Addition Operators +> and <+
- [6.5.1](#651--collection-a-) +> _collection_ _a_ ...
- [6.5.2](#652--collection-a-) <+ _collection_ _a_ ...
- [7](#7-string-and-byte-vector-formatting) String and Byte Vector Formatting
- [7.0.1](#701-stdformatter-format-string) std:formatter _format-string_
- [7.1](#71-formatting-numbers) Formatting Numbers
- [8](#8-data-structure-matchers-selectors-and-string-patternsregex) Data Structure Matchers, Selectors and String Patterns/Regex
- [8.1](#81-data-structure-matcher) Data Structure Matcher
- [8.1.1](#811-match-value-expr-match-pair1--default-expr) match _value-expr_ _match-pair1_ ... [_default-expr_]
- [8.1.2](#812-m-expr) $M _expr_
- [8.1.3](#813-data-structure-matcher-syntax) Data Structure Matcher Syntax
- [8.2](#82-data-structure-selectors-s) Data Structure Selectors `$S(...)`
- [8.2.1](#821-selector-and-wlambda-regex-syntax) Selector and WLambda Regex Syntax:
- [8.2.2](#822-stdselector-string) std:selector _string_
- [8.3](#83-string-patterns-regex-r) String Patterns (Regex) `$r/.../`
- [8.3.1](#831-global-patterns-rg) Global Patterns `$rg/.../`
- [8.3.2](#832-pattern-substitutions-rs) Pattern Substitutions `$rs/.../`
- [8.3.3](#833-pattern-syntax-overview) Pattern Syntax Overview
- [8.3.4](#834-standard-regular-expressions) Standard Regular Expressions
- [8.3.5](#835-stdpattern-string-mode) std:pattern _string_ [_mode_]
- [9](#9-modules) Modules
- [9.1](#91-export) export
- [9.2](#92-import) import
- [10](#10-core-library) Core Library
- [10.0.1](#1001-type-value) type _value_
- [10.0.2](#1002-len-value) len _value_
- [10.0.3](#1003-panic-message) panic _message_
- [11](#11-standard-library) Standard Library
- [11.0.1](#1101-stdshuffle-randfunc-vec) std:shuffle _rand\_func_ _vec_
- [11.0.2](#1102-stddelete-vector-or-map-index-or-key) std:delete _vector-or-map_ _index-or-key_
- [11.0.3](#1103-stdrefid-value) std:ref\_id _value_
- [11.0.4](#1104-stdcopy-vecormap) std:copy _vec\_or\_map_
- [11.0.5](#1105-stdvalues-collection-or-iter) std:values _collection-or-iter_
- [11.0.6](#1106-stdkeys-collection-or-iter) std:keys _collection-or-iter_
- [11.0.7](#1107-stdsort-comparefun-vec) std:sort [_compare\_fun_] _vec_
- [11.0.8](#1108-stdcmpnumasc-a-b) std:cmp:num:asc _a_ _b_
- [11.0.9](#1109-stdcmpnumdesc-a-b) std:cmp:num:desc _a_ _b_
- [11.0.10](#11010-stdcmpstrasc-a-b) std:cmp:str:asc _a_ _b_
- [11.0.11](#11011-stdcmpstrdesc-a-b) std:cmp:str:desc _a_ _b_
- [11.0.12](#11012-stdreverse-value) std:reverse _value_
- [11.0.13](#11013-stddisplayln-arg1-) std:displayln _arg1_ ...
- [11.0.14](#11014-debug-arg1-) $DEBUG _arg1_ ...
- [11.0.15](#11015-stdwriteln-arg1-) std:writeln _arg1_ ...
- [11.0.16](#11016-stdeval-code-string) std:eval _code-string_
- [11.0.17](#11017-stdassert-bool-message) std:assert _bool_ \[_message_]
- [11.0.18](#11018-stdasserteq-actual-expected-message) std:assert\_eq _actual_ _expected_ \[_message_]
- [11.0.19](#11019-stdassertstreq-actual-expected) std:assert\_str\_eq _actual_ _expected_
- [11.0.20](#11020-stdassertreleq-l-r-epsilon-message) std:assert\_rel\_eq _l_ _r_ _epsilon_ \[_message_]
- [11.0.21](#11021-stdmeasuretime-unit-function) std:measure\_time _unit_ _function_
- [11.1](#111-io) I/O
- [11.1.1](#1111-stdioline) std:io:line
- [11.1.2](#1112-stdiolines-value) std:io:lines [_value_]
- [11.1.3](#1113-stdiofilereadtext-filename) std:io:file:read\_text _filename_
- [11.1.4](#1114-stdiofileread-filename) std:io:file:read _filename_
- [11.1.5](#1115-stdiofilewritesafe-filename-bytes-or-string) std:io:file:write\_safe _filename_ _bytes-or-string_
- [11.1.6](#1116-stdiofileappend-filename-bytes-or-string) std:io:file:append _filename_ _bytes-or-string_
- [11.1.7](#1117-stdiostdoutnewline) std:io:stdout:newline
- [11.1.8](#1118-stdiostdoutflush) std:io:stdout:flush
- [11.1.9](#1119-stdiostdoutprint-value) std:io:stdout:print _value_
- [11.1.10](#11110-stdiostdoutwrite-value) std:io:stdout:write _value_
- [11.1.11](#11111-stdioflush-handle) std:io:flush _handle_
- [11.1.12](#11112-stdioreadsome-handle) std:io:read\_some _handle_
- [11.1.13](#11113-stdiowrite-handle-data-offs) std:io:write _handle_ _data_ [_offs_]
- [11.1.14](#11114-stdiowritesome-handle-data) std:io:write\_some _handle_ _data_
- [11.2](#112-networking) Networking
- [11.2.1](#1121-stdnettcpconnect-socket-addr-connect-timeout) std:net:tcp:connect _socket-addr_ [_connect-timeout_]
- [11.2.2](#1122-stdnettcplisten-socket-addr-function) std:net:tcp:listen _socket-addr_ _function_
- [11.2.3](#1123-stdnetudpnew-socket-addr-connect-addr) std:net:udp:new _socket-addr_ [_connect-addr_]
- [11.2.4](#1124-stdnetudpsend-socket-data-socket-addr) std:net:udp:send _socket_ _data_ [_socket-addr_]
- [11.2.5](#1125-stdnetudprecv-socket-byte-count) std:net:udp:recv _socket_ [_byte-count_]
- [11.3](#113-processes) Processes
- [11.3.1](#1131-stdprocessrun-executable-path-arguments) std:process:run _executable-path_ [_arguments_]
- [11.3.2](#1132-stdprocessspawn-executable-path-arg-vector-inheritout--inheritall) std:process:spawn _executable-path_ _arg-vector_ [:inherit\_out | :inherit\_all]
- [11.3.3](#1133-stdprocesstrywait-child-handle) std:process:try\_wait _child-handle_
- [11.3.4](#1134-stdprocesskillwait-child-handle) std:process:kill\_wait _child-handle_
- [11.3.5](#1135-stdprocesswait-child-handle) std:process:wait _child-handle_
- [11.4](#114-file-system) File System
- [11.4.1](#1141-stdfsrename-file-path-new-file-name) std:fs:rename _file-path_ _new-file-name_
- [11.4.2](#1142-stdfscopy-src-file-path-dst-file-path) std:fs:copy _src-file-path_ _dst-file-path_
- [11.4.3](#1143-stdfsreaddir-path-function) std:fs:read\_dir _path_ _function_
- [11.4.4](#1144-stdfsremovefile-file-path) std:fs:remove\_file _file-path_
- [11.4.5](#1145-stdfsremovedir-dir-path) std:fs:remove\_dir _dir-path_
- [11.4.6](#1146-stdfsremovedirall-dir-path) std:fs:remove\_dir\_all _dir-path_
- [11.5](#115-system) System
- [11.5.1](#1151-stdsysos) std:sys:os
- [11.6](#116-threading) Threading
- [11.6.1](#1161-stdthreadspawn-string-globals-map) std:thread:spawn _string_ [_globals-map_]
- [11.6.2](#1162-stdthreadsleep-duration) std:thread:sleep _duration_
- [11.6.3](#1163-thread-handle-api) Thread Handle API
- [11.6.3.1](#11631-thdljoin) thdl.join
- [11.6.3.2](#11632-thdlrecvready) thdl.recv\_ready
- [11.6.4](#1164-atom-api) Atom API
- [11.6.4.1](#11641-stdsyncatomnew-value) std:sync:atom:new _value_
- [11.6.4.2](#11642-atomread) atom.read
- [11.6.4.3](#11643-atomwrite-value) atom.write _value_
- [11.6.4.4](#11644-atomswap-value) atom.swap _value_
- [11.6.5](#1165-atom-value-slot-api) Atom Value Slot API
- [11.6.5.1](#11651-stdsyncslotnew) std:sync:slot:new
- [11.6.5.2](#11652-atomslotsend-value) atom\_slot.send _value_
- [11.6.5.3](#11653-atomslotrecv) atom\_slot.recv
- [11.6.5.4](#11654-atomslottryrecv) atom\_slot.try\_recv
- [11.6.5.5](#11655-atomslotrecvtimeout-duration) atom\_slot.recv\_timeout _duration_
- [11.6.5.6](#11656-atomslotcheckempty) atom\_slot.check\_empty
- [11.6.5.7](#11657-atomslotwaitempty) atom\_slot.wait\_empty
- [11.6.5.8](#11658-atomslotwaitemptytimeout-duration) atom\_slot.wait\_empty\_timeout _duration_
- [11.6.6](#1166-channel-api) Channel API
- [11.6.6.1](#11661-stdsyncmpscnew) std:sync:mpsc:new
- [11.6.6.2](#11662-channelsend-value) channel.send _value_
- [11.6.6.3](#11663-channelrecv) channel.recv
- [11.6.6.4](#11664-channeltryrecv) channel.try\_recv
- [11.6.6.5](#11665-channelrecvtimeout-duration) channel.recv\_timeout _duration_
- [12](#12-optional-standard-library) Optional Standard Library
- [12.1](#121-serialization) serialization
- [12.1.1](#1211-stdserwlambda-arg) std:ser:wlambda _arg_
- [12.1.2](#1212-stdserjson-data-nopretty) std:ser:json _data_ \[_no\_pretty_]
- [12.1.3](#1213-stddeserjson-string) std:deser:json _string_
- [12.1.4](#1214-stdsercsv-fielddelim-rowseparator-escapeall-table) std:ser:csv _field\_delim_ _row\_separator_ _escape\_all_ _table_
- [12.1.5](#1215-stddesercsv-fielddelim-rowseparator-data) std:deser:csv _field\_delim_ _row\_separator_ _data_
- [12.1.6](#1216-stdsermsgpack-data) std:ser:msgpack _data_
- [12.1.7](#1217-stddesermsgpack-bytes) std:deser:msgpack _bytes_
- [12.2](#122-regular-expressions-more-classic-syntax) Regular Expressions (more classic syntax)
- [12.2.1](#1221-stdrematch-regex-string-input-string-function) std:re:match _regex-string_ _input-string_ _function_
- [12.2.2](#1222-stdrematchcompile-regex-string) std:re:match\_compile _regex-string_
- [12.2.3](#1223-stdremap-regex-string-function-input-string) std:re:map _regex-string_ _function_ _input-string_
- [12.2.4](#1224-stdrereplaceall-regex-string-replace-function-input-string) std:re:replace\_all _regex-string_ _replace-function_ _input-string_
- [12.3](#123-xml) xml
- [12.3.1](#1231-stdxmlreadsax-xml-string-event-callback-function-do-not-trim-text) std:xml:read\_sax _xml-string_ _event-callback-function_ [_do-not-trim-text_]
- [12.3.2](#1232-stdxmlcreatesaxwriter-indent) std:xml:create\_sax\_writer [_indent_]
- [12.3.3](#1233-stdxmlcreatetreebuilder) std:xml:create\_tree\_builder
- [12.4](#124-chrono) chrono
- [12.4.1](#1241-stdchronotimestamp-format) std:chrono:timestamp \[_format_]
- [12.4.2](#1242-stdchronoformatutc-utc-timestamp-format) std:chrono:format\_utc _utc-timestamp_ [_format_]
- [12.4.3](#1243-stdchronoformatlocal-utc-timestamp-format) std:chrono:format\_local _utc-timestamp_ [_format_]
- [12.5](#125-color-conversion) color conversion
- [12.5.1](#1251-stdvrgb2hsv-color-vector) std:v:rgb2hsv _color-vector_
- [12.5.2](#1252-stdvhsv2rgb-color-vector) std:v:hsv2rgb _color-vector_
- [12.5.3](#1253-stdvrgba2hex-color-vector) std:v:rgba2hex _color-vector_
- [12.5.4](#1254-stdvhex2rgbaf-string) std:v:hex2rgba\_f _string_
- [12.5.5](#1255-stdvhex2rgbai-string) std:v:hex2rgba\_i _string_
- [12.5.6](#1256-stdvhex2hsvai-string) std:v:hex2hsva\_i _string_
- [12.5.7](#1257-stdvhex2hsvaf-string) std:v:hex2hsva\_f _string_
- [12.6](#126-hash) hash
- [12.6.1](#1261-stdhashfnv1a-arg1-) std:hash:fnv1a _arg1_ ...
- [12.7](#127-rand) rand
- [12.7.1](#1271-stdrandsplitmix64new) std:rand:split\_mix64\_new
- [12.7.2](#1272-stdrandsplitmix64newfrom-seed) std:rand:split\_mix64\_new\_from _seed_
- [12.7.3](#1273-stdrandsplitmix64next-smstate-count) std:rand:split\_mix64\_next _sm\_state_ \[_count_]
- [12.7.4](#1274-stdrandsplitmix64nextopen01-smstate-count) std:rand:split\_mix64\_next\_open01 _sm\_state_ \[_count_]
- [12.7.5](#1275-stdrandsplitmix64nextopenclosed01-smstate-count) std:rand:split\_mix64\_next\_open\_closed01 _sm\_state_ \[_count_]
- [12.7.6](#1276-stdrandsplitmix64nextclosedopen01-smstate-count) std:rand:split\_mix64\_next\_closed\_open01 _sm\_state_ \[_count_]
- [12.8](#128-utility-functions) Utility Functions
- [12.8.1](#1281-stddumpupvals-function) std:dump\_upvals _function_
- [12.8.2](#1282-stdwlambdaversion) std:wlambda:version
- [12.8.3](#1283-stdwlambdasizes) std:wlambda:sizes
- [12.8.4](#1284-stdwlambdaparse-string) std:wlambda:parse _string_
- [12.9](#129-http-client) HTTP Client
- [12.9.1](#1291-stdhttpclientnew) std:http:client:new
- [12.9.2](#1292-stdhttpget-http-client-url-string-headers-and-options-map) std:http:get _http-client_ _url-string_ [_headers-and-options-map_]
- [12.9.3](#1293-stdhttppost-http-client-url-string-body-bytes-headers-and-options-map) std:http:post _http-client_ _url-string_ _body-bytes_ [_headers-and-options-map_]
- [12.9.4](#1294-stdhttprequest-http-client-method-string-url-string-body-bytes-headers-and-options-map) std:http:request _http-client_ _method-string_ _url-string_ [_body-bytes_ [_headers-and-options-map_]]
- [12.10](#1210-mqtt-messaging) MQTT Messaging
- [12.10.1](#12101-stdmqttbrokernew-config) std:mqtt:broker:new _config_
- [12.10.1.1](#121011-brokerpublish-topic-string-payload-bytes) broker.publish _topic-string_ _payload-bytes_
- [12.10.2](#12102-stdmqttclientnew-channel-client-id-broker-host-broker-port) std:mqtt:client:new _channel_ _client-id_ _broker-host_ _broker-port_
- [12.10.2.1](#121021-mqttclientpublish-topic-string-payload-bytes) mqtt\_client.publish _topic-string_ _payload-bytes_
- [12.10.2.2](#121022-mqttclientsubscribe-topic-string) mqtt\_client.subscribe _topic-string_
- [13](#13-wlambda-lexical-syntax-and-grammar) WLambda Lexical Syntax and Grammar
- [13.1](#131-special-forms) Special Forms
- [13.2](#132-string-formatting-syntax) String Formatting Syntax
- [13.3](#133-format-string-syntax-for-stdbytespack-and-stdbytesunpack) Format String Syntax for std:bytes:pack and std:bytes:unpack
-----
## <a name="1-variable-definition-and-assignment"></a>1 - Variable Definition and Assignment
As this manual assumes you have some programming knowledge,
we will just take a short look at the variable definition and assignment
syntax:
```wlambda
!a = 10; # variable definition & initialization
.a = 20; # assignment of a new value to a variable
```
WLambda also supports destructuring assignment of vectors:
```wlambda
!v = $[1,2,3];
!(a, b, c) = v; # destructuring definition of variables
.(a, b, c) = v; # destructuring assignment
std:assert_eq a 1;
std:assert_eq b 2;
std:assert_eq c 3;
```
This also works with maps, where the key names are matched to
the variable names:
```wlambda
!m = ${ a = 10, b = 20, c = 30 };
!(a, b, c) = m; # destructuring definition by map
.(a, b, c) = m; # destructuring assignment by map
std:assert_eq a 10;
std:assert_eq b 20;
std:assert_eq c 30;
```
And also with pairs:
```wlambda
!p = $p(10, 20);
!(a, b) = p;
.(a, b) = p;
std:assert_eq a 10;
std:assert_eq b 20;
```
### <a name="11-destructuring-to-variables"></a>1.1 - Destructuring to Variables
Like highlighted in the previous section you can define and assign to
multiple variables at once. Following data types support destructuring:
- Vectors:
```wlambda
!(a, b, c) = $[1, 2, 3];
std:assert_eq a 1;
std:assert_eq b 2;
std:assert_eq c 3;
```
- Maps:
```wlambda
!(x, foo, lol) = ${foo = 33, lol = 42, x = 2};
std:assert_eq x 2;
std:assert_eq foo 33;
std:assert_eq lol 42;
```
- Pairs:
```wlambda
!(x, y) = $p("ex", "uepsilon");
std:assert_eq x "ex";
std:assert_eq y "uepsilon";
```
- Numerical Vectors:
```wlambda
!(x, y, z) = $i(3, 44, 4);
std:assert_eq x 3;
std:assert_eq y 44;
std:assert_eq z 4;
!(r, g, b, a) = $f(0.3, 1.0, 0.4, 1.0);
std:assert_eq r 0.3;
std:assert_eq g 1.0;
std:assert_eq b 0.4;
std:assert_eq a 1.0;
```
### <a name="12-global-variables"></a>1.2 - Global Variables
You can define global variables that are not bound to
a lexical scope as follows:
```wlambda
{
!:global a = 13;
}[];
std:assert_eq a 13;
```
Global variables however do not live beyond file or module boundaries.
### <a name="13-constants"></a>1.3 - Constants
WLambda supports constant _variables_. These are global variables you can't
assign to. They are resolved and inserted at compile time and offer a slight
performance advantage (roughly 3-4%) over (global or local) variables.
```wlambda
!:const X = 11;
std:assert_eq X 11;
# Destructuring works too, but only with compile time literal values
# in the vectors / maps:
!:const (ON, OFF) = $[$true, $false];
!:const (RED, BLUE) = ${
BLUE = 0x0000FF,
RED = 0xFF0000,
};
std:assert_eq ON $true;
std:assert_eq OFF $false;
std:assert_eq RED 0xFF0000;
std:assert_eq BLUE 0x0000FF;
```
However, be aware that these _constants_ are not really constant. Due to
performance reasons referential values like Lists or Maps are not copied
(neither shallow, nor deep) if you access them through a constant.
```wlambda
!:const V = $[1,2,3];
std:assert_eq (str V) (str $[1,2,3]);
std:push V 43; # Mutation of a 'constant'
std:assert_eq V.3 43;
```
Constants also work across module borders:
```wlambda
!:const X = 10;
# When imported the X will remain constant:
!@export X = X;
```
## <a name="2-functions-part-12"></a>2 - Functions (part 1/2)
A function can be defined using the `{ ... }` syntax and the `\ _statement_`
syntax: To give functions a name, you need to assign them to a variable with
the `!_name_ = _expr_` syntax.
### <a name="21-closures"></a>2.1 - Closures
Functions take values from the outer scope by promoting the variable
at runtime to a hidden reference to their previous value:
```wlambda
!a = 10;
!b = 20;
# function transforms a and b to hidden references
!add_a_and_b = { a + b };
std:assert_eq add_a_and_b[] 30;
# The assignment assigns to the hidden reference, so the closure add_a_and_b
# also receives the new value:
.a = 33;
std:assert_eq add_a_and_b[] 53;
# a and b are dereferenced on local variable access.
std:assert_eq a + b 53;
```
#### <a name="211-object-oriented-programming-with-closures"></a>2.1.1 - Object Oriented Programming with Closures
This section explains how to create objects and hide state using closures.
Keep in mind, that there are also `$self` and `$data` available, which
allow a different approach for referring to the object state/data than to
capture the object as reference in a closure.
Keep in mind, that care must be taken (references need to be captures weakly)
with the references as shown below, because otherwise you will get reference
cycles and memory leaks.
```wlambda
!new_Cat = {!(name) = @;
# Captures by closures upgrade the outer `self` variable to a _hidden_
# reference, which is then captured. As the closure is stored in
# `self`, this would create a ref cycle. This is why we needed
# to make a weak reference to self.
# Make an explicit hidden reference:
!self_ = $& ${
name = name,
};
# Create a weak reference form the hidden reference:
!self = $weak& $:self_;
self.meow = { std:displayln self.name " meows!"; };
self.get_name = { self.name };
# To keep the object alive, we retrieve a strong reference
# from the hidden reference:
$:self
};
!my_cat = new_Cat "Spot";
my_cat.meow[]; # Prints 'Spot meows!'
std:assert_eq my_cat.get_name[] "Spot";
```
Alternatively you can just make the cat name private:
```wlambda
!new_Cat = {!(name) = @;
# This does not make cycles, because `name` does not contain
# the closures in the end.
!cat_name = name;
!meow = { std:displayln cat_name " meows!"; };
!get_name = { $*cat_name };
!set_name = { .*cat_name = _; };
# Just holds the methods
${
meow = meow,
get_name = get_name,
set_name = set_name,
};
};
!my_cat = new_Cat "Spot";
my_cat.meow[]; # Prints 'Spot meows!'
std:assert_eq my_cat.get_name[] "Spot";
my_cat.set_name "Spotty";
std:assert_eq my_cat.get_name[] "Spotty";
```
### <a name="22-function-calling"></a>2.2 - Function calling
To call functions, you have at least 4 alternatives. First is the bare
`_expr_ arg1 arg2 arg3 arg4` syntax. And the second is the fully delimited
variant: `_expr_[arg1, arg2, arg3, ...]`. You can always delimit the first
variant using the `( ... )` parenthesis around the whole call,
i.e. `(_expr_ arg1 arg2 arg3 arg4)`.
Third you can call a function with a vector as argument with `_expr_[[_expr_]]`,
where the second expression should return a vector (if it doesn't it will use the
value as first argument).
The fourth alternative is the `&>` and `<&` (and the apply variants `&@>` and
`<@&`) argument pipe operators which can be conveniently used in conjunction
with the first variant to prevent some parenthesis. Also belonging into the
category of function calling operators there is the collection addition operators
`+>` and `<+` which are described in their own section.
Here are examples:
```wlambda
# All the second variant:
std:assert_eq[std:str:cat[1, 2, 3], "123"];
# Can also be written as:
std:assert_eq (std:str:cat 1 2 3) "123";
# As the third variant:
!some_args = $[1, 2, 3];
std:assert_eq std:str:cat[[some_args]] "123";
# The fourth variant:
std:assert_eq str <& $[1, 2, 3] "$[1,2,3]";
std:assert_eq $[1, 2, 3] &> str "$[1,2,3]";
```
The arguments passed to the function are accessible using the `_`, `_1`, `_2`, ..., `_9`
variables. If you need to access more arguments the `@` variable holds a vector of all
arguments.
```wlambda
!twoify = { _ * 2 };
std:assert_eq twoify[2] 4;
!twoify2 = \_ * 2;
std:assert_eq twoify2[2] 4;
# You may also call them directly, notice the parenthesis ( ... ) syntax
# for delimiting the inner function call:
std:assert_eq ({ _ * 2 } 2) 4;
```
If you want to name arguments, you can use the destructuring assignment
syntax:
```wlamdba
!add = {!(a, b) = @;
a + b
};
std:assert_eq add[1, 2] 3;
```
### <a name="23-function-arity-checks"></a>2.3 - Function arity checks
Functions check the number of arguments passed to them. The compiler tries to
infer the number of arguments the function requires by looking at the parameter
variables `_` to `_9` and `@`. If the compiler gets it wrong, you can:
* Define minimum and maximum number of arguments with: `{|min < max| ... }`
* Define exact number of arguments with: `{|num_of_args| ... }`
* Accept any number of arguments: `{|| ... }`
For the shortened function syntax there is:
* `\|min < max| ...`
* `\|num_of_args| ...`
* `\|| ...`
Here an example:
```wlambda
!dosomething = {|2 < 4| !(a, b, c, d) = @;
# Please note: We have to assign the
# parameters to named values here, because
# the arms of the conditional below have
# their own set of arguments.
(is_none c) { a + b } { a * b + c * d }
};
std:assert_eq dosomething[1, 2] 3;
std:assert_eq dosomething[2, 2, 3, 4] 16;
```
#### <a name="231-stdtonoarity-function"></a>2.3.1 - std:to\_no\_arity _function_
This function disables all arity checks of a function. Use this with care
and diligence.
```wlambda
!f = { _ }; # accepts exactly 1 param
# f keeps its arity checks, but f2 will
# call the same function, but without arity checks.
!f2 = std:to_no_arity f;
std:assert_eq (f2 1 2 3) 1;
```
### <a name="24-calling-fields--method-calling"></a>2.4 - Calling fields / Method calling
If you use the '.' for accessing fields in a map,
the object the most recent field is accessed of is passed
to the called function. The object the function/method
was called upon can be accessed using the special value '$self'.
```wlambda
!some_map = ${
some_func = { $self.a_value },
a_value = 11,
};
std:assert_eq some_map.some_func[] 11;
```
This in combination with the special key `'_proto'` can be used to
implement a basic form of object orientation with prototype inheritance.
It can also be combined with the closure OOP approach or used for
other purposes.
You can also use a vector/list as object, in that case the `_proto`
field that holds the class method map is the first element of the
vector. The second element of the vector can be accessed using `$data`.
#### <a name="241-the-self-and-data-special-variables"></a>2.4.1 - The $self and $data special variables
If you call a method using the dot `.`, and the value on the left
side is a map or vector, you will get the map or vector by `$self`.
However, if you define a `_data` key on the map, or put something in
the second element of the vector, you can refer to it using `$data`.
You can use this to refer to the members and other functions of a structure:
```wlambda
!new_ab_struct = {
${
_data = ${ a = 1, b = 2 },
inc_a = { $data.a += 1; },
inc_b = { $data.b += 1; },
a = { $data.a },
b = { $data.b },
inc_both = {
$self.inc_a[];
$self.inc_b[];
},
}
};
!ab = new_ab_struct[];
ab.inc_a[];
std:assert_eq ab.a[] 2;
ab.inc_b[];
std:assert_eq ab.b[] 3;
ab.inc_both[];
std:assert_eq ab.a[] 3;
std:assert_eq ab.b[] 4;
```
The next seconds show how this can be used to do prototyped object
oriented programming.
#### <a name="242-object-oriented-programming-with-prototypes"></a>2.4.2 - Object Oriented Programming with Prototypes
Instead of using closures for OOP the preferred way is to use
maps of functions as classes and form an inheritance hierarchy
by using the `'_proto'` key of a map:
```wlambda
!class_a = ${
# $self is set by any key access using the '.' calling form:
new = { ${ _proto = $self } },
generate = { "I am A" }, # A method
};
!a_instance = class_a.new[];
std:assert_eq a_instance.generate[] "I am A";
```
The special key `'_data'` can be used (and is encouraged to be used)
as storage for data members of your objects. This is useful to separate
method name space inside objects from the data member namespace.
To quickly access the data members you can use the special value `$data`,
which will evaluate to `$self._data` in case `$self` is a map, and
to `$self.1` in case `$self` is a vector.
Here is an example with a map and data:
```wlambda
!class_b = ${
new = {
${
_proto = $self, # $self is class_b
_data = ${
a = 10
},
}
},
gen = { _ * $data.a }, # $data is equivalent to `$self._data` here
gen2 = { _ * $self._data.a },
};
!inst = class_b.new[];
std:assert_eq inst.gen[2] 20;
std:assert_eq inst.gen2[2] 20;
```
You can also use vectors as objects, which can be beneficial as they are
a bit slimmer and access to `_proto` and `_data` are reduced to a single
vector index lookup instead of an array lookup.
```wlambda
!class_b = ${
new = {
$[ # return a vector
$self, # $self is class_b
${ a = 10 },
]
},
gen = { _ * $data.a }, # $data is equivalent to `$self.1` here
gen2 = { _ * $self.1.a },
};
!inst = class_b.new[];
std:assert_eq inst.gen[3] 30;
std:assert_eq inst.gen2[4] 40;
```
#### <a name="243-object-oriented-with-prototypes-and-inheritance"></a>2.4.3 - Object Oriented with Prototypes and Inheritance
You can inherit functionality from a different class by assigning
it to the prototype of the class itself.
```wlambda
!SuperClass = ${
init_super_class = {
$data.inc = 0;
},
inc = {
$data.inc += 1;
$data.inc
},
};
```
Please notice, that _SuperClass_ does not have it's own constructor,
instead you should define a custom init function like `init_super_class`,
to define the used fields. The _SuperClass_ will refer to the
`$data` of the object that is going to be created by _MyClass_ in the next
step.
```wlambda
!SuperClass = ${
init_super_class = {
$data.inc = 0;
},
inc = {
$data.inc += 1;
$data.inc
},
};
!MyClass = ${
_proto = SuperClass,
new = {
!self = ${
_proto = $self,
_data = ${ other = 10 },
};
self.init_super_class[];
self
},
get_other = { $data.other },
get_inc = { $data.inc },
};
!my_obj = MyClass.new[];
std:assert_eq my_obj.get_other[] 10;
std:assert_eq my_obj.inc[] 1;
std:assert_eq my_obj.inc[] 2;
std:assert_eq my_obj.inc[] 3;
std:assert_eq my_obj._data.other 10;
std:assert_eq my_obj._data.inc 3;
```
#### <a name="244-object-oriented-with-prototypes-and-self-references-and-data-references"></a>2.4.4 - Object Oriented with Prototypes and $self References and $data References
There might come a time, when you want to pass a reference of your
object around, but you want to prevent cyclic references.
For this you will need to return a strong reference `$&&` from your
constructor as `$self` and if you want to refer to `$data` from callback
functions, you are advised to also wrap it into a strong reference.
```wlambda
!destroyed = $false;
!MyClass = ${
new = {
$&& ${
_proto = $self,
_data = $&& ${
x = 1
},
dropper = std:to_drop { .destroyed = $t; },
}
},
inc_x = { $data.x += 1 },
install_on = {!(callchain) = @;
!self = $w& $self;
std:push callchain { self.inc_x[]; };
},
install_getter = {!(callchain) = @;
!data = $w& $data;
std:push callchain { data.x };
},
};
# Create instance:
!my_obj = MyClass.new[];
my_obj.inc_x[];
!chain = $[];
my_obj.install_on chain;
my_obj.install_getter chain;
# There are now 3 references to 'my_obj':
# - my_obj variable
# - first callback in chain
# - second callback in chain
std:assert_eq my_obj._data.x 2;
chain.0[]; # calls my_ocj.inc_x[];
std:assert_eq my_obj._data.x 3;
# Second callback gets x:
std:assert_eq chain.1[] 3;
!my_obj = $n; # destroy only strong reference
std:assert destroyed;
```
Of course the callbacks now refer to `$none` to call `inc_x`, a more
sophisticated way of cleanup is of course necessary. But this is just an
example.
### <a name="25-function-call-composition"></a>2.5 - Function call composition
- chaining
- traditional () call syntax
- ~ syntax
- || syntax
>> $[] || push 10
> $[10]
>> $[] || push 10 || push 20
> $[10,20]
>> !x = { push _1 _ };
> $n
>> $[] | x 10 | x 20
> $[10,20]
>>
- [...] syntax
#### <a name="251--tail-argument-function-chaninig"></a>2.5.1 - '|' Tail Argument Function Chaninig
This syntax is useful if you have following function call composition:
```text
(fn arg1 arg2 (fn2 arg_b1 arg_b2 (fn3 arg_c1 arg_c2 ...)))
```
These can be written more comfortably like this:
```text
fn3 arg1 arg2 | fn2 arg_b1 arg_b2 | fn arg1 arg2
```
An example with actual values:
```wlambda
!x = 10 | { _ * 4 } | { _ + 2 };
std:assert_eq x 42;
```
Think of it as if the value `10` was _piped_ through the
functions on the right.
The call reordering of the `|` operator looks like this:
```text
fn1 a1 a2 | fn2 b1 b2 ( ) => fn2 b1 b2 (fn1 a1 a2)
""""""""" ^
v |
--------------------|
```
#### <a name="252--left-hand-function-chaining"></a>2.5.2 - '|>' Left Hand Function Chaining
This syntax is useful if you want to make deep call chains like these:
```text
(((fn arg1 arg2 ...) arg_b1 arg_b2 ...) arg_c1 arg_c2 ...)
```
These can be written more comfortably like this:
```text
fn arg1 arg2 |> arg_b1 arg_b2 |> arg_c1 arg_c2
```
or nicer formatted:
```text
fn arg1 arg2
|> arg_b1 arg_b2
|> arg_c1 arg_c2
```
Here an actual example:
```wlambda
!res = $@v
1 + 1
|> $["abc", "def", "ceg"]
|> { $+ ~ std:str:cat "|" _ "|" };
std:assert_eq res.0 "|c|";
std:assert_eq res.1 "|e|";
std:assert_eq res.2 "|g|";
```
The call reordering of the `|>` operator looks like this:
```text
fn1 a1 a2 |> b1 b2 => (( ) )
""""""""" """"" ^ ^
v v | |
-----------|--------------| |
-------------------|
```
#### <a name="253-forward-argument-pipe-arg--fun"></a>2.5.3 - Forward Argument Pipe `arg &> fun`
This operator has the highest precedence over all other operators
and is used to be able to write this:
```wlambda
if "foob" &> $r/f(^*)b/ {
std:assert_eq $\.1 "oo";
} {
std:assert $false;
}
```
That means `f a &> b` is equivalent to writing `f[b[a]]` or `(f (b a))`.
Chaining multiple is also possible and left associative: `a &> b &> c` is `(c (b a))`.
You can see it as piping operation:
```wlambda
!r = "ABC" &> std:str:to_lowercase &> \std:str:pad_start 10 "0" _;
std:assert_eq r "0000000abc";
```
#### <a name="254-forward-argument-apply-pipe-list--fun"></a>2.5.4 - Forward Argument Apply Pipe `list &@> fun`
This operator is like `&>`. But it will call the function with the elements
in the given _list_ as arguments.
That means: `list &@> fun` is equivalent to `fun[[list]]`.
```wlambda
std:assert_eq $[2, 5] &@> `+` 7;
```
#### <a name="255-reverse-argument-pipe-fun--arg"></a>2.5.5 - Reverse Argument Pipe `fun <& arg`
Like the `&>` operator this operator, but it has a lower precedence (does not bind
as strongly as `&>`) and is right associative. That means you can write this:
```wlambda
!r = (\std:str:pad_start 10 "0" _) <& std:str:to_lowercase <& "ABC";
std:assert_eq r "0000000abc";
```
That means, writing `f <& a <& x` becomes `f[a[x]]` or `(f (a x))`.
#### <a name="256-reverse-argument-apply-pipe-list--fun"></a>2.5.6 - Reverse Argument Apply Pipe `list &@> fun`
This operator is like `<&`. But it will call the function with the elements
in the given _list_ as arguments.
That means: `fun <@& list` is equivalent to `fun[[list]]`.
```wlambda
std:assert_eq `+` <@& $[2, 5] 7;
```
### <a name="26-control-flow---returning"></a>2.6 - Control Flow - Returning
WLambda uses labelled blocks for control flow, as returning from the current function would not be
very helpful for the control flow in wlambda in case of conditional execution using the
boolean calling semantics.
```wlambda
!some_func = \:outer {
!x = 10;
# does stuff...
(x == 10) {
return :outer 20
};
# more stuff that is not executed if x == 10.
}
```
#### <a name="261-return-label-value"></a>2.6.1 - return [_label_] _value_
Returns _value_ from the current function if no _label_ is given.
If _label_ is given, the call stack will unwind until either a `block`
or a function with the given _label_ is encountered.
```wlambda
!f = {
10;
return 20;
30
};
std:assert_eq f[] 20;
```
Here an example for unwinding two call frames:
```wlambda
!f = \:x {
10;
{ return :x 20 }[];
30;
};
std:assert_eq f[] 20;
```
The labels do not adhere to lexical scoping and are dynamically scoped:
```wlambda
!g = { return :x 30 };
!f = \:x { 20; g[]; 40 };
std:assert_eq f[] 30;
```
#### <a name="262-block-label-function"></a>2.6.2 - block [label] _function_
Calls the _function_ with the given _label_ for `return`to jump to.
If you just want to setup a point inside a function to jump to
with `return` the `block` function is more convenient to use:
```wlambda
!y = 1;
!res = block :x {
.y = y + 1;
(y >= 2) \return :x 20;
.y = y + 1;
.y = y + 1;
};
std:assert_eq res 20;
```
The alternative is the less clear syntax would be in this case:
```wlambda
!y = 1;
!res = \:x {
.y = y + 1;
(y >= 2) \return :x 20;
.y = y + 1;
.y = y + 1;
}[];
std:assert_eq res 20;
```
#### <a name="263-stdtodrop-function-or-raii-destructors-or-drop-functions"></a>2.6.3 - std:to\_drop _function_ (or RAII, Destructors or Drop Functions)
You can create a function that is called when it is
dropped/its reference count goes to 0.
```wlambda
!dropped = $false;
!x = std:to_drop { .dropped = $true; };
std:assert not[dropped];
.x = $none;
std:assert dropped;
```
Please note, that the drop function will be executed in a newly constructed
default EvalContext, this means there is some overhead and that the EvalContext
dependent results of `std:eval` might be different.
#### <a name="264-stdtimenow-unit"></a>2.6.4 - std:time:now [_unit_]
Returns the current system time since the UNIX epoch (1970-01-01 00:00:00 UTC).
If no _unit_ is specified, the default is `:ms`. Following units are available:
- seconds: `"s"`
- milliseconds: `"ms"`
- microseconds: `"us"`
- nanoseconds: `"ns"`
```wlambda
std:assert (std:time:now :s) > 1000;
std:assert (std:time:now :ms) > 1000;
std:assert len[str[std:time:now :ns]] > 18;
```
#### <a name="265-stdsrand-seed"></a>2.6.5 - std:srand [_seed_]
With this function you can seed the internal pseudo random number
generator based on an unspecified PRNG algorithm, that might or might not
change in the next WLambda version.
If no _seed_ is provided, the current system time (in `ns` resolution) is used.
If _seed_ is provided, it is set to the integer value of that.
```wlambda
std:srand[];
std:srand 1000;
```
#### <a name="266-stdrand-max-or-mode"></a>2.6.6 - std:rand [_max-or-mode_]
Returns a random number between 0 and _max_. The interval 0
to _max-or-mode_ is closed/open, that means 0 is included but _max-or-mode_
is not included.
If _max-or-mode_ is a string `"i64"` or symbol `:i64`, then std:rand will
return a random signed 64 bit integer.
If _max-or-mode_ is not provided, a float number between 0.0
and 1.0 (including 0.0 but not including 1.0) is returned.
```wlambda
std:srand 1234567890;
!zeros = $@i iter i 0 => 1000 {
if std:rand[100] == 0 \$+ 1;
};
!count_100 = $@i iter i 0 => 1000 {
if std:rand[100] == 100 \$+ 1;
};
std:assert zeros > 0;
std:assert count_100 == 0;
std:assert std:rand[] < 1.0;
std:assert std:rand[] >= 0.0;
```
Please note: The PRNG algorithm used for `std:rand` may change
without further notice. If you require your project to have consistent
PRNG results across all WLambda versions use `std:rand:split_mix64_*`.
### <a name="27-function-utilities"></a>2.7 - Function utilities
#### <a name="271-isfun-value"></a>2.7.1 - is\_fun _value_
Returns `$true` if _value_ is a function.
```wlambda
std:assert ~ is_fun {};
std:assert ~ is_fun is_fun;
std:assert ~ not ~ is_fun ${a=10};
```
## <a name="3-data-types"></a>3 - Data Types
### <a name="31-none-sentinel-value-n-or-none"></a>3.1 - None sentinel value: `$n` or `$none`
This is a special sentinel value that is returned by functions and
when a non existing field of a datastructure is accessed. It's semantic
meaning is that there is no value.
Most functions that expect a string value will turn a `$none` into an
empty string. If you need an unambigous representation use `std:ser:wlambda`
for dumping WLambda data structures.
Please note for API design: In case of errornous states you should not
return a `$none` but an `$error` value.
```wlambda
std:assert ~ $n == $none;
std:assert ~ int[$n] == 0;
std:assert ~ float[$n] == 0.0;
std:assert ~ str[$n] == "";
std:assert ~ std:ser:wlambda[$n] == "$n";
std:assert ~ is_none[$n];
```
#### <a name="311-isnone-value"></a>3.1.1 - is\_none _value_
Returns `$true` if _value_ is `$none` or `$o()`.
```wlambda
std:assert ~ is_none $none;
std:assert ~ is_none $o();
std:assert ~ not ~ is_none $false;
std:assert ~ not ~ is_none $o(10);
```
#### <a name="312-issome-value"></a>3.1.2 - is\_some _value_
Returns `$true` if _value_ is anything except `$none` or `$o()`.
```wlambda
std:assert ~ not ~ is_some $none;
std:assert ~ not ~ is_some $o();
std:assert ~ is_some $false;
std:assert ~ is_some 30;
std:assert ~ is_some $o(30);
```
### <a name="32-optional-values-o-and-o"></a>3.2 - Optional values `$o()` and `$o(...)`
An optional value can either contain another value, or contain no value at all.
An empty optional value is not much different from `$none`, but it is sometimes
desirabel to make a difference between an optional value and a `$none` value
if the `$none` value is used as sentinel value.
Optional values were introduced for functions that lookup stuff and either
return _something_ that might be `$none` (eg. if some element in a vector is
searched for), or return that nothing was found.
The functions `is_none` and `is_some` like stated above work for these
optional values too:
```wlambda
std:assert ~ is_none $o();
std:assert ~ is_some $o(10);
std:assert ~ is_some $o($none);
std:assert ~ is_some $o($o());
```
Calling an optional value will return it's contents or `$none`:
```wlambda
std:assert_eq $o()[] $none;
std:assert_eq $o(10)[] 10;
!do_something = {
if _ == 0 {
$o()
} {
$o(_ + 10)
}
};
!result = do_something 11;
std:assert_eq result[] 21;
!result = do_something 0;
std:assert_eq result[] $none;
```
In a boolean context an optional becomes `$true` if it contains
something and `$false` if it has nothing.
```wlambda
std:assert ~ not ~ bool $o();
std:assert ~ bool $o(10);
!x = $o();
!res1 = if x "something" "nothing";
std:assert_eq res1 "nothing";
.x = $o(30);
!res2 = if x "something" "nothing";
std:assert_eq res2 "something";
```
Many other operations are just forwarded to the contents of the
optional value:
```wlambda
std:assert_eq $o(33) + 44 77;
!x = $o($[1,2,3]);
std:push x 4;
std:assert_eq (str x) (str $[1,2,3,4]);
std:assert_eq (float $o(4.4)) 4.4;
std:assert_eq (int $o(4.4)) 4;
```
An optional value can also be dereferenced:
```wlambda
std:assert_eq $*$o() $none;
std:assert_eq $*$o(10) 10;
```
Calls with more than zero arguments are forwarded to the contents:
```wlambda
std:assert_eq ($o("xx") "yy") "xxyy";
!x = { _ * 20 };
std:assert_eq ($o(x) 30) 600;
```
#### <a name="321-isoptional-value"></a>3.2.1 - is\_optional _value_
Returns `$true` if _value_ is an optional value. That means either `$o()` or
`$o(...)`.
```wlambda
std:assert ~ is_optional $o();
std:assert ~ is_optional $o($none);
std:assert ~ is_optional $o(10);
std:assert ~ not ~ is_optional $true;
std:assert ~ not ~ is_optional $none;
std:assert ~ not ~ is_optional $false;
std:assert ~ not ~ is_optional 303;
```
#### <a name="322-unwrapping-optionals"></a>3.2.2 - Unwrapping optionals
You can unwrap an optional with `unwrap`. It will panic if there is no value provided.
Otherwise it will return the contents.
```wlambda
std:assert_eq unwrap[$o(10)] 10;
```
### <a name="33-error-values-e-expr-or-error-expr"></a>3.3 - Error values: `$e expr` or `$error expr`
There are no exceptions in WLambda, except the panic, that
halts all execution of the currently running WLambda
program. To signal errors, you return an `$error` value.
These error values, if not handled, will cause a panic of your
program. This means, you need to handle returned error values
one way or the other.
The error value wraps any value you pass to the `$error` or `$e`
constructor syntax.
```wlambda
std:assert ~ is_err ~ $e "something went wrong!"
```
There are more routines except `is_err` to handle an error.
`_?` will return from the currently executed function
up until some given label. `on_error` executes a function
if the second argument was an error value. Otherwise it
just passes through the value. `unwrap` will explicitly cause
an panic if an error value was passed to it. All other values
will be passed through. And `unwrap_err` unwraps an error value, it's
the opposite of `unwrap` because it will cause a panic if you don't pass
an error value.
Most functions don't accept errors in their arguments.
If an error is encountered, a panic will occur. There are only
a few functions that accept error values in their arguments:
- panic
- `_?`
- unwrap_err
- std:error_to_str
- unwrap
- on_error
- return
- break
- bool
- type
- match
- assert
- assert_eq
- is_some
- is_none
- is_err
- is_map
- is_vec
- is_fun
- is_str
- is_wref
- is_ref
- is_bool
- is_bytes
- is_sym
- is_float
- is_optional
- is_int
- ==
- !=
- std:to_ref
- std:ref_id
- std:ser:wlambda
All other functions don't accept errors as their argument.
#### <a name="331--label-value"></a>3.3.1 - _? [_label_] _value_
Unwind the call stack from the current function to a given _label_ if _value_ is an error value.
If no _label_ is given only the current function is returned from with the error value. If there
is no error, the given value is returned.
The best usecase is, if you just want to hand any errors that might be returned
further upwards the call stack for the parent functions to handle.
```wlambda
!func = { $e "this failed!" };
!other = {
# some code ...
_? func[]; # If you would not catch the error value here,
# the program would panic, as an error value
# must not be ignored!
# other code ...
panic "this will never be reached!";
# something here...
};
std:assert ~ (unwrap_err other[]) == "this failed!";
```
`_?` can take up to 2 arguments. If so, the first argument is interpreted
as jump label. That is handy if you want to jump up multiple call frames:
```wlambda
!failing_func = { $e :FAIL };
!func = \:some_unique_label {
( _ == 42 ) {
std:displayln "We got 42!";
# The `then` branch we are currently in is a call frame.
# To jump further up the call stack, we need the label
# we defined for the function above.
!val = _? :some_unique_label failing_func[];
std:displayln "Returned:" val;
}
};
std:assert_eq (unwrap_err ~ func 42) :FAIL;
```
A more elaborate example:
```wlambda
!do_fail = $false;
!maybe_fails1 = { 10 };
!maybe_fails2 = {
do_fail { $error "something is wrong" }
{ .do_fail = $true; 2 };
};
!a = {
!x = _? maybe_fails1[];
.x = x + (_? maybe_fails2[]);
x
};
!first = a[];
!second = a[];
std:assert_eq first 12;
std:assert (is_err second);
```
#### <a name="332-unwrap-value"></a>3.3.2 - unwrap _value_
Unwraps the given _value_. If the _value_ is an error object it will panic.
Otherwise it will just return the given value. If the _value_ is an optional
value, it will return the value that is wrapped in the optional value.
If it is an empty optional, it will also panic.
Here an demonstration of the unwrap panic:
```wlambda
match (std:eval $code { unwrap $e XXX })
($e err) => {
std:assert ~ std:str:find "Variable 'XXX' undefined" $\.err;
}
{ std:assert $false };
```
And here how to unwrap optionals:
```wlambda
std:assert_eq (unwrap $o(123)) 123;
match (std:eval $code { unwrap $o() })
($e err) => {
std:assert ~ std:str:find "unwrap empty option" $\.err;
}
{ std:assert $false };
```
#### <a name="333-unwraperr-error-value"></a>3.3.3 - unwrap\_err _error-value_
Unwraps an error value. Does panic if _error-value_ is not an error value.
If it is an error value, the inner wrapped value is returned.
```wlambda
!v = unwrap_err $e "Some Error";
std:assert_eq v "Some Error";
```
#### <a name="334-onerror-handler-maybe-error-value"></a>3.3.4 - on\_error _handler_ _maybe-error-value_
The first parameter to `on_error` should be a _handler_ function,
which will be called with four parameters.
The first of these parameters is the error text,
followed by the line number, column number and file name
from which the error originates.
The given _handler_ is called when an error value is encountered
as second argument, the _maybe-error-value_.
An example to demonstrate the handler arguments:
```wlambda
on_error {!(func, line, col, filename) = @;
# ...
} ($e "test");
```
A usage example:
```wlambda
!func = {
(_ == 13) {
$e "this failed!"
} {
"all ok!"
}
};
!x = $n;
# The first function of on_error will be called with the unwrapped
# error if an error occured.
on_error {|4| .x = _; } ~ func 13;
std:assert_eq x "this failed!";
!ret = on_error {|4| .x = _; } ~ func 1;
std:assert_eq ret "all ok!";
```
#### <a name="335-iserr-value"></a>3.3.5 - is\_err _value_
Returns `$true` if _value_ is an error value.
```wlambda
std:assert ~ is_err $e "foo";
std:assert ~ not ~ is_err $none;
std:assert ~ not ~ is_err 10;
```
#### <a name="336-stderrortostr-value"></a>3.3.6 - std:error\_to\_str _value_
This function accepts an error value in contrast to `str`, but does
not panic but transform the error value into its string representation.
```wlambda
!r = std:error_to_str $e "TEST";
std:assert_eq r "$e \"TEST\" [@ <wlambda::eval>:1:26 Err]";
```
WARNING: The string representation might change between wlambda versions.
Please use `on_error` to access the individual parts
(line, column, filename, error value) of the error.
### <a name="34-booleans"></a>3.4 - Booleans
True and false are represented by `$t` and `$f` or `$true` and `$false`,
whatever suits your coding style better.
You can either use a boolean value with one or two arguments, where `$true`
will call the first argument, and `$false` the second argument. If a second argument
isn't provided and the value is `$false`, `$none` is returned. So to
check for truthness you can just do:
```wlambda
!x = 10;
!some_num =
(x == 10) { "it is ten" } { "it is not ten" };
std:assert_eq some_num "it is ten";
.x = 20;
.some_num =
(x == 10) { "it is ten" } { "it is not ten" };
std:assert_eq some_num "it is not ten";
```
#### <a name="341-isbool-any-value"></a>3.4.1 - is\_bool _any-value_
You can check if something is a boolean with `is_bool`:
```wlambda
std:assert ~ is_bool $true;
std:assert ~ is_bool $false;
std:assert ~ not[is_bool $n];
std:assert ~ not[is_bool ""];
std:assert ~ not[is_bool 0];
```
#### <a name="342-bool-any-value"></a>3.4.2 - bool _any-value_
You can cast _any-value_ into a boolean with the `bool` function:
```wlambda
std:assert_eq (bool 1) $true;
std:assert_eq (bool 0) $false;
std:assert_eq (bool $e :x) $false;
std:assert_eq (bool $n) $false;
std:assert_eq (bool "") $false;
std:assert_eq (bool "0") $false;
std:assert_eq (bool "1") $true;
std:assert_eq (bool :0) $false;
std:assert_eq (bool :1) $true;
std:assert_eq (bool 0.0) $false;
std:assert_eq (bool 0.1) $false;
std:assert_eq (bool 1.0) $true;
std:assert_eq (bool {}) $true;
std:assert_eq (bool $b"") $false;
std:assert_eq (bool $b"\x00") $false;
std:assert_eq (bool $b"\x01") $true;
```
#### <a name="343-not-value"></a>3.4.3 - not _value_
This function negates the boolean _value_. If it is not a boolean, it will
be casted into one before negating.
```wlambda
std:assert ~ not $false;
std:assert ~ not 0;
std:assert ~ not $none;
```
#### <a name="344-boolean-list-indexing"></a>3.4.4 - Boolean List Indexing
Booleans can also be used to pick a value from a list
by calling the boolean with a list as first argument:
```wlambda
std:assert_eq ($true $[:a, :b]) :b;
std:assert_eq ($false $[:a, :b]) :a;
```
### <a name="35-64-bit-integers"></a>3.5 - 64-Bit Integers
WLambda's most basic numeric data type is the 64-Bit integer, aka _i64_ in Rust.
Like with other numbers multiple radix literal forms are supported:
```wlambda
# Decimal:
std:assert_eq 10r99 99;
# Hexadecimal:
std:assert_eq 0xFF01 65281;
# Binary:
std:assert_eq 0b1011 11;
std:assert_eq -0b1011 -11;
# Radix 4:
std:assert_eq 4r31 13;
```
#### <a name="351-int-value"></a>3.5.1 - int _value_
Returns the integer casted version of _value_.
Mostly interesting for converting a string to an integer (in radix 10)
or for getting the truncated value of a float.
```wlambda
std:assert_eq (int 4.2) 4;
std:assert_eq (int "402") 402;
std:assert_eq (int "a3") 0;
std:assert_eq (int $b"@") 0x40; # Returns the byte value of the first char
std:assert_eq (int $[4,4,4]) 3; # Same as `len`
std:assert_eq (int ${a=4,b=4}) 2; # Same as `len`
```
#### <a name="352-isint-value"></a>3.5.2 - is\_int _value_
Returns `$true` if _value_ is of data type integer. Otherwise it returns `$false`.
#### <a name="353-stdnegi64-integer"></a>3.5.3 - std:neg\_i64 _integer_
Negates the _integer_, which makes a negative from a positive and positive
from a negative number.
```wlambda
std:assert_eq (std:neg_i64 -1) 1;
std:assert_eq (std:neg_i64 1) -1;
std:assert_eq (std:neg_i64 0xFF) -255;
```
#### <a name="354-stdnoti64-integer"></a>3.5.4 - std:not\_i64 _integer_
Flips the bits of the signed 64-Bit _integer_.
```wlambda
std:assert_eq (std:not_i64 -1) 0;
std:assert_eq (std:not_i64 1) -2;
std:assert_eq (std:not_i64 0xFF) -256;
```
#### <a name="355-stdnegu32-integer"></a>3.5.5 - std:neg\_u32 _integer_
Negates the _integer_ as if it was an unsigned 32-Bit integer.
```wlambda
std:assert_eq (std:neg_u32 0xFF) 4294967041;
std:assert_eq (std:neg_u32 0x1) 4294967295;
std:assert_eq (std:neg_u32 0x0) 0;
```
#### <a name="356-stdnotu32-integer"></a>3.5.6 - std:not\_u32 _integer_
Flips the bits of the _integer_ as if it was an unsigned 32-Bit integer.
```wlambda
std:assert_eq (std:not_u32 0xFF) 4294967040;
std:assert_eq (std:not_u32 0x1) 4294967294;
std:assert_eq (std:not_u32 0x0) 4294967295;
```
### <a name="36-64-bit-floats"></a>3.6 - 64-Bit Floats
WLambda supports 64-Bit floating point numbers, aka _f64_ in Rust.
Like with other numbers multiple radix literal forms are supported:
```wlambda
# Decimal:
std:assert_eq 10r9.92 9.92;
# Hexadecimal:
std:assert_eq 0xFF.1 255.0625;
# Binary:
std:assert_eq 0b1011.101 11.625;
# Radix 4:
std:assert_eq 4r3.3 3.75;
```
#### <a name="361-float-value"></a>3.6.1 - float _value_
This function casts _value_ into a float:
```wlambda
std:assert_eq (float 10) 10.0;
std:assert_eq (float $t) 1.0;
std:assert_eq (float $f) 0.0;
std:assert_eq (float :"32.2") 32.2;
std:assert_eq (float "5.42") 5.42;
std:assert_eq (float "5.42") 5.42;
std:assert_eq (float $b"\xFF") 255.0;
```
#### <a name="362-isfloat-value"></a>3.6.2 - is\_float _value_
Returns `$true` if _value_ is a float, otherwise `$false` is returned.
```wlambda
std:assert ~ is_float 4.4;
std:assert ~ is_float 1.0 + 1;
std:assert ~ not ~ is_float 1 + 1.0;
std:assert ~ not ~ is_float 4;
std:assert ~ not ~ is_float $true;
```
#### <a name="363-stdnumacos-float"></a>3.6.3 - std:num:acos _float_
Computes the arccosine of a number. Return value is in radians in the range [0,
pi] or NaN if the number is outside the range [-1, 1].
#### <a name="364-stdnumacosh-float"></a>3.6.4 - std:num:acosh _float_
Inverse hyperbolic cosine function.
#### <a name="365-stdnumasin-float"></a>3.6.5 - std:num:asin _float_
Computes the arcsine of a number. Return value is in radians in the range
[-pi/2, pi/2] or NaN if the number is outside the range [-1, 1].
#### <a name="366-stdnumasinh-float"></a>3.6.6 - std:num:asinh _float_
Inverse hyperbolic sine function.
#### <a name="367-stdnumatan-float"></a>3.6.7 - std:num:atan _float_
Computes the arctangent of a number. Return value is in radians in the range
[-pi/2, pi/2].
#### <a name="368-stdnumatan2-y-x"></a>3.6.8 - std:num:atan2 _y_ _x_
Computes the four quadrant arctangent of _y_ and other _x_ in radians.
- x = 0, y = 0: 0
- x >= 0: arctan(y/x) -> [-pi/2, pi/2]
- y >= 0: arctan(y/x) + pi -> (pi/2, pi]
- y < 0: arctan(y/x) - pi -> (-pi, -pi/2)
#### <a name="369-stdnumatanh-float"></a>3.6.9 - std:num:atanh _float_
Inverse hyperbolic tangent function.
#### <a name="3610-stdnumcbrt-float"></a>3.6.10 - std:num:cbrt _float_
Takes the cubic root of a number.
#### <a name="3611-stdnumceil-float"></a>3.6.11 - std:num:ceil _float_
Returns the smallest integer (still a float) greater than or equal to a number.
#### <a name="3612-stdnumcos-float"></a>3.6.12 - std:num:cos _float_
Computes the cosine of a number (in radians).
#### <a name="3613-stdnumcosh-float"></a>3.6.13 - std:num:cosh _float_
Hyperbolic cosine function.
#### <a name="3614-stdnumexp-float"></a>3.6.14 - std:num:exp _float_
Returns e ^ _float_, (the exponential function).
#### <a name="3615-stdnumexp2-float"></a>3.6.15 - std:num:exp2 _float_
Returns 2 ^ _float_.
#### <a name="3616-stdnumexpm1-float"></a>3.6.16 - std:num:exp\_m1 _float_
Returns (e ^ _float_ - 1) in a way that is accurate even if the number is close
to zero.
#### <a name="3617-stdnumfloor-float"></a>3.6.17 - std:num:floor _float_
Returns the largest integer (still as float) less than or equal to a number.
#### <a name="3618-stdnumhypot-y-x"></a>3.6.18 - std:num:hypot _y_ _x_
Calculates the length of the hypotenuse of a right-angle triangle given legs of
length _x_ and _y_.
#### <a name="3619-stdnumln-float"></a>3.6.19 - std:num:ln _float_
Returns the natural logarithm of the number.
#### <a name="3620-stdnumlog-float"></a>3.6.20 - std:num:log _float_
Returns the logarithm of the number with respect to an arbitrary base.
The result may not be correctly rounded owing to implementation details;
`std:log2` can produce more accurate results for base 2, and `std:log10` can
produce more accurate results for base 10.
#### <a name="3621-stdnumlog10-float"></a>3.6.21 - std:num:log10 _float_
Returns the base 10 logarithm of the number.
#### <a name="3622-stdnumlog2-float"></a>3.6.22 - std:num:log2 _float_
Returns the base 2 logarithm of the number.
#### <a name="3623-stdnumpow-float"></a>3.6.23 - std:num:pow _float_
Raises a number to a floating point power.
You may also use the `^` operator, which also works for integers.
#### <a name="3624-stdnumrecip-float"></a>3.6.24 - std:num:recip _float_
Takes the reciprocal (inverse) of a number, 1/x.
#### <a name="3625-stdnumround-float"></a>3.6.25 - std:num:round _float_
Returns the nearest integer (still a float) to a number. Round half-way cases
away from 0.0.
#### <a name="3626-stdnumsin-float"></a>3.6.26 - std:num:sin _float_
Computes the sine of a number (in radians).
#### <a name="3627-stdnumsinh-float"></a>3.6.27 - std:num:sinh _float_
Hyperbolic sine function.
#### <a name="3628-stdnumsqrt-float"></a>3.6.28 - std:num:sqrt _float_
Takes the square root of a number.
#### <a name="3629-stdnumtan-float"></a>3.6.29 - std:num:tan _float_
Computes the tangent of a number (in radians).
#### <a name="3630-stdnumtanh-float"></a>3.6.30 - std:num:tanh _float_
Hyperbolic tangent function.
#### <a name="3631-stdnumtodegrees-float"></a>3.6.31 - std:num:to\_degrees _float_
Converts radians to degrees.
#### <a name="3632-stdnumtoradians-float"></a>3.6.32 - std:num:to\_radians _float_
Converts degrees to radians.
#### <a name="3633-stdnumtrunc-float"></a>3.6.33 - std:num:trunc _float_
Returns the integer part of a number.
#### <a name="3634-stdnumlerp-a-b-x"></a>3.6.34 - std:num:lerp _a_ _b_ _x_
Linear interpolation between _a_ and _b_ by _x_. Where _x_ is
in the range of `[0.0, 1.0]`.
```wlambda
!res = int ~ std:num:lerp 0.0 100.0 0.5;
std:assert_eq res 50;
```
#### <a name="3635-stdnumsmoothstep-a-b-x"></a>3.6.35 - std:num:smoothstep _a_ _b_ _x_
Interpolates smoothly from 0.0 to 1.0 where _x_ is in the range of `[a, b]`.
```wlambda
!res = int ~ 1000.0 * (std:num:smoothstep 0.0 100.0 10.0);
std:assert_eq res 28;
```
#### <a name="3636-stdnumfract-float"></a>3.6.36 - std:num:fract _float_
Returns the fractional part of the floating point number _float_.
```wlambda
std:assert ((std:num:fract 4.25) - 0.25) < 0.00001
```
### <a name="37-numeric-functions"></a>3.7 - Numeric Functions
These functions work for all types of numbers.
#### <a name="371-stdnumabs-number"></a>3.7.1 - std:num:abs _number_
Takes the absolute value of _number_. If _number_ is not a number
it will be converted into an integer.
```wlambda
std:assert_eq (std:num:abs -10) 10;
std:assert_eq (std:num:abs -13.3) 13.3;
```
#### <a name="372-stdnumsignum-number"></a>3.7.2 - std:num:signum _number_
Returns either 1 or -1, depending on the sign of the given _number_.
```wlambda
std:assert_eq (std:num:signum -4) -1;
std:assert_eq (std:num:signum 4) 1;
std:assert_eq (std:num:signum -4.0) -1.0;
std:assert_eq (std:num:signum 4.0) 1.0;
```
#### <a name="373-stdnuminttoclosedopen01-integer"></a>3.7.3 - std:num:int\_to\_closed\_open01 _integer_
Transforms the given 64-Bit _integer_ into a number in the range `0.0` to `1.0`.
Inclusive `0.0`, exclusive `1.0`. This function is mainly useful if you generated
the integer from a random number generator.
```wlambda
std:assert_rel_eq (std:num:int_to_closed_open01 0) 0.0 0.00000001;
std:assert_rel_eq (std:num:int_to_closed_open01 -1) 0.999999999 0.00000001;
```
#### <a name="374-stdnuminttoopen01-integer"></a>3.7.4 - std:num:int\_to\_open01 _integer_
Transforms the given 64-Bit _integer_ into a number in the range `0.0` to `1.0`.
Exclusive `0.0`, exclusive `1.0`. This function is mainly useful if you generated
the integer from a random number generator.
```wlambda
std:assert (std:num:int_to_open01 0) > 0.0;
std:assert (std:num:int_to_open01 -1) < 1.0;
```
#### <a name="375-stdnuminttoopenclosed01-integer"></a>3.7.5 - std:num:int\_to\_open\_closed01 _integer_
Transforms the given 64-Bit _integer_ into a number in the range `0.0` to `1.0`.
Inclusive `0.0`, inclusive `1.0`. This function is mainly useful if you generated
the integer from a random number generator.
```wlambda
std:assert (std:num:int_to_open_closed01 0) > 0.0;
std:assert (std:num:int_to_open_closed01 -1) == 1.0;
```
### <a name="38-numerical-mathematical-vectors"></a>3.8 - Numerical Mathematical Vectors
In order to aid in the development of GUIs, games, and other physics/geometry adjacent software,
WLambda comes with a built in datatype for mathematical vectors, which can contain floats and integers
and have between two and four dimensions.
```wlambda
# integer vectors
std:assert ~ $i(-1, 2).y == 2;
std:assert ~ (ivec ${z=3}) == $i(0,0,3);
std:assert ~ (ivec4 $[]) == $i(0,0,0,0);
std:assert ~ $i(1.49, -2.72) == $i(1,-2);
# float vectors
std:assert ~ $f(1.00, -33).x == $f(1, 200).first;
std:assert ~ $f(-0, 2.4).y == $f(1.6, 2.4).second;
std:assert ~ (fvec3 ${w=0.1}) == $f(0,0,0);
# conversion
std:assert ~ (fvec3 $i(1, 2))/10 == $f(0.1, 0.2, 0);
std:assert ~ (ivec2 $f(1.3, 2.7, -5.8)) == $i(1, 2);
std:assert ~ (ivec $f(1.3, 2.7, -5.8)) == $i(1, 2, -5);
```
#### <a name="381-vector-conversions"></a>3.8.1 - Vector Conversions
There are eight functions for converting other values into vectors
and vectors of integers into vectors of floats:
- `ivec`
- `ivec2`
- `ivec3`
- `ivec4`
- `fvec`
- `fvec2`
- `fvec3`
- `fvec4`
The functions without a dimension suffix fill in as many dimensions
as are present in the object being converted.
The functions with dimension suffixes fill in any missing dimensions
with `0`s and ignore dimensions as necessary.
NOTE: `ivec` will always truncate (i.e. round down) floats into integers when converting,
just like when converting floats into integers implicitly elsewhere in WLambda.
#### <a name="382-vector-component-access"></a>3.8.2 - Vector Component Access
There are 12 functions for accessing the components of vectors,
but only four have unique behavior (the rest are aliases).
- `x`/`r`/`h`/`0`/`first`,
- `y`/`g`/`s`/`1`/`second`,
- `z`/`b`/`v`/`2`/`third`,
- `w`/`3`/`fourth`
```wlambda
!my_vec = $f(39.3, 404.504, 333.8);
std:assert_eq my_vec.x my_vec.0;
std:assert_eq my_vec.x my_vec.first;
std:assert_eq my_vec.y my_vec.1;
std:assert_eq my_vec.y my_vec.second;
std:assert_eq my_vec.z my_vec.2;
std:assert_eq my_vec.z my_vec.third;
std:assert_eq my_vec.w my_vec.3;
std:assert_eq my_vec.w my_vec.fourth;
```
#### <a name="383-named-field-access-and-swizzling"></a>3.8.3 - Named Field Access and Swizzling
You can access the fields of numeric vectors with different keys:
```wlambda
std:assert_eq $i(2,3,4,5).x 2;
std:assert_eq $i(2,3,4,5).y 3;
std:assert_eq $i(2,3,4,5).z 4;
std:assert_eq $i(2,3,4,5).w 5;
std:assert_eq $i(5,6,7,8).r 5;
std:assert_eq $i(5,6,7,8).g 6;
std:assert_eq $i(5,6,7,8).b 7;
std:assert_eq $i(5,6,7,8).a 8;
std:assert_eq $i(5,6,7,8).h 5;
std:assert_eq $i(5,6,7,8).s 6;
std:assert_eq $i(5,6,7,8).v 7;
std:assert_eq $i(5,6,7,8).a 8;
std:assert_eq $i(5,6,7,8).0 5;
std:assert_eq $i(5,6,7,8).1 6;
std:assert_eq $i(5,6,7,8).2 7;
std:assert_eq $i(5,6,7,8).3 8;
```
You can also use **swizzling** to quickly make a new vector:
```wlambda
std:assert_eq $i(2,3,4).xx $i(2,2);
std:assert_eq $i(2,3,4).xyxz $i(2,3,2,4);
std:assert_eq $i(2,3,4).bgr $i(4,3,2);
std:assert_eq $i(2,3).xyrg $i(2,3,2,3);
std:assert_eq $i(2,3,4,5).zw $i(4,5);
```
#### <a name="384-euler-additionsubtraction"></a>3.8.4 - Euler Addition/Subtraction
You can add vectors to each other and subtract them from each other.
The type of the resulting vector will be the same as the vector on the left.
The number of dimensions in the resulting vector will be the same as the vector
with the highest number of dimensions that was involved in the operation.
If the value on the right isn't a vector, it will be converted into one,
just as if it were passed through `ivec` or `fvec`, meaning that as many
dimensions are kept as are present.
```wlambda
std:assert_eq[ $i(0.1, 0.9) + $i(1, 0) , $i(1, 0) ];
std:assert_eq[ $f(0.1, 0.9) + $i(1, 0) , $f(1.1, 0.9) ];
std:assert_eq[ $f(0.1, 0.9) + ${ w=7 } , $f(0.1, 0.9, 0, 7) ];
std:assert_eq[ std:v:mag2 $i(-1, 5) + $i(1, -5) , 0.0 ];
```
#### <a name="385-scalar-multiplicationdivision"></a>3.8.5 - Scalar Multiplication/Division
You can multiply and divide integer and float vectors by single numbers.
This copies the vector, multiplies or divides each component of the vector by the single number,
and returns the result.
NOTE: Dividing `ivec`s will always truncate (i.e. round down) floats into integers.
```wlambda
std:assert ~ $i(3, 6)/2 == $i(1, 3);
std:assert ~ $f(3, 6)/2 == $f(1.5, 3);
std:assert ~ $f(0.5, 0) * 1.3 == $f(0.65,0);
std:assert ~ (std:v:mag (std:v:norm $[40.19, 0.399]) * 10) == 10.0;
```
#### <a name="386-unary-vector-operations"></a>3.8.6 - Unary Vector Operations
Calling `-` on a vector returns a new vector with all of its fields negated.
This is equivalent to multiplying the vector by `-1`.
Calling `+` on a vector returns a copy of the exact same vector.
This is equivalent to multiplying the vector by `1`.
```wlambda
!my_vec = $f(1.2, 2.3, 3.4);
std:assert_eq (ivec (-my_vec)) $i(-1, -2, -3);
std:assert_eq (+my_vec) my_vec;
# adding something to its inverse yields all 0s
std:assert_eq[ my_vec + (-my_vec), my_vec * 0 ];
```
#### <a name="387-isfvec-value"></a>3.8.7 - is\_fvec _value_
Returns `$true` if _value_ is a float vector.
```wlambda
std:assert_eq (is_fvec $f(1,2)) $true;
std:assert_eq (is_fvec $f(1,2,3)) $true;
std:assert_eq (is_fvec $f(1,2,3,4)) $true;
std:assert_eq (is_fvec $none) $false;
std:assert_eq (is_fvec $i(1,2)) $false;
std:assert_eq (is_fvec $[3.4, 4.5]) $false;
std:assert_eq (is_fvec fvec <& $[3.4, 4.5]) $true;
# References are not dereferenced:
std:assert_eq (is_fvec $&&$f(3,4)) $false;
std:assert_eq (is_fvec $*$&&$f(3,4)) $true;
```
#### <a name="388-isivec-value"></a>3.8.8 - is\_ivec _value_
Returns `$true` if _value_ is an integer vector.
```wlambda
std:assert_eq (is_ivec $i(1,2)) $true;
std:assert_eq (is_ivec $i(1,2,3)) $true;
std:assert_eq (is_ivec $i(1,2,3,4)) $true;
std:assert_eq (is_ivec $none) $false;
std:assert_eq (is_ivec $[3, 4]) $false;
std:assert_eq (is_ivec ivec <& $[3.4, 4.5]) $true;
# References are not dereferenced:
std:assert_eq (is_ivec $&& $i(3,4)) $false;
std:assert_eq (is_ivec $* $&& $i(3,4)) $true;
```
#### <a name="389-isnvec-value"></a>3.8.9 - is\_nvec _value_
Returns `$true` if _value_ is either a numerical float or integer vector.
```wlambda
std:assert_eq (is_nvec $i(1,2)) $true;
std:assert_eq (is_nvec $i(1,2,3)) $true;
std:assert_eq (is_nvec $i(1,2,3,4)) $true;
std:assert_eq (is_nvec $f(1,2)) $true;
std:assert_eq (is_nvec $f(1,2,3)) $true;
std:assert_eq (is_nvec $f(1,2,3,4)) $true;
std:assert_eq (is_nvec $none) $false;
std:assert_eq (is_nvec $[3, 4]) $false;
std:assert_eq (is_nvec fvec <& $[3.4, 4.5]) $true;
std:assert_eq (is_nvec ivec <& $[3.4, 4.5]) $true;
```
#### <a name="3810-nveclen-value"></a>3.8.10 - nvec\_len _value_
Returns the length of a numerical vector, commonly known as the dimension.
Either 2, 3 or 4.
```wlambda
std:assert_eq (nvec_len $i(1, 2)) 2;
std:assert_eq (nvec_len $i(1, 2, 3)) 3;
std:assert_eq (nvec_len $i(1, 2, 3, 4)) 4;
std:assert_eq (nvec_len $f(1.1, 2.2)) 2;
std:assert_eq (nvec_len $f(1.1, 2.2, 3.3)) 3;
std:assert_eq (nvec_len $f(1.1, 2.2, 3.3, 4.4)) 4;
```
#### <a name="3811-fvec-value"></a>3.8.11 - fvec _value_
Will cast _value_ into a float vector. You can cast a multitude of data types
into a float vector:
```wlambda
std:assert_eq (fvec $[1,2,3,4]) $f(1,2,3,4);
std:assert_eq (fvec $[1,2,3]) $f(1,2,3);
std:assert_eq (fvec $[1,2]) $f(1,2);
std:assert_eq (fvec $i(1,2)) $f(1,2);
std:assert_eq (fvec $i(1,2,3)) $f(1,2,3);
std:assert_eq (fvec $i(1,2,3,4)) $f(1,2,3,4);
std:assert_eq (fvec $p("2", "3.4")) $f(2,3.4);
!i = $iter $[] +> $p(3,4) +> $[5,6];
std:assert_eq (fvec i) $f(3,4);
std:assert_eq (fvec i) $f(5,6);
std:assert_eq (fvec ${x = 1, y = 2}) $f(1,2);
std:assert_eq (fvec ${x = 1, y = 2, z = 3}) $f(1,2,3);
std:assert_eq (fvec ${x = 1, y = 2, z = 3, w = 4}) $f(1,2,3,4);
```
#### <a name="3812-fvec2-value"></a>3.8.12 - fvec2 _value_
Like `fvec` but always returns a 2 dimensional vector.
```wlambda
std:assert_eq (fvec2 $i(3,4,5)) $f(3,4);
std:assert_eq (fvec2 $[4,5,6,7,8]) $f(4,5);
std:assert_eq (fvec2 ${x = 1, y = 2, z = 3, w = 4}) $f(1,2);
```
#### <a name="3813-fvec3-value"></a>3.8.13 - fvec3 _value_
Like `fvec` but always returns a 3 dimensional vector.
```wlambda
std:assert_eq (fvec3 $i(3,4,5)) $f(3,4,5);
std:assert_eq (fvec3 $[4,5,6,7,8]) $f(4,5,6);
std:assert_eq (fvec3 ${x = 1, y = 2, z = 3, w = 4}) $f(1,2,3);
```
#### <a name="3814-fvec4-value"></a>3.8.14 - fvec4 _value_
Like `fvec` but always returns a 4 dimensional vector.
```wlambda
std:assert_eq (fvec4 $i(3,4,5)) $f(3,4,5,0);
std:assert_eq (fvec4 $[4,5,6,7,8]) $f(4,5,6,7);
std:assert_eq (fvec4 ${x = 1, y = 2, z = 3, w = 4}) $f(1,2,3,4);
```
#### <a name="3815-ivec-value"></a>3.8.15 - ivec _value_
Will cast _value_ into a float vector. You can cast a multitude of data types
into a float vector:
```wlambda
std:assert_eq (ivec $[1,2,3,4]) $i(1,2,3,4);
std:assert_eq (ivec $[1,2,3]) $i(1,2,3);
std:assert_eq (ivec $[1,2]) $i(1,2);
std:assert_eq (ivec $f(1.1,2.1)) $i(1,2);
std:assert_eq (ivec $f(1.1,2.1,3.2)) $i(1,2,3);
std:assert_eq (ivec $f(1.1,2.1,3.2,4)) $i(1,2,3,4);
std:assert_eq (ivec $p("2", "3")) $i(2,3);
!i = $iter $[] +> $p(3,4) +> $[5,6];
std:assert_eq (ivec i) $i(3,4);
std:assert_eq (ivec i) $i(5,6);
std:assert_eq (ivec ${x = 1, y = 2}) $i(1,2);
std:assert_eq (ivec ${x = 1, y = 2, z = 3}) $i(1,2,3);
std:assert_eq (ivec ${x = 1, y = 2, z = 3, w = 4}) $i(1,2,3,4);
```
#### <a name="3816-ivec2-value"></a>3.8.16 - ivec2 _value_
Like `ivec` but always returns a 2 dimensional vector.
```wlambda
std:assert_eq (ivec2 $f(3,4,5)) $i(3,4);
std:assert_eq (ivec2 $[4,5,6,7,8]) $i(4,5);
std:assert_eq (ivec2 ${x = 1, y = 2, z = 3, w = 4}) $i(1,2);
```
#### <a name="3817-ivec3-value"></a>3.8.17 - ivec3 _value_
Like `ivec` but always returns a 3 dimensional vector.
```wlambda
std:assert_eq (ivec3 $f(3,4,5)) $i(3,4,5);
std:assert_eq (ivec3 $[4,5,6,7,8]) $i(4,5,6);
std:assert_eq (ivec3 ${x = 1, y = 2, z = 3, w = 4}) $i(1,2,3);
```
#### <a name="3818-ivec4-value"></a>3.8.18 - ivec4 _value_
Like `ivec` but always returns a 4 dimensional vector.
```wlambda
std:assert_eq (ivec4 $f(3,4,5)) $i(3,4,5,0);
std:assert_eq (ivec4 $[4,5,6,7,8]) $i(4,5,6,7);
std:assert_eq (ivec4 ${x = 1, y = 2, z = 3, w = 4}) $i(1,2,3,4);
```
#### <a name="3819-stdvdims-vec"></a>3.8.19 - std:v:dims _vec_
You can use this function to retrieve the number of dimensions in _vec_.
Like most other std:v functions,
it will coerce whatever value is passed into it into a `ivec`,
if that value is not a `fvec`.
This function always returns an integer, regardless of whether an `ivec` or `fvec` is passed in.
```wlambda
# the least number of dimensions a vector can have is 2.
std:assert_eq (std:v:dims $[]) 2;
# while the most is 4.
std:assert_eq (std:v:dims ${w=0}) 4;
std:assert_eq (std:v:dims $f(1,2)) (std:v:dims $i(1,2));
```
#### <a name="3820-stdvmag2-vec"></a>3.8.20 - std:v:mag2 _vec_
Returns the magnitude of _vec_, squared.
Calculating the squared magnitude is a little bit faster,
so you should prefer this method where performance is paramount.
The magnitude is always a float, regardless of whether the parameter is an `ivec` or `fvec`.
```wlambda
std:assert_eq (std:v:mag2 ${w=4}) 16.0;
```
#### <a name="3821-stdvmag-vec"></a>3.8.21 - std:v:mag _vec_
Returns the magnitude (also known as the length) of _vec_.
The magnitude is always a float, regardless of whether the parameter is an `ivec` or `fvec`.
```wlambda
std:assert_eq (std:v:mag ${w=4}) 4.0;
```
#### <a name="3822-stdvnorm-vec"></a>3.8.22 - std:v:norm _vec_
Returns a new vector which has a magnitude of `1`, but points in the same direction as _vec_.
Vectors with a length of one are also known as unit vectors.
Note that this still returns an `ivec` when used on `ivec`s,
meaning that when used on an `ivec2` only four values are possible:
- `$i(1, 0)`
- `$i(-1, 0)`
- `$i(0, 1)`
- `$i(0, -1)`
These are the only `ivec2`s that have a length of `1`.
```wlambda
!p1 = fvec ${ x = 20, y = 30.5 };
!p2 = fvec ${ x = -10, y = 0.5 };
# get the delta representing how far you'd have to travel to get from p1 to p2
!delta = p2 - p1;
# the normalized delta represents a single 1 sized step you could take to get to p2 from p1.
!n = std:v:norm delta;
# the length of this step is reflected in the magnitude of the vectors
std:assert_eq[ (std:v:mag delta) - 1, std:v:mag (p1 + n) - p2 ];
```
#### <a name="3823-stdvdot-vec1-vec2"></a>3.8.23 - std:v:dot _vec1_ _vec2_
Returns the sum of all components after multiplying each component
in _vec1_ with the corresponding component of _vec2_.
This can be used to represent the "sameness" of two vectors (especially unit vectors):
the degree to which they are pointing in the same direction.
Returns an integer when used on an `ivec`, and a float when used on an `fvec`.
If _vec1_ is an `fvec`, then _vec2_ will also be coerced into one.
If _vec1_ isn't an `fvec`, then it's coerced into an `ivec`, just like the other `std:v` functions.
```wlambda
!at = fvec ${ x = 20 , y = 30.5 }; # where you're at
!goal = fvec ${ x = -10, y = 0.5 }; # where you want to look
!looking = std:v:rad2vec (std:num:to_radians 90); # direction you're looking in
# do you need to turn left or right to look at `goal`,
# if you're standing at `at` looking in `looking`?
# find the unit vector representing the space between where you want to look and where you're at.
!delta = std:v:norm goal - at;
# the direction you need to turn in can be found by checking the sign of
# the dot product of where you're currently looking and where you're at.
!dir = std:v:dot delta looking;
std:assert_eq[ (dir < 0) "left" "right", "left" ];
```
#### <a name="3824-stdvcross-vec1-vec2"></a>3.8.24 - std:v:cross _vec1_ _vec2_
Returns a vector perpendicular to _vec1_ and _vec2_.
Similar to the dot product, but instead of returning a single value it returns another vector,
and is only useful in three (and seven, but WLambda's vectors don't support so many) dimensions.
Regardless of the number of dimensions in the input vectors, this function will return a 3d vector.
```wlambda
!x = fvec ${x=1};
!y = fvec ${y=1};
# the cross product of these two values will represent the third axis, z, and will be
# perpendicular to both other vectors.
!z = std:v:cross x y;
std:assert_eq z (fvec ${z=1});
# because all three vectors are perpindicular, they'll all have the same dot product from each other.
std:assert_eq[(std:v:dot x y), (std:v:dot y z)];
std:assert_eq[(std:v:dot y z), (std:v:dot z x)];
```
#### <a name="3825-stdvlerp-vec1-vec2-t"></a>3.8.25 - std:v:lerp _vec1_ _vec2_ _t_
`lerp` stands for linear interpolation.
This function is useful when animating positions, whereas slerp is useful for animating rotations.
Creates a new vector in a new position relative to _vec1_ and _vec2_.
Aside from the two reference vectors, this function also takes a variable, _t_,
which represents how far relative to the first and second vector the new vector should be.
If _t_ is `0`, _vec1_ is returned. If _t_ is `1`, then _vec2_ is returned.
If _t_ is `0.5`, the resulting vector will be halfway in between the first and second vector.
```wlambda
std:assert_eq[ std:v:lerp $f(1,0) $f(0,1) 0.5 , $f(0.5, 0.5) ];
std:assert_eq[ std:v:lerp $f(5,10) ${y=10} 0.75 , $f(1.25, 10) ];
std:assert_eq[ std:v:lerp $[-2,5] $[2,-5] 0.5 , $i(0, 0) ];
!a = $f(83, -49.5);
std:assert_eq[ (std:v:mag a) / 2 , std:v:mag (std:v:lerp a $[] 0.5) ];
std:assert_eq[ (std:v:mag a) * 2 , std:v:mag (std:v:lerp $f(0,0) a 2.0) ];
!b = $f(-484.58, -19);
std:assert_eq[ std:v:lerp b a 1.5 , std:v:lerp a b -0.5 ];
```
#### <a name="3826-stdvslerp-vec1-vec2-t"></a>3.8.26 - std:v:slerp _vec1_ _vec2_ _t_
`slerp` stands for spherical linear interpolation.
This function is useful when animating rotations, whereas lerp is useful for animating positions.
In most cases, you'll want to pass in unit vectors representing rotations to slerp.
You should get back unit vectors in the vast majority of cases,
but if perfect accuracy is required normalizing the output of this function is suggested.
Creates a new vector in a new position relative to _vec1_ and _vec2_.
Aside from the two reference vectors, this function also takes a variable, _t_,
which represents how far relative to the first and second vector the new vector should be.
If _t_ is `0`, _vec1_ is returned. If _t_ is `1`, then _vec2_ is returned.
If _t_ is `0.5`, the resulting vector will be halfway in between _vec1_ and _vec2_.
```wlambda
# compare this to the one for std:v:lerp! note that the length of this one is almost 1.
# this is definitely not the case for std:v:lerp's output with the same input.
!v = std:v:slerp $f(1,0) $f(0,1) 0.5;
# the values may not be exact because of floating point rounding errors,
# but they should be pretty close.
std:assert_rel_eq v.x 0.7071067811865476 0.000001;
std:assert_rel_eq v.y 0.7071067811865476 0.000001;
# The values are interpolated around a circle, so if you raise t high enough you'll start
# getting the same values as you get with a lower t, although not quite because of float rounding.
!half = (std:v:slerp $f(1,0) $f(0,1) 0.5);
!four = (std:v:slerp $f(1,0) $f(0,1) 4.5);
std:assert_rel_eq half.x four.x 0.000001;
std:assert_rel_eq half.y four.y 0.000001;
```
#### <a name="3827-stdvvec2rad-vec"></a>3.8.27 - std:v:vec2rad _vec_
Creates a rotation in radians from the x and y components of _vec_.
Always returns a float.
Coerces the argument into an `ivec` unless it's a `fvec`.
```wlambda
std:assert_eq[ std:num:to_degrees (std:v:vec2rad ${x=1}) , 0.0 ];
std:assert_eq[ std:num:to_degrees (std:v:vec2rad ${y=1}) , 90.0 ];
# halfway in between 0.0 and 90.0 should be 45.
# note that lerp would work here as well
!h = std:v:slerp $f(1, 0) $f(0, 1) 0.5;
std:assert_eq[ std:num:to_degrees (std:v:vec2rad h) , 45.0 ];
```
#### <a name="3828-stdvrad2vec-radians"></a>3.8.28 - std:v:rad2vec _radians_
Creates a unit vector from _radians_.
Always returns an `fvec`.
```wlambda
std:assert_eq[ std:v:rad2vec (std:num:to_radians 0.0) , $f(1, 0)];
std:assert_eq[ ivec (std:v:rad2vec (std:num:to_radians 90.0)), $i(0, 1)];
# halfway in between 0.0 and 90.0 should be 45.
# note that lerp would NOT work here as well, rad2vec returns a unit vector.
!h = std:v:slerp $f(1, 0) $f(0, 1) 0.5; # slerp because rotations
!r = std:v:rad2vec (std:num:to_radians 45.0);
std:assert_rel_eq r.x h.x 0.0001;
std:assert_rel_eq r.y h.y 0.0001;
```
### <a name="39-characters-and-bytes"></a>3.9 - Characters and Bytes
WLambda has a data type for single characters and bytes. The lexical syntax is
a character or escape sequence delimited by `'`:
```wlambda
std:assert_eq (type 'a') "char";
std:assert_eq (type $b'a') "byte";
std:assert_eq (type '\u{40}') "char";
std:assert_eq (type $b'\x40') "byte";
# You can use the unicode escapes up to the first 256 code points in bytes too:
std:assert_eq (char $b'\u{40}') '\u{40}';
# Beyond that, you will get the byte '?':
std:assert_eq (char $b'\u{3131}') '?';
```
The can be used interchangeably almost everywhere. They can often also be used
instead of a string, because they are handled like a single character long string.
They are useful because they do not require an extra allocation in the background.
They are not boxed like strings:
```wlambda
std:assert_eq ("foo" $p(0, 1)) "f"; # requires allocation
std:assert_eq ("foo".0) 'f'; # requires NO allocation
```
#### <a name="391-byte-value"></a>3.9.1 - byte _value_
Converts the _value_ to a byte. If _value_ is a number, it must be
below or equal to 255, otherwise it will result in the byte `'?'`.
```wlambda
std:assert_eq (byte 64) $b'@';
std:assert_eq (byte 300) $b'?';
std:assert_eq (byte "ABC") $b'A';
std:assert_eq (byte $b"\xFF\xF0") $b'\xFF';
std:assert_eq (byte "\xFF\xF0") $b'\xC3'; # first byte of an utf-8 sequence!
```
#### <a name="392-char-value"></a>3.9.2 - char _value_
Converts the _value_ to a Unicode character.
```wlambda
std:assert_eq (char $b'\xFF') 'ÿ';
std:assert_eq (char 0x262F) '☯';
std:assert_eq (char "☯xyz") '☯';
```
#### <a name="393-isbyte-value"></a>3.9.3 - is\_byte _value_
Checks if _value_ is of the byte data type.
```wlambda
std:assert (is_byte $b'X');
std:assert not[is_byte 'X'];
std:assert not[is_byte 123];
std:assert (is_byte $b"abc".0);
std:assert not[is_byte "abc".0];
std:assert not[is_byte $b"abc"];
```
#### <a name="394-ischar-value"></a>3.9.4 - is\_char _value_
Check if _value_ is of the Unicode character data type.
```wlambda
std:assert (is_char 'X');
std:assert not[is_char $b'X'];
std:assert not[is_char 123];
std:assert (is_char "abc".0);
std:assert not[is_char $b"abc".0];
std:assert not[is_char $b"abc"];
std:assert not[is_char "abc"];
```
#### <a name="395-stdchartolowercase-value"></a>3.9.5 - std:char:to\_lowercase _value_
Turns the _value_ into a lower case Unicode character.
```wlambda
std:assert_eq (std:char:to_lowercase 'A') 'a';
std:assert_eq (std:char:to_lowercase 65) 'a';
```
#### <a name="396-stdchartouppercase-value"></a>3.9.6 - std:char:to\_uppercase _value_
Turns the _value_ into an upper case Unicode character.
```wlambda
std:assert_eq (std:char:to_uppercase 'a') 'A';
std:assert_eq (std:char:to_uppercase 97) 'A';
```
### <a name="310-strings"></a>3.10 - Strings
Strings in WLambda are like Rust UTF-8 encoded immutable Unicode strings.
There are two types of literal forms for strings:
```wlambda
"abc def \"foo\"";
std:assert_eq $q/any delimiter may be used instead of/
"any delimiter may be used instead of";
# Unicode escapes are also working:
std:assert_eq "\u{2211}" "∑";
```
#### <a name="3101-string-literal-syntaxes"></a>3.10.1 - String Literal Syntaxes
There are multiple kinds of syntax constructs you can use to
notate string (and byte vector) literals:
- Regular strings
```wlambda
!s = "a b c";
std:assert_eq s "a b c";
```
- Byte vectors `$b"\x02FOO\x03"`
- Quoted strings `$q(123433)`
- Quoted byte vectors `$Q(XZY)`
- WLambda code strings
```wlambda
# Short form $c works too.
!code = $code {
!this = is a block;
It just needs to be in valid WLambda[:Syntax];
.x = But it does not need to pass the compiler
phase.x;
};
# Primary use case is `eval` and `std:thread:spawn`:
!v = (std:thread:spawn $code {
!@import std std;
!res = "x" "y" "z";
std:str:cat res 33;
}).join[];
std:assert_eq v "xyz33";
```
#### <a name="3102-str-value"></a>3.10.2 - str _value_
Casts _value_ to a string and returns it.
Also dereferences a value.
```wlambda
std:assert_eq (str "\xFF") "ÿ";
std:assert_eq (str "\x0A") "\n";
std:assert_eq (str 1) "1";
std:assert_eq (str $n) "";
std:assert_eq (str $t) "$true";
std:assert_eq (str $f) "$false";
std:assert_eq (str $&10) "10";
std:assert_eq (str $&&10) "10";
std:assert_eq (str ${a=10}) "${a=10}";
std:assert_eq (str $[1,2,3]) "$[1,2,3]";
std:assert_eq (str $o(42)) "42";
std:assert_eq (str $o()) "";
!x = $&&10;
std:assert_eq (str ~ std:ref:weaken x) "10";
```
#### <a name="3103-stdwritestr-value"></a>3.10.3 - std:write\_str _value_
Writes a WLambda syntax representation of the given _value_ to a string.
This is useful for debugging purposes or in combination with `std:eval`.
```wlambda
std:assert_eq (std:write_str "foo") "\"foo\"";
std:assert_eq (std:write_str $&&10) "$&&10";
std:assert_eq (std:write_str $[1, 2, 3]) "$[1,2,3]"
```
Here an example in combination with `std:eval`:
```wlambda
!code =
std:str:cat
"$@i iter i "
(std:write_str $[1, 2, 3, 4])
" { $+ i }";
std:assert_eq (std:eval code) 10;
```
#### <a name="3104-isstr-value"></a>3.10.4 - is\_str _value_
Returns `$true` if _value_ is a string.
```wlambda
std:assert ~ is_str "foo";
std:assert ~ not ~ is_str $b"foo";
std:assert ~ not ~ is_str :foo;
std:assert ~ not ~ is_str 324;
std:assert ~ not ~ is_str $&&"foo";
std:assert ~ is_str $*$&&"foo";
```
#### <a name="3105-stdstrcat-a-b-"></a>3.10.5 - std:str:cat _a_ _b_ ...
Stringifies (like with `str`) and concatenates all its arguments.
If an argument is a vector, it's elements will be stringified and concatenated.
```wlambda
std:assert_eq
(std:str:cat :a 10 23.2 "ab" "cd" $[1, 2, 3])
"a1023.2abcd123";
```
If a vector argument is given, it's elements are stringified, thats
useful if you prepare substrings to be concatenated in one single action:
```wlambda
!out = $[];
std:push out "abc";
std:push out "123";
std:push out "XXX";
!s = std:str:cat out;
std:assert_eq s "abc123XXX";
```
#### <a name="3106-stdstrjoin-sep-vector"></a>3.10.6 - std:str:join _sep_ _vector_
Join's the stringified elements of _vector_ with the _sep_ string.
Will return an error if _vector_ is not a vector.
```wlambda
std:assert_eq
(std:str:join "::" $[1,2,3])
"1::2::3";
```
#### <a name="3107-stdstrlen-value"></a>3.10.7 - std:str:len _value_
Returns the length of the stringified _value_ in unicode characters.
The core function `len` does return the number of bytes in the string
instead.
```wlambda
std:assert_eq (len "∑") 3;
std:assert_eq (std:str:len "∑") 1;
std:assert_eq (len "∑ÄÄ") 7;
std:assert_eq (std:str:len "∑ÄÄ") 3;
std:assert_eq (len "abcd") 4;
std:assert_eq (std:str:len "abcd") 4;
```
#### <a name="3108-stdstrfind-pattern-string-offset"></a>3.10.8 - std:str:find _pattern_ _string_ [_offset_]
Searches for the string _pattern_ in the _string_ and returns the 0 based position
in the string the given _pattern_ starts.
If no pattern was found `$none` is returned.
```wlambda
std:assert_eq (std:str:find "xyz" "abcxyz") 3;
std:assert_eq (std:str:find "xyz" "abcxyzxyz" 6) 6;
std:assert_eq (std:str:find "xyz" "abcxyzfooxyz" 6) 9;
```
#### <a name="3109-stdstrreplace-pattern-replacement-string"></a>3.10.9 - std:str:replace _pattern_ _replacement_ _string_
Replaces every occurence of _pattern_ in _string_ with _replacement_
and returns a new string. All values will be casted to a string if
they aren't.
```wlambda
!s = std:str:replace "dog" "cat"
"I really like my dog, because when you dog, you can put dog in the dog!";
std:assert_eq s
"I really like my cat, because when you cat, you can put cat in the cat!";
!s = std:str:replace "9" "1" "9999";
std:assert_eq s "1111";
```
#### <a name="31010-stdstrreplacen-pattern-replacement-count-string"></a>3.10.10 - std:str:replace\_n _pattern_ _replacement_ _count_ _string_
Replaces _count_ occurences of _pattern_ in _string_ with _replacement_
and returns a new string. All values will be casted to a string if
they aren't.
```wlambda
!s = std:str:replace_n "dog" "cat" 2
"I really like my dog, because when you dog, you can put dog in the dog!";
std:assert_eq s
"I really like my cat, because when you cat, you can put dog in the dog!";
!s = std:str:replace_n "9" "1" 3 "9999";
std:assert_eq s "1119";
```
#### <a name="31011-stdstrtrim-value"></a>3.10.11 - std:str:trim _value_
Trims off any (unicode) white space from the start and end of the
stringified _value_.
```wlambda
std:assert_eq
(std:str:trim "\nfooo bar ")
"fooo bar";
```
#### <a name="31012-stdstrtrimstart-value"></a>3.10.12 - std:str:trim\_start _value_
Trims off any (unicode) white space from the start of the stringified _value_.
```wlambda
std:assert_eq
(std:str:trim_start " \nfooo bar \n")
"fooo bar \n";
```
#### <a name="31013-stdstrtrimend-value"></a>3.10.13 - std:str:trim\_end _value_
Trims off any (unicode) white space from the end of the stringified _value_.
```wlambda
std:assert_eq
(std:str:trim_end " \nfooo bar \n")
" \nfooo bar";
```
#### <a name="31014-stdstrpadstart-len-pad-str-value"></a>3.10.14 - std:str:pad\_start _len_ _pad-str_ _value_
Pads the stringified _value_ by _pad-str_ up to _len_ characters, inserting
at the start of the string.
The output string is guaranteed to be exactly _len_ unicode characters
long and not longer. If _pad-str_ is empty, nothing is done.
```wlambda
std:assert_eq
(std:str:pad_start 2 "Ä" "0")
"Ä0";
std:assert_eq
(std:str:pad_start 5 "∑∑∑" "∑∑")
"∑∑∑∑∑";
std:assert_eq
(std:str:pad_start 8 "Ä∑ßs" "∑∑")
"ßsÄ∑ßs∑∑";
# Empty _pad-str_ is not an error but a nop:
std:assert_eq
(std:str:pad_start 8 "" "∑∑")
"∑∑";
# also works with characters
std:assert_eq
(std:str:pad_start 3 'x' "0")
"xx0";
# also works with bytes
std:assert_eq
(std:str:pad_start 3 $b'x' "0")
"xx0";
```
#### <a name="31015-stdstrpadend-len-pad-str-value"></a>3.10.15 - std:str:pad\_end _len_ _pad-str_ _value_
Pads the stringified _value_ by _pad-str_ up to _len_ characters,
appending at the end.
The output string is guaranteed to be exactly _len_ unicode characters
long and not longer. If _pad-str_ is empty, nothing is done.
```wlambda
std:assert_eq
(std:str:pad_end 2 "Ä" "0")
"0Ä";
std:assert_eq
(std:str:pad_end 5 "∑∑∑" "∑∑")
"∑∑∑∑∑";
std:assert_eq
(std:str:pad_end 8 "Ä∑ßs" "∑∑")
"∑∑Ä∑ßsÄ∑";
# Empty _pad-str_ is not an error but a nop:
std:assert_eq
(std:str:pad_end 8 "" "∑∑")
"∑∑";
# also works with characters
std:assert_eq
(std:str:pad_end 3 'x' "0")
"0xx";
# also works with bytes
std:assert_eq
(std:str:pad_end 3 $b'x' "0")
"0xx";
```
#### <a name="31016-stdstrtobytes-string"></a>3.10.16 - std:str:to\_bytes _string_
Encodes _string_ in UTF-8 and returns a byte vector containing all it's bytes.
```wlambda
!b = std:str:to_bytes "1234";
std:assert_eq b $b"1234";
!b = std:str:to_bytes "Äß日本人";
std:assert_eq b $b"\xC3\x84\xC3\x9F\xE6\x97\xA5\xE6\x9C\xAC\xE4\xBA\xBA";
```
#### <a name="31017-stdstrtobyteslatin1-string"></a>3.10.17 - std:str:to\_bytes\_latin1 _string_
Encodes _string_ as bytes in Latin1 (ISO-8859-1) encoding and returns
a byte vector containing all it's bytes. If a character is outside the Latin1 Unicode
range, it will be replaced by a "?".
```wlambda
!b = std:str:to_bytes_latin1 "\u{FF}\u{F0}";
std:assert_eq b $b"\xFF\xF0";
!b = std:str:to_bytes_latin1 "\u{FE00}\u{FF}\u{3232}";
std:assert_eq b $b"?\xFF?";
```
#### <a name="31018-stdstrfromlatin1-byte-vector"></a>3.10.18 - std:str:from\_latin1 _byte-vector_
Converts the _byte-vector_ to a Unicode string, assuming Latin 1 (ISO-8859-1) encoding
and returns it.
```wlambda
!s = std:str:from_latin1 $b"Ä";
std:assert_eq s "\u{C3}\u{84}";
!s = std:str:from_latin1 $b"\xFF\xF0";
std:assert_eq s "\u{FF}\u{F0}";
```
#### <a name="31019-stdstrfromutf8-byte-vector"></a>3.10.19 - std:str:from\_utf8 _byte-vector_
Converts the _byte-vector_ to a Unicode string and returns it.
If the _byte-vector_ contains invalid UTF-8 sequences an
error value is returned.
```wlambda
!s = _? ~ std:str:from_utf8 $b"\xC3\x84\xC3\x9F\xE6\x97\xA5\xE6\x9C\xAC\xE4\xBA\xBA";
std:assert_eq s "Äß日本人";
!r = on_error {|| _ } ~ std:str:from_utf8 $b"\xFF\xFF";
std:assert_eq r "str:from_utf8 decoding error: invalid utf-8 sequence of 1 bytes from index 0";
```
#### <a name="31020-stdstrfromutf8lossy-byte-vector"></a>3.10.20 - std:str:from\_utf8\_lossy _byte-vector_
Converts the _byte-vector_ to a Unicode string and returns it.
If the _byte-vector_ contains invalid UTF-8 sequences a `"�"` will be
inserted.
```wlambda
!s = _? ~ std:str:from_utf8_lossy
$b"\xC3\x84\xFF\xC3\x9F\xE6\x97\xA5\xE6\x9C\xAC\xE4\xBA\xBA\xFF\xFF\x00";
std:assert_eq s "Ä�ß日本人��\0";
```
#### <a name="31021-stdstrtocharvec-string"></a>3.10.21 - std:str:to\_char\_vec _string_
Converts the _string_ into a vector of integers which represent the Unicode
character number.
```wlambda
!v = std:str:to_char_vec "1234";
std:assert_eq (str v) ~ str $[49,50,51,52];
!v = std:str:to_char_vec "Äß日本人";
std:assert_eq (str v) ~ str $[196,223,0x65E5,0x672C,0x4EBA];
```
#### <a name="31022-stdstrfromcharvec-vector"></a>3.10.22 - std:str:from\_char\_vec _vector_
The reverse operation of `std:str:to_char_vec`. It converts
a vector of integers to a unicode string. Any integer that has
no associated Unicode character will be converted to `"?"`.
```wlambda
std:assert_eq (std:str:from_char_vec $[9999999999]) "?";
std:assert_eq
(std:str:from_char_vec
$[49,50,196,223,0x65E5,0x672C,0x4EBA])
"12Äß日本人";
```
#### <a name="31023-stdstrtolowercase-string"></a>3.10.23 - std:str:to\_lowercase _string_
Swaps all (Unicode) characters in _string_ to their lowercase version.
```wlambda
std:assert_eq (std:str:to_lowercase "ZABzabÄßÜÖ") "zabzabäßüö";
```
#### <a name="31024-stdstrtouppercase-string"></a>3.10.24 - std:str:to\_uppercase _string_
Swaps all (Unicode) characters in _string_ to their lowercase version.
```wlambda
std:assert_eq (std:str:to_uppercase "ZABzabäßüö") "ZABZABÄSSÜÖ";
```
#### <a name="31025-stdstreditdistance-str-a-strb"></a>3.10.25 - std:str:edit\_distance _str-a_ _str\_b
Calculates the Levenshtein distance between two (Unicode) strings.
```wlambda
std:assert_eq (std:str:edit_distance "aaa" "aba") 1;
```
### <a name="311-byte-vectors"></a>3.11 - Byte Vectors
Bytes (plural of Byte) are a vector of bytes. Unlike strings they don't have any encoding.
Literal syntax however supports inserting unicode characters:
```wlambda
$b"abc";
$b"\xFF\xFD\x00";
$Q/ABCDEF\xFD/; # \xFD is not an escape sequence here!
```
#### <a name="3111-call-properties-of-bytes"></a>3.11.1 - Call Properties of Bytes
You can index inside a byte array by calling it with an integer:
```wlambda
std:assert_eq ($b"ABC" 1) $b'B';
```
You can extract a whole range when calling with 2 integers:
```wlambda
std:assert_eq ($b"ABCDEF" 2 3) $b"CDE";
```
If you call a bytes value with a map as argument, the bytes value is
converted to a string internally using `str` and the value from the map
is returned:
```wlambda
!some_map = ${ a = 20, b = 30 };
std:assert_eq ($b"a" some_map) 20;
std:assert_eq ($b"b" some_map) 30;
std:assert_eq some_map.$b"a" 20; # with method call syntax
```
If you call bytes with a pair as argument, you can do a multitude of
operations, from replacement to finding a byte:
```wlambda
# replacing substrings:
std:assert_eq ($b"a,b,c,d" $p($b',', $b';')) $b"a;b;c;d";
std:assert_eq ($b"a,b,c,d" $p($b"a,", $b"XXX")) $b"XXXb,c,d";
# also works with strings and chars:
std:assert_eq ($b"a,b,c,d" $p("a,", "XXX")) $b"XXXb,c,d";
std:assert_eq ($b"a,b,c,d" $p("a,", 'O')) $b"Ob,c,d";
# finding a character/byte:
std:assert_eq ($b"a,b,c,d" $p(0, $b'c')) 4;
std:assert_eq ($b"a,b,c,d" $p(0, 'c')) 4;
# splitting:
std:assert_str_eq ($b"A\<SOH>B\<SOH>C" $p($b'\<SOH>', 0)) $[$b"A", $b"B", $b"C"];
```
See also the section [Calling Semantics of Data Types](#319-calling-semantics-of-data-types).
#### <a name="3112-byte-conversion-functions"></a>3.11.2 - Byte Conversion Functions
You can convert bytes to strings in a multitude of ways:
- str _bytes_
```wlambda
std:assert_eq (str $b"abc") "abc";
std:assert_eq (str $b"abc\xFF") "abcÿ";
std:assert_eq (str $Q/ABCDEF\xFD/) "ABCDEF\\xFD";
```
- std:bytes:to\_hex _bytes_ \[_group-len_ \[_group-sep_]]
```wlambda
std:assert_eq (std:bytes:to_hex $b"\xFF\x0A\xBE\xEF")
"FF0ABEEF";
std:assert_eq (std:bytes:to_hex $b"\xFF\x0A\xBE\xEF" 2)
"FF 0A BE EF";
std:assert_eq (std:bytes:to_hex $b"\xFF\x0A\xBE\xEF" 2 ":")
"FF:0A:BE:EF";
```
- std:str:from\_latin1 _bytes_
```wlambda
std:assert_eq (std:str:from_latin1 $b"\xFF\xF0") "\u{FF}\u{F0}";
```
- std:str:from\_utf8 _bytes_
```wlambda
std:assert_eq (std:str:from_utf8 $b"\xC3\xA4\xC3\x9F\xC3\xBF") "äßÿ";
std:assert_eq (std:str:from_utf8 [std:str:to_bytes "äßÿ"]) "äßÿ";
# broken UTF8 will result in an error:
std:assert ~ is_err (std:str:from_utf8 $b"\xC3\xC3\xA4\xC3\x9F\xC3\xBF");
```
- std:str:from\_utf8\_lossy _bytes_
```wlambda
std:assert_eq (std:str:from_utf8_lossy $b"\xC3\xC3\xA4\xC3\x9F\xC3\xBF") "�äßÿ";
```
You can even convert bytes to vectors of integers back and forth:
```wlambda
!v = std:bytes:to_vec $b"ABC";
std:assert_eq (str v) (str $[65, 66, 67]);
std:push v 64;
!b = std:bytes:from_vec v;
std:assert_eq b $b"ABC@";
```
There is also an inverse operation to `bytes:to_hex`:
```wlambda
std:assert_eq (std:bytes:from_hex ~ std:bytes:to_hex $b"ABC") $b"ABC";
```
#### <a name="3113-isbytes-value"></a>3.11.3 - is\_bytes _value_
Returns `$true` if _value_ is a byte vector.
```wlambda
std:assert ~ is_bytes $b"ABC";
std:assert ~ not ~ is_bytes "ABC";
```
#### <a name="3114-stdbytesfind-pattern-string-offset"></a>3.11.4 - std:bytes:find _pattern_ _string_ [_offset_]
Searches for the string _pattern_ in the _string_ and returns the 0 based position
in the string the given _pattern_ starts.
If no pattern was found `$none` is returned.
```wlambda
std:assert_eq (std:bytes:find $b"xyz" $b"abcxyz") 3;
std:assert_eq (std:bytes:find $b"xyz" $b"abcxyzxyz" 6) 6;
std:assert_eq (std:bytes:find $b"xyz" $b"abcxyzfooxyz" 6) 9;
```
#### <a name="3115-stdbytesreplace-byte-vector-pattern-replacement"></a>3.11.5 - std:bytes:replace _byte-vector_ _pattern_ _replacement_
Replaces all occurences of _pattern_ in _byte-vector_ with _replacement_.
```wlambda
std:assert_eq
(std:bytes:replace $b"XXX\x01\x02\x03OOO" $b"\x01\x02\x03" $b"---")
$b"XXX---OOO";
std:assert_eq
(std:bytes:replace $b"XXX\x01\x02\x03OOO" $b"\x01\x02\x03" $b"")
$b"XXXOOO";
std:assert_eq
(std:bytes:replace $b"XXX\x01\x02\x03OOO" $b"\x01\x02\x03" $b"\xFF\xFF\xFF\xFF")
$b"XXX\xFF\xFF\xFF\xFFOOO";
```
#### <a name="3116-stdbytesfromhex-string-with-hex-chars"></a>3.11.6 - std:bytes:from\_hex _string-with-hex-chars_
This function decodes a string of hex characters into a byte vector.
```wlambda
!bv = std:bytes:from_hex "62797465";
std:assert_eq bv $b"byte";
```
#### <a name="3117-stdbytesfromvec-vector-of-ints"></a>3.11.7 - std:bytes:from\_vec _vector-of-ints_
Decodes a vector of integers in the range 0-255 into a byte vector. If an
integer is larger than 255 don't expect a sensible result. But it will most
likely just wrap around.
```wlambda
std:assert_eq
(std:bytes:from_vec $[1,2,3,0x62,0x79,0x74,0x65])
$b"\x01\x02\x03byte";
```
#### <a name="3118-stdbytestohex-byte-vector"></a>3.11.8 - std:bytes:to\_hex _byte-vector_
Converts the given byte vector to a string of hex encoded bytes.
```wlambda
std:assert_eq
(std:bytes:to_hex $b"byte")
"62797465";
```
#### <a name="3119-stdbytestobase64-byte-vector-config"></a>3.11.9 - std:bytes:to\_base64 _byte-vector_ [_config_]
Converts the given byte vector to a Base64 encoded string. With _config_ you can
define the encoding style:
- `:std`
- `:std_no_pad`
- `:url`
- `:url_no_pad`
```wlambda
std:assert_eq
(std:bytes:to_base64 $b"\x00\xFF")
"AP8=";
std:assert_eq
(std:bytes:to_base64 "test")
"dGVzdA==";
std:assert_eq
(std:bytes:to_base64 "test" :std_no_pad)
"dGVzdA";
std:assert_eq
(std:bytes:from_base64 (std:bytes:to_base64 "test"))
$b"test";
std:assert_eq
(std:bytes:from_base64 (std:bytes:to_base64 "test" :url) :url)
$b"test";
```
#### <a name="31110-stdbytesfrombase64-byte-vector-config"></a>3.11.10 - std:bytes:from\_base64 _byte-vector_ [_config_]
Converts the given byte vector to a Base64 encoded string. With _config_ you can
define the encoding style, see also `to_base64` for a list of possible values.
```wlambda
std:assert_eq
(std:bytes:from_base64 "AP8=")
$b"\x00\xFF";
std:assert_eq
(std:bytes:from_base64 "dGVzdA")
$b"test";
```
#### <a name="31111-stdbytestovec-byte-vector"></a>3.11.11 - std:bytes:to\_vec _byte-vector_
Converts the given byte vector to a vector of integers in the range 0-255.
```wlambda
std:assert_str_eq
(std:bytes:to_vec $b"byte")
$[98, 121, 116, 101];
```
#### <a name="31112-stdbytespack-pack-format-string-list-of-values"></a>3.11.12 - std:bytes:pack _pack-format-string_ _list-of-values_
Returns a byte vector containing the values of _list-of-values_
serialized in binary form (packed) according to the given _pack-format-string_.
If the syntax of the _pack-format-string_ has errors, an error value is returned.
See also the section [Format String Syntax](#133-format-string-syntax-for-stdbytespack-and-stdbytesunpack)
for a description of the syntax for _pack-format-string_.
This function is very useful for constructing binary data for file formats and
network protocols.
```wlambda
std:assert_eq
(std:bytes:pack "> i16 x s8 x f" $[1, "test", 0.5])
$b"\0\x01\0\x04test\0?\0\0\0";
```
#### <a name="31113-stdbytesunpack-pack-format-string-byte-vector"></a>3.11.13 - std:bytes:unpack _pack-format-string_ _byte-vector_
Decodes the given _byte-vector_ according to the _pack-format-string_ and returns it
as list of values.
If the syntax of the _pack-format-string_ has errors or the given _byte-vector_
is too short, an error value is returned.
See also the section [Format String Syntax](#133-format-string-syntax-for-stdbytespack-and-stdbytesunpack)
for a description of the syntax for _pack-format-string_.
```wlambda
std:assert_str_eq
(std:bytes:unpack
"< i16 x c3 s16 x y"
$b"\x10\x00\x00ABC\x02\x00XY\x00This is the rest")
$[16, $b"ABC", $b"XY", $b"This is the rest"];
```
### <a name="312-symbols"></a>3.12 - Symbols
Symbols are a special kind of strings that are interned by the runtime. That
means, comparing two symbols is an O(1) operation and not an O(n) operation on
the length of the string. Symbols are also used as keys for maps. Use them
however you see fit. They will do a key lookup (on maps, vectors (as indices)
and user values) if they are called with an argument.
```wlambda
std:assert_eq (:1 $[1,2,3]) 2;
std:assert_eq (:a ${a=30}) 30;
```
They are basically the same as string, but strings have
slightly different calling semantics and a different literal syntax.
Often you can use them as shortform literal in places where a string
is expected:
```wlambda
std:assert_eq (std:str:replace :A :a "All AbabA") "all ababa";
```
They can be very useful as sentinel values or custom enums:
```wlambda
!x = :ON;
!y = :OFF;
std:assert_eq ((x == :ON) { 10 }) 10;
# They don't match with strings:
std:assert_eq ((x == "ON") { 10 } { 20 }) 20;
# Work together nicely with `match`:
!state = "";
match x
:ON => { .state = "is on" }
:OFF => { .state = "is off" };
std:assert_eq state "is on";
match y
:ON => { .state = "is on" }
:OFF => { .state = "is off" };
std:assert_eq state "is off";
```
Keep in mind, that all symbols are interned strings. And if you create many
symbols that are not used anymore, you might need to trigger a cleanup
with `std:symbols::collect`.
The collection of dead symbols is also run automatically for every 100th newly
allocated symbol.
#### <a name="3121-sym-value"></a>3.12.1 - sym _value_
Casts the given _value_ into a symbol.
```wlambda
std:assert_eq (sym "a") :a;
std:assert_eq (sym $b"a") :a;
std:assert_eq (sym $[]) :"$[]";
std:assert_eq (sym 10) :10;
```
#### <a name="3122-issym-value"></a>3.12.2 - is\_sym _value_
Returns `$true` if the _value_ is symbol.
```wlambda
std:assert ~ is_sym :a;
std:assert ~ is_sym ~ sym "a";
std:assert ~ is_sym ~ sym "a";
std:assert ~ is_sym ~ sym $b"a";
std:assert ~ not ~ is_sym "a";
std:assert ~ not ~ is_sym $b"a";
std:assert ~ not ~ is_sym $&&:a;
std:assert ~ not ~ is_sym ${};
std:assert ~ not ~ is_sym $none;
std:assert ~ not ~ is_sym $true;
```
#### <a name="3123-stdsymbolscollect"></a>3.12.3 - std:symbols:collect
Collect and remove all interned symbols in the current thread that are no
longer used. Returns the number of freed symbols. Please keep in mind, that
the `std:ref_id` of any collected symbol will be different from a symbol that
is created later with the same characters.
The collection of dead symbols is also run automatically for every 100th newly
allocated symbol.
If you rely on the reference ID of a symbol, you should make sure to keep it
around. Literal symbols are always kept around as long as the code is running
or referenced somewhere (eg. by a function).
```wlambda
std:symbols:collect[];
!probably_unique_sym = sym "onceonly_used";
std:assert_eq
(std:ref_id ~ sym "onceonly_used")
(std:ref_id probably_unique_sym);
std:assert_eq std:symbols:collect[] 0;
.probably_unique_sym = $none;
std:assert_eq std:symbols:collect[] 1;
```
### <a name="313-syntax-block"></a>3.13 - Syntax `$%:Block`
A syntax element is an element of an abstract syntax tree as it is returned
by `std:wlambda:parse` for instance. They carry the type of syntax node
and debug information with them.
#### <a name="3131-stdsynpos-syntax"></a>3.13.1 - std:syn:pos _syntax_
Returns the position of the syntax element in the source code.
```wlambda
!ast = std:wlambda:parse "1 + 2";
!add_syntax = ast.1.0;
std:assert_str_eq
(std:syn:pos add_syntax)
$["<wlambda:parse:input>", 1, 3];
```
#### <a name="3132-stdsyntype-syntax"></a>3.13.2 - std:syn:type _syntax_
Converts the syntax element to a symbol, so you can determine it's type:
```wlambda
!ast = std:wlambda:parse "1 + 2";
!add_syntax = ast.1.0;
std:assert_str_eq
(std:syn:type add_syntax)
:BinOpAdd;
```
### <a name="314-pairs-pa-b"></a>3.14 - Pairs `$p(a, b)`
A pair is an immutable tuple of 2 values. You can use it for returning two
values from a function as it is a slight bit slimmer than a vector with two
values. Unlike a vector, pairs are compared by `==` according to their contents
and not by referencial equality.
There are two ways to form a pair:
- Pair value syntax: `$p(a, b)`
- Pair right associative operator: `a => b`
You can access the pair values by the following keys:
```wlambda
!v = $p(11, 12);
std:assert_eq v.0 11;
std:assert_eq v.1 12;
std:assert_eq (0 v) 11;
std:assert_eq (1 v) 12;
std:assert_eq v.car 11;
std:assert_eq v.cdr 12;
std:assert_eq v.head 11;
std:assert_eq v.tail 12;
std:assert_eq v.first 11;
std:assert_eq v.second 12;
# Pairs are often used to represent map entries,
# so you can use `key` and `value`
# and the short forms `k` and `v` too:
std:assert_eq v.value 11;
std:assert_eq v.key 12;
std:assert_eq v.v 11;
std:assert_eq v.k 12;
```
Comparison does happen by their contents:
```wlambda
std:assert $p(1, 2) == $p(1, 2);
std:assert $p(2, 2) != $p(1, 2);
# In contrast to vectors:
std:assert not ~ $[1, 2] == $[1, 2];
```
The index is wrapping around, that means `$p(a, b).2` is the first element again:
```wlambda
!v = $p(33, 44);
!l = $[];
iter i $i(0, 4)
~ std:push l v.(i);
std:assert_eq (str l) (str $[33, 44, 33, 44]);
```
A pair is a referencial data type, that means you can use `std:ref_id` on it:
```wlambda
!a = $p(1, 2);
!b = $p(2, 3);
!id_a = std:ref_id a;
!id_b = std:ref_id b;
std:assert (id_a != id_b);
std:assert std:ref_id[a] == id_a;
!v = $[a];
std:assert std:ref_id[v.0] == id_a;
```
#### <a name="3141-pair-operator-a--b"></a>3.14.1 - Pair Operator `a => b`
Writing `a => b` operator is the same as writing `$p(a, b)`. However, the
precedence of the `=>` operator is the lowest and right associative, so writing
this is possible:
```wlambda
!p = 1 + 2 => 3 + 4;
std:assert_eq p $p(3, 7);
```
The following example shows off the associativity of the operator:
```wlambda
!a = 1 => 2;
!b = 2 => 3 => 4;
std:assert_eq a $p(1, 2);
std:assert_eq b $p(2, $p(3, 4));
std:assert_eq b 2 => 3 => 4;
```
#### <a name="3142-cons-a-b"></a>3.14.2 - cons _a_ _b_
Creates a new pair from the values _a_ and _b_.
```wlambda
!p = cons 3 4;
std:assert_eq p $p(3, 4);
```
#### <a name="3143-pair-stringbyte-vector-operations"></a>3.14.3 - Pair string/byte vector operations
If you call a pair with a string or byte vector as argument, there are some
operations that can be done:
##### <a name="31431-p-from--count--string-or-byte-vec"></a>3.14.3.1 - $p( _from_ , _count_ ) _string-or-byte-vec_
Returns a substring starting at _from_ with the length _count_.
```wlambda
std:assert_eq ($p(2, 4) "abcdefgh") "cdef";
```
The same works for byte vectors:
```wlambda
std:assert_eq ($p(2, 4) $b"abcdefgh") $b"cdef";
```
##### <a name="31432-p-pattern--replacement--string-or-byte-vec"></a>3.14.3.2 - $p( _pattern_ , _replacement_ ) _string-or-byte-vec_
Replaces all _pattern_ occurences in _string_ by _replacement_.
```wlambda
std:assert_eq ($p(";", "_") "A;B;D;EFG;HI") "A_B_D_EFG_HI";
```
The same works for byte vectors:
```wlambda
std:assert_eq ($p($b";", $b"_") $b"A;B;D;EFG;HI") $b"A_B_D_EFG_HI";
```
##### <a name="31433-p-split-pattern--max--string-or-byte-vec"></a>3.14.3.3 - $p( _split-pattern_ , _max_ ) _string-or-byte-vec_
Splits _string_ at _split-pattern_ a _max_ number of times.
If _max_ is 0, it is split completely.
```wlambda
std:assert_eq str[$p(";", 3) "A;B;D;EFG;HI"] ~ str $["A", "B", "D;EFG;HI"];
std:assert_eq str[$p(";", 0) "A;B;D;EFG;HI"] ~ str $["A", "B", "D", "EFG", "HI"];
```
The same works for byte vectors:
```wlambda
std:assert_eq str[$p($b";", 0) $b"A;B;D;EFG;HI"] ~ str $[$b"A", $b"B", $b"D", $b"EFG", $b"HI"];
```
#### <a name="3144-pair-to-iterator"></a>3.14.4 - Pair to Iterator
Pairs play a special role if you make an iterator from it.
It can be used to create a specialized iterator that only
iterates over keys or values of a map. Or that enumerates
a vector or map.
##### <a name="31441-iter---range"></a>3.14.4.1 - Iter - Range
`$iter $p(0, 10)` is the same as `$iter $i(0, 10)` and will construct an
iterator that iterates from `0` to `9` (inclusive).
Because of the pair operator `a => b` we can nicely write a counting loop like this:
```wlambda
!sum = $@i
iter i 0 => 10 {
$+ i;
};
std:assert_eq sum 45;
```
##### <a name="31442-iter---enumerate"></a>3.14.4.2 - Iter - Enumerate
If the first value of the pair is `:enumerate`
it will enumerate entries in a map or values in a vector.
```wlambda
!v = $[];
# $iter is only explicit here for demonstration
# purposes! `iter` will make an iter from the pair
# if you don't pass one!
iter i ($iter $p(:enumerate, $[:a, :b, :c]))
~ std:push v i;
std:assert_eq (str v) (str $[0, 1, 2]);
```
For maps:
```wlambda
!v = $[];
iter i $p(:enumerate, ${a = 10, b = 20})
~ std:push v i;
std:assert_eq (str v) (str $[0, 1]);
```
##### <a name="31443-iter---values"></a>3.14.4.3 - Iter - Values
This is useful for iterating over the values in a map in an undefined order:
```wlambda
!m = ${ a = 10, b = 20, c = 33 };
!sum = $@i iter v $p(:values, m) ~ $+ v;
std:assert_eq sum 63;
```
##### <a name="31444-iter---keys"></a>3.14.4.4 - Iter - Keys
You can also iterate over map keys in an undefined order:
```wlambda
!m = ${ :10 = :c, :20 = :b, :30 = :a };
!sum = $@i iter v $p(:keys, m) ~ $+ v;
std:assert_eq sum 60;
```
#### <a name="3145-ispair-value"></a>3.14.5 - is\_pair _value_
Checks if _value_ is a pair.
```wlambda
std:assert ~ is_pair $p(1, 2);
std:assert not ~ is_pair $[1, 2];
std:assert not ~ is_pair $i(1, 2);
```
### <a name="315-vectors-or-lists"></a>3.15 - Vectors (or Lists)
The literal syntax for vectors (or sometimes also called lists in WLambda)
is `$[...]`. You may write any kind of expression in it and you will get
a vector from it.
For iteration over a vector please refer to [5.2 Collection Iteration](#52-collection-iteration).
To access the elements of a vector you have to call a number with a vector
as first argument. The field syntax is a more convenient shorthand syntax.
The following example demonstrates it:
```wlambda
!add20 = { _ + 20 };
!some_vec = $[1, 2 * 10, add20 10];
# Index calling:
std:assert_eq (0 some_vec) 1;
std:assert_eq (1 some_vec) 20;
std:assert_eq (2 some_vec) 30;
# Field syntax:
std:assert_eq some_vec.0 1;
std:assert_eq some_vec.1 20;
std:assert_eq some_vec.2 30;
```
To add elements to a vector, you can use the prepend and append operators `+>`
and `<+` too:
```wlambda
!v = $[1];
0 <+ v;
v +> 2;
std:assert_str_eq v $[0,1,2];
```
#### <a name="3151-stdpush-vector-item"></a>3.15.1 - std:push _vector_ _item_
Pushes _item_ to the end of _vector_. Returns _item_.
Be aware, that there is also the `+>` operator, that will append elements
to a vector.
```wlambda
!v = $[1,2];
std:push v 3;
std:assert_eq (str v) (str $[1,2,3]);
```
#### <a name="3152-stdpop-vector"></a>3.15.2 - std:pop _vector_
Pops off the last element of _vector_. Returns `$none` if the vector is empty
or if _vector_ is not a vector.
```wlambda
!v = $[1,2,3];
std:assert_eq (std:pop v) 3;
std:assert_eq (str v) (str $[1,2]);
```
#### <a name="3153-stdunshift-vector-item"></a>3.15.3 - std:unshift _vector_ _item_
Inserts _item_ at the front of _vector_. Returns _item_ and mutates _vector_
inplace. Be aware that this operation is of O(n) complexity.
Be aware, that there is also the `<+` operator, that will prepend elements
to a vector (with O(n) complexity however).
```wlambda
!v = $[1,2];
std:unshift v 3;
std:assert_eq (str v) (str $[3,1,2]);
```
#### <a name="3154-isvec-value"></a>3.15.4 - is\_vec _value_
Returns `$true` if _value_ is a vector.
```wlambda
std:assert ~ is_vec $[];
std:assert ~ is_vec $[1,2,3];
std:assert ~ not ~ is_vec 0;
std:assert ~ not ~ is_vec $none;
std:assert ~ not ~ is_vec $true;
std:assert ~ not ~ is_vec $p(1,2);
std:assert ~ not ~ is_vec $i(1,2);
std:assert ~ not ~ is_vec $f(1,2);
std:assert ~ not ~ is_vec ${a = 10};
```
#### <a name="3155-vector-splicing"></a>3.15.5 - Vector Splicing
You can splice vectors directly into their literal form with the `$[..., * vec_expr, ...]`
syntax. Here is an example:
```wlambda
!make_some = { $[_ + 1, _ + 2] };
!some_vec = $[ 0, *make_some 1 ];
std:assert_eq some_vec.1 2;
std:assert_eq some_vec.2 3;
# There can be any expression after the `.` if you wrap it into `(...)`:
std:assert_eq some_vec.(1 + 1) 3;
# A more direct example:
std:assert_eq (str $[1,2,*$[3,4]]) "$[1,2,3,4]";
```
#### <a name="3156-stdappend-vec-a-value-or-vec-"></a>3.15.6 - std:append _vec-a_ _value-or-vec_ ...
Appends _value-or-vec_ and all following items to _vec-a_.
If _value-or-vec_ is a vector, all its items will be appended to _vec-a_.
```wlambda
!v = std:append $[1,2,3] :a :b $[:c, :d];
std:assert_eq (str v) "$[1,2,3,:a,:b,:c,:d]";
```
If _vec-a_ is not a vector, a vector containing it will be created:
```wlambda
!v = std:append 1 :a :b $[:c, :d];
std:assert_eq (str v) "$[1,:a,:b,:c,:d]";
```
#### <a name="3157-stdprepend-vec-a-value-or-vec-"></a>3.15.7 - std:prepend _vec-a_ _value-or-vec_ ...
Prepends _value-or-vec_ and all following items to the front of _vec-a_.
If _value-or-vec_ is a vector, all its items will be prepended to _vec-a_.
```wlambda
!v = std:prepend $[1,2,3] :a :b $[:c, :d];
std:assert_eq (str v) (str $[:d, :c, :b, :a, 1, 2, 3]);
```
If _vec-a_ is not a vector, a vector containing it will be created:
```wlambda
!v = std:prepend 1 :a :b $[:c, :d];
std:assert_eq (str v) (str $[:d, :c, :b, :a, 1]);
```
#### <a name="3158-stdtake-count-vector"></a>3.15.8 - std:take _count_ _vector_
Takes and returns the first _count_ elements of _vector_. Does not
mutate _vector_.
```wlambda
!v = $[1,2,3,4,5,6];
!t = std:take 4 v;
std:assert_eq (str v) "$[1,2,3,4,5,6]";
std:assert_eq (str t) "$[1,2,3,4]";
```
#### <a name="3159-stddrop-count-vector"></a>3.15.9 - std:drop _count_ _vector_
Drops _count_ elements from _vector_ and returns them as new vector.
Does not mutate _vector_.
```wlambda
!v = $[1,2,3,4,5,6];
!t = std:drop 4 v;
std:assert_eq (str v) "$[1,2,3,4,5,6]";
std:assert_eq (str t) "$[5,6]";
```
### <a name="316-associative-maps-or-string-to-value-mappings"></a>3.16 - Associative Maps (or String to Value mappings)
Aside from vectors there are associative maps in WLambda. Their syntax is
`${ key = expr, ... }`. The keys of these maps have to be symbols (or strings),
the values in the literals can be any expression. Keys for maps are interned strings,
so keep that in mind if you fill a map with garbage keys.
For iteration over a map please refer to [5.2 Collection Iteration](#52-collection-iteration).
You can call a symbol or a string with an associative map to get the value in
the map with the string value as key. There is also, like vectors, the field
calling syntax. Here are some examples:
```wlambda
!some_map = ${ a = 1, b = 2 };
# Symbol calling:
std:assert_eq (:a some_map) 1;
std:assert_eq (:b some_map) 2;
std:assert_eq ("a" some_map) 1;
std:assert_eq ("b" some_map) 2;
# Field syntax:
std:assert_eq some_map.a 1;
std:assert_eq some_map.b 2;
# There can be any expression after the `.` if you wrap it into `(...)`,
# also strings:
std:assert_eq some_map.("a") 1;
std:assert_eq some_map.("b") 2;
```
Keys can also be computed at runtime in the literal form:
```wlambda
!some_map = ${ (std:str:cat "a" "b") = 10 };
std:assert_eq (str some_map) "${ab=10}";
```
If you call a field that is being accessed directly using
the field accessing syntax `some_map.a`, the function is passed the map `some_map`
via the special value `$self`. There is another special variable `$data`
that allows you to access the `$self._data` field.
#### <a name="3161-map-splicing"></a>3.16.1 - Map Splicing
Like vectors you can splice map values directly into map literals:
```wlambda
!map_gen = { ${ (std:str:cat "_" _) = _ } };
!some_map = ${ a = 10, *map_gen "x" };
std:assert_eq some_map.a 10;
std:assert_eq some_map._x "x";
std:assert_eq (str ${*${a=10}}) "${a=10}";
# As a reminder, a full expression can come after the '*':
std:assert_eq (str ${*map_gen "y"}) $q/${_y="y"}/;
```
#### <a name="3162-ismap-value"></a>3.16.2 - is\_map _value_
Returns `$true` if _value_ is a map.
```wlambda
std:assert ~ is_map ${};
std:assert ~ is_map ${a = 10};
std:assert ~ not ~ is_map $&&${};
std:assert ~ not ~ is_map $&${};
std:assert ~ not ~ is_map $[:a, 10];
std:assert ~ not ~ is_map $p(:a, 10);
std:assert ~ not ~ is_map $none;
std:assert ~ not ~ is_map $true;
```
### <a name="317-references"></a>3.17 - References
TODO
- 3 types: strong, hidden, weak
- strong:
- `$&&`
- mention DWIM'ery
- hidden:
- `$&`
- mention usage for closures
- how to "Unhide" a reference using `$:`
- weak:
- std:ref:weaken and `$w&`
- how to break reference cycles
- how weak references are also caught weakly
by closures and not strongly.
- how to get a strong reference using `$:`
Some data structures already have reference characteristics, such as strings,
vectors and maps. But you can also wrap other values like integers, floats, ...
into a reference. There are 3 types of references in WLambda that have
different usecases and semantics. These referential types are neccessary to
mutate lexical variables from a parent scope. To give a rather natural example:
```wlambda
!x = 10;
{ .x = 20; }[];
std:assert_eq x 20;
```
The example works rather intuitively. There is however lots of implicit
referential stuff going on. Once `x` is captured by a closure its contents is implicitly
changed in to a _hidden_ `$&` reference. The closure then stores this hidden
reference too. You have to be aware of this, because in some use cases this can lead
to cyclic reference structures, which are not automatically freed. Please use
weak references `$w&` for mitigating this.
Hidden references and weak references captured by a closure are dereferenced
implicitly if you access the variables. Weak references in the local scope are
not implicitly dereferenced. However, sometimes it's desirable to have a more
explicit reference data types. For this the strong references `$&&` are
available. Use `$*` for accessing the value of a strong reference or a weak reference
in the local scope.
TODO
These types of references exist:
- `$&` - A _hidden_ reference, that is captured by closures or constructed using `$&`.
- `$w&` - A _weak_ reference, can't be constructed literally, only indirectly
as upvalue of a closure or by `std:ref:weaken`.
- `$&&` - A _strong_ reference, that is captured strongly by closures.
Inside closures they are also implicitly dereferenced by assignment
and access by variable name.
```wlambda
!x = $& 10;
{ .x = 20; }[]; # Closures implicitly handle weak references
std:assert_eq x 20;
```
And the same with strong references:
```wlambda
!x = $&& 10;
.*x = 11;
{ .*x = 20; }[]; # Closures need explicit handling of strong references
std:assert_eq $*x 20;
```
Strong references can also be created using the `std:to_ref` function and
the `$:` operation.
#### <a name="3171-stdtoref-value"></a>3.17.1 - std:to\_ref _value_
Creates a new strong reference that refers to a cell that stores _value_.
```wlambda
!x = std:to_ref 10;
std:assert_eq (std:ser:wlambda x) "$&&10";
std:assert_eq $*x 10;
```
#### <a name="3172-stdrefweaken-ref"></a>3.17.2 - std:ref:weaken _ref_
You can weaken any of those two types of references manually using the
`std:ref:weaken` function.
```wlambda
!drop_check = $& $f;
# Set `drop_check` to $true when all (non weak) references to it are gone.
!x = $&& (std:to_drop {|| .drop_check = $true });
# Create a weakened reference to the value referred to by x:
!y = std:ref:weaken x;
# The reference to the drop function is removed and this means
# that the weak reference in y is invalidated and returns $n in future.
.x = $n;
# Deref y now gives you $n:
std:assert_eq $*y $n;
std:assert drop_check;
```
Please note that you can use `$w&`/`$weak&` as a shortcut to calling the library function:
```wlambda
!x = $&& 10;
!x_weak = $w& x;
std:assert_eq x &> type "ref_strong";
std:assert_eq x_weak &> type "ref_weak";
```
#### <a name="3173-stdrefhide-value"></a>3.17.3 - std:ref:hide _value_
Creates a hidden reference from a given value or reference.
```wlambda
!r = $&& 10; # strong ref to value 10
# hide the reference for direct access via local variables
!h = std:ref:hide r;
.h += 11;
std:assert_eq $*r 21;
std:assert_eq h 21;
std:assert_eq (std:write_str $:h) "$&&21";
std:assert_eq (std:write_str $[r, $:h]) "$[$<1=>$&&21,$<1>]";
```
#### <a name="3174-isref-value"></a>3.17.4 - is\_ref _value_
Returns `$true` if _value_ is a reference (strong, hidden or weak).
```wlambda
!x = $&&10;
std:assert ~ is_ref ~ std:ref:weaken x;
std:assert ~ is_ref $&10;
std:assert ~ is_ref $&&10;
std:assert ~ not ~ is_ref $[1,2,3];
std:assert ~ not ~ is_ref ${a=10};
std:assert ~ not ~ is_ref $true;
std:assert ~ not ~ is_ref $none;
```
#### <a name="3175-iswref-value"></a>3.17.5 - is\_wref _value_
Returns `$true` if _value_ is a weak reference.
```wlambda
!x = $&& 10;
!y = std:ref:weaken x;
std:assert ~ is_wref y;
std:assert ~ not ~ is_wref x;
```
#### <a name="3176-stdrefstrengthen-ref"></a>3.17.6 - std:ref:strengthen _ref_
You can convert a weak reference (weakened by `std:ref:weaken`) or a captured weak
reference `$&` to strong with `std:ref:strengthen
```wlambda
!x = $&&10;
!y = std:ref:weaken x;
.x = $none;
std:assert ~ is_none $*y;
.x = $&&10;
.y = std:ref:weaken x;
!y2 = std:ref:strengthen y; # Here we take a second strong reference from a weak one
.x = $none;
std:assert ~ is_some $*y;
std:assert ~ is_some $*y2;
.y2 = $none;
std:assert ~ is_none $*y;
```
#### <a name="3177-stdrefset-ref-value"></a>3.17.7 - std:ref:set _ref_ _value_
Sets the value of the reference _ref_ to _value_.
If _ref_ is not a strong, hidden or weak reference nothing happens.
Returns _value_ or `$none`.
```wlambda
!r1 = $&&1;
std:ref:set r1 10;
std:assert_eq $*r1 10;
# Note that $& references in local variables are
# automatically dereferenced. Because of that we need to wrap it into
# an extra reference.
!r2 = $& $& 1;
std:ref:set r2 11;
std:assert_eq $*r2 11;
!r3 = $& $& 1;
!w3 = std:ref:weaken r3;
std:ref:set w3 14; # Set reference via the weak reference in w3 to r3.
std:assert_eq $*r3 14;
```
### <a name="318-iterators-iter-expression"></a>3.18 - Iterators $iter _expression_
As a companion to the `iter` operation there are the iterator values.
These are a special kind of values that generate a value when they are called.
It supports to make an iterator from the same values as the `iter` operation.
You can create an iterator from vectors and maps, but also specialized
iterators that return a range of numbers or only keys of a map.
About this see the section _Iterator Kinds_ below.
The `$iter` syntax takes a complete expression as argument, that means
you can directly write `$iter function arg1 arg2 ...` without
delimiting the function call.
Here is an example how to make an iterator over a vector:
```wlambda
!it = $iter $[1,2,3,4];
!first = it[]; # returns an optional value $o(1)
!second = it[]; # returns an optional value $o(2)
std:assert_eq first $o(1);
std:assert_eq second $o(2);
```
You can also directly cast an iterator, which will also
make it return a value:
```wlambda
!it = $iter $[1,2,3,4];
std:assert_eq (int it) 1;
std:assert_eq (int it) 2;
```
You can pass an iterator also to the `iter` operation:
```wlambda
!it = $iter $[1,2,3];
!sum = 0;
iter i it {
.sum = sum + i;
};
std:assert_eq sum 6;
```
#### <a name="3181-iterator-kinds"></a>3.18.1 - Iterator Kinds
Here is a table of the behaviour of iterators created from WLambda data.
| Data | Iterator return values |
|-----------|-----------|
| vector | Each element of the vector. |
| map | Each key/value pair of the map in undefined order. |
| `$none` | Returns nothing |
| optional | Returns the optional value on first invocation. |
| `$o()` | Returns nothing. |
| int | Returns the integer value on first invocation. |
| float | Returns the integer value on first invocation. |
| string | Returns the individual characters as string. |
| symbol | Returns the individual characters as string. |
| byte vector | Returns the individual bytes as byte vector. |
| `$error` | Returns the error value on first invocation. |
| `$i(a, b)` | The integers in the range of _a_ to _b_, not including _b_. |
| `$i(a, b, step)` | The integers in the range of _a_ to _b_ advanced by _step_, not including _b_. |
| `$f(a, b)` | The floats in the range of _a_ to _b_ advanced by `1.0`, not including _b_. |
| `$f(a, b, step)` | The floats in the range of _a_ to _b_ advanced by _step_, not including _b_. |
| `$p(:enumerate, map)` | Returns integers in the range of `0` to `len map`. |
| `$p(:enumerate, vector)` | Returns integers in the range of `0` to `len vector`. |
| `$p(:values, map)` | Returns the values of the _map_ in undefined order. |
| `$p(:keys, map)` | Returns the keys of the _map_ in undefined order. |
| `$p(int_a, int_b)` | The same as `$i(a, b)`. This makes it possible to write `$iter 0 => 10`. |
| `$p(iterator_a, iterator_b)` | Returns a zip operation of the elements returned by the iterator_a and iterator_b until one of both returns `$o()`. |
| `$p(iterator, x)` | Returns a zip operation of the elements returned by the iterator and the newly created iterator`$iter x`. |
#### <a name="3182-iterators-on-mutated-data"></a>3.18.2 - Iterators on mutated data
Iterators hold a reference to the collection values. That means, if you mutate
a vector while you iterate over it, it will not crash but it might produce
weird effects.
```wlambda
!v = $[1,2,3];
!it = $iter v;
iter i v {
if i <= 3 {
std:push v i + 10; # This is not recommended however...
};
};
std:assert_eq (str v) (str $[1, 2, 3, 11, 12, 13]);
```
This will also work for maps, but as the order of the map entries
is undefined it will produce very indeterministic effects and it's really
not recommended.
#### <a name="3183-splicing-an-iterator"></a>3.18.3 - Splicing an Iterator
You can directly insert the values produced by an iterator into a vector or map:
```wlambda
!it = $iter $[1,2,3,4];
!v = $[10, 20, *it, 99];
std:assert_eq (str v) (str $[10, 20, 1, 2, 3, 4, 99]);
```
Same goes for maps:
```wlambda
!it = $iter ${a = 10, b = 20};
!m = ${ x = 99, *it };
std:assert_eq m.a 10;
std:assert_eq m.b 20;
std:assert_eq m.x 99;
```
#### <a name="3184-calling-an-iterator-with-a-function"></a>3.18.4 - Calling an Iterator with a Function
When an iterator gets called with a function as first argument
it will repeatedly call that function until no more values are
available:
```wlambda
!it = $iter $[1,2,3];
!sum = 0;
it { .sum = sum + _ };
std:assert_eq sum 6;
```
#### <a name="3185-zip-iterators"></a>3.18.5 - Zip Iterators
To highlight this feature from the table above: You can zip two iterators if
you pass an iterator as first part of a pair `$p(a, b)`:
```wlambda
!v = $["a", "b", "c"];
!elems = $@vec
iter i $p($iter v, $iter $i(0, 10)) {
$+ i;
};
std:assert_eq
(str elems)
(str $[$p("a", 0), $p("b", 1), $p("c", 2)]);
```
#### <a name="3186-isiter-value"></a>3.18.6 - is\_iter _value_
Returns `$true` if _value_ is an iterator.
```wlambda
std:assert (is_iter $iter $n);
std:assert (is_iter $iter 0 => 30);
std:assert not <& (is_iter $[1,2,3]);
std:assert (is_iter $iter $[1,2,3]);
std:assert (not <& is_iter <& $true);
std:assert (not <& is_iter <& $false);
std:assert (not <& is_iter <& 4);
std:assert (not <& is_iter <& $p(1, 2));
```
### <a name="319-calling-semantics-of-data-types"></a>3.19 - Calling Semantics of Data Types
You can call almost all basic data types of WLambda.
Here is an overview of the data type calling semantics:
| Type | Args | Semantics |
|------------------------------|------------------------------|-----------|
| `$none` | - | Any call to `$none` will result in a panic. |
| `$error` | - | Any call to `$error` will result in a panic. |
| function | * | Will call the function with the specified arguments. |
| `$true` | `f1, f2` | Will call `f1`. |
| `$false` | `f1, f2` | Will call `f2` or return `$n` if `f2` is not provided. |
| `$true` | `$[1,2]` | Will return the second element `2` of the list. |
| `$false` | `$[1,2]` | Will return the first element `1` of the list. |
| integer | vector, string, byte_vec, iterator | Will return the element at the given integer index. |
| symbol | map, userval | Will retrieve the value in the map at the key equal to the symbol. |
| map | anything | Will call `anything` for each value and key in the map and return a list with the return values. |
| string | string, byte_vec, char or byte | Append operation, works with multiple arguments. |
| byte_vec | string, byte_vec, char or byte | Append operation, works with multiple arguments. |
| string | `$p(int_offs, string/byte_vec/char/byte)` | Find operation, search from int_offs for the given string, byte_vec, char or byte. See also `std:str:find` and `std:bytes:find`. |
| byte_vec | `$p(int_offs, string/byte_vec/char/byte)` | Find operation, search from int_offs for the given string, byte_vec, char or byte. See also `std:str:find` and `std:bytes:find`. |
| `$p(char or byte, int_count)`| - | Create a string or byte_vec containing int_count of copies. |
| `$p(int_offs, string/byte_vec/char/byte)` | string | Find operation, search from int_offs for the given string, byte_vec, char or byte. |
| `$p(int_offs, string/byte_vec/char/byte)` | byte_vec | Find operation, search from int_offs for the given string, byte_vec, char or byte. |
| `$p(int_from, int_count)` | vector | Vector slice operation. |
| `$i(int_from, int_count)` | vector | Vector slice operation. |
| `$p(int_from, int_count)` | numeric vector | Creates a vector slice from a numeric vector. |
| `$i(int_from, int_count)` | numeric vector | Creates a vector slice from a numeric vector. |
| `$p(int_from, int_count)` | iterator | Iterator result list slice operation. |
| `$i(int_from, int_count)` | iterator | Iterator result list slice operation. |
| `$p(int_from, int_count)` | string | Substring operation. (See also section about pairs) |
| `$i(int_from, int_count, ...)`| string | Substring operation. |
| `$p(int_from, int_count)` | byte_vec | Substring operation. (See also section about pairs) |
| `$i(int_from, int_count, ...)`| byte_vec | Substring operation on the byte vector. |
| string |`$i(int_from, int_count, ...)`| Substring operation. |
| byte_vec |`$i(int_from, int_count, ...)`| Substring operation on the byte vector. |
|`$p(string, int)` | string | Split operation. (See also section about pairs) |
|`$p(byte_vec, int)` | byte_vec | Split operation. (See also section about pairs) |
| string |`$p(string, int)` | Split operation. |
| byte_vec |`$p(byte_vec, int)` | Split operation. |
| string |`$p(string, string)` | Replace all operation. |
| byte_vec |`$p(byte_vec, byte_vec)` | Replace all operation. |
| `$p(string, string)` | string | Replace all operation. (See also section about pairs) |
| `$p(byte_vec, byte_vec)` | byte_vec | Replace all operation. (See also section about pairs) |
| `$p(pat_char, repl_char)` | string or byte_vec | Replace all pat_char in string or byte_vec with repl_char. |
| `$p(pat_byte, repl_byte)` | string or byte_vec | Replace all pat_char in string or byte_vec with repl_char. |
| `$p(char, char)` | char or byte | Range check operation, whether char or byte is inside to/from range. |
| `$p(byte, byte)` | char or byte | Range check operation, whether char or byte is inside to/from range. |
| `$p(int_a, int_b)` | char or byte | Range check operation, whether char or byte is inside to/from range. |
| `$i(int_a, int_b)` | char or byte | Range check operation, whether char or byte is inside to/from range. |
| char or byte |`$p(char, char)` | Range check operation, whether char or byte is inside to/from range. |
| char or byte |`$p(byte, byte)` | Range check operation, whether char or byte is inside to/from range. |
| char or byte |`$p(int_a, int_b)` | Range check operation, whether char or byte is inside to/from range. |
| char or byte |`$i(int_a, int_b)` | Range check operation, whether char or byte is inside to/from range. |
| `$o()` | - | Returns $none. |
| `$o(x)` | - | Returns _x_. |
| `$o()` | * | Calls $none with arguments, leading to a panic. |
| `$o(x)` | * | Calls _x_ with the given arguments. |
| | | |
## <a name="4-conditional-execution---if--then--else"></a>4 - Conditional Execution - if / then / else
### <a name="41-if-condition-then-expr-else-expr"></a>4.1 - if/? _condition_ _then-expr_ [_else-expr_]
The keyword for conditional execution is either `if` or just the question mark `?`.
It takes 3 arguments: The first is an expression that will be evaluated
and cast to a boolean. If the boolean is `$true`, the second argument is
evaluated. If the boolean is `$false` the thrid argument is evaluated.
The third argument is optional.
```wlambda
!x = 10;
!msg = "x is ";
if x > 4 {
.msg = std:str:cat msg "bigger than 4";
} {
.msg = std:str:cat msg "smaller than or equal to 4";
};
std:assert_eq msg "x is bigger than 4";
# You may also use `if` in case it suits your coding style better:
if x == 10 {
std:assert $true;
} {
std:assert $false;
};
```
The _condition_ can also be a function block, which will be evaluated:
```wlambda
!res =
if { !x = 2; x > 1 } "x > 1";
std:assert_eq res "x > 1";
```
### <a name="42-using-booleans-for-conditional-execution"></a>4.2 - Using Booleans for Conditional Execution
Conditional execution is also provided by the bool data type. As in WLambda
everything can be called like a function, you can just pass other functions as
arguments to `$true` and `$false`. If you pass a function as first argument to
`$true`, it will be executed. If you pass a function as second argument to
`$false` then that will be executed.
```wlambda
(10 == 10) { std:displayln "10 is 10" }; #=> prints "10 is 10"
(10 != 10) { std:displayln "10 is not 10" }; #=> doesn't print anything
!x = 20;
(x == 20) {
std:displayln "x is 20";
} {
std:displayln "x is 20";
}; # Do not forget the ";"!
```
Actually, as the values `$true` and `$false` can be called like any other
function you may write it also like this, which is not the recommended
syntax, but still works:
```wlambda
(10 == 10)[{ std:displayln "10 is 10" }];
!x = 21;
(x == 20)[{ std:displayln "x is 20" }, { std:displayln "x isn't 20" }]; #=> print "x isn't 20"
```
#### <a name="421-pick-bool-a--b-"></a>4.2.1 - pick _bool_ _a_ -b-
Often, you may want to choose one variable (_a_) or another (_b_) based on some predicate (_bool_).
For these situations, the `pick` function is available.
For example, perhaps you want to make a function which can take any number of parameters,
or a single list parameter.
```wlambda
!sum = \|| std:fold 0 { _ + _1 } ~ pick (is_vec _) _ @;
```
#### <a name="422-indexing-by-booleans"></a>4.2.2 - Indexing by Booleans
Booleans can also be used to index into lists.
When this is done, `$t` represents `1` and `$f` represents `0`.
This means that we can also express our `sum` function as:
```wlambda
!sum = \|| std:fold 0 { _ + _1 } $[@, _].(is_vec _);
```
Furthermore, as `a.b` is equivalent to `b[a]`, one can also write this `sum` function
by simply invoking `(is_vec _)` and passing in the list of options as a parameter.
```wlambda
!sum = \|| std:fold 0 { _ + _1 } ~ (is_vec _) $[@, _];
```
When comparing the `pick` and indexing approaches it is important to note
that the two possible return values are inverted:
```wlambda
!x = 20;
!res = pick (x == 20) "x is 20" "x isn't 20";
std:assert_eq res "x is 20";
.res = $["x isn't 20", "x is 20"].(x == 20);
std:assert_eq res "x is 20";
```
With `pick`, the value to return in the `$t` case comes first, followed by the `$f` case's value,
whereas with indexing approach, the opposite is true.
### <a name="43-value-matching-with---match-value-expr-"></a>4.3 - Value matching with - match _value-expr_ ...
See also [8.1.1](#811-match-value-expr-match-pair1--default-expr) for a more
comprehensive discussion of `match` and structure matchers.
`match` allows for easily select from a set of values:
```wlambda
!check_fun = {
match _
20 => "It's 20"
30 => "It's 20"
"No idea?"
};
std:assert_eq check_fun[20] "It's 20";
std:assert_eq check_fun[34] "No idea?";
```
Also works for deeper data structures:
```wlambda
!val = $[1, ${a = 10}, ${a = 10}, ${a = 10}, ${a = 10}, 2, 2, 2, 10];
!res =
match val
$[1, _*, 3, 10] => :a
$[1, a ~ _* ${ a = y }, b ~ _+ 2, 10] => {
${
a_elems = $\.a,
a_value = $\.y,
b_vals = $\.b,
}
};
std:assert_str_eq res.a_elems $[${ a = 10 }, ${ a = 10 }, ${ a = 10 }, ${ a = 10 }];
std:assert_str_eq res.a_value 10;
std:assert_str_eq res.b_vals $[2,2,2];
```
## <a name="5-loops-and-iteration"></a>5 - Loops And Iteration
WLambda has many ways to loop and iterate:
- Counting loop with `range`
- While some condition is `$true` with the `while` special form.
- Over the items in a vector or map with the `iter` special form.
- Calling an `$iter` iterator value with a function as first argument.
- Over the items in a vector with either `for` or by calling the vector
with a function as first argument.
- Over the items in a map with either `for` or by calling the map
with a function as first argument.
- Over the characters in a string with either `for` or by calling it
with a function.
- Over the bytes in a byte vector with either `for` or by calling it
with a function.
`for` just iterates through the value and provides the individual items as first argument to the
iteration function. But if you call the value with a function as first argument a mapping iteration
is done. That means, the return value of the operation is a list with the return values of the
iteration function. If you don't need that list you should use `for`.
### <a name="51-control-flow"></a>5.1 - Control Flow
#### <a name="511-while-predicate-body"></a>5.1.1 - while _predicate_ _body_
`while` will evaluate _body_ until the evaluation of _predicate_ function returns `$false`.
Or `break` is used to end the loop. The loop can be restarted using `next`.
This is the most basic loop for iteration:
```wlambda
!i = 0;
!out = $[];
while i < 10 {
std:push out i;
.i = i + 1;
};
std:assert_eq (str out) "$[0,1,2,3,4,5,6,7,8,9]";
```
If you need an endless loop you can pass `$true` as predicate:
```wlambda
!i = 0;
while $true {
(i >= 4) break;
.i = i + 1;
};
std:assert_eq i 4;
```
The first
#### <a name="512-iter-var-iterable-body"></a>5.1.2 - iter _var_ _iterable_ _body_
This is the primary syntax of WLambda to iterate over collections,
numeric ranges and generally everything you can create an iterator from
using the `$iter` syntax.
The _var_ will be defined inside the _body_ and be filled with the
value that was generated for the current iteration.
And _iterable_ is everything that `$iter` can make an iterator from.
Please refer to the section `Iterator Kinds` for a listing of this.
Like usual, the control flow manipulators `next` and `break` also work
for this kind of loop.
##### <a name="5121-counting-loop-with-iter"></a>5.1.2.1 - Counting loop with _iter_
Here is an example how to iterate over a range from 1 to 9 and
collect the sum of those integers using an accumulator:
```wlambda
!sum = $@int iter i $i(1,10) ~ $+ i;
std:assert_eq sum 45;
```
Because `$iter $p(1, 10)` is the same as `$iter $i(1, 10)` and because
there is the pair constructor operator `a => b`, the above can also be written as:
```wlambda
!sum = $@int iter i 1 => 10 ~ $+ i;
std:assert_eq sum 45;
```
##### <a name="5122-vector-iteration-with-iter"></a>5.1.2.2 - Vector iteration with _iter_
Here is a simple example of how to iterate over all items of a vector
in order:
```wlambda
!sum = 0;
iter i $[1,2,3,4,5,6] {
.sum = sum + i;
};
std:assert_eq sum 21;
```
Even if you pass the syntax for constructing a function to `iter` it will
create a block of statements from it. So this will work too (also for `while` above):
```wlambda
!sum = 0;
iter i $[1,2,3,4,5,6] \.sum = sum + i;
std:assert_eq sum 21;
```
However _body_ does not have to be a function definition or block, it can also
be just a regular call argument:
```wlambda
!sum = 0;
!inc = { .sum = sum + _; };
iter i $[1,2,3,4] inc[i];
std:assert_eq sum 10;
```
To iterate over a vector by index you can use this:
```wlambda
!v = $[1,2,3,4,5];
!sum = 0;
iter i $i(0, len v) {
.sum = sum + v.(i);
};
std:assert_eq sum 15;
```
##### <a name="5123-map-iteration-with-iter"></a>5.1.2.3 - Map iteration with _iter_
Iteration over a map is also easy and concise. The map entry
will be represented using a pair value `$p(value, key)`.
You can access the first and second element of a pair using the `v`/`value`
and `k`/`key` keys of a pair (but also all other pair accessors defined
in the section for pairs):
```wlambda
!sum = 0;
iter i ${ a = 10, b = 20 } {
.sum = sum + i.v;
};
std:assert_eq sum 30;
```
Very useful for iterating just over the keys or values of a map can also be the
special iterator values you get from the pair constructors:
```wlambda
!m = ${ a = 10, b = 20 };
!sum = 0;
iter v $p(:values, m) {
.sum = sum + v;
};
std:assert_eq sum 30;
```
Or if you need the keys:
```wlambda
!m = ${ a = 10, b = 20 };
!sum = 0;
iter k $p(:keys, m) {
std:displayln "FOO" k;
.sum = sum + m.(k);
};
std:assert_eq sum 30;
```
##### <a name="5124-closures-and-iter-iter-i-"></a>5.1.2.4 - Closures and _iter_ `iter i ...`
If you need a new variable for capturing it in a closure on each
iteration you need to make a new variable binding for each iteration:
```wlambda
!closures = $[];
# Without the rebinding of the variable `i`, `i` would be captured as hidden
# reference and each iteration would update the contents of that reference.
iter i $i(0, 10) {
!i = i;
std:push closures { i * 10 };
};
std:assert_eq ($@i closures \$+ _[]) 450;
```
#### <a name="513-range-start-end-step-fun"></a>5.1.3 - range _start_ _end_ _step_ _fun_
`range` counts from _start_ to _end_ by increments of _step_ and calls _fun_
with the counter. The iteration is inclusive, this means if _start_ == _end_
the function _fun_ will be called once.
In contrast to `iter` this is not a special syntax, but just a regular function
that calls another function repeatedly. You can control it using the `break` and
`next` functions however.
```wlambda
!out = $[];
range 0 9 1 {!(i) = @;
std:push out i;
};
std:assert_eq (str out) "$[0,1,2,3,4,5,6,7,8,9]";
```
The construct also works for floating point numbers,
but be aware of the inherent floating point errors:
```wlambda
!out = $[];
range 0.3 0.4 0.01 {
std:push out ~ std:num:round 100.0 * _;
};
# 40 is not in the set because the accumulation of 0.01 results
# in a value slightly above 0.4 and ends the range iteration:
std:assert_eq (str out) "$[30,31,32,33,34,35,36,37,38,39]";
```
#### <a name="514-break-value"></a>5.1.4 - break _value_
`break` stops the inner most iterative construct, which then will return _value_.
This should work for all repeatedly calling operations, such as
`for`, `while`, `iter` and when calling lists directly. Also most library functions
that iteratively call you react to it, like `std:re:map` and `std:re:replace_all`.
Be aware, that returning a value might not be supported by all iterative constructs.
```wlambda
!ret = range 0 9 1 {!(i) = @;
(i > 4) { break :DONE };
};
std:assert_eq ret :DONE;
```
An example where the list iteration is stopped:
```wlambda
!val = $[1,2,3,4] { (_ > 3) { break :XX }; _ };
std:assert_eq val :XX;
```
#### <a name="515-next"></a>5.1.5 - next
`next` stops execution of the current function or statement block and continues
with the next iteration of the inner most iteration.
```wlambda
!sum = $@i $[1,2,3,4] {
(_ == 3) next;
$+ _;
};
std:assert_eq sum 7;
```
```wlambda
!sum = $@i range 1 10 1 {
(_ % 2 == 0) next;
$+ _;
};
std:assert_eq sum 25;
```
#### <a name="516-jump-index-val-branch1--last-branch"></a>5.1.6 - jump _index-val_ _branch1_ ... _last-branch_
This is a jump table operation, it's a building block for the more
sophisticated `match` operation. The first argument is an index into the table.
If the index is outside the table the _last-branch_ is jumped to. The branches
are compiled like the bodies of `while`, `iter`, `match` and `if` into a runtime
evaluated block.
```wlambda
!x = 10;
!idx = 2;
!res =
jump idx
{ x + 3 }
{ x + 4 }
{ x + 5 };
std:assert_eq res 15;
```
The arms don't have to be in `{ ... }` because they are blocks
and the above could be written like this:
```wlambda
!x = 10;
!idx = 2;
!res =
jump idx
x + 3
x + 4
x + 5;
std:assert_eq res 15;
# or even this:
!res = x + (jump idx 3 4 5);
std:assert_eq res 15;
```
### <a name="52-collection-iteration"></a>5.2 - Collection Iteration
#### <a name="521-iteration-over-vectors"></a>5.2.1 - Iteration over vectors
Iterating over a vector is the most basic iteration supported by WLambda.
You just call the vector with a function as first argument:
```wlambda
!sum = 0;
$[1, 2, 3] {
.sum = sum + _;
};
std:assert_eq sum 6;
```
You can also use `for` if you like.
#### <a name="522-iteration-over-maps"></a>5.2.2 - Iteration over maps
Iterating over a map is as simple as iterating over a vector.
The map can be called with a function as first argument and it starts
iterating over its key/value pairs. The first argument of the
function is the value, the second argument is the key.
```wlambda
!sum = 0;
!keys = $[];
${a = 10, b = 20, c = 30} {
!(v, k) = @;
.sum = sum + v;
std:push keys k;
};
std:assert_eq sum 60;
std:assert_eq (std:str:join "," ~ std:sort keys) "a,b,c";
```
You can also use `for` if you like.
#### <a name="523-for-iteratable-value-function"></a>5.2.3 - for _iteratable-value_ _function_
Calls _function_ for every element of _iteratable-value_.
Iteratable values are:
- Vectors
```wlambda
!product = 1;
for $[3,4,5] {
.product = product * _;
};
std:assert_eq product 60;
```
- Maps
```wlambda
!product = 1;
!keys = $[];
for ${a = 10, b = 20, c = 30} {
!(v, k) = @;
.product = product * v;
std:push keys k;
};
std:assert_eq (std:str:join "," ~ std:sort keys) "a,b,c";
std:assert_eq product 6000;
```
- Byte Vectors
```wlambda
!byte_sum = 0;
for $b"abc" {
.byte_sum = byte_sum + (int _);
};
std:assert_eq byte_sum 294;
```
- Strings
```wlambda
!str_chars = $[];
for "abc" {
std:push str_chars _;
};
std:assert_eq (str str_chars) (str $['a', 'b', 'c']);
```
- Symbols
```wlambda
!str_chars = $[];
for :abc {
std:push str_chars _;
};
std:assert_eq (str str_chars) (str $['a', 'b', 'c']);
```
#### <a name="524-map-function-iterable"></a>5.2.4 - map _function_ _iterable_
Maps anything that is _iterable_ by calling _function_ with each item as
first argument and collecting the return values in a vector.
If a map is passed as _iterable_ then _function_ is called with two arguments,
the first being the map entry value and the second the key.
Note: When iterating over maps, don't assume any order.
It is very similar to `$@vec iter i <iterable> { $+ ... }`.
```wlambda
# Lists:
std:assert_str_eq
(map { float[_] / 2.0 } $[1,2,3,4,5])
$[0.5, 1, 1.5, 2, 2.5];
std:assert_str_eq
(map { _ * 10 } $[$b'a', $b'b', $b'c', 10, 20])
($@vec
iter i $[$b'a', $b'b', $b'c', 10, 20] {
$+ i * 10
});
# Great for working with strings too:
std:assert_str_eq
(map std:str:to_uppercase
$["abc", "bcbc", "aaad", "afoo", "foo"])
$["ABC", "BCBC", "AAAD", "AFOO", "FOO"];
# Maps:
std:assert_str_eq
(std:sort ~ map { @ } ${a = 10, b = 20})
$[$[10, "a"], $[20, "b"]];
# Generally anything that you can pass into `$iter`:
std:assert_str_eq
(map { _ * 2 } 0 => 10)
$[0,2,4,6,8,10,12,14,16,18];
```
#### <a name="525-filter-function-iterable"></a>5.2.5 - filter _function_ _iterable_
Filters anything that is _iterable_ by the given _function_.
The _function_ is called with each item and if it returns a `$true` value,
the item will be collected into a vector that is returned later.
If a map is passed as _iterable_ then _function_ is called with two arguments,
the first being the map entry value and the second the key.
It is very similar to `$@vec iter i <iterable> { if some_function[_] { $+ ... } }`.
```wlambda
# Lists:
std:assert_str_eq
(filter { (_ 0 1) == "a" } $["abc", "bcbc", "aaad", "afoo", "foo"])
$["abc","aaad","afoo"];
# Good in combination with `map` too:
std:assert_str_eq
(map std:str:to_uppercase
~ filter { (_ 0 1) == "a" }
$["abc", "bcbc", "aaad", "afoo", "foo"])
$["ABC","AAAD","AFOO"];
# Also like `map` works fine with maps, but the function
# needs to take two arguments and returns a pair:
std:assert_str_eq
(std:sort
~ filter { @.0 % 2 == 0 }
${a = 2, b = 43, c = 16, d = 13 })
$[16 => "c", 2 => "a"];
# Generally anything that you can pass into `$iter`:
std:assert_str_eq
(filter { _ % 2 == 0 } 0 => 10)
$[0,2,4,6,8];
```
### <a name="53-accumulation-and-collection"></a>5.3 - Accumulation and Collection
WLambda provides special syntax and semantics for accumulating or collecting
values while iterating through lists. There are following special syntax
constructs:
| Syntax | Semantics |
|-------------------|-----------|
| $@v _expr_ | Setup collection of values in a vector, evaluates _expr_ and returns the vector. |
| $@vec _expr_ | Same as $@v |
| $@m _expr_ | Setup collection of key/value pairs in a map, evaluates _expr_ and returns the vector. |
| $@map _expr_ | Same as $@m |
| $@s _expr_ | Setup appending of values to a string, evaluates _expr_ and returns the string. |
| $@string _expr_ | Same as $@s |
| $@b _expr_ | Setup collection of values in a byte vector, evaluates _expr_ and returns byte vector. |
| $@bytes _expr_ | Same as $@b |
| $@i _expr_ | Setup accumulation in an integer, evaluates _expr_ and returns the integer sum. |
| $@int _expr_ | Same as $@i |
| $@f _expr_ | Setup accumulation in a float, evaluates _expr_ and returns the float sum. |
| $@flt _expr_ | Same as $@f |
| $+ | Evaluated to a function that can be called to add/append a new value to the current collection/accumulation. |
| $@@ | Access the current accumulation value. |
These syntaxes are not lexically scoped. That means `$+` and `$@@` can be used
in other functions:
```wlambda
!out_mul = { $+ _ * 20 };
!v = $@vec iter i $i(1,5) ~ out_mul i;
std:assert_eq (str v) (str $[20, 40, 60, 80]);
```
However, due to issues with coupling your functions to the usage
of accumulators this style is recommended:
```wlambda
!mul = { _ * 20 };
!v = $@vec iter i $i(1,5) ~ $+ mul[i];
std:assert_eq (str v) (str $[20, 40, 60, 80]);
```
#### <a name="531-transforming-a-vector"></a>5.3.1 - Transforming a vector
If you just want to do something with items in a vector and
construct a new one from the results:
```wlambda
!result = $@vec $[1,2,3,4] \$+ _ * 2; # multiply each item by 2
std:assert_eq (str result) "$[2,4,6,8]";
```
#### <a name="532-example-of-"></a>5.3.2 - Example of `$@@`
Here is an interesting example how $@@ might be used:
```wlambda
!list_of_lists = $[];
!result = $@vec $[1,2,3,4] {
$+ 2 * _; # put the value into the list
std:push list_of_lists
~ std:copy $@@; # construct a list of intermediate results
};
std:assert_eq (str result) "$[2,4,6,8]";
std:assert_eq (str list_of_lists)
"$[$[2],$[2,4],$[2,4,6],$[2,4,6,8]]";
```
#### <a name="533-transforming-a-vector-to-a-map"></a>5.3.3 - Transforming a vector to a map
For constructing maps the `$@map` construct is available.
In the following example we transform a vector of pairs into a map:
```wlambda
!result = $@map $[ $[:a, 10], $[:b, 33], $[:c, 99] ] {
!(key, value) = _;
$+ key value;
};
std:assert_eq result.a 10;
std:assert_eq result.b 33;
std:assert_eq result.c 99;
```
#### <a name="534-iteratively-concatenating-strings"></a>5.3.4 - Iteratively concatenating strings
In case you need to construct a longer text the `$@string` construct allows
you to efficiently create a long string. For demonstration purposes
we compare the following inefficient code with the usage of `$@string`:
```wlambda
# Inefficient example:
!accum = "";
$["abc", "def", "ghi", "XXX"] {
.accum = accum _; # allocates a new string each iteration
};
std:assert_eq accum "abcdefghiXXX";
```
In theory for this constructed example the quickest way would
be to use `std:str:join`:
```wlambda
!accum = std:str:join "" $["abc", "def", "ghi", "XXX"];
std:assert_eq accum "abcdefghiXXX";
```
But maybe you need to transform or construct the strings before joining:
```wlambda
!transform = { ">" _ };
!accum = $@string $["abc", "def", "ghi", "XXX"] {
$+[transform _] # appends the string to the accumulation string
};
std:assert_eq accum ">abc>def>ghi>XXX";
```
#### <a name="535-accumulating-sums"></a>5.3.5 - Accumulating sums
The following examples show how accumulation of values with `$@int` and `$@float` work.
```wlambda
!sum = $@int $[1,2,3,4] {
$+ _
};
std:assert_eq sum 10;
```
And with floats:
```wlambda
!sum = $@float $[1.2,1.3,2.2,3.4] {
$+ _
};
std:assert_eq (std:num:round 10.0 * sum) 81.0;
```
### <a name="54-utilities"></a>5.4 - Utilities
#### <a name="541-stdaccum-collection-a-b-"></a>5.4.1 - std:accum _collection_ _a_ _b_ ...
This function accumulates all its arguments in the _collection_.
It does the same form of accumulation as `$+` does.
```wlambda
std:assert_eq (str ~ std:accum $[] 1 2 3) "$[1,2,3]";
std:assert_eq (std:accum "" 1 2 3) "123";
std:assert_eq (str ~ std:accum $b"" 1 2 3) "\x01\x02\x03";
std:assert_eq (str ~ std:accum 10 1 2 3) "16";
```
#### <a name="542-stdzip-vector-map-fn"></a>5.4.2 - std:zip _vector_ _map-fn_
Creates a generator that calls _map_fn_ with the consecutive elements of _vector_
as the last argument of _map-fn_. All arguments passed to std:zip
are appended to the argument list.
This is useful for combining the iteration over two vectors or collections.
```wlambda
!l = $@v $[13, 42, 97] ~ std:zip $["Foo", "Bar", "Baz"] { $+ @ };
std:assert_eq (str l) (str $[$[13, "Foo"], $[42, "Bar"], $[97, "Baz"]]);
```
#### <a name="543-stdfold-accumulator-func-iteratable"></a>5.4.3 - std:fold _accumulator_ _func_ _iteratable_
This function iterates over _iteratable_ while providing the current element
from _iteratable_ as first and the _accumulator_ variable to _func_ as second
argument.
The _accumulator_ for the next iteration is always the return value of the
previous execution of _func_.
This is a convenience function in cases where the accumulator syntax `$@`
does not fit the use-case.
Returns the most recently returned value from _func_.
Calculate the product of the first 5 integers.
```wlambda
!v = std:fold 1 {!(x, acc) = @;
x * acc
} $[1,2,3,4,5];
std:assert_eq v 120;
```
Another contrived example:
```wlambda
!v = std:fold $[] {!(x, acc) = @;
std:displayln @;
((std:cmp:str:asc "c" x) > 0) {
std:push acc x;
};
acc
} "abcdef";
std:assert_eq (str v) (str $['d', 'e', 'f']);
```
#### <a name="544-stdenumerate-map-fn"></a>5.4.4 - std:enumerate _map-fn_
Creates a generator that calls _map-fn_ with a counter that is incremented
after each call, starting with 0. The counter is appended to the
argument list after the regular arguments.
```wlambda
!l = $@v $["lo", "mid", "hi"] ~ std:enumerate { $+ $[_1, _] };
std:assert_eq (str l) (str $[$[0, "lo"], $[1, "mid"], $[2, "hi"]]);
```
## <a name="6-operators"></a>6 - Operators
### <a name="61-operator-assignment"></a>6.1 - Operator Assignment
Please note, that you can use all these operators, as well as special operators
like `=>`, `&>` and `<&` with assignment operations:
```wlambda
!x = 10;
.x += 3;
std:assert_eq x 13;
# also comparison operators work
!y = 10;
.y < = 10;
std:assert_eq y $false;
# function argument pipelining also works in this context
!f = \_ * 10;
.f <&= 10;
std:assert_eq f 100;
!x = 10;
.x &>= \_ * 10;
std:assert_eq f 100;
```
### <a name="62-arithmetic"></a>6.2 - Arithmetic
The output type (float vs. integer) of the numerical arithmetic operators is defined
by the _first_ operand of the operation. Use the casting functions `float` or
`int` if you are unsure.
Please note that not all operators are available as plain identifiers and need
to be quoted when used in their prefix form or as functions, some of them are
`*`, `/`, `%` and some others.
#### <a name="621--operand-1-operand-2-"></a>6.2.1 - + _operand-1_ _operand-2_ ...
This function implements arithmetic addition. If the first operand is a
float number, the substraction will return a float result. If it is an integer
or anything else (like a string), an integer result is returned.
```wlambda
std:assert_eq (+ 5.5 0.5) 6.0;
std:assert_eq (5.5 + 0.5) 6.0;
std:assert_eq (+ 5 2) 7;
std:assert_eq (+ "5" 2) 7;
std:assert_eq (+ :5 2) 7;
```
#### <a name="622---operand-1-operand-2-"></a>6.2.2 - - _operand-1_ _operand-2_ ...
This function implements arithmetic substraction. If the first operand is a
float number, the substraction will return a float result. If it is an integer
or anything else (like a string), an integer result is returned.
```wlambda
std:assert_eq (- 5.5 0.5) 5.0;
std:assert_eq (5.5 - 0.5) 5.0;
std:assert_eq (- 5 2) 3;
std:assert_eq (- "5" 2) 3;
std:assert_eq (- :5 2) 3;
```
#### <a name="623--op-a-op-b"></a>6.2.3 - * _op-a_ _op-b_
Returns the multiplication of the two operands.
```wlambda
std:assert 10 * 4 == 40;
std:assert 10.1 * 4 == 40.4;
std:assert "10" * 4 == 40;
std:assert (`*` 10 4) == 40;
std:assert (float "10.1") * 4 == 40.4;
```
#### <a name="624--op-a-op-b"></a>6.2.4 - / _op-a_ _op-b_
Returns the division of the two operands.
```wlambda
std:assert 10 / 4 == 2;
std:assert 10.0 / 4 == 2.5;
std:assert "10" / 2 == 5;
std:assert (`/` 10 4) == 2;
std:assert (float "10.1") * 4 == 40.4;
```
#### <a name="625--op-a-op-b"></a>6.2.5 - % _op-a_ _op-b_
Returns the remainder of the division of _op-a_ by _op-b_.
```wlambda
std:assert 5 % 4 == 1;
std:assert (`%` 5 4) == 1;
```
#### <a name="626--op-a-op-b"></a>6.2.6 - ^ _op-a_ _op-b_
Returns _op-a_ raised by the power of _op-b_.
Supports float and integers.
```wlambda
std:assert_eq 2 ^ 4 16;
std:assert_eq std:num:round[(2.0 ^ 2.1) * 1000] 4287.0;
std:assert_eq 2 ^ 2.1 4; # first arg type matters!
```
### <a name="63-comparison"></a>6.3 - Comparison
#### <a name="631--op-a-op-b"></a>6.3.1 - == _op-a_ _op-b_
Checks whether the two operands are equal to each other. Data types like
booleans, integers, floats, symbols and strings are compared by their contents.
Other types like vectors, maps, functions, errors or references are compared
by referential equality.
```wlambda
std:assert $none == $none;
std:assert 1 == 2 - 1;
std:assert "aa" == ("a" "a");
std:assert :xxy == :xxy;
std:assert not ~ $[1,2] == $[1,2];
std:assert $p(1,2) == $p(1,2);
std:assert $i(1,2) == $i(1,2);
std:assert $i(1,2,3) == $i(1,2,3);
std:assert not ~ $i(1,2,3) == $f(1.0,2.0,3.0);
std:assert $f(1.0,2.0,3.0) == $f(1.0,2.0,3.0);
std:assert ~ `==` 1 (2 - 1); # prefix form
```
#### <a name="632--op-a-op-b"></a>6.3.2 - != _op-a_ _op-b_
Checks whether the two operands are distinct from each other. Data types like
booleans, integers, floats, symbols and strings are compared by their contents.
Other types like vectors, maps, functions, errors or references are compared
by referential equality.
It's generally the opposite of `==`.
```wlambda
std:assert 1 != 2;
std:assert not[2 != 2];
std:assert "foo" != "bar";
std:assert not["foo" != "foo"];
std:assert ~ `!=` 1 2;
!r1 = $[1,2];
!r2 = $[1,2];
std:assert r1 != r2;
```
#### <a name="633--op-a-op-b"></a>6.3.3 - < _op-a_ _op-b_
Numerical comparison operator that checks whether _op-a_ is less than _op-b_
```wlambda
std:assert 10 < 11;
std:assert 10.1 < 10.2;
std:assert not[10 < 10.1]; # the type of the first argument decides return type!
```
#### <a name="634--op-a-op-b"></a>6.3.4 - <= _op-a_ _op-b_
Numerical comparison operator that checks whether _op-a_ is less or equal to _op-b_
```wlambda
std:assert 10 <= 11;
std:assert 10.1 <= 10.2;
std:assert 10 <= 10.1; # integer <=, the type of the first argument decides return type!
```
#### <a name="635--op-a-op-b"></a>6.3.5 - > _op-a_ _op-b_
Numerical comparison operator that checks whether _op-a_ is greater than _op-b_
```wlambda
std:assert 11.1 > 11;
std:assert 11.1 > 11.0;
std:assert not[10 > 10.1]; # the type of the first argument decides return type!
```
#### <a name="636--op-a-op-b"></a>6.3.6 - >= _op-a_ _op-b_
Numerical comparison operator that checks whether _op-a_ is greater or equal to _op-b_
```wlambda
std:assert 11 >= 11;
std:assert 10.2 >= 10.1;
std:assert 10 >= 10.1; # integer >=, the type of the first argument decides return type!
```
### <a name="64-bit-operations"></a>6.4 - Bit Operations
#### <a name="641--op-a-op-b"></a>6.4.1 - & _op-a_ _op-b_
Binary `and` operation between two integers.
```wlambda
std:assert (0b0011 & 0b1011) == 0b011;
std:assert (3 & 11) == 3;
```
#### <a name="642--op-a-op-b"></a>6.4.2 - &^ _op-a_ _op-b_
Binary `xor` operation between two integers.
```wlambda
std:assert (0b0011 &^ 0b1011) == 0b1000;
std:assert (3 &^ 11) == 8;
```
#### <a name="643--op-a-op-b"></a>6.4.3 - &| _op-a_ _op-b_
Binary `or` operation between two integers.
```wlambda
std:assert (0b0011 &| 0b1000) == 0b1011;
std:assert (3 &| 8) == 11;
```
#### <a name="644--op-a-op-b"></a>6.4.4 - << _op-a_ _op-b_
Binary `left shift` operation of _op-a_ by _op-b_ bits.
```wlambda
std:assert (0b0011 << 3) == 0b11000;
std:assert (`<<` 0b1011 2) == 0b101100
```
#### <a name="645--op-a-op-b"></a>6.4.5 - >> _op-a_ _op-b_
Binary `right shift` operation of _op-a_ by _op-b_ bits.
```wlambda
std:assert (0b0011 >> 2) == 0b0;
std:assert (0b1100 >> 2) == 0b11;
std:assert (`>>` 0b1011000 3) == 0b1011
```
### <a name="65-collection-addition-operators--and-"></a>6.5 - Collection Addition Operators +> and <+
`+>` and `<+` are special operators for convenient and quick collection creation.
You can use them also to call a function with multiple arguments.
```wlambda
!vec = $[] +> 1 +> 2 +> 3;
std:assert_str_eq vec $[1, 2, 3];
!map = ${}
+> (:a => 1)
+> (:b => 2);
map +> (:c => 3);
std:assert_str_eq map ${a=1,b=2,c=3};
```
Usually these operators just append (`+>`) or prepend (`<+`) the right/left hand
side to the collection. But there are some special values which do special things.
First and foremost the iterator data type. If you pass an iterator to this
operator, the iterator will be iterated and all returned elements are added
to the collection in the order of the operator:
```wlambda
!v = $[] +> ($iter 0 => 4) +> ($iter "abc");
std:assert_str_eq v $[0,1,2,3,'a','b','c'];
```
As the `<+` operator prepends the individual elements, the
same is happening with the iterated elements. Which means
that their order is reversed:
```wlambda
!v = ($iter 0 => 4) <+ ($iter "abc") <+ $[];
std:assert_str_eq v $[3,2,1,0,'c','b','a'];
```
The following data types can be used as collection for
these operators:
- Vectors `$[]`
- Maps `${}`
- Strings `""`
- Byte vectors `$b""`
- Functions
The most special cases are maps and functions, which are described
in more detail in the next sections.
#### Collection Addition with Maps
If you add to a map, there is some special behavior for some data types.
Adding a key value pair is done with pairs:
```wlambda
!m = ${}
+> $p(:a, 10)
+> :b => 20;
std:assert_str_eq m ${a=10,b=20};
```
If you add an iterator, the iterator is walked and the given keys are
added if present:
```wlambda
!m = ${}
+> ($iter $[1, 2, 3])
+> ($iter ${ a = 10, b = 20 });
std:assert_str_eq m ${1=1,2=2,3=3,a=10,b=20};
```
If you add a list to a map, the first element of that list is used
as key, and the list as value:
```wlambda
!m = ${}
+> $[:a, 1, 2]
+> $[:b, 3, 4];
std:assert_str_eq m ${a=$[:a,1,2],b=$[:b,3,4]};
```
#### Collection Addition with Function
If you pass a function as collection to either `+>` or `<+` the function
is called for each added element. The return value of the expression
is the return value of the most recent function call.
```wlambda
!v = $[];
!v2 = { std:push v _; v } +> 1 +> 2 +> ${ a = 3, b = 4 };
std:assert_str_eq v $[1,2,${a=3,b=4}];
std:assert_str_eq v2 $[1,2,${a=3,b=4}];
```
#### <a name="651--collection-a-"></a>6.5.1 - +> _collection_ _a_ ...
Append to collection operator.
```wlambda
!v = $[] +> 1 +> "x" +> $b"y" +> ($iter 0 => 3);
!v2 = `+>` $[] 1 "x" $b"y" ($iter 0 => 3);
std:assert_str_eq v v2;
```
#### <a name="652--collection-a-"></a>6.5.2 - <+ _collection_ _a_ ...
Prepend to collection operator. Please note that the arguments are
reversed to the order in an operator expression.
```wlambda
!v = ($iter 0 => 3) <+ 1 <+ "x" <+ $b"y" <+ $[];
!v2 = `<+` $[] $b"y" "x" 1 ($iter 0 => 3);
std:assert_str_eq v v2;
```
## <a name="7-string-and-byte-vector-formatting"></a>7 - String and Byte Vector Formatting
WLambda comes with a built in functionality for string (and byte vector)
formatting. It works by creating a specialized formatting function from a
given string literal at compile time with the `$F"..."` syntax, or a string at
runtime with the `std:formatter _str_` function.
The formatter syntax is documented in detail at [12.2 String Formatting
Syntax](#132-string-formatting-syntax). It is basically the Rust `std::fmt`
Syntax with a few extensions for WLambda data types and the dynamically typed
nature of WLambda.
The WLambda syntax for `$F` is: `$F string-literal`. This means, you can
use any WLambda string literal after `$F`:
```wlambda
$F"..."; # normal string
$F$b"..."; # byte vector
$F$q/.../; # normal string, quote syntax
$F$Q"..."; # byte vector quote syntax
$F$code{ }; # code block string
```
(Please note, that `$code{ ... }` is not as useful in this context, because
the formatter placeholders usually are not valid WLambda syntax.)
This is a very simple example:
```wlambda
!x = "abc";
!s = $F"x = {}" x;
std:assert_eq s "x = abc";
```
You can also use string formatting to generate byte vectors:
```wlambda
!bv = $F$b"x={}" $b"\xFF";
std:assert_eq bv $b"x=\xFF";
```
If you want to generate the WLambda written representation of a piece of
data like `std:write_str` would return it, you have to specify the
special formatting syntax `{:...!w}`:
```wlambda
std:assert_eq ($F"x={:!w}" $&&$[1, 2, 3, 4]) "x=$&&$[1,2,3,4]";
# Without the `!w` the reference would just
# be auto dereferenced like `str` would do it:
std:assert_eq ($F"x={}" $&&$[1, 2, 3, 4]) "x=$[1,2,3,4]";
```
#### <a name="701-stdformatter-format-string"></a>7.0.1 - std:formatter _format-string_
Returns a formatting function that takes exactly the arguments specified
in the _format-string_. If the format syntax is wrong, an error is returned.
This is useful, if you need to build a format string at runtime,
because `$F` only allows string/byte vector literals.
```wlambda
!fmt = ">6.2";
!fmt_fun = (std:formatter (std:str:cat "{1} [{0:" fmt "}]"));
std:assert_eq (fmt_fun 3.43554 1.2323) "1.2323 [ 3.44]";
```
### <a name="71-formatting-numbers"></a>7.1 - Formatting Numbers
Number formatting, that is integers, float and numerical vectors, require an
extension of the formatting syntax. You need to specify whether an integer
`"{:...!i}"` or a float `{:...!f}` is formatted. Otherwise WLambda will cast
everything to a string for formatting.
Here are some examples:
```wlambda
std:assert_eq ($F "{:8!i}" 123) " 123";
std:assert_eq ($F "{:08!i}" 123) "00000123";
std:assert_eq ($F "{:<8!i}" 123) "123 ";
std:assert_eq ($F "{:^8!i}" 123) " 123 ";
std:assert_eq ($F "{:>8!i}" 123) " 123";
std:assert_eq ($F "{:8.2!f}" 123.567) " 123.57";
std:assert_eq ($F "{:08.2!f}" 123.567) "00123.57";
std:assert_eq ($F "{:<8.2!f}" 123.567) "123.57 ";
std:assert_eq ($F "{:^8.2!f}" 123.567) " 123.57 ";
std:assert_eq ($F "{:>8.2!f}" 123.567) " 123.57";
# Note: For floats, the "!f" is implicit if you specify a precision:
std:assert_eq ($F "{:8.2}" 123.567) " 123.57";
std:assert_eq ($F "{:08.2}" 123.567) "00123.57";
std:assert_eq ($F "{:<8.2}" 123.567) "123.57 ";
std:assert_eq ($F "{:^8.2}" 123.567) " 123.57 ";
std:assert_eq ($F "{:>8.2}" 123.567) " 123.57";
```
You can even format numbers in numerical vectors, data vector, pairs or maps:
```wlambda
std:assert_eq ($F "{:8.2}" $f(1.2, 3.456, 8.232)) "( 1.20, 3.46, 8.23)";
std:assert_eq ($F "{:8.2}" $[1.2, 3.456, 8.232]) "[ 1.20, 3.46, 8.23]";
std:assert_eq ($F "{:8.2}" $p(1.2, 3.456)) "( 1.20, 3.46)";
std:assert_eq ($F "{:8.2}" ${x = 1.2, y = 3.456, z = 8.232})
"{x: 1.20, y: 3.46, z: 8.23}";
std:assert_eq
($F "{:>8!i}" $i(1.2, 3.456, 8.232))
"( 1, 3, 8)";
```
Also hexadecimal, octal and binary are supported for integers, they come after the `!i`:
```wlambda
std:assert_eq ($F "{:5!ix}" 321) " 141";
std:assert_eq ($F "{:5!io}" 321) " 501";
std:assert_eq ($F "{:<11!ib}" 321) "101000001 ";
std:assert_eq ($F "{:011!ib}" 321) "00101000001";
```
## <a name="8-data-structure-matchers-selectors-and-string-patternsregex"></a>8 - Data Structure Matchers, Selectors and String Patterns/Regex
WLambda comes with a builtin DSL (domain specific language) for
shallow data structure matches and deep data structure selection and regular expression (regex) pattern
matching on strings. A _selector_ (structure selection) gives you the
ability to search deep into WLambda data structures like
[CSS Selectors](https://www.w3.org/TR/selectors-3/) into HTML DOM trees
or [XPath](https://www.w3.org/TR/xpath-31/) into XML.
While a _structure matcher_, as used by the `match` operation,
allows you to directly match a certain WLambda piece of data.
A subset of the _selectors_ are the _patterns_, which are able to
match strings like regular expressions. The syntax of _patterns_
is a bit different from normal regular expressions like Perl,
Python or JavaScript has. This is partly due to the fact that
these patterns aim to be easily used to match parts of a specific
string like filename globs `photo_???_*.jpg`.
For an in depth description of the _selector_ and _pattern_ syntax
please refer to the [Pattern and Selector Syntax](#821-selector-and-wlambda-regex-syntax).
### <a name="81-data-structure-matcher"></a>8.1 - Data Structure Matcher
This is probably one of the most convenient matching features of WLambda.
While selectors (`$S[a / * / b]`) allow searching deep into data structures,
the matches allow to efficient precise shallow selection and matching.
The `match` operation allows to match a value against multiple matchers,
while the `$M ...` syntax allows to define a matcher function for a single
match (commonly used in an if expression).
For a reference of the matcher syntax see below.
#### <a name="811-match-value-expr-match-pair1--default-expr"></a>8.1.1 - match _value-expr_ _match-pair1_ ... [_default-expr_]
The match operation is a very versatile control flow operation.
#### <a name="812-m-expr"></a>8.1.2 - $M _expr_
This is a structure matcher expression. It will compile _expr_ into a structure
matcher function. The reslting function will match it's first argument agianst
the match and return a map containing the capture variables (or just an empty map).
It will also bind the result map to `$\`. This makes it possible to easily match
a data structure in an if statement:
```wlambda
!some_struct = $[:TEST, ${ a = 10, b = 1442 }];
if some_struct &> ($M $[sym, ${ a = 10, b = x }]) {
std:assert_eq $\.sym :TEST;
std:assert_eq $\.x 1442;
} {
panic "It should've matched!";
};
```
#### <a name="813-data-structure-matcher-syntax"></a>8.1.3 - Data Structure Matcher Syntax
This the the compiletime syntax that is understood by the
structure matchers that are used by `$M ...` and `match`.
- `$M`, `$M1`, `$M2`, ... in the following table stands for a structure matcher expression.
- All other tokens or values stand for themself.
| WLambda Value | Semantics |
|-|-|
| `x` | Matches any value and assigns it to the variable `x`. |
| `?` | Matches any value, but does not assign it. |
| `x $M $M1 ... $Mn` | Assign the value that matched $M, $M1 or $Mn to the variable `x`. |
| `? $M $M1 ... $Mn` | Matches if $M, $M1 or $Mn matches. |
| `_*` | Placeholder for 0 or N items that match any items in the vector. |
| `_+` | Placeholder for 1 or N items that match any items in the vector. |
| `_?` | Placeholder for 0 or 1 items that match any items in the vector. |
| `_* $M` | Placeholder for 0 or N items that match $M in the vector. |
| `_+ $M` | Placeholder for 1 or N items that match $M in the vector. |
| `_? $M` | Placeholder for 0 or 1 items that match $M in the vector. |
| `_type? :integer ...` | Matches an element of one of the given types. Symbol names should have the same name as the type names returned by the `type` function. |
| `$r/.../` | Matches any element, where it's string contents matches the given pattern. |
| `$rg/.../` | Matches any element, where it's string contents matches the given pattern. Returns a list with all global matches. |
| `$M1 &or $M2` | Matches if $M1 or $M2 matches. |
| `$M1 &and $M2` | Matches if $M1 and $M2 matches. |
| `$[$M1, $M2, ...]` | Matches a vector. |
| `${ $Mkey1 = $Mval1, ...}`| Matches a map. $Mkey1 can also be a $M match, but keep in mind that maps can only have symbols as keys. You can however match symbols using regex patterns for instance. If you only use symbols as keys in this match, the map access is optimized a bit, because there is no need to iterate over all keys then. |
| `$p($M1, $M2)` | Matches a pair. |
| `$i($M1, ...)` | Matches an integer vector. |
| `$f($M1, ...)` | Matches a float vector. |
| `$o($M)` | Matches an optional where the value matches $M. |
| `$e $M` | Matches an error value that matches $M. |
| `$n` | Matches $none. |
| literal values | Literal values like booleans, strings, symbols and numbers match their value. |
### <a name="82-data-structure-selectors-s"></a>8.2 - Data Structure Selectors `$S(...)`
This section shows how data structure selectors can be used.
TODO
#### <a name="821-selector-and-wlambda-regex-syntax"></a>8.2.1 - Selector and WLambda Regex Syntax:
```ebnf
(* NOTE: Whitespace is not part of a pattern in most places. This means
if you want to match whitespace, you will have to escape
it either with a '\', with a [ ] character class or match
one whitespace char with $s. *)
class_char = { ?any character except "]"? }
(* special sequence: "\^" => "^" and "\\" => "\"
and "\]" => "]" *)
;
ident_char_in_selector =
(* if regex is used inside a selector: *)
{ ?any character except whitespace,
"!", "?", "/", "\", "|", "^", ",",
"'", "&", ":", ";", "$", "(", ")",
"{", "}", "[", "]", "*" and "="? }
(* allows the usual backslash escaping from strings! *)
;
ident_char_in_direct_pattern =
| (* if regex is used as pattern directly: *)
{ ?any character except whitespace,
"?", "|", "$", "(", ")", "[", "]" and "*"? }
(* allows the usual backslash escaping from strings! *)
;
ident_char = ident_char_in_direct_pattern
| ident_char_in_selector
;
ident = ident_char, { ident_char }
;
index = digit, { digit }
;
rx_atom = pat_glob
| ident_char
;
glob_atom = pat_glob
| ident
;
rx_match_mod = "L" (* transforms the input string from the match
position on to lower case. *)
| "U" (* transforms the input string from the match
position on to upper case. *)
;
pat_regex = "*", rx_atom (* matches sub pattern 0 or N times *)
| "+", rx_atom (* matches sub pattern 1 or N times *)
| "<", [ ("*" | "+" | "?") ], rx_atom
(* non greedy version of the above *)
| "?", rx_atom (* matches sub pattern 0 or 1 times *)
| "!", rx_atom (* matches (zero width) if next pattern does not match *)
| "=", rx_atom (* matches (zero width) if next pattern does match *)
| "^" (* matches (zero width) start of string *)
| "$" (* matches (zero width) end of string *)
| "s" (* matches one whitespace character *)
| "S" (* matches one non-whitespace character *)
| "&", rx_match_mod
;
glob_group = "(", "^", pattern, ")" (* capturing sub group *)
| "(", pattern, ")" (* sub group *)
;
class_range = class_char, "-", class_char (* contains a range of chars, eg. [a-z] *)
;
glob_cclass = "[", { class_char | class_range }, "]" (* character class match for 1 char *)
| "[^", { class_char | class_range }, "]" (* negated character class match for 1 char *)
;
pat_glob = "*" (* 0 or N any characters *)
| "?" (* any character *)
| "$", pat_regex
| glob_cclass
| glob_group
;
pat_branch = { glob_atom }
;
pattern = pat_branch, [ "|", pattern ]
;
key = index | pattern
;
kv = key, "=", pattern
;
kv_item = "{", kv, { ",", kv }, "}"
;
node_match = ":", ["!"], "(", selector, ")"
| ":", ["!"], kv_item
| ":", ["!"], "type", "=", pattern
(* pattern is matched against
vval type as returned by `type` *)
| ":", ["!"], "str", "=", pattern
(* pattern is matched against
the string contents or stringified
representation of the value *)
;
node_cond = node_match
| node_match, "&", node_cond
| node_match, "|", node_cond
;
reckey_cond = "!", "key", "=", pattern
(* recurse only into values if they are not referred
to by a key matching the given pattern. *)
;
recval_cond = "=", node_cond
(* recurse only into values if they match the given
condition *)
;
node = key, { node_cond }
(* marks it for referencing it in the result set *)
| "**", [ reckey_cond ], [ recval_cond ], { node_cond }
(* deep expensive recursion *)
| "^", node
;
selector = node, { "/", node }
;
```
#### <a name="822-stdselector-string"></a>8.2.2 - std:selector _string_
Parses the given _string_ as WLambda data structure selector and returns
a function that takes a data structure as first argument. That function will
then query the data structure according to the given selector.
That function will also set the global variable `$\` to the result.
The main usage of this function is, when you want to define the selector
at runtime. Otherwise WLambda provides the handy `$S(...)` syntax for
generating the structure pattern function at compile time.
```wlambda
!runtime_name = "foo";
!sel = std:selector (" * / " runtime_name);
if sel <& $[${foo = 1}, ${foo = 2}, ${foo = 3}] {
std:assert_str_eq $\ $[1,2,3];
} {
std:assert $false
};
```
### <a name="83-string-patterns-regex-r"></a>8.3 - String Patterns (Regex) `$r/.../`
This section shows how to use the builtin pattern regex engine
in WLambda. You can embed patterns directly in your WLambda source
with `$rQ...Q`. Where `Q` stands for the usual string quoting mechanism
in WLambda (like `$q/foo/`, `$q(foo bar)`, ...). This has the advantage
that the pattern syntax is checked on compile time of your WLambda program.
The result of the expression `$r/foo/` is a function, which takes as first
arguments a string and returns a vector of substrings of that input
string. First element of that vector is always the matched sub string
of the input string. All elements after that correspond to a pattern
capture `(^...)` like in `$r/foo(^bar)/`.
The function returns `$none` if the pattern could not be found in the input string.
Lets start off with a simple example:
```wlambda
# Please note: Whitespace inside the pattern is allowed and will not be matched!
!res = $r/a (^*) b/ "fooaxxxxbber";
std:assert_eq res.0 "axxxxbb";
std:assert_eq res.1 "xxxxb";
```
To match a whole string you can anchor using `$^` and `$$`:
```wlambda
!res = $r/$^ a (^*) b $$/ "axxxxbb";
std:assert_eq res.0 "axxxxbb";
std:assert_eq res.1 "xxxxb";
```
To match special the characters `$` you can use the backslash escaping `\$`:
```wlambda
std:assert_eq ($r/$+ \$/ "FF$$$FF").0 "$$$";
```
To access captured groups you can either use the return value of the
matcher function, or use the global variable `$\` which will contain
the results of the latest match that was executed:
```wlambda
# Notice the usage of the `<&` function call operator:
!res =
if "foo//\\/foo" &> $r| $<*? (^$+[\\/]) * | {
std:assert_eq $\.0 "foo//\\/foo";
$\.1
};
std:assert_eq res "//\\/";
```
#### <a name="831-global-patterns-rg"></a>8.3.1 - Global Patterns `$rg/.../`
With the `g` modifier the regex can be modified and will match the input
string with the given pattern repeatedly and call a given function
for each match.
The match function will receive the input string as first argument
and a function that will be called for each match as second argument.
Inside the match function, you can use the control flow functions `break`
and `next` to skip ahead.
The match function receives the contents of `$\` as first argument,
the offset of the match in the input string as second argument
and the length of the match as third argument:
```wlambda
!found = $@vec $rg/x(^?)y/ "aax9yaaxcy" {!(match, offs, len) = @;
$+ $[match.1, offs, len]
};
std:assert_str_eq found $[$["9", 2, 3], $["c", 7, 3]];
```
#### <a name="832-pattern-substitutions-rs"></a>8.3.2 - Pattern Substitutions `$rs/.../`
The `s` modifier creates a substitution that will substitute each match
of the pattern in the given input string with the return value of the
match function. The match function is called with the same values as `$rg`
does.
```wlambda
!digits =
$["zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine"];
!ret = $rs/[0-9]/ "on 1 at 0 of 8" {!(match, offs, len) = @;
digits.(int match.0)
};
std:assert_eq ret "on one at zero of eight";
```
Inside the match function, you can use the control flow functions `break`
and `next`. You can use that to control which occurence within the string to
replace:
```wlambda
!res = $rs/xxx/ "fxxxfxxxfxxxf" { break "O" };
std:assert_eq res "fOfxxxfxxxf";
```
#### <a name="833-pattern-syntax-overview"></a>8.3.3 - Pattern Syntax Overview
While
[Selector and WLambda Regex Syntax](#821-selector-and-wlambda-regex-syntax)
describes the pattern syntax in detail,
here is the WLambda pattern regex syntax in a nutshell:
| Pattern Syntax | Semantics |
|-|-|
| `?|$()[]*` | Many special chars are reserved in WLambda patterns. Be aware that more characters are reserved if you use the patterns in a data structure selector, instead of a single pattern. Please escape then using backslash like `\\/` or `[/]`.|
| _whitespace_ | Please note, that whitespace to be matched must be escaped using '\' or inside a character calss `[ ]`. |
| `\.` | Backslash escapes work the same as in regular WLambda strings. `\` escapes the following character to have no special syntactic meaning in a pattern except matching itself. While escape sequences like `\x41` match the character `A` or `\u{2211}` matches `∑`. These also work inside of character classes. |
| `*` | Match 0 to N occurences of any character. |
| `?` | Match 1 occurences of any character. |
| `(...)` | A match group (does not capture). |
| `(^...)` | A capturing match group. |
| `[abcA-Z]` | A character class, matching the listed characters or ranges. |
| `[^abcA-Z]` | A negative character class, matching all character except the listed characters or ranges. |
| `$^` | String start anchor. Matches only the start of the string. Useful for specifying patterns that match the complete string (in combination with `$$`). |
| `$$` | String end anchor. Matches only the end of the string. Useful for specifying patterns that must match the complete string. |
| `$*X` | Greedly matches the pattern part `X` 0 or N times. For grouping pattern parts use `(...)` like in `$*(abc)`. |
| `$<*X` | Non-greedly matches the pattern part `X` 0 or N times. |
| `$+X` | Greedly matches the pattern part `X` 1 or N times. For grouping pattern parts use `(...)` like in `$+(abc)`. |
| `$<+X` | Non-greedly matches the pattern part `X` 1 or N times. |
| `$?X` | Greedly matches 0 or 1 occurences of the pattern part `X`. Like usual, you can group using `(...)`. |
| `$<?X` | Non-greedly matches 0 or 1 occurences of the pattern part `X`. |
| `$!X` | Zero-width negative look-ahead. `($^$!a*)` matches any string not starting with an `a`. |
| `$=X` | Zero-width positive look-ahead. `($^$=a*)` matches any string starting with an `a`. |
| `$s` | Matches one (Unicode) whitespace character. |
| `$S` | Matches one (Unicode) non-whitespace character. |
| `$&L` | Transforms the input string for the following pattern matching parts to lowercase (attention: O(n) operation on the complete rest of the string!). Useful for matching case-insensitively. |
| `$&U` | Transforms the input string for the following pattern matching parts to uppercase (attention: O(n) operation on the complete rest of the string!). Useful for matching case-insensitively. |
#### <a name="834-standard-regular-expressions"></a>8.3.4 - Standard Regular Expressions
Please note that WLambda can optionally be compiled with the `regex` crate,
which implements a more common syntax for regular expressions.
Please refer to the functions `std:re:match` in the WLambda standard library
for this.
#### <a name="835-stdpattern-string-mode"></a>8.3.5 - std:pattern _string_ [_mode_]
Compiles the regex pattern _string_ to a function just like `$r/.../` would do.
The _mode_ can either be `:g` (global match like `$rg...`), `:s` (substitution
like `$rs...`) or `$none`. Useful for composing WLambda patterns at runtime:
```wlambda
!rx = std:pattern ~ std:str:cat "(^" "$+" "[a-z]" ")";
std:assert_eq (rx "foo").1 "foo";
```
Returns an error if the syntax failes to parse as pattern:
```wlambda
!err = unwrap_err ~ std:pattern "($+[a-z]";
std:assert_eq $i(0, 11)[err] "bad pattern";
```
Here an example of substitution:
```wlambda
!subs = std:pattern "$+x" :s;
std:assert_eq subs["fooxxxoxx", \"a"] "fooaoa";
```
## <a name="9-modules"></a>9 - Modules
### <a name="91-export"></a>9.1 - export
```wlambda
!expr = { _ + 30 };
!@export symbol = expr; # exports symbol with value of expr (a function)
```
**Warning:** Do not expect the declared variables in the module to exist beyond
execution time. Weak caught values will vanish like usual once the module scope
is exited. This means, if you declare helper functions in local variables, do
this with the `:global` modifier:
```wlambda
!:global helper = { _ * 2 };
!@export doit = { helper 10 };
```
Alternatively make the helper a strong reference:
```wlambda
!helper = $&& { _ * 2 };
!@export doit = { helper 10 };
```
### <a name="92-import"></a>9.2 - import
```wlambda
!@import x = tests:test_mod; # prefixes everything from modixes with x:
std:assert ~ (x:symbol 10) == 40;
```
You can also skip the prefix:
```wlambda
!@import std;
!v = $[];
std:push v 10;
std:push v 20;
std:assert_eq (str v) "$[10,20]";
```
## <a name="10-core-library"></a>10 - Core Library
This library contains all the core functions which belong to the
core of the WLambda Programming Language. These functions can be seen
as keywords of WLambda. Some functions are also available as operators.
#### <a name="1001-type-value"></a>10.0.1 - type _value_
Returns the name of the data type of _value_ as string.
```wlambda
std:assert_eq (type 10) "integer";
std:assert_eq (type 10.0) "float";
std:assert_eq (type {}) "function";
!y = $&&std:to_drop { };
std:assert_eq (type y) "drop_function";
std:assert_eq (type :s) "symbol";
std:assert_eq (type "s") "string";
std:assert_eq (type $[]) "vector";
std:assert_eq (type ${}) "map";
std:assert_eq (type $b"") "bytes";
std:assert_eq (type $n) "none";
std:assert_eq (type $t) "bool";
std:assert_eq (type $e $n) "error";
std:assert_eq (type $&&10) "ref_strong";
std:assert_eq (type $&10) "ref_hidden";
!x = $&&10;
std:assert_eq (type ~ $w&x) "ref_weak";
```
#### <a name="1002-len-value"></a>10.0.2 - len _value_
Returns the length of _value_. Depending on the data type you will get
different semantics.
```wlambda
# Always zero for scalar non sequential/collection values:
std:assert_eq (len 10) 0;
std:assert_eq (len 10.1) 0;
std:assert_eq (len $t) 0;
std:assert_eq (len $f) 0;
std:assert_eq (len $n) 0;
std:assert_eq (len "\xFF") 2; # byte length of the UTF-8 string
std:assert_eq (len $b"\xFF") 1;
std:assert_eq (len $[1,2,3,4,5]) 5;
std:assert_eq (len ${a=1, b=2}) 2;
std:assert_eq (len ${a=1, b=2}) 2;
```
#### <a name="1003-panic-message"></a>10.0.3 - panic _message_
If your program runs into something that deserves a slap on the fingers
of the developer you can use `panic` to do that.
## <a name="11-standard-library"></a>11 - Standard Library
#### <a name="1101-stdshuffle-randfunc-vec"></a>11.0.1 - std:shuffle _rand\_func_ _vec_
Shuffles the _vec_ in place. The function _rand_func_ needs to return
a random 64 bit integer on each call. Here is an example:
```wlambda
std:srand 1234;
!vec = $[1,2,3,4,5,6,7,8];
std:shuffle { std:rand :i64 } vec;
std:assert_eq (str vec) "$[2,1,7,4,8,5,3,6]";
```
An Example with std:rand:split\_mix64\_next:
```wlambda
!sm = std:rand:split_mix64_new_from 1234;
!vec = $[1,2,3,4,5,6,7,8];
std:shuffle { std:rand:split_mix64_next sm } vec;
std:assert_eq (str vec) "$[2,1,7,4,8,5,3,6]";
```
#### <a name="1102-stddelete-vector-or-map-index-or-key"></a>11.0.2 - std:delete _vector-or-map_ _index-or-key_
This removes the designated element from the collection (either vector or map).
This works for:
- Vectors:
```wlambda
!v = $[1,2,3];
std:assert_eq (std:delete v 1) 2;
std:assert_eq (str v) (str $[1,3]);
```
- Maps:
```wlambda
!m = ${a = 10, b = 20};
std:assert_eq (std:delete m :a) 10;
std:assert_eq (str m) (str ${b = 20});
```
Please note that this operation is potentially O(n) on vectors.
#### <a name="1103-stdrefid-value"></a>11.0.3 - std:ref\_id _value_
Returns an integer identifier for a given referential value.
The ID will stay the same as long as the reference is allocated.
This returns a value for all data types that have some form
of internal reference to a value on the heap.
The main usage of this function is to get a pre process unique ID
for an allocated value. But be aware, that once the value is deallocated,
the reference ID does not belong to that value anymore.
```wlambda
!v = $[1,2,3];
!v_id1 = std:ref_id v;
std:push v 4;
!v_id2 = std:ref_id v;
std:assert_eq v_id1 v_id2;
```
#### <a name="1104-stdcopy-vecormap"></a>11.0.4 - std:copy _vec\_or\_map_
Makes a shallow copy of the given vector or map.
```wlambda
!a = $[1,2,3];
!b = std:copy a;
b.0 = 10;
std:assert_eq a.0 1;
std:assert_eq b.0 10;
```
#### <a name="1105-stdvalues-collection-or-iter"></a>11.0.5 - std:values _collection-or-iter_
This function returns all values in the given collection or iterator
as vector. _collection-or-iter_ can have be one of the following data
types:
- vector
- numerical float or integer vector
- map
- iterator `$iter`
```wlambda
std:assert_str_eq (std:values $iter 0 => 5) $[0,1,2,3,4];
std:assert_str_eq (std:values ${a = 10}) $[10];
std:assert_str_eq (std:values $iter ${a = 10}) $[10];
std:assert_str_eq (std:values $[1,2,3]) $[1,2,3];
std:assert_str_eq (std:values $i(1,2,3)) $[1,2,3];
```
#### <a name="1106-stdkeys-collection-or-iter"></a>11.0.6 - std:keys _collection-or-iter_
This function returns all keys in the given _collection_ or _iterator_.
It's most useful for the map data type, but also returns the indices in
a vector or numerical vector.
```wlambda
std:assert_str_eq (std:keys ${a = 10}) $["a"];
std:assert_str_eq (std:keys $iter ${a = 10}) $["a"];
std:assert_str_eq (std:keys $[3,3,3]) $[0,1,2];
std:assert_str_eq (std:keys $i(4,4,4)) $[0,1,2];
std:assert_str_eq (std:keys $i(4,4)) $[0,1];
```
#### <a name="1107-stdsort-comparefun-vec"></a>11.0.7 - std:sort [_compare\_fun_] _vec_
Sorts the given _vec_ in place. The comparison function _compare_fun_ gets the
two values a and b and needs to return -1 if a < b, 0 if a = b and 1 if a > b.
There are four functions that implement numeric and lexicographic ordering:
- `std:cmp:num:asc`
- `std:cmp:num:desc`
- `std:cmp:str:asc`
- `std:cmp:str:desc`
If no _compare_fun_ is given, the ordering will be ascending and lexicographic
vs. numeric will be chosen by the type of the `a` value (if it is an integer or
float it will be numeric, otherwise lexicographic).
```wlambda
!v = $[$[1], $[-1], $[3]];
std:sort { std:cmp:num:desc _.0 _1.0 } v;
std:assert_eq v.0.0 3;
std:assert_eq v.1.0 1;
std:assert_eq v.2.0 -1;
```
#### <a name="1108-stdcmpnumasc-a-b"></a>11.0.8 - std:cmp:num:asc _a_ _b_
Compares _a_ and _b_ numerically and returns:
| Cases | Return Value |
|---------------|--------------|
| _a_ > _b_ | -1 |
| _a_ == _b_ | 0 |
| _a_ < _b_ | 1 |
```wlambda
std:assert_eq (std:cmp:num:asc 20 2) -1;
std:assert_eq (std:cmp:num:asc "20" "20") 0;
std:assert_eq (std:cmp:num:asc 20 21) 1;
```
#### <a name="1109-stdcmpnumdesc-a-b"></a>11.0.9 - std:cmp:num:desc _a_ _b_
Compares _a_ and _b_ numerically descending and returns:
| Cases | Return Value |
|---------------|--------------|
| _a_ > _b_ | 1 |
| _a_ == _b_ | 0 |
| _a_ < _b_ | -1 |
```wlambda
std:assert_eq (std:cmp:num:desc "20" "2") 1;
std:assert_eq (std:cmp:num:desc "20" "20") 0;
std:assert_eq (std:cmp:num:desc 20 21) -1;
```
#### <a name="11010-stdcmpstrasc-a-b"></a>11.0.10 - std:cmp:str:asc _a_ _b_
Compares _a_ and _b_ lexicographically by their byte values. This orders
Unicode code points based on their positions in the code charts.
| Cases | Return Value |
|---------------|--------------|
| _a_ > _b_ | -1 |
| _a_ == _b_ | 0 |
| _a_ < _b_ | 1 |
```wlambda
std:assert_eq (std:cmp:str:asc "abc" "aba") -1;
std:assert_eq (std:cmp:str:asc "abc" "abc") 0;
std:assert_eq (std:cmp:str:asc "abc" "abd") 1;
```
#### <a name="11011-stdcmpstrdesc-a-b"></a>11.0.11 - std:cmp:str:desc _a_ _b_
Compares _a_ and _b_ lexicographically by their byte values. This orders
Unicode code points based on their positions in the code charts.
| Cases | Return Value |
|---------------|--------------|
| _a_ > _b_ | 1 |
| _a_ == _b_ | 0 |
| _a_ < _b_ | -1 |
```wlambda
std:assert_eq (std:cmp:str:desc "abc" "aba") 1;
std:assert_eq (std:cmp:str:desc "abc" "abc") 0;
std:assert_eq (std:cmp:str:desc "abc" "abd") -1;
```
#### <a name="11012-stdreverse-value"></a>11.0.12 - std:reverse _value_
Reverses the given sequence of values. This works for following data types:
- Strings
- Byte vectors
- Vectors
- Numeric vectors
- Iterators
```wlambda
std:assert_str_eq (std:reverse $[1, 2, 3, 4]) $[4,3,2,1];
std:assert_str_eq (std:reverse "ABC") "CBA";
std:assert_str_eq (std:reverse $b"ABC") $b"CBA";
std:assert_str_eq (std:reverse $i(1,2,3,4)) $i(4,3,2,1);
std:assert_str_eq (std:reverse $f(1.1,2.2,3.3,4.4)) $f(4.4,3.3,2.2,1.1);
```
#### <a name="11013-stddisplayln-arg1-"></a>11.0.13 - std:displayln _arg1_ ...
This function writes a humand readable version of all the arguments
(with a space inbetween) to the standard output. This means that:
```text
std:displayln "foo"
```
Will just print `foo` and a newline.
If you need a less ambigous form, use `std:writeln`, which
handles its argument like written via `std:ser:wlambda` instead of `str`.
#### <a name="11014-debug-arg1-"></a>11.0.14 - $DEBUG _arg1_ ...
This is a special value that evaluates to a print function that supplies the
current position in the source code. For example this:
```wlambda
!k = $[1, 2, 3];
$DEBUG "I got values:" k 99;
```
Will print this (assuming it's at line 1 col 3 in file `file_foo.wl`):
```text
[1,3:<file_foo.wl>] DEBUG: "I got values:"(string) $[1,2,3](vector) 99(integer)
```
In case you want to directly write a string or some value, you will have
to prefix the argument with the symbol `:\`:
```wlambda
!k = 30;
$DEBUG :\ "k =" :\ k;
```
Will print like this:
```text
[1,11:<wlambda::eval>] DEBUG: k = 30
```
#### <a name="11015-stdwriteln-arg1-"></a>11.0.15 - std:writeln _arg1_ ...
This function writes the WLambda representation of its arguments
(with a space inbetween) to standard output. This means that:
```text
std:displayln "foo"
```
Will print `"foo"` and a newline.
See also the description of `std:ser:wlambda`.
If you need a more human readable form use `std:displayln`.
#### <a name="11016-stdeval-code-string"></a>11.0.16 - std:eval _code-string_
Evaluates _code-string_ in the current global environment and returns
the generated value. If the code leads to any kind of evaluation error,
an error object is returned.
```wlambda
std:assert_eq (std:eval "1 + 2") 3;
!:global X = 20;
std:assert_eq (std:eval "1 + X") 21;
```
#### <a name="11017-stdassert-bool-message"></a>11.0.17 - std:assert _bool_ \[_message_]
Just a simple assertion function that panics if the first argument is not true.
Returns the passed value if it is a true value.
You can pass an optional message as second parameter.
```norun_wlambda
std:assert $false; #=> Panic
std:assert 120; #=> 120
```
#### <a name="11018-stdasserteq-actual-expected-message"></a>11.0.18 - std:assert\_eq _actual_ _expected_ \[_message_]
This function checks if the _actual_ value is equal to the
_expected_ value and panics if not. The optional _message_ is
passed in the panic for reference.
```wlambda
!x = 30 * 2;
std:assert_eq x 60 "30 * 2 == 60";
```
#### <a name="11019-stdassertstreq-actual-expected"></a>11.0.19 - std:assert\_str\_eq _actual_ _expected_
This function stringifies _actual_ and _expected_ using the `str` function
and compares the resulting strings.
This is very useful to compare data structures, as map keys are sorted
if the maps are stringified using `str`:
```wlambda
std:assert_str_eq $[1, 2, 3] $[1, 2, 3];
```
#### <a name="11020-stdassertreleq-l-r-epsilon-message"></a>11.0.20 - std:assert\_rel\_eq _l_ _r_ _epsilon_ \[_message_]
This function checks if `l` is within `epsilon` of `r`.
If the absolute value of the difference between `l` and `r` is greater than `epsilon`,
this function will panic, also displaying the optional message if present.
```wlambda
# these two are within 1 of each other
!x = 10.5;
!y = 11.3;
std:assert_rel_eq x y 1;
# but not within 0.5 of each other, so this line is commented out.
# std:assert_eq x y 0.5;
```
#### <a name="11021-stdmeasuretime-unit-function"></a>11.0.21 - std:measure\_time _unit_ _function_
This function measures the time the given _function_ took to execute.
The _unit_ defines how precisely the time is measured. Following strings are supported
units:
- `s` - seconds
- `ms` - milliseconds
- `us` - microseconds
- `ns` - nanoseconds
The return value is a vector where the first element is the
time it took to execute the function, and the second element is the
return value of that function.
```wlambda
!res = std:measure_time :ns { $@i iter i 0 => 100000 { $+ i } };
std:assert res.0 > 100;
std:assert_eq res.1 4999950000;
```
### <a name="111-io"></a>11.1 - I/O
#### <a name="1111-stdioline"></a>11.1.1 - std:io:line
Reads a line from standard input and returns it. Returns an error if something
went wrong.
```text
!line = unwrap std:io:line[];
std:displayln "you entered: " std:str:trim_end[line];
```
#### <a name="1112-stdiolines-value"></a>11.1.2 - std:io:lines [_value_]
Calls the given _value_ for each line in standard input until EOF and returns
the last returned value from that call. If _value_ is not given, all lines
will be appended to a new vector and returned. Returns an error if some IO
error occured.
```text
!lines = std:io:lines[];
!lines = $@v std:io:lines $+;
std:io:lines {!(line) = @;
std:displayln "You entered: [" std:str:trim[line] "]";
};
```
#### <a name="1113-stdiofilereadtext-filename"></a>11.1.3 - std:io:file:read\_text _filename_
Opens the file _filename_ and returns its contents interpreted as UTF8
text as string.
```wlambda
std:io:file:write_safe "prelude_test.txt" "abcäöü";
!t = std:io:file:read_text "prelude_test.txt";
std:assert_eq t "abcäöü" "reading text from file works";
```
#### <a name="1114-stdiofileread-filename"></a>11.1.4 - std:io:file:read _filename_
Opens the file _filename_ and returns its contents as byte buffer.
```wlambda
std:io:file:write_safe "prelude_test.txt" "abcäöü";
!t = std:io:file:read "prelude_test.txt";
.t = std:str:from_utf8 t;
std:assert_eq t "abcäöü" "reading binary from file works";
```
#### <a name="1115-stdiofilewritesafe-filename-bytes-or-string"></a>11.1.5 - std:io:file:write\_safe _filename_ _bytes-or-string_
Creates a new file with the given filename but with a "~" appended
and writes the contents into it. After successful write, it renames
the file to the given filename.
#### <a name="1116-stdiofileappend-filename-bytes-or-string"></a>11.1.6 - std:io:file:append _filename_ _bytes-or-string_
Opens the given filename in append mode and appends _bytes-or-string_ to the
end of the file.
#### <a name="1117-stdiostdoutnewline"></a>11.1.7 - std:io:stdout:newline
Writes a newline to standard output. Returns an error if an error occured or
`$true` otherwise.
#### <a name="1118-stdiostdoutflush"></a>11.1.8 - std:io:stdout:flush
Flushes the standard output buffer. Returns an error if an error occured or
`$true` otherwise.
#### <a name="1119-stdiostdoutprint-value"></a>11.1.9 - std:io:stdout:print _value_
Writes the given _value_ to standard output in a human readable form like `std:displayln`
does. Returns an error if an error occured. `$true` if everything is fine.
```text
std:io:stdout:write "xxx"; # => Writes `xxx` to standard output
```
#### <a name="11110-stdiostdoutwrite-value"></a>11.1.10 - std:io:stdout:write _value_
Writes a WLambda representation of _value_ to standard output.
Returns an error if an error occured. `$true` if everything is fine.
```text
std:io:stdout:write "xxx"; # => Writes `"xxx"` to standard output
```
#### <a name="11111-stdioflush-handle"></a>11.1.11 - std:io:flush _handle_
Flushes the internal buffers of _handle_. _handle_ can be any kind of IO handle,
like a file handle or networking socket.
```text
!socket = unwrap ~ std:net:tcp:connect "127.0.0.1:80";
std:io:write socket $b"GET / HTTP/1.0\r\n\r\n";
std:io:flush socket;
```
#### <a name="11112-stdioreadsome-handle"></a>11.1.12 - std:io:read\_some _handle_
Reads some amount of data from _handle_. The default maximum amount
of bytes read is 4096. This function returns `$o(bytes)` if something
was read. It returns `$o()` when EOF is encountered. `$none` is
returned when the IO operation was interrupted or did timeout.
An `$error` is returned if some kind of error happened, like loss of
TCP connection.
Here is an example how to read everything from a socket until EOF is
encountered:
```text
!socket = unwrap ~ std:net:tcp:connect "127.0.0.1:80";
std:io:write socket $b"GET / HTTP/1.0\r\n\r\n";
std:io:flush socket;
!buf = $b"";
!done = $f;
while not[done] {
match std:io:read_some[socket]
$o(buf) => { .buf = buf +> $\.buf; }
$o() => { .done = $t; }
($e _) => { .done = $t; };
};
```
#### <a name="11113-stdiowrite-handle-data-offs"></a>11.1.13 - std:io:write _handle_ _data_ [_offs_]
Write all data as byte vector to the IO _handle_ (socket, file handle, ...),
starting at _offs_ (0 default).
Returns the number of bytes written, an error or `$none` if interrupted.
Note: This function does not respect the underlying write timeout in _handle_ properly.
```text
!socket = unwrap ~ std:net:tcp:connect "127.0.0.1:80";
std:io:write socket $b"GET / HTTP/1.0\r\n\r\n";
```
#### <a name="11114-stdiowritesome-handle-data"></a>11.1.14 - std:io:write\_some _handle_ _data_
Try to write some data as byte vector to the IO _handle_ (socket, file handle,
...), starting at _offs_ (0 default).
Returns the number of bytes written, an error or `$none` if a timeout or interrupt
ended the write operation.
```text
!socket = unwrap ~ std:net:tcp:connect "127.0.0.1:80";
!bytes = $b"GET / HTTP/1.0\r\n\r\n";
!written = 0;
while len[bytes] > written {
match (std:io:write_some socket bytes written)
$o(n) => { .written += n; }
$none => {}
($e _) => { break[] };
}
```
### <a name="112-networking"></a>11.2 - Networking
#### <a name="1121-stdnettcpconnect-socket-addr-connect-timeout"></a>11.2.1 - std:net:tcp:connect _socket-addr_ [_connect-timeout_]
This tries to connect to _socket-addr_. If a _connect-timeout_ is given, it
will try to connect within that time. Please note that the returned sockets are
thread safe and can be passed to another thread via an _Atom_, _Atom Value
Slot_ or _Channel_ for instance.
_socket-addr_ can be:
- A pair `$p(host, port)`
- A string like "host:port".
- A pair within a pair to specify whether to use IPv4 or IPv6
addresses only:
- `$p(:v4, "host:port")`
- `$p(:v4, $p(host, port))`
- `$p(:v6, "host:port")`
- `$p(:v6, $p(host, port))`
About _connect-timeout_ see std:thread:sleep.
```test
!socket =
match (std:net:tcp:connect "127.0.0.1:8328")
($e err) => { panic ("Couldn't connect: " + str[$\.err]) }
socket => $\.socket;
```
#### <a name="1122-stdnettcplisten-socket-addr-function"></a>11.2.2 - std:net:tcp:listen _socket-addr_ _function_
Tries to bind a local port to _socket-addr_ (see std:net:tcp:connect about
_socket-addr_. Note: you can't use `$p(:v4 / :v6, ...)`).
Returns an error if something bad happened.
For every new connection _function_ is called with the socket
as first argument:
```text
unwrap ~ std:net:tcp:listen "0.0.0.0:8292" {!(socket) = @;
std:io:write socket "Hello!\r\n";
};
```
Please note that you can share the socket with other threads, see also `std:net:tcp:connect`.
#### <a name="1123-stdnetudpnew-socket-addr-connect-addr"></a>11.2.3 - std:net:udp:new _socket-addr_ [_connect-addr_]
Creates a new UDP socket and binds it to an endpoint. The arguments
_socket-addr_ and _connect-addr_ have the same properties as the _socket-addr_
that `std:net:tcp:connect` receives.
If _connect-addr_ is given, a connected UDP port is created and
`std:net:udp:send` does not need to pass a _socket-addr_.
Returns a socket or an error.
The socket can be shared between threads, so you can have a receiving
thread and a sending one.
```wlambda
!socket = std:net:udp:new "0.0.0.0:31889";
std:net:udp:send socket $b"TEST" "127.0.0.1:31888";
```
Here is a more elaborate example using threads:
```wlambda
!hdl = std:thread:spawn $code {
!socket = std:net:udp:new "0.0.0.0:31889";
_READY.send :ok;
!(data, addr) = std:net:udp:recv socket;
std:displayln "PING" data;
unwrap ~ std:net:udp:send socket ("Test:" data) addr;
};
hdl.recv_ready[];
!socket = std:net:udp:new "0.0.0.0:31888" "127.0.0.1:31889";
unwrap ~ std:net:udp:send socket $b"XYB123";
!(data, addr) = unwrap ~ std:net:udp:recv socket;
std:displayln "PONG" data;
std:assert_eq data $b"Test:XYB123";
hdl.join[];
```
#### <a name="1124-stdnetudpsend-socket-data-socket-addr"></a>11.2.4 - std:net:udp:send _socket_ _data_ [_socket-addr_]
Sends the _data_ to the given _socket-addr_ or to the connected
address of the _socket_.
Returns the number of bytes sent or an error.
```wlambda
!socket = std:net:udp:new "0.0.0.0:31889";
std:net:udp:send socket $b"TEST" "127.0.0.1:31888";
```
#### <a name="1125-stdnetudprecv-socket-byte-count"></a>11.2.5 - std:net:udp:recv _socket_ [_byte-count_]
Receives _byte-count_ number of bytes from the given _socket_.
If _byte-count_ is omitted 512 is assumed.
Returns the byte vector with the data and the endpoint address
that it was received from. Or an error.
```text
!socket = std:net:udp:new "0.0.0.0:31889";
!(buf, addr) = std:net:udp:recv socket;
```
### <a name="113-processes"></a>11.3 - Processes
This chapter documents how to execute new processes.
#### <a name="1131-stdprocessrun-executable-path-arguments"></a>11.3.1 - std:process:run _executable-path_ [_arguments_]
Starts the given _executable-path_ with the given (but optional) _arguments_.
_arguments_ can be a vector or an iterator. A data structure containing
information about the finished child process or an error is returned if something
went wrong.
This function will block the current thread while the child process
is executed. It collects the output of the child process and returns
it in a data structure of the following form:
```text
{
status = 0, # exit code
success = $true, # $true or $false
stdout = $b"...", # data printed to stdout
stderr = $b"...", # data printed to stderr
}
```
Here is an example for Linux:
```text
!ret = unwrap ~ std:process:run "sh" $["-c", "echo \"test\""];
std:assert_eq ret.status 0;
std:assert_eq ret.success $true;
std:assert_eq ret.stdout $b"test\n";
std:assert_eq ret.stderr $b"";
```
#### <a name="1132-stdprocessspawn-executable-path-arg-vector-inheritout--inheritall"></a>11.3.2 - std:process:spawn _executable-path_ _arg-vector_ [:inherit\_out | :inherit\_all]
Like `std:process:run` starts a process from the _executable-path_ and _arg-vector_.
But it does not wait until the process finished running, it returns a child process handle
or an error if something went wrong.
The handle can then be used by functions like:
* `std:process:kill_wait` - to kill and wait for the process to exit
* `std:process:try_wait` - to check if the process exited
* `std:process:wait` - to wait until the process exits
The third argument specifies what happens with the standard I/O file handles.
By default the child process gets _null_ handles, so neither output is captured
nor input is passed:
* _default_ - child process gets _null_ handles for stdin, stdout and stderr.
* `:inherit_out` - child process inherits stdout and stderr, but stdin will be _null_.
* `:inherit_all` - child process inherits all (stdout, stderr and stdin) from the
parent and uses them until it exits.
TODO: Implement pipe to/from the child process to be
read/written to via `std:io:read_some` and `std:io:write`.
```wlambda
!hdl = unwrap ~ std:process:spawn "bash" $[
"-c", "for i in `seq 0 10`; do echo $i; sleep 0.2; done; exit 20"
];
# do something in your program....
!result = unwrap ~ std:process:wait hdl;
std:assert ~ not result.success;
std:assert result.status == 20;
```
#### <a name="1133-stdprocesstrywait-child-handle"></a>11.3.3 - std:process:try\_wait _child-handle_
Checks if the child process behind _child-handle_ exited. Returns `$none` if
it did not exit yet. Returns a map with the structure
`${ status = ..., success = $true / $false }` if the child exited.
Or an error if something failed.
```wlambda
!hdl = unwrap ~ std:process:spawn "bash" $[
"-c", "for i in `seq 0 10`; do echo $i; sleep 0.2; done; exit 20"
];
!counter = 0;
!ret = $none;
while $true {
std:thread:sleep :ms => 250;
.counter += 1;
.ret = unwrap ~ std:process:try_wait hdl;
if ret {
break ret;
};
};
std:assert counter > 0;
std:assert ~ not ret.success;
std:assert ret.status == 20;
```
#### <a name="1134-stdprocesskillwait-child-handle"></a>11.3.4 - std:process:kill\_wait _child-handle_
Kills the child process behind _child-handle_ and waits for it to return the exit status.
Returns a map with the structure `${ status = ..., success = $true / $false }` if the child exited.
Or an error if something failed.
```wlambda
!hdl = unwrap ~ std:process:spawn "bash" $[
"-c", "for i in `seq 0 10`; do echo $i; sleep 0.2; done; exit 20"
];
!res = std:process:kill_wait hdl;
std:assert ~ not res.success;
std:assert_eq res.status -1;
```
#### <a name="1135-stdprocesswait-child-handle"></a>11.3.5 - std:process:wait _child-handle_
Waits until the child process behind _child-handle_ exits by itself.
Returns a map with the structure `${ status = ..., success = $true / $false }`
if the child exited. Or an error if something failed.
```wlambda
!hdl = unwrap ~ std:process:spawn "bash" $[
"-c", "for i in `seq 0 10`; do echo $i; sleep 0.2; done; exit 20"
];
!res = std:process:wait hdl;
std:assert ~ not res.success;
std:assert_eq res.status 20;
```
### <a name="114-file-system"></a>11.4 - File System
#### <a name="1141-stdfsrename-file-path-new-file-name"></a>11.4.1 - std:fs:rename _file-path_ _new-file-name_
Renames the file at _file-path_ to the new name _new-file-name_. This
usually does only work on a single file system.
Returns `$true` if renaming was successful, and an error object if it was not
successful.
#### <a name="1142-stdfscopy-src-file-path-dst-file-path"></a>11.4.2 - std:fs:copy _src-file-path_ _dst-file-path_
Copies the file _src-file-path_ to the _dst-file-path_.
Returns an error if something went wrong.
#### <a name="1143-stdfsreaddir-path-function"></a>11.4.3 - std:fs:read\_dir _path_ _function_
Calls _function_ with the first argument being the directory entry as map
of this structure:
```wlambda
${
atime = 1587628635, # seconds since UNIX Epoch
ctime = 1557382099, # seconds since UNIX Epoch
mtime = 1557382099, # seconds since UNIX Epoch
len = 478, # bytes
name = "test", # file name
path = "..\\test", # path name
read_only = $false, # read only flag
type = :f # possible values:
# - :f for files
# - :l for symlinks
# - :d for directories
}
```
If the _function_ is called with a directory, you can recurse into that
directory by returning a `$true` value.
You can format the timestamps using `std:chrono:format_utc`.
#### <a name="1144-stdfsremovefile-file-path"></a>11.4.4 - std:fs:remove\_file _file-path_
Removes the file at the given _file-path_.
Returns an error if the file is missing or some other error occured.
#### <a name="1145-stdfsremovedir-dir-path"></a>11.4.5 - std:fs:remove\_dir _dir-path_
Removes the dir at the given _dir-path_.
Returns an error if the dir is missing, is not empty
or some other error occured.
#### <a name="1146-stdfsremovedirall-dir-path"></a>11.4.6 - std:fs:remove\_dir\_all _dir-path_
Removes the given _dir-path_ recursively. Use with care!
Returns an error if the directory does not exist.
### <a name="115-system"></a>11.5 - System
This chapter contains a few system as in _operating system_ related functions.
#### <a name="1151-stdsysos"></a>11.5.1 - std:sys:os
Returns the name of the operating system. These are some possible
values:
- linux
- macos
- ios
- freebsd
- dragonfly
- netbsd
- openbsd
- solaris
- android
- windows
```wlambda
std:assert (len std:sys:os[]) > 0;
```
### <a name="116-threading"></a>11.6 - Threading
WLambda leverages the `std::thread` implementation of Rust's standard library
to provide safe threading. Threading works by spawning new threads that
get sent a piece of WLambda code (as string) and some arguments.
Most WLambda data can be shared between threads. An exception are
UserData values that are not thread safe. Also things like sharing
cyclic data structures are not possible, as the references are currently
broken up.
Sharing data is done by WLambda by transforming the _VVal_ data structures
into a thread safe shareable represenation called _AVal_. An AVal is a
deep copy of the original VVal and can additionally contain atoms (see `std:sync:atom:new`),
MPSC queues (see `std:sync:mpsc:new`) and value slots (see `std:sync:slot:new`).
The scope of threading in WLambda is primarily to provide a way to do asynchronous
work and not so much for high performance computing. You can of course enhance
performance a bit with threading, but if you want to do heavy computing I recommend
implementing your algorithm in Rust instead. You can of course still manage
thread orchestration in WLambda if you just provide a simple function
API to your algorithms.
#### <a name="1161-stdthreadspawn-string-globals-map"></a>11.6.1 - std:thread:spawn _string_ [_globals-map_]
This evaluates the given _string_ as WLambda code in a new thread.
It returns a thread handle, that can be used to join the thread or
wait for it to have properly started.
The new thread starts out with a completely empty global environment.
If you need any builtin functions, use `!@wlambda`, `!@import std`
and other import statements to load the needed functions.
If a _globals-map_ is given, inside the thread the given global
variables will be set to the given value.
Inside the thread, a global variable called `_READY` is set to
an atomic slot, which can be used to signal the parent thread
that the new thread has successfully spawned.
This is a very basic example how to calculate something in
a worker thread and wait in a blocking manner:
```wlambda
!handle = std:thread:spawn $code {
# Attention: Even if this example does not use any
# built in functions, it's a good practice
# to load them into the global environment
# of the thread!
!@wlambda;
!@import std;
!sum = $@i iter i 0 => 10 {
$+ i;
};
_READY.send $[:ok, sum];
100
};
std:assert_str_eq handle.recv_ready[] $[:ok, 45];
!res = handle.join[];
std:assert_str_eq res 100;
```
Here an example on how to pass values to the thread.
Please be aware, that only a limited set of data types can be
passed to a thread. Functions and iterators can not be passed for
instance.
```wlambda
!globals = ${
a = 99,
b = $[1, 3, 4],
};
!handle = std:thread:spawn $code {
a + b.1
} globals;
!res = handle.join[];
std:assert_str_eq res 102;
```
#### <a name="1162-stdthreadsleep-duration"></a>11.6.2 - std:thread:sleep _duration_
Lets the current thread sleep for the given _duration_.
_duration_ can either be an integer that will be interpreted
as milliseconds to sleep. Or a pair, containing the duration unit as first element
and the integer as second element. Following units are supported:
- `$p(:s, _seconds_)`
- `$p(:ms, _milliseconds_)`
- `$p(:us, _microseconds_)`
- `$p(:ns, _nanoseconds_)`
```wlambda
!before = std:time:now :ms;
!thrd = std:thread:spawn $code {
!@import std;
std:thread:sleep :ms => 150;
};
thrd.join[];
!after = std:time:now :ms;
std:assert (after - before) >= 150;
```
#### <a name="1163-thread-handle-api"></a>11.6.3 - Thread Handle API
##### <a name="11631-thdljoin"></a>11.6.3.1 - thdl.join
This method will wait for the thread to finish and return
the return value of the thread.
```wlambda
!thdl = std:thread:spawn "4 + 3";
std:assert_eq thdl.join[] 7;
```
This method will return an error if the thread handle was already joined.
##### <a name="11632-thdlrecvready"></a>11.6.3.2 - thdl.recv\_ready
Waits for the global `_READY` atomic value slot to be sent a value by the
thread. This is useful for waiting until the thread has started without an
error before expecting it to run properly.
If an error happens, you will receive an error object as return value of
`recv_ready`:
```wlambda
!thdl = std:thread:spawn $code {
!@wlambda; # importing `panic`
panic "SOME ERR";
_READY.send :ok;
};
!err_msg =
match thdl.recv_ready[]
($e err) => { $\.err.0 }
v => $\.v;
$DEBUG err_msg;
std:assert err_msg &> $r/ *SOME\ ERR* /;
thdl.join[];
```
This method might return an error if the thread provider
made a handle without a ready slot.
#### <a name="1164-atom-api"></a>11.6.4 - Atom API
For threads a VVal (WLambda data value) is transformed into a value
that can be shared between threads safely. For this the data values are cloned
deeply and transformed into a structure of atomic values.
These values are then stored in a so called _Atom_. They can be safely changed
by threads.
##### <a name="11641-stdsyncatomnew-value"></a>11.6.4.1 - std:sync:atom:new _value_
Creates an Atom, containing the given _value_. The data types for _value_
is limited to these:
- Numbers (Integer, Float)
- Numerical Vectors
- Vectors
- Maps
- Strings
- Symbols
- Byte vectors
- Pairs
- Booleans
- Optionals
- Errors
And also these special types:
- Atom
- Atom Value Slot
- Channel
```wlambda
!at = std:sync:atom:new $[1, 2, 3];
!thdl = std:thread:spawn $code {
at.write ~ $@i iter i at.read[] {
$+ i;
};
_READY.send :ok;
} ${ at = at };
thdl.recv_ready[];
std:assert_eq at.read[] 6;
thdl.join[]
```
##### <a name="11642-atomread"></a>11.6.4.2 - atom.read
Returns the value stored in the atom.
```wlambda
!at = std:sync:atom:new 99;
std:assert_eq at.read[] 99;
```
This method might return an error if the internal mutex was poisoned.
##### <a name="11643-atomwrite-value"></a>11.6.4.3 - atom.write _value_
Overwrites the contents of the atom with the given _value_.
```wlambda
!at = std:sync:atom:new 99;
at.write 100;
std:assert_eq at.read[] 100;
```
This method might return an error if the internal mutex was poisoned.
##### <a name="11644-atomswap-value"></a>11.6.4.4 - atom.swap _value_
Returns the previous value of the atom and writes in
the given _value_.
```wlambda
!at = std:sync:atom:new 99;
std:assert_eq at.swap[100] 99;
std:assert_eq at.read[] 100;
```
This method might return an error if the internal mutex was poisoned.
#### <a name="1165-atom-value-slot-api"></a>11.6.5 - Atom Value Slot API
An Atom value slot offers more synchronization than a normal Atom value.
It allows you to set the value of the slot, wait for it to be collected
and wait for a value becoming available.
It can be thought of a single element queue, where the element will be
overwritten when a new value is sent to the slot.
You can theoretically receive or wait from multiple threads and also write from
multiple threads. But be aware, that the order of which threads get to read or
write is determined by the operating system and might lead to reader or writer
starvation.
Best recommendation here is to use a slot only from a single writer and
a single reader.
##### <a name="11651-stdsyncslotnew"></a>11.6.5.1 - std:sync:slot:new
Constructs a new Atom slot and returns it.
The slot has the initial status of being _empty_.
If a value is sent to it, it will not be _empty_ anymore.
After a value is received from the slot, the status is _empty_ again.
##### <a name="11652-atomslotsend-value"></a>11.6.5.2 - atom\_slot.send _value_
This method sends the value into the slot, overwriting any previous
set values. The slot can also ha
```wlambda
!slot = std:sync:slot:new[];
std:assert slot.check_empty[];
slot.send $[:ok, $i(1,2,3)];
std:assert ~ not slot.check_empty[];
std:assert_eq slot.recv[].1 $i(1,2,3);
std:assert slot.check_empty[];
```
This method might return an error if there was an issue with locking
the internal mutex or the mutex was poisoned.
##### <a name="11653-atomslotrecv"></a>11.6.5.3 - atom\_slot.recv
If the slot is empty, it will wait for a value to become available.
Once a value is available it is returned and the slot is set to _empty_ again.
```wlambda
!slot = std:sync:slot:new[];
!thrd = std:thread:spawn $code {
slot.send 99;
} ${ slot = slot };
std:assert_eq slot.recv[] 99;
thrd.join[];
```
This method might return an error if there was an issue with locking
the internal mutex or the mutex was poisoned.
##### <a name="11654-atomslottryrecv"></a>11.6.5.4 - atom\_slot.try\_recv
This method returns an optional value. It will provide an empty optional
value if no value is stored in the slot. But if the slot contains
a value, it will return the value (wrapped in an optional) and set the
slot to be _empty_ again.
```wlambda
!slot = std:sync:slot:new[];
!start_flag = std:sync:slot:new[];
!thrd = std:thread:spawn $code {
start_flag.recv[]; # sync with parent
slot.send 99;
_READY.send :ok;
} ${ slot = slot, start_flag = start_flag };
std:assert_eq slot.try_recv[] $o();
start_flag.send :ok;
thrd.recv_ready[];
std:assert_eq slot.try_recv[] $o(99);
thrd.join[];
```
This method might return an error if there was an issue with locking
the internal mutex or the mutex was poisoned.
##### <a name="11655-atomslotrecvtimeout-duration"></a>11.6.5.5 - atom\_slot.recv\_timeout _duration_
Acts like `atom_slot.recv`, but it will only wait for the given _duration_. If
no value was received in the given _duration_ (see std:thread:sleep), `$o()` is
returned. Otherwise the optional value will contain the received value.
```wlambda
!slot = std:sync:slot:new[];
std:assert_eq (slot.recv_timeout :ms => 100) $o();
slot.send 4;
std:assert_eq (slot.recv_timeout :ms => 100) $o(4);
```
This method might return an error if there was an issue with locking
the internal mutex or the mutex was poisoned.
##### <a name="11656-atomslotcheckempty"></a>11.6.5.6 - atom\_slot.check\_empty
Returns `$true` if the slot is empty.
This method might return an error if there was an issue with locking
the internal mutex or the mutex was poisoned.
##### <a name="11657-atomslotwaitempty"></a>11.6.5.7 - atom\_slot.wait\_empty
Waits until the slot is empty and then returns `$true`.
This method might return an error if there was an issue with locking
the internal mutex or the mutex was poisoned.
##### <a name="11658-atomslotwaitemptytimeout-duration"></a>11.6.5.8 - atom\_slot.wait\_empty\_timeout _duration_
Waits a predefined timeout until the slot is empty. If it did become
empty within the given _duration_ (see std:thread:sleep) it will return `$true`.
Otherwise it will return `$false`.
This method might return an error if there was an issue with locking
the interal mutex or the mutex was poisoned.
#### <a name="1166-channel-api"></a>11.6.6 - Channel API
A channel is a multiple sender, single consumer queue. It can be used to
establish a message passing based communication between threads.
It is basically a wrapper around the Rust `std::sync::mpsc::channel`.
```wlambda
!chan = std:sync:mpsc:new[];
!thdl = std:thread:spawn $code {
_READY.send :ok;
iter i 0 => 10 {
chan.send $p(:val, i);
};
chan.send $p(:quit, $none);
} ${ chan = chan };
match thdl.recv_ready[]
($e ?) => { std:assert $false };
!item = $none;
!sum = $@i
while { .item = chan.recv[]; item.0 == :val } {
$+ item.1;
};
std:assert_eq sum 45;
thdl.join[];
```
##### <a name="11661-stdsyncmpscnew"></a>11.6.6.1 - std:sync:mpsc:new
This creates a new channel. You can safely send from multiple threads
while reading from one thread at a time.
```wlambda
!chan = std:sync:mpsc:new[];
chan.send :a;
chan.send :b;
chan.send :c;
std:assert_eq chan.recv[] :a;
std:assert_eq chan.recv[] :b;
std:assert_eq chan.recv[] :c;
```
##### <a name="11662-channelsend-value"></a>11.6.6.2 - channel.send _value_
Sends the given _value_ to the channel queue.
```wlambda
!chan = std:sync:mpsc:new[];
chan.send :a;
std:assert_eq chan.recv[] :a;
```
This method might return an error if the channel failed, for instance due
to a poisoned internal mutex.
##### <a name="11663-channelrecv"></a>11.6.6.3 - channel.recv
Receives the next element from the channel. If no element is available
this method will block the thread until an element becomes available.
```wlambda
!chan = std:sync:mpsc:new[];
chan.send :a;
std:assert_eq chan.recv[] :a;
```
This method might return an error if the channel failed, for instance due
to a poisoned internal mutex.
##### <a name="11664-channeltryrecv"></a>11.6.6.4 - channel.try\_recv
Tries to receive the next element from the channel and return it wrapped
into an optional. If no element is available an empty optional `$o()` is returned.
```wlambda
!chan = std:sync:mpsc:new[];
std:assert_eq chan.try_recv[] $o();
chan.send :a;
std:assert_eq chan.try_recv[] $o(:a);
```
This method might return an error if the channel failed, for instance due
to a poisoned internal mutex.
##### <a name="11665-channelrecvtimeout-duration"></a>11.6.6.5 - channel.recv\_timeout _duration_
Tries to receive the next element in the given _duration_ (see std:thread:sleep)
and return it wrapped into an optional. If no element could be received
within that time an empty optional is returned `$o()`.
```wlambda
!chan = std:sync:mpsc:new[];
std:assert_eq (chan.recv_timeout $p(:ms, 100)) $o();
chan.send :x;
std:assert_eq (chan.recv_timeout $p(:ms, 100)) $o(:x);
```
This method might return an error if the channel failed, for instance due
to a poisoned internal mutex.
## <a name="12-optional-standard-library"></a>12 - Optional Standard Library
### <a name="121-serialization"></a>12.1 - serialization
#### <a name="1211-stdserwlambda-arg"></a>12.1.1 - std:ser:wlambda _arg_
Returns the serialized WLambda representation of the value _arg_ as string.
Most values have the same represenation like a WLambda literal,
but there are other values that don't have a literal representation.
Warning: Consider all values that don't have a fixed literal representation
in the WLambda syntax as debug output that might change in future versions.
```wlambda
std:assert_eq (std:ser:wlambda "foo") $q|"foo"|;
std:assert_eq (std:ser:wlambda $none) $q|$n|;
std:assert_eq (std:ser:wlambda $[1,:a]) $q|$[1,:a]|;
```
#### <a name="1212-stdserjson-data-nopretty"></a>12.1.2 - std:ser:json _data_ \[_no\_pretty_]
Serializes the _data_ and returns a JSON formatted (and pretty printed) string.
Optionally not pretty printed if _no_pretty_ is a true value.
```wlambda
!str = std:ser:json $[1,2.3,${a=4}] $t;
std:assert_eq str "[1,2.3,{\"a\":4}]";
```
#### <a name="1213-stddeserjson-string"></a>12.1.3 - std:deser:json _string_
Deserializes the JSON formatted _string_ into a data structure.
```wlambda
!data = std:deser:json ~ std:ser:json $[1,2.3,${a=4}];
std:assert_eq data.0 1;
std:assert_eq data.1 2.3;
std:assert_eq data.(2).a 4;
```
#### <a name="1214-stdsercsv-fielddelim-rowseparator-escapeall-table"></a>12.1.4 - std:ser:csv _field\_delim_ _row\_separator_ _escape\_all_ _table_
This serializes the _table_ as CSV with the given _field_delim_
and _row_separator_. If _escape_all_ is `$true` all fields will be
put into '"'.
```wlambda
!csv_str =
std:ser:csv
";" "|" $f
$[ $[1,2,3,4,$q/foo"bar/],
$[44,55],
$[]]
| std:displayln;
std:assert_eq csv_str $q/1;2;3;4;"foo""bar"|44;55||/;
std:assert_eq
(std:ser:csv ";" "|" $f $[$[:a,$q/;/, $q/|/, $q/ /]])
"a;\";\";\"|\";\" \"|";
```
#### <a name="1215-stddesercsv-fielddelim-rowseparator-data"></a>12.1.5 - std:deser:csv _field\_delim_ _row\_separator_ _data_
Parses the string _data_ as CSV. With the field delimiter _field_delim_
and the _row_separator_ for the data rows.
```wlambda
!table = std:deser:csv ";" "\r\n" "foo;bar\r\nx;y\r\n";
std:assert_eq table.0.0 "foo";
std:assert_eq table.0.1 "bar";
std:assert_eq table.1.1 "y";
```
#### <a name="1216-stdsermsgpack-data"></a>12.1.6 - std:ser:msgpack _data_
Serializes the _data_ and returns a msgpack bytes value.
```wlambda
std:assert_eq (std:ser:msgpack $b"abc") $b"\xC4\x03abc";
```
#### <a name="1217-stddesermsgpack-bytes"></a>12.1.7 - std:deser:msgpack _bytes_
Deserializes the msgpack bytes value into a data structure.
```wlambda
std:assert_eq (std:deser:msgpack $b"\xC4\x03abc") $b"abc";
```
### <a name="122-regular-expressions-more-classic-syntax"></a>12.2 - Regular Expressions (more classic syntax)
WLambda supports a more common form of regular expression syntax
if the "regex" feature is enabled when compiling WLambda.
It uses the regex syntax as described in the Rust "regex" crate:
[https://docs.rs/regex/newest/regex/](https://docs.rs/regex/newest/regex/#syntax).
#### <a name="1221-stdrematch-regex-string-input-string-function"></a>12.2.1 - std:re:match _regex-string_ _input-string_ _function_
If the given regular expression _regex-string_ matches the given _input-string_
the _function_ will be called with a vector containing the matched string
and the captures as first argument.
The return value is the return value of the function call or `$none`.
Returns an error if the _regex-string_ has a syntax error.
```wlambda
!ret = std:re:match $q|(\d+)-(\d+)-(\d+)| "2020-05-01" {
std:str:join "." std:reverse <& (1 => 3) <&_;
};
std:assert_str_eq ret "01.05.2020";
```
#### <a name="1222-stdrematchcompile-regex-string"></a>12.2.2 - std:re:match\_compile _regex-string_
This function compiles the given _regex-string_ and returns a function that
will execute the regex matching. If the syntax in _regex-string_ is wrong,
an error is returned.
The returned function takes the string to match against as first parameter
and the function that is called if the regex matches as second parameter.
```wlambda
!match_fun = std:re:match_compile $q|(\d+)-(\d+)-(\d+)|;
!ret = match_fun "2020-05-01" {
std:str:join "." std:reverse <& (1 => 3) <&_;
};
std:assert_str_eq ret "01.05.2020";
```
#### <a name="1223-stdremap-regex-string-function-input-string"></a>12.2.3 - std:re:map _regex-string_ _function_ _input-string_
Executes _function_ for each match of the regex defined by _regex-string_
on the _input-string_. Returns an error if there is a syntax error in the regex.
Otherwise returns the last return value of _function_.
```wlambda
!res = $@vec std:re:map $q|(\d+)-(\d+)-(\d+)| {!(str, d1, d2, d3) = _;
$+ $[int d1, int d2, int d3]
} "foo bar 2020-05-01 fofo 2020-06-01 bar 2019-12-31";
std:assert_str_eq res $[$[2020,5,1], $[2020,6,1], $[2019,12,31]];
```
#### <a name="1224-stdrereplaceall-regex-string-replace-function-input-string"></a>12.2.4 - std:re:replace\_all _regex-string_ _replace-function_ _input-string_
This function replaces all matches of the regex _regex-string_ in the _input-string_
by the return value of the _replace-function_. Returns an error if there is
a syntax error in the _regex-string_.
```wlambda
!res = std:re:replace_all $q"(\d+)" {
str (int _.1) + 1
} "foo 32 fifi 99";
std:assert_eq res "foo 33 fifi 100";
```
### <a name="123-xml"></a>12.3 - xml
#### <a name="1231-stdxmlreadsax-xml-string-event-callback-function-do-not-trim-text"></a>12.3.1 - std:xml:read\_sax _xml-string_ _event-callback-function_ [_do-not-trim-text_]
```wlambda
\:x {
$@v _? :x ~ std:xml:read_sax
"x<x a='12'>fooo fweor weio ew <i/> foefoe</i></x>y"
$+;
}[]
```
#### <a name="1232-stdxmlcreatesaxwriter-indent"></a>12.3.2 - std:xml:create\_sax\_writer [_indent_]
Creates an XML SAX event based writer function. The function
should be called with single events as received by `std:xml:read_sax`.
To receive the final output of the writer, call the returned function
without any arguments.
```wlambda
!writer = std:xml:create_sax_writer[];
writer $[:start, "test"];
writer $[:end, "test"];
std:assert_eq writer[] "<test></test>";
```
#### <a name="1233-stdxmlcreatetreebuilder"></a>12.3.3 - std:xml:create\_tree\_builder
This function returns a function that acts as WLambda data structure builder
that accepts SAX events as given to the callback of `std:xml:read_sax`.
The returned data structure is a tree build of the following elements:
- `$[:decl, ${ version=..., encoding=..., standalone=... }]`
- `$[:elem, name, ${ <attributes> }, $[ <child elements> ]]`
- `$[:comment, text]`
- `$[:pi, text]`
- `$[:text, text]`
- `$[:doctype, text]`
- `$[:cdata, text]`
Here is a more elaborate example on how to use it:
```wlambda
!tb = std:xml:create_tree_builder[];
!tree = std:xml:read_sax $q$
<foo>
<i x="here"/>
hello
</foo>
<foo>
blop<i x="10"/>
lol
</foo>$
tb;
std:assert_str_eq
tree
$[:root,$n,$n,$[
$[:elem,"foo",$n,$[
$[:elem,"i",${x="here"},$n],$[:text,"hello"]]],
$[:elem,"foo",$n,$[
$[:text,"blop"],$[:elem,"i",${x="10"},$n],$[:text,"lol"]]]
]];
# Here we use the structure matchers for finding the values of the x attributes
# of the <i> elements:
std:assert_str_eq
$S[ 3 / *:{0=elem,1=foo} /
3 / *:{0=elem,1=i} /
2 / x
] <& tree
$["here","10"];
```
### <a name="124-chrono"></a>12.4 - chrono
#### <a name="1241-stdchronotimestamp-format"></a>12.4.1 - std:chrono:timestamp \[_format_]
For the documentation of _format_ please consule the
chrono Rust crate documentation: [chrono crate strftime format](https://docs.rs/chrono/latest/chrono/format/strftime/index.html#specifiers).
```wlambda
!year_str = std:chrono:timestamp "%Y";
std:displayln :XXXX ~ (year_str | int) == 2022;
std:assert ~ (year_str | int) == 2022;
!now_str = std:chrono:timestamp[];
```
#### <a name="1242-stdchronoformatutc-utc-timestamp-format"></a>12.4.2 - std:chrono:format\_utc _utc-timestamp_ [_format_]
Formats the given _utc-timestamp_ in seconds according to _format_.
For the documentation of _format_ please consule the
chrono Rust crate documentation: [chrono crate strftime format](https://docs.rs/chrono/latest/chrono/format/strftime/index.html#specifiers).
```wlambda
!year_str = std:chrono:format_utc 1603796989 "%H:%M:%S %Y";
std:assert_str_eq year_str "11:09:49 2020";
```
#### <a name="1243-stdchronoformatlocal-utc-timestamp-format"></a>12.4.3 - std:chrono:format\_local _utc-timestamp_ [_format_]
Formats the given _utc-timestamp_ in seconds according to _format_ in the local timezone.
For the documentation of _format_ please consule the
chrono Rust crate documentation: [chrono crate strftime format](https://docs.rs/chrono/latest/chrono/format/strftime/index.html#specifiers).
```wlambda
!year_str = std:chrono:format_local 1603796989 "%H:%M:%S %Y";
std:assert_str_eq year_str "12:09:49 2020";
```
### <a name="125-color-conversion"></a>12.5 - color conversion
This section highlights the color conversion functions available in WLambda.
Numerical vectors are used in WLambda to represent colors. There are two
representations of a color.
If you use a float vector `$f(r, g, b, a)` the values for RGB are in the range
of 0.0 to 1.0. For HSV `$f(h, s, v, a)` h is within 0.0 to 360.0 while the
others are in the range 0.0 to 1.0.
For integer vectors the values for RGB are in the range 0 to 255.
And the values for HSV are in the range 0 to 360, and the others in the range
0 to 100.
You can also use 3 dimensional vectors without the alpha value: `$i(r, g, b)` / `$i(h, s, v)`
and `$f(r, g, b)` / `$f(h, s, v)`.
#### <a name="1251-stdvrgb2hsv-color-vector"></a>12.5.1 - std:v:rgb2hsv _color-vector_
Converts an RGB color into a HSV color representation.
```wlambda
std:assert_eq std:v:rgb2hsv <& $i(0, 255, 0, 255) $i(120, 100, 100, 100);
std:assert_eq std:v:rgb2hsv <& $i(0, 255, 0) $i(120, 100, 100);
std:assert_eq std:v:rgb2hsv <& $f(0, 1.0, 0, 1.0) $f(120, 1, 1, 1);
std:assert_eq std:v:rgb2hsv <& $f(0, 1.0, 0) $f(120, 1, 1);
std:assert_eq std:v:rgb2hsv <& $f(0, 0.5, 0, 1.0) $f(120, 1, 0.5, 1);
std:assert_eq std:v:rgb2hsv <& $f(0.1, 0.5, 0.1, 1.0) $f(120, 0.8, 0.5, 1);
```
#### <a name="1252-stdvhsv2rgb-color-vector"></a>12.5.2 - std:v:hsv2rgb _color-vector_
Converts a color from HSV to RGB representation.
```wlambda
std:assert_eq std:v:hsv2rgb <& $i(120, 80, 50, 100) $i(25,128,25,255);
!clr = std:v:hsv2rgb <& $f(120, 0.8, 0.5, 1.0);
std:assert_rel_eq clr.r 0.1 0.001;
std:assert_rel_eq clr.g 0.5 0.001;
std:assert_rel_eq clr.b 0.1 0.001;
```
#### <a name="1253-stdvrgba2hex-color-vector"></a>12.5.3 - std:v:rgba2hex _color-vector_
This function converts a color to a string of hex digits (without the common '#'
prefix however).
```wlambda
std:assert_eq std:v:rgba2hex <& $i(255, 128, 64, 32) "ff804020";
std:assert_eq std:v:rgba2hex <& $f(1.0, 0.5, 0.25, 0.125) "ff804020";
```
#### <a name="1254-stdvhex2rgbaf-string"></a>12.5.4 - std:v:hex2rgba\_f _string_
Interprets _string_ as an hex encoded color and
returns a 4 element big float vector. The color components
of the float vector go from 0.0 to 1.0.
The string can be:
- 8 characters: `"RRGGBBAA"`
- 6 characters: `"RRGGBB"`, alpha will be 1.0
- 4 characters: `"RGBA"`
- 3 characters: `"RGB"`, alpha will be 1.0
- 2 characters: `"YY"`, where YY is put into R, G and B. Alpha will be 1.0.
```wlambda
!color = std:v:hex2rgba_f "FF00FFFF";
std:assert_rel_eq color.r 1.0 0.001;
std:assert_rel_eq color.g 0.0 0.001;
std:assert_rel_eq color.b 1.0 0.001;
std:assert_rel_eq color.a 1.0 0.001;
!color2 = std:v:hex2rgba_f "C83F";
std:assert_rel_eq color2.r 0.8 0.001;
std:assert_rel_eq color2.g 0.533 0.001;
std:assert_rel_eq color2.b 0.2 0.001;
std:assert_rel_eq color2.a 1.0 0.001;
```
#### <a name="1255-stdvhex2rgbai-string"></a>12.5.5 - std:v:hex2rgba\_i _string_
Like `std:v:hex2rgba_f` this function converts a hex encoded color
from _string_ but returns an integer vector with 4 elements.
The integers are in the range of 0 to 255.
About the format of _string_ please refer to `std:v:hex2rgba_f`.
```wlambda
!color = std:v:hex2rgba_i "FF00FFFF";
std:assert_eq color.r 255;
std:assert_eq color.g 0;
std:assert_eq color.b 255;
std:assert_eq color.a 255;
!color2 = std:v:hex2rgba_i "C83F";
std:assert_eq color2.r 204;
std:assert_eq color2.g 136;
std:assert_eq color2.b 51;
std:assert_eq color2.a 255;
```
#### <a name="1256-stdvhex2hsvai-string"></a>12.5.6 - std:v:hex2hsva\_i _string_
Converts the hex represenation of a HSVA color to an integer vector `$i(h, s, v, a)`.
This function is probably not that useful, as the bit distribution along
the 3 bytes is not ideal. If you want to store colors properly, don't use this.
It's mostly useful for testing and quick experiments.
```wlambda
!color = std:v:hex2hsva_i "FF8040FF";
std:assert_eq color $i(360,50,25,100);
```
#### <a name="1257-stdvhex2hsvaf-string"></a>12.5.7 - std:v:hex2hsva\_f _string_
Converts the hex represenation of a HSVA color to a float vector `$i(h, s, v, a)`.
This function is probably not that useful, as the bit distribution along
the 3 bytes is not ideal. If you want to store colors properly, don't use this.
It's mostly useful for testing and quick experiments.
```wlambda
!color = std:v:hex2hsva_f "FF8040FF";
std:assert_rel_eq color.0 360.0 1.0;
std:assert_rel_eq color.1 50.0 1.0;
std:assert_rel_eq color.2 25.0 1.0;
std:assert_rel_eq color.3 100.0 1.0;
```
### <a name="126-hash"></a>12.6 - hash
#### <a name="1261-stdhashfnv1a-arg1-"></a>12.6.1 - std:hash:fnv1a _arg1_ ...
Hashes all the arguments as FNV1a and returns an integer.
### <a name="127-rand"></a>12.7 - rand
#### <a name="1271-stdrandsplitmix64new"></a>12.7.1 - std:rand:split\_mix64\_new
Initializes the _sm_state_ from the current time (seconds) and returns it.
The time is retrieved in seconds, so don't expect different seed states
if you call this multiple times in the same wall clock second.
The returned value is supposed to be passed to `rand:split_mix64_next`
or `rand:split_mix64_next_open01`.
#### <a name="1272-stdrandsplitmix64newfrom-seed"></a>12.7.2 - std:rand:split\_mix64\_new\_from _seed_
Initializes the _sm_state_ from the given _seed_ and returns it.
The returned value is supposed to be passed to `rand:split_mix64_next`
or `rand:split_mix64_next_open01`.
#### <a name="1273-stdrandsplitmix64next-smstate-count"></a>12.7.3 - std:rand:split\_mix64\_next _sm\_state_ \[_count_]
Returns the _count_ next integer values generated from the given
_sm_state_.
#### <a name="1274-stdrandsplitmix64nextopen01-smstate-count"></a>12.7.4 - std:rand:split\_mix64\_next\_open01 _sm\_state_ \[_count_]
Returns the _count_ next float values (in an open [0, 1) interval)
generated from the given _sm_state_.
#### <a name="1275-stdrandsplitmix64nextopenclosed01-smstate-count"></a>12.7.5 - std:rand:split\_mix64\_next\_open\_closed01 _sm\_state_ \[_count_]
Returns the _count_ next float values (in an open (0, 1] interval)
generated from the given _sm_state_.
#### <a name="1276-stdrandsplitmix64nextclosedopen01-smstate-count"></a>12.7.6 - std:rand:split\_mix64\_next\_closed\_open01 _sm\_state_ \[_count_]
Returns the _count_ next float values (in an open [0, 1) interval)
generated from the given _sm_state_.
### <a name="128-utility-functions"></a>12.8 - Utility Functions
#### <a name="1281-stddumpupvals-function"></a>12.8.1 - std:dump\_upvals _function_
Returns a vector of all the upvalues of the _function_.
Please use this function for debugging purposes only, as the order of the
variables, while consistent for a specific WLambda version,
is not defined at this point.
```wlambda
!x = 1;
!y = 2;
!fun = { _ + x + y };
std:assert_eq fun[3] 6;
.x = 3;
std:assert_eq fun[3] 8;
!upvs = std:dump_upvals fun;
std:assert_eq (str upvs) "$[$&3,$&2]";
.y = 4;
std:assert_eq (str upvs) "$[$&3,$&4]";
std:assert_eq $*(upvs.0) 3;
std:assert_eq $*(upvs.1) 4;
```
#### <a name="1282-stdwlambdaversion"></a>12.8.2 - std:wlambda:version
Returns the version number of the WLambda crate when called.
#### <a name="1283-stdwlambdasizes"></a>12.8.3 - std:wlambda:sizes
Writes a table of internal data structure sizes to stdout. Just for development
purposes.
#### <a name="1284-stdwlambdaparse-string"></a>12.8.4 - std:wlambda:parse _string_
Parses _string_ as if it was a piece of WLambda code and returns an abstract syntax tree.
```wlambda
std:assert_str_eq
(std:wlambda:parse "1 + 2")
$[$%:Block,$[$%:BinOpAdd,1,2]]
```
### <a name="129-http-client"></a>12.9 - HTTP Client
WLambda offers an optional integrated HTTP client by enabling the `reqwest`
feature at compile time. With this you can create a new client using `std:http:client:new` and make HTTP requests using `std:http:get`, `std:http:post` and `std:http:request`. Also support for basic authentication and token based bearer authentication
is there.
#### <a name="1291-stdhttpclientnew"></a>12.9.1 - std:http:client:new
Creates a new HTTP client instance and returns it. You can use it to make
HTTP requests afterwards.
```wlambda
!client = std:http:client:new[];
!response = std:http:get client "https://duckduckgo.com/";
!body = std:str:from_utf8_lossy response.body;
std:assert ($p(0, "</html>") body) > 0;
std:assert_eq response.status 200;
std:assert_eq response.headers.content-type "text/html; charset=UTF-8";
```
See also `std:http:get` for a more elaborate example with providing headers.
#### <a name="1292-stdhttpget-http-client-url-string-headers-and-options-map"></a>12.9.2 - std:http:get _http-client_ _url-string_ [_headers-and-options-map_]
Performs a HTTP GET request to the given _url-string_ using the _http-client_.
The _headers-and-options-map_ can contain following special keys apart from your
custom HTTP headers themself:
- `@basic_auth` with `$[user, $none]` or `$[user, password]` as value.
- `@bearer_auth` with `token` as value.
- `@timeout` with a timeout duration as value. (See also `std:thread:sleep`).
- `@query` with a map of query parameters and their values to modify the
query string of the _url-string_. This properly encodes the strings.
The client will either return an error or a response map with following keys:
- `status` contains the HTTP response code (usually `200` if everything went fine).
- `reason` contains a human readable canonical reason string for the status.
- `body` contains the byte vector with the body data.
- `headers` contains a map of all headers, where the keys are the lower case
header names.
Here is an example on how to use this while providing HTTP Headers:
```wlambda
!client = std:http:client:new[];
!response =
std:http:get client "https://crates.io/api/v1/crates?page=1&per_page=10&q=wlambda" ${
Accept = "application/json",
Cache-Control = "no-cache",
DNT = 1,
User-Agent = "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:97.0) Gecko/20100101 Firefox/97.0",
};
!body = std:deser:json ~ std:str:from_utf8_lossy response.body;
# std:displayln ~ std:ser:json body $f;
std:assert_eq body.crates.0.name "wlambda";
std:assert_eq response.headers.content-type "application/json; charset=utf-8";
std:assert_eq response.status 200;
```
#### <a name="1293-stdhttppost-http-client-url-string-body-bytes-headers-and-options-map"></a>12.9.3 - std:http:post _http-client_ _url-string_ _body-bytes_ [_headers-and-options-map_]
This call is like `std:http:get` but makes a HTTP POST request with the given payload _body_. For HTTP requests with other methods please look at `std:http:request`. The rest of the options are the same as `std:http:get`. But here is an example how to
transmit a piece of JSON easily:
```wlambda
!client = std:http:client:new[];
!response =
std:http:post client "http://httpbin.org/post"
(std:str:to_bytes ~ std:ser:json $[
:x => 10,
${ y = 20 },
]);
!body = std:deser:json ~ std:str:from_utf8_lossy response.body;
std:assert_eq body.url "http://httpbin.org/post";
std:assert_str_eq body.json $[ $["x", 10], ${ y = 20 } ];
std:assert_eq response.status 200;
```
#### <a name="1294-stdhttprequest-http-client-method-string-url-string-body-bytes-headers-and-options-map"></a>12.9.4 - std:http:request _http-client_ _method-string_ _url-string_ [_body-bytes_ [_headers-and-options-map_]]
This call is like `std:http:post` but makes HTTP requests with an almost
arbitrary method with the optional given payload _body_. The rest of the
options are the same as `std:http:get`.
```wlambda
!client = std:http:client:new[];
!response =
std:http:request client :GET "http://httpbin.org/get";
!body = std:deser:json ~ std:str:from_utf8_lossy response.body;
std:assert_eq body.url "http://httpbin.org/get";
```
Or a POST request:
```wlambda
!client = std:http:client:new[];
!response =
std:http:request client :POST "http://httpbin.org/post"
(std:str:to_bytes ~ std:ser:json $[
:x => 10,
${ y = 20 },
]);
!body = std:deser:json ~ std:str:from_utf8_lossy response.body;
std:assert_eq body.url "http://httpbin.org/post";
std:assert_str_eq body.json $[ $["x", 10], ${ y = 20 } ];
std:assert_eq response.status 200;
```
### <a name="1210-mqtt-messaging"></a>12.10 - MQTT Messaging
WLambda offers an optional support for the MQTT protocol. You can setup a MQTT client
as well as an embedded MQTT broker. The very simple integration offers you a very
easy way to setup inter process communication between WLambda applications.
Support for MQTT has to be explicitly compiled into WLambda by selecting the
`mqtt` feature.
#### <a name="12101-stdmqttbrokernew-config"></a>12.10.1 - std:mqtt:broker:new _config_
This function sets up an embedded MQTT broker. A handle is returned that you can use
to publish messages using the locally connected client link.
You can configure it's endpoints via the _config_.
The _config_ offers following keys:
```text
${
id = 0, # Broker ID
listen = "0.0.0.0:1883", # Broker server endpoint
console_listen = "0.0.0.0:18088", # An extra HTTP console endpoint
link = ${ # Configure the local link
client_id = "some_id", # Link client ID, default is 'wl_local'
recv = <std:sync:mpsc channel>, # Channel to receive messages for the
# subscribed topics.
# Topics to subscribe to, if not given, '#' will be used:
topics = $["test/me", "another/topic", ...]
},
}
```
The default maximum MQTT payload the broker is setup to support is 10240 bytes
(10 kb).
Here is an example:
```wlambda
!broker = std:mqtt:broker:new ${
listen = "0.0.0.0:1883",
console_listen = "0.0.0.0:18080",
};
# sleep a bit until the broker is initialized:
std:thread:sleep :ms => 500;
!chan = std:sync:mpsc:new[];
!cl = std:mqtt:client:new chan "test1" "localhost" 1883;
# let it connect:
std:thread:sleep :ms => 200;
!_ = cl.subscribe "test/me";
!_ = cl.publish "test/me" $b"test123\xFF";
std:assert_str_eq chan.recv[] $p(:"$WL/connected", $n);
std:assert_str_eq chan.recv[] $p("test/me", $b"test123\xFF");
```
##### <a name="121011-brokerpublish-topic-string-payload-bytes"></a>12.10.1.1 - broker.publish _topic-string_ _payload-bytes_
Publishes the _payload-bytes_ under the _topic-string_. Returns an error
if something went wrong (client not connected, or some other error). It might
block.
#### <a name="12102-stdmqttclientnew-channel-client-id-broker-host-broker-port"></a>12.10.2 - std:mqtt:client:new _channel_ _client-id_ _broker-host_ _broker-port_
This sets up a MQTT client that connects to the given _broker-host_ and _broker-port_.
It will connect and reconnect upon connection failure in the background automatically
for you. So you don't have to manage the connection yourself.
This function returns a client handle that is describe a little bit further below.
The _client-id_ should be a unique ID to identify your MQTT client.
The _channel_ must be a `std:sync:mpsc` channel that you can create using `std:sync:mpsc:new`.
It's the source of incoming messages and connection control information.
It will send you following possible message data:
- `$p(:"$WL/connected", $n)` - When the client connection was setup
- `$p(:"$WL/error", "some message here...")` - When an error occurred in the connection handling.
- `$p(topic, payload_bytes)` - An incoming MQTT message that belongs to the _topic_.
Here is an example of a common client setup:
```wlambda
!broker = std:mqtt:broker:new ${
listen = "0.0.0.0:1883",
console_listen = "0.0.0.0:18080",
};
# sleep a bit until the broker is initialized:
std:thread:sleep :ms => 500;
!chan = std:sync:mpsc:new[];
!cl = std:mqtt:client:new chan "test1" "localhost" 1883;
# let it connect:
std:thread:sleep :ms => 200;
!_ = cl.subscribe "test/me";
!_ = cl.publish "test/me" $b"test";
!_ = cl.publish "test/me" $b"quit";
!got_some_stuff = $n;
while $t {
!msg = chan.recv[];
match msg
$p(topic, $b"quit") => { break[]; }
$p(topic, data) => { .got_some_stuff = std:copy $\; }; # std:copy because $\ is changing!
};
std:assert_eq got_some_stuff.topic "test/me";
std:assert_eq got_some_stuff.data $b"test";
```
The returned client handle understands the following methods:
##### <a name="121021-mqttclientpublish-topic-string-payload-bytes"></a>12.10.2.1 - mqtt\_client.publish _topic-string_ _payload-bytes_
Publishes the _payload-bytes_ under the _topic-string_. Returns an error
if something went wrong (client not connected, or some other error). It might
block.
##### <a name="121022-mqttclientsubscribe-topic-string"></a>12.10.2.2 - mqtt\_client.subscribe _topic-string_
Subscribes to the _topic-string_. Returns an error if something went wrong.
It might block.
## <a name="13-wlambda-lexical-syntax-and-grammar"></a>13 - WLambda Lexical Syntax and Grammar
White space is everything that satisfies `std::char::is_whitespace`,
so unicode white space is respected. Comments have the following syntax:
```ebnf
comment = "#" ?anything except "\n"? "\n"
```
In the following grammar, white space and comments are omitted:
```ebnf
ident_start = ( ?alphabetic? | "_" | "@" | "?" )
ident_end = { ?any character?
- ( ?white space?
| "." | "," | ";"
| "{" | "}" | "[" | "]" | "(" | ")"
| "~" | "|" | "=" ) }
;
qident = ident_end
(* a quoted identifier can not appear anywhere,
it's usually delimited or follows something that
makes sure we are now expecting an identifier *)
| "`", { ?any character except '`'? }, "`" (* quoted identifier *)
;
ident = ident_start, [ ident_end ]
| "`", { ?any character except '`'? }, "`" (* quoted identifier *)
;
ref_specifier = ":", qident
;
digit = "0" | "1" | "2" | "3" | "4" | "5"
| "6" | "7" | "8" | "9"
;
integer = digit, { digit }
;
radix = integer
;
radix_digits = (* digits in the radix specified
earlier in the number.
Default radix is of course 10. *)
number = [ "-" | "+" ],
[ ( radix, "r"
| "0", "x"
| "0", "b"
| "0", "o"
) ],
radix_digits,
[ ".", radix_digits ]
;
hexdigit = ?hexdigit, upper or lower case?
;
ascii_c_name = (* note: upper and lower case versions are possible *)
"NULL" | "SOH" | "STX" | "ETX" | "EOT" | "ENQ" | "ACK"
| "BEL" | "BS" | "HT" | "LF" | "VT" | "FF" | "CR" | "SO"
| "SI" | "DLE" | "DC1" | "DC2" | "DC3" | "DC4" | "NAK"
| "SYN" | "ETB" | "CAN" | "EM" | "SUB" | "ESC" | "FS"
| "GS" | "RS" | "US" | "DEL" | "SPACE" | "NBSP"
;
string_escape = "x", hexdigit, hexdigit (* byte/ascii escape *)
| "n" (* newline *)
| "r" (* carriage return *)
| "t" (* horizontal tab *)
| "0" (* nul byte/char *)
| "u", hexdigit, { hexdigit }
(* unicode char, or in byte strings
their utf-8 encoded form *)
| "\""
| "\'"
| "\\"
| "\\<", ascii_c_name, ">" (* the corresponding ascii value *)
;
string_lit = string
| character
| "$", quote_string
| "$", byte_string
| "$", code_string
;
character = "'", ( "\\", string_escape | ?any character? - "\\" and "'" ), "'"
;
string = "\"", { "\\", string_escape | ?any character? - "\\" and "\"" },"\""
;
byte_char = "b", character
;
byte_string = "b", string
;
quote_string = "q", ?any character as quote?,
{ ?any character? },
?any character as quote?
| "Q", ?any character as quote?,
{ ?any character? },
?any character as quote?
(* but Q generates a byte string instead! *)
selector = "S", ?any character as quote?,
selector_rs_syntax,
?any character as quote?
(* parses substring like 'q', but constructs a
selector_rs_syntax matcher at compile time *)
;
pattern = "r", [ "g" ], (* "g" for find all *)
?any character as quote?,
selector_rs_pattern_syntax,
?any character as quote?
(* parses substring like 'q', but constructs a
pattern matcher at compile time *)
;
struct_match = "M", expr (* compiles expr as structure matcher function.
If called, it matches the first argument against
the literal structure and returns a map of
matched variables. If nothing matches $none
is returned. *)
formatter = "F", string_lit (* compiles the string into a formatter, that
takes as many arguments as there are format
specifiers in the string. See also: String
formatting syntax in the next section. *)
list_expr = "*", expr (* splices the vector result of 'expr'
into the currently parsed list *)
| expr
;
list = "[", [ list_expr, { ",", list_expr }, [ "," ] ],"]"
;
map_expr = (ident | expr), "=", expr
| "*", expr (* splices the map result of 'expr'
into the currently parsed map *)
;
map = "{", [ map_expr, { ",", map_expr }, [ "," ] ], "}"
;
self = "s" | "self"
;
true = "t" | "true"
;
false = "f" | "false"
;
none = "n" | "none"
;
code_string = ("c" | "code" ), block
| ("c" | "code" ), expr
;
pair = "p", "(", expr, "," expr, ")"
;
err = ("e" | "error"), expr
;
nvec = ("i" | "f"), "(", expr, { ",", expr }, ")"
;
ref = "&&", value
;
ref_hidden = "&", value
;
ref_weak = ("w&" | "weak&"), value
;
syntax = "%:", {? any possible Syntax Type Identifier
eg. "Block", "Call", ... ?}
(* A syntax VVal, the possible identifiers are one of the
possible result symbols of std:syn:type. This also
saves the current parser position. *)
;
accumulator = "@", ("i" | "int"
|"s" | "string"
|"f" | "float"
|"b" | "bytes"
|"v" | "vec"
|"m" | "map" ), expr
(* defines a new accumulator context *)
| "@@" (* returns the current accumulator value *)
| "+" (* resolves to the current accumulator function *)
;
debug_print = "DEBUG" (* evaluates to a debug print function that
also prints source position and dynamic type.
very useful for debugging. *)
;
import = "@import", ident, [ ident ]
;
export = "@export", ident, [ "=" ], expr
;
capture_ref = ":", var
;
deref = "*", value
;
special_value = list
| map
| none
| true
| false
| self
| err
| nvec
| pair
| ref
| ref_hidden
| ref_weak
| deref
| capture_ref
| accumulator
| selector
| pattern
| struct_match
| debug_print
| "\" (* The global variable with the name "\" *)
;
arity_def = "|", number, "<", number, "|" (* set min/max *)
| "|", number, "|" (* set min and max *)
| "|", "|" (* no enforcement *)
;
function = [ "\:", ident ], "{", [ arity_def ], block, "}"
| "\", [ arity_def ], statement
;
var = ident
;
symbol = ":", qident
| ":", "\"", (? any char, quoted \\ and \" ?), "\""
(*
symbols are usually used to specify
fields in literal map definitions
and lots of other places as stringy sentinel values
*)
;
value = number
| string_lit
| "$", special_value
| "(", expr, ")"
| function
| symbol
| var
;
op = (* here all operators are listed line by line regarding
their precedence, top to bottom *)
"&>" | "&@>" (* call rhs with lhs operator *)
| "<&" | "<@&" (* call lhs with rhs operator *)
| "^"
| "*" | "/" | "%"
| "-" | "+"
| "<<" | ">>" (* binary shift *)
| "<" | ">" | "<=" | ">="
| "==" | "!="
| "&" (* binary and *)
| "&^" (* binary xor *)
| "&|" (* binary or *)
| "&and" (* logical and, short circuit *)
| "&or" (* logical or, short circuit *)
| "=>" (* pair constructor *)
| "+>" (* take lhs, wrap it into list if not already
and append the right side.
if lhs is an iterator, append all elements. *)
| "<+" (* take rhs, wrap it into list if not already
and prepend the left side.
if rhs is an iterator, prepend all elements. *)
;
bin_op = call_no_ops, { op, bin_op } (* precedence parsing is done
in a Pratt parser style *)
;
arg_list = "[", [ expr, { ",", expr }, [ "," ] ], "]"
| "[[", expr, "]]" (* apply result vector of expr as argument list *)
;
field = ".", ( integer | ident | value ), [ field ]
;
field_access = field, [ op ], "=", expr
| field, arg_list
| field
(* please note, that a field access like:
`obj.field` is equivalent to the call:
`field[obj]`. That also means that
`obj.field[...]` is transformed into
`field[obj][...]`.
The exception is "=" which assigns
the field as specified.
BUT: There is a special case, when you specify
an identifier as field, it is quoted and
interpreted as symbol. *)
;
call_no_ops = value, { arg_list | field_access }
;
call = value,
{ arg_list | field_access | bin_op | value },
[ "~", expr ] (* this is a tail argument, if present the
expr is appended to the argument list *)
;
expr = call, { "|", call }
| call, { "|>", call }
| call, { "||", call }
;
simple_assign = qident, [ op ], "=", expr
;
destr_assign = "(", [ qident, { ",", qident } ], ")", "=" expr
;
definition = [ ref_specifier ], ( simple_assign | destr_assign )
;
import = "!", "@import", symbol, [ [ "=" ], symbol ]
| "!", "@wlambda"
;
export = "!", "@export", symbol, [ "=" ], expr
;
statement = "!" definition
| "." simple_assign
| "." destr_assign
| import
| export
| expr
;
block = "{", { statement, ";", {";"}}, [ statement, {";"} ], "}"
| { statement, ";", {";"} }, [ statement, {";"} ]
;
code = block
;
```
### <a name="131-special-forms"></a>13.1 - Special Forms
There are certain calls that are handled by the compiler differently.
- `if _condition_ _then-block-or-expr_ [_else-block-or-expr_]`
- `while _condition_ _block-or-expr_`
- `iter _var_ _value-expr_ _block-or-expr_`
- `next _x_`
- `break`
- `match _value-expr_ $p(structure_pattern, branch_block) ... [ branch_block ]
- `jump _idx-expr_ _block1_ ...`
### <a name="132-string-formatting-syntax"></a>13.2 - String Formatting Syntax
The `$F` special value takes a string and creates a formatting function.
The syntax for formatting is very similar to Rust's string formatting:
```ebnf
format_string = <text>, { maybe-format, <text> }
;
maybe-format = '{', '{'
| '}', '}'
| <format>
;
format = '{' [ argument ], [ ':' format_spec ] '}'
;
argument = integer (* index into argument vector *)
| identifier (* requires a map as parameter *)
;
format_spec = [[fill]align][sign]['#']['0'][width]['.' precision]['!' cast_type][type]
;
fill = character
;
align = '<' | '^' | '>'
;
sign = '+' | '-'
;
width = count
;
precision = count | '*'
;
cast_type = 'i' (* interpret arg as integer *)
| 'f' (* interpret arg as float *)
| 's' (* (default) interpret arg as string *)
;
type = 'x' (* hex lower case *)
| 'X' (* hex upper case *)
| 'b' (* binary *)
| 'o' (* octal *)
| 'j', character (* a string join of a vector,
separated by 'character' *)
| 'C', character (* A CSV row, separated by 'character'.
This also supports proper
escaping using '"' *)
| 'J', ['p'] (* JSON, optional pretty printed *)
| 'w', ['p'] (* print written WLambda representation,
optional pretty printed *)
| ''
;
count = parameter | integer
;
parameter = argument, '$'
;
```
### <a name="133-format-string-syntax-for-stdbytespack-and-stdbytesunpack"></a>13.3 - Format String Syntax for std:bytes:pack and std:bytes:unpack
This syntax describes the accepted format strigns for the `std:bytes:pack` and
`std:bytes:unpack`. The format is loosely adapted from the Lua syntax for
`string.pack` and `string.unpack`.
| Syntax | Semantics |
|-|-|
| `<` | Sets little endian |
| `>` | Sets big endian |
| `=` | Sets native endian |
| `x` | One zero byte of padding |
| `y` | Byte content with unspecified length. On `unpack` this reads to the end of the input. |
| `z` | A zero-terminated string of bytes. |
| `c<n>` | An `n` long field of bytes. |
| `b` | A single byte. |
| `s<bits>` | A string of bytes that is prefixed with a `<bits>` wide binary length field. |
| `u<bits>` | A `<bits>` wide unsigned integer field. |
| `i<bits>` | A `<bits>` wide signed integer field. |
| `f` | A float field (32 bits). |
| `d` | A double field (64 bits). |
- `<n>` can be any number.
- `<bits>` can be 8, 16, 32, 64 or 128.
[]: ---- REFERENCE DOC END ----
*/
const VERSION: &str = env!("CARGO_PKG_VERSION");
use crate::compiler::*;
use crate::vval::*;
use crate::util;
use std::rc::Rc;
use crate::threads::*;
use crate::selector::*;
use crate::formatter::*;
use crate::io::print_value;
macro_rules! func {
($g: ident, $name: expr, $cb: expr, $min: expr, $max: expr, $err_arg_ok: expr) => {
$g.fun($name, $cb, $min, $max, $err_arg_ok);
}
}
macro_rules! add_func {
($g: ident, $op: tt, $env: ident, $argc: ident, $b: block, $min: expr, $max: expr, $err_arg_ok: expr) => {
$g.fun(stringify!($op), |$env: &mut Env, $argc: usize| $b, $min, $max, $err_arg_ok);
}
}
macro_rules! add_multi_op {
($g: ident, $op: tt) => {
add_func!($g, $op, env, argc, {
if argc <= 0 { return Ok(VVal::None); }
if let VVal::Flt(f) = env.arg(0) {
let mut accum = f;
for i in 1..argc { accum = accum $op env.arg(i).f() }
Ok(VVal::Flt(accum))
} else {
let mut accum = env.arg(0).i();
for i in 1..argc { accum = accum $op env.arg(i).i() }
Ok(VVal::Int(accum))
}
}, Some(2), None, false)
}
}
macro_rules! add_multi_op_zero {
($g: ident, $op: tt, $err: expr) => {
add_func!($g, $op, env, argc, {
if argc <= 0 { return Ok(VVal::None); }
if let VVal::Flt(f) = env.arg(0) {
let mut accum = f;
for i in 1..argc { accum = accum $op env.arg(i).f() }
Ok(VVal::Flt(accum))
} else {
let mut accum = env.arg(0).i();
for i in 1..argc {
let v = env.arg(i).i();
if v == 0 {
return
Err(StackAction::panic_str(
format!("{}", $err),
None,
env.argv()));
}
accum = accum $op v;
}
Ok(VVal::Int(accum))
}
}, Some(2), None, false)
}
}
macro_rules! add_bool_bin_op {
($g: ident, $op: tt) => {
add_func!($g, $op, env, argc, {
if argc < 2 { return Ok(VVal::None); }
let a = env.arg(0);
if let VVal::Flt(af) = a { Ok(VVal::Bol(af $op env.arg(1).f())) }
else { Ok(VVal::Bol(a.i() $op env.arg(1).i())) }
}, Some(2), Some(2), false)
}
}
macro_rules! add_fi_bin_op {
($g: ident, $op: tt, $a: ident, $b: ident, $ef: expr, $ei: expr) => {
add_func!($g, $op, env, argc, {
if argc < 2 { return Ok(VVal::None); }
let $a = env.arg(0);
let $b = env.arg(1);
if let VVal::Flt(_) = $a { $ef }
else { $ei }
}, Some(2), Some(2), false)
}
}
macro_rules! add_bin_op_err_ok {
($g: ident, $op: tt, $a: ident, $b: ident, $e: expr) => {
add_func!($g, $op, env, argc, {
if argc < 2 { return Ok(VVal::None); }
let $a = env.arg(0);
let $b = env.arg(1);
$e
}, Some(2), Some(2), true)
}
}
macro_rules! add_sbin_op {
($g: ident, $op: literal, $a: ident, $b: ident, $e: expr) => {
func!($g, $op, |env: &mut Env, argc: usize| {
if argc < 2 { return Ok(VVal::None); }
let $a = env.arg(0);
let $b = env.arg(1);
$e
}, Some(2), Some(2), false);
}
}
macro_rules! add_num_fun {
($g: ident, $op: literal, $e: tt) => {
func!($g, $op,
|env: &mut Env, _argc: usize| {
Ok(match env.arg(0).deref() {
VVal::Int(f) => { VVal::Int(f.$e()) }
v => { VVal::Flt(v.f().$e()) }
})
}, Some(1), Some(1), false);
}
}
macro_rules! add_num_fun_flt {
($g: ident, $op: literal, $e: tt) => {
func!($g, $op,
|env: &mut Env, _argc: usize| {
Ok(VVal::Flt(env.arg(0).f().$e()))
}, Some(1), Some(1), false);
}
}
macro_rules! add_num_fun_flt2 {
($g: ident, $op: literal, $e: tt) => {
func!($g, $op,
|env: &mut Env, _argc: usize| {
Ok(VVal::Flt(env.arg(0).f().$e(env.arg(1).f())))
}, Some(2), Some(2), false);
}
}
macro_rules! sizeof_writeln {
($write: ident, $type: ty) => {
writeln!($write, "sizeof {:40}: {:2} bytes",
stringify!($type),
std::mem::size_of::<$type>()).expect("stdout access works");
}
}
macro_rules! process_vec_input {
($env: ident, $arg: ident, $v: ident, $x: ident, $y: ident, $z: ident, $w: ident, $three_i: block, $three_f: block, $four_i: block, $four_f: block) => {
match $arg.nvec_len() {
3 => match $arg {
VVal::FVec($v) => {
let $x = $v.x_raw();
let $y = $v.y_raw();
let $z = $v.z_raw().unwrap_or(0.0);
$three_f
},
VVal::IVec($v) => {
let $x = $v.x_raw();
let $y = $v.y_raw();
let $z = $v.z_raw().unwrap_or(0);
$three_i
},
_ => {
Ok($env.new_err(
"expects float or int vectors".to_string()))
},
},
4 => match $arg {
VVal::FVec($v) => {
let $x = $v.x_raw();
let $y = $v.y_raw();
let $z = $v.z_raw().unwrap_or(0.0);
let $w = $v.w_raw().unwrap_or(0.0);
$four_f
},
VVal::IVec($v) => {
let $x = $v.x_raw();
let $y = $v.y_raw();
let $z = $v.z_raw().unwrap_or(0);
let $w = $v.w_raw().unwrap_or(0);
$four_i
},
_ => {
Ok($env.new_err(
"v:rgba2hex expects float or int vectors"
.to_string()))
},
},
_ => Ok($env.new_err(
"expects 3 or 4 dimensional vectors".to_string()))
}
}
}
/// Returns a SymbolTable with all WLambda core language symbols.
#[allow(clippy::cast_lossless,clippy::assign_op_pattern)]
pub fn core_symbol_table() -> SymbolTable {
let mut st = SymbolTable::new();
// The implementations for +/- are essentially just like the `add_multi_op`
// implementations, except for how they accept down to 1 parameter for
// unary +/-.
add_func!(st, +, env, argc, {
Ok(match (argc, env.arg(0)) {
(0, _) => VVal::None,
(1, VVal::Flt(f)) => VVal::Flt(f),
(1, VVal::Int(i)) => VVal::Int(i),
(1, VVal::FVec(fv)) => VVal::FVec(fv),
(1, VVal::IVec(iv)) => VVal::IVec(iv),
(a, VVal::Flt(f)) => {
let mut accum = f;
for i in 1..a { accum = accum + env.arg(i).f() }
VVal::Flt(accum)
}
(a, v) => {
let mut accum = v.i();
for i in 1..a { accum = accum + env.arg(i).i() }
VVal::Int(accum)
}
})
}, Some(1), None, false);
add_func!(st, -, env, argc, {
Ok(match (argc, env.arg(0)) {
(0, _) => VVal::None,
(1, VVal::Int(i)) => VVal::Int(-i),
(1, VVal::Flt(f)) => VVal::Flt(-f),
(1, VVal::FVec(fv)) => VVal::FVec(Box::new(-*fv)),
(1, VVal::IVec(iv)) => VVal::IVec(Box::new(-*iv)),
(a, VVal::Flt(f)) => {
let mut accum = f;
for i in 1..a { accum = accum - env.arg(i).f() }
VVal::Flt(accum)
}
(a, v) => {
let mut accum = v.i();
for i in 1..a { accum = accum - env.arg(i).i() }
VVal::Int(accum)
}
})
}, Some(1), None, false);
add_multi_op!(st, *);
add_multi_op_zero!(st, /, "Division by 0");
add_multi_op_zero!(st, %, "Remainder with divisor by 0");
add_bool_bin_op!(st, <);
add_bool_bin_op!(st, >);
add_bool_bin_op!(st, <=);
add_bool_bin_op!(st, >=);
add_bin_op_err_ok!(st, ==, a, b, Ok(VVal::Bol(a.eqv(&b))));
add_bin_op_err_ok!(st, !=, a, b, Ok(VVal::Bol(!a.eqv(&b))));
add_sbin_op!(st, "&|", a, b,
Ok(VVal::Int(((a.i() as u32) | (b.i() as u32)) as i64)));
add_sbin_op!(st, "&", a, b,
Ok(VVal::Int(((a.i() as u32) & (b.i() as u32)) as i64)));
add_sbin_op!(st, "&^", a, b,
Ok(VVal::Int(((a.i() as u32) ^ (b.i() as u32)) as i64)));
add_sbin_op!(st, "<<", a, b,
Ok(VVal::Int(((a.i() as u32) << (b.i() as u32)) as i64)));
add_sbin_op!(st, ">>", a, b,
Ok(VVal::Int(((a.i() as u32) >> (b.i() as u32)) as i64)));
add_fi_bin_op!(st, ^, a, b,
Ok(VVal::Flt(a.f().powf(b.f()))),
Ok(VVal::Int(a.i().pow(b.i() as u32))));
func!(st, "//",
|env: &mut Env, _argc: usize| {
let a = env.arg(0);
let b = env.arg(1);
Ok(match a {
VVal::Opt(Some(a)) => a.as_ref().clone(),
VVal::Opt(None) => b,
VVal::None => b,
_ => a,
})
}, Some(2), Some(2), true);
func!(st, "/?",
|env: &mut Env, _argc: usize| {
let a = env.arg(0);
let b = env.arg(1);
Ok(match a {
VVal::Opt(Some(a)) => a.as_ref().clone(),
VVal::Opt(None) => b,
VVal::Err(_) => b,
VVal::None => b,
_ => a,
})
}, Some(2), Some(2), true);
func!(st, "/$n",
|env: &mut Env, _argc: usize| {
let a = env.arg(0);
let b = env.arg(1);
Ok(match a {
VVal::None => b,
_ => a,
})
}, Some(2), Some(2), true);
func!(st, "/$o",
|env: &mut Env, _argc: usize| {
let a = env.arg(0);
let b = env.arg(1);
Ok(match a {
VVal::Opt(Some(a)) => a.as_ref().clone(),
VVal::Opt(None) => b,
_ => a,
})
}, Some(2), Some(2), true);
func!(st, "/$e",
|env: &mut Env, _argc: usize| {
let a = env.arg(0);
let b = env.arg(1);
Ok(match a {
VVal::Err(_) => b,
_ => a,
})
}, Some(2), Some(2), true);
func!(st, "+>",
|env: &mut Env, argc: usize| {
use crate::compiler::collection_add;
collection_add(env, argc, CollectionAdd::Push)
}, None, None, false);
func!(st, "<+",
|env: &mut Env, argc: usize| {
use crate::compiler::collection_add;
collection_add(env, argc, CollectionAdd::Unshift)
}, None, None, false);
func!(st, "not",
|env: &mut Env, _argc: usize| {
Ok(VVal::Bol(!env.arg(0).b()))
}, Some(1), Some(1), false);
func!(st, "panic",
|env: &mut Env, _argc: usize| {
Err(env.new_panic(env.arg(0)))
}, Some(1), Some(1), true);
func!(st, "block",
|env: &mut Env, argc: usize| {
let mut label = VVal::None;
let fn_arg_idx = if argc <= 1 { 0 } else { label = env.arg(0); 1 };
match env.arg(fn_arg_idx).call_no_args(env) {
Ok(v) => Ok(v),
Err(StackAction::Return(ret)) => {
if ret.0.eqv(&label) { Ok(ret.1) }
else { Err(StackAction::Return(ret)) }
},
Err(e) => Err(e),
}
}, Some(1), Some(2), false);
func!(st, "_?",
|env: &mut Env, argc: usize| {
let mut lbl = VVal::None;
let err_val = if argc > 1 {
lbl = env.arg(0);
env.arg(1)
} else { env.arg(0) };
match err_val {
VVal::Err(e) => Err(StackAction::Return(Box::new((lbl, VVal::Err(e))))),
v => Ok(v),
}
}, Some(1), Some(2), true);
func!(st, "unwrap_err",
|env: &mut Env, _argc: usize| {
match env.arg(0) {
VVal::Err(err_v) => {
Ok(err_v.borrow().0.clone())
},
v => {
Err(StackAction::panic_msg(
format!("unwrap_err on non error value: {}", v.s())))
},
}
}, Some(1), Some(1), true);
func!(st, "unwrap",
|env: &mut Env, _argc: usize| {
match env.arg(0) {
VVal::Err(err_v) => {
Err(StackAction::panic_str(
format!("unwrap error: {}", err_v.borrow().0.s()),
Some(err_v.borrow().1.clone()),
err_v.borrow().0.clone()))
},
VVal::Opt(None) => {
Err(StackAction::panic_str(
"unwrap empty option!".to_string(), None,
VVal::None))
},
VVal::Opt(Some(v)) => Ok((*v).clone()),
v => Ok(v)
}
}, Some(1), Some(1), true);
func!(st, "on_error",
|env: &mut Env, _argc: usize| {
let err_fn = env.arg(0);
match env.arg(1) {
VVal::Err(err_v) => {
env.with_restore_sp(|e: &mut Env| {
e.push(err_v.borrow().0.clone());
e.push(VVal::Int(err_v.borrow().1.line() as i64));
e.push(VVal::Int(err_v.borrow().1.col() as i64));
e.push(VVal::new_str(err_v.borrow().1.filename()));
err_fn.call_internal(e, 4)
})
},
e => Ok(e)
}
}, Some(2), Some(2), true);
func!(st, "return",
|env: &mut Env, argc: usize| {
if argc < 1 { return Err(StackAction::Return(Box::new((VVal::None, VVal::None)))); }
if argc < 2 { return Err(StackAction::Return(Box::new((VVal::None, env.arg(0))))); }
Err(StackAction::Return(Box::new((env.arg(0), env.arg(1)))))
}, Some(1), Some(2), true);
func!(st, "break",
|env: &mut Env, argc: usize| {
if argc < 1 { return Err(StackAction::Break(Box::new(VVal::None))); }
Err(StackAction::Break(Box::new(env.arg(0))))
}, Some(0), Some(1), true);
func!(st, "next",
|_env: &mut Env, _argc: usize| {
Err(StackAction::Next)
}, Some(0), Some(0), false);
func!(st, "pick",
|env: &mut Env, _argc: usize| Ok(if env.arg(0).b() { env.arg(1) } else { env.arg(2) }),
Some(3), Some(3), false);
func!(st, "ivec",
|env: &mut Env, _argc: usize| Ok(VVal::IVec(Box::new(env.arg(0).nvec()))),
Some(1), Some(1), false);
func!(st, "ivec2",
|env: &mut Env, _argc: usize| Ok(VVal::IVec(Box::new(env.arg(0).nvec().vec2()))),
Some(1), Some(1), false);
func!(st, "ivec3",
|env: &mut Env, _argc: usize| Ok(VVal::IVec(Box::new(env.arg(0).nvec().vec3()))),
Some(1), Some(1), false);
func!(st, "ivec4",
|env: &mut Env, _argc: usize| Ok(VVal::IVec(Box::new(env.arg(0).nvec().vec4()))),
Some(1), Some(1), false);
func!(st, "fvec",
|env: &mut Env, _argc: usize| Ok(VVal::FVec(Box::new(env.arg(0).nvec()))),
Some(1), Some(1), false);
func!(st, "fvec2",
|env: &mut Env, _argc: usize| Ok(VVal::FVec(Box::new(env.arg(0).nvec().vec2()))),
Some(1), Some(1), false);
func!(st, "fvec3",
|env: &mut Env, _argc: usize| Ok(VVal::FVec(Box::new(env.arg(0).nvec().vec3()))),
Some(1), Some(1), false);
func!(st, "fvec4",
|env: &mut Env, _argc: usize| Ok(VVal::FVec(Box::new(env.arg(0).nvec().vec4()))),
Some(1), Some(1), false);
func!(st, "is_nvec",
|env: &mut Env, _argc: usize| Ok(VVal::Bol(env.arg(0).is_nvec())),
Some(1), Some(1), false);
func!(st, "is_ivec",
|env: &mut Env, _argc: usize| Ok(VVal::Bol(env.arg(0).is_ivec())),
Some(1), Some(1), false);
func!(st, "is_fvec",
|env: &mut Env, _argc: usize| Ok(VVal::Bol(env.arg(0).is_fvec())),
Some(1), Some(1), false);
func!(st, "nvec_len",
|env: &mut Env, _argc: usize| Ok(VVal::Int(env.arg(0).nvec_len() as i64)),
Some(1), Some(1), false);
func!(st, "bool",
|env: &mut Env, _argc: usize| { Ok(VVal::Bol(env.arg(0).b())) },
Some(1), Some(1), true);
func!(st, "float",
|env: &mut Env, _argc: usize| { Ok(VVal::Flt(env.arg(0).f())) },
Some(1), Some(1), false);
func!(st, "int",
|env: &mut Env, _argc: usize| { Ok(VVal::Int(env.arg(0).i())) },
Some(1), Some(1), false);
func!(st, "char",
|env: &mut Env, _argc: usize| { Ok(VVal::Chr(VValChr::Char(env.arg(0).c()))) },
Some(1), Some(1), false);
func!(st, "byte",
|env: &mut Env, _argc: usize| { Ok(VVal::Chr(VValChr::Byte(env.arg(0).byte()))) },
Some(1), Some(1), false);
func!(st, "str",
|env: &mut Env, _argc: usize| { Ok(VVal::new_str_mv(env.arg(0).s_raw())) },
Some(1), Some(1), false);
func!(st, "sym",
|env: &mut Env, _argc: usize|
env.arg(0).with_s_ref(|s: &str| { Ok(VVal::new_sym(s)) }),
Some(1), Some(1), false);
func!(st, "is_some",
|env: &mut Env, _argc: usize| { Ok(VVal::Bol(env.arg(0).is_some())) },
Some(1), Some(1), true);
func!(st, "is_none",
|env: &mut Env, _argc: usize| { Ok(VVal::Bol(env.arg(0).is_none())) },
Some(1), Some(1), true);
func!(st, "is_err",
|env: &mut Env, _argc: usize| { Ok(VVal::Bol(env.arg(0).is_err())) },
Some(1), Some(1), true);
func!(st, "is_map",
|env: &mut Env, _argc: usize| { Ok(VVal::Bol(env.arg(0).is_map())) },
Some(1), Some(1), true);
func!(st, "is_vec",
|env: &mut Env, _argc: usize| { Ok(VVal::Bol(env.arg(0).is_vec())) },
Some(1), Some(1), true);
func!(st, "is_fun",
|env: &mut Env, _argc: usize| { Ok(VVal::Bol(env.arg(0).is_fun())) },
Some(1), Some(1), true);
func!(st, "is_str",
|env: &mut Env, _argc: usize| { Ok(VVal::Bol(env.arg(0).is_str())) },
Some(1), Some(1), true);
func!(st, "is_wref",
|env: &mut Env, _argc: usize| { Ok(VVal::Bol(env.arg(0).is_wref())) },
Some(1), Some(1), true);
func!(st, "is_ref",
|env: &mut Env, _argc: usize| { Ok(VVal::Bol(env.arg(0).is_ref())) },
Some(1), Some(1), true);
func!(st, "is_bool",
|env: &mut Env, _argc: usize| { Ok(VVal::Bol(env.arg(0).is_bool())) },
Some(1), Some(1), true);
func!(st, "is_bytes",
|env: &mut Env, _argc: usize| { Ok(VVal::Bol(env.arg(0).is_bytes())) },
Some(1), Some(1), true);
func!(st, "is_sym",
|env: &mut Env, _argc: usize| { Ok(VVal::Bol(env.arg(0).is_sym())) },
Some(1), Some(1), true);
func!(st, "is_float",
|env: &mut Env, _argc: usize| { Ok(VVal::Bol(env.arg(0).is_float())) },
Some(1), Some(1), true);
func!(st, "is_pair",
|env: &mut Env, _argc: usize| { Ok(VVal::Bol(env.arg(0).is_pair())) },
Some(1), Some(1), true);
func!(st, "is_optional",
|env: &mut Env, _argc: usize| { Ok(VVal::Bol(env.arg(0).is_optional())) },
Some(1), Some(1), true);
func!(st, "is_iter",
|env: &mut Env, _argc: usize| { Ok(VVal::Bol(env.arg(0).is_iter())) },
Some(1), Some(1), true);
func!(st, "is_int",
|env: &mut Env, _argc: usize| { Ok(VVal::Bol(env.arg(0).is_int())) },
Some(1), Some(1), true);
func!(st, "is_char",
|env: &mut Env, _argc: usize| { Ok(VVal::Bol(env.arg(0).is_char())) },
Some(1), Some(1), true);
func!(st, "is_byte",
|env: &mut Env, _argc: usize| { Ok(VVal::Bol(env.arg(0).is_byte())) },
Some(1), Some(1), true);
func!(st, "len",
|env: &mut Env, _argc: usize| { Ok(VVal::Int(env.arg(0).len() as i64)) },
Some(1), Some(1), false);
func!(st, "type",
|env: &mut Env, _argc: usize| {
Ok(VVal::new_str(env.arg(0).type_name()))
}, Some(1), Some(1), true);
func!(st, "cons",
|env: &mut Env, _argc: usize| {
Ok(VVal::pair(env.arg(0), env.arg(1)))
}, Some(2), Some(2), true);
func!(st, "filter",
|env: &mut Env, _argc: usize| {
let f = env.arg(0);
let val = env.arg(1);
val.with_iter(move |iter| {
let ret = VVal::vec();
for (v, k) in iter {
env.push(v.clone());
let n =
if let Some(k) = &k { env.push(k.clone()); 2 }
else { 1 };
match f.call_internal(env, n) {
Ok(test) => {
if test.b() {
if let Some(k) = k { ret.push(VVal::pair(v, k)); }
else { ret.push(v); }
}
},
Err(StackAction::Break(v)) => { env.popn(n); return Ok(*v); },
Err(StackAction::Next) => { },
Err(e) => { env.popn(n); return Err(e); }
}
env.popn(n);
}
Ok(ret)
})
}, Some(2), Some(2), false);
func!(st, "map",
|env: &mut Env, _argc: usize| {
let f = env.arg(0);
let val = env.arg(1);
val.with_iter(move |iter| {
let ret = VVal::vec();
for (v, k) in iter {
env.push(v);
let n =
if let Some(k) = k { env.push(k); 2 }
else { 1 };
match f.call_internal(env, n) {
Ok(v) => { ret.push(v); },
Err(StackAction::Break(v)) => { env.popn(n); return Ok(*v); },
Err(StackAction::Next) => { },
Err(e) => { env.popn(n); return Err(e); }
}
env.popn(n);
}
Ok(ret)
})
}, Some(2), Some(2), false);
func!(st, "for",
|env: &mut Env, _argc: usize| {
let val = env.arg(0);
let f = env.arg(1);
val.with_iter(move |iter| {
let mut ret = VVal::None;
for (v, k) in iter {
env.push(v);
let n =
if let Some(k) = k { env.push(k); 2 }
else { 1 };
match f.call_internal(env, n) {
Ok(v) => { ret = v; },
Err(StackAction::Break(v)) => { env.popn(n); return Ok(*v); },
Err(StackAction::Next) => { },
Err(e) => { env.popn(n); return Err(e); }
}
env.popn(n);
}
Ok(ret)
})
}, Some(2), Some(2), false);
func!(st, "range",
|env: &mut Env, _argc: usize| {
let from = env.arg(0);
let to = env.arg(1);
let step = env.arg(2);
let f = env.arg(3);
//println!("RAGEN from={} to={} f={}", from.s(), to.s(), f.s());
if let VVal::Flt(_) = from {
let mut from = from.f();
let to = to.f();
let step = step.f();
let mut ret = VVal::None;
#[allow(unused_must_use)]
while from <= to {
ret = VVal::None;
env.push(VVal::Flt(from));
match f.call_internal(env, 1) {
Ok(v) => { ret = v; },
Err(StackAction::Break(v)) => { env.popn(1); return Ok(*v); },
Err(StackAction::Next) => { },
Err(e) => { env.popn(1); return Err(e); }
}
from += step;
env.popn(1);
}
Ok(ret)
} else {
let mut from = from.i();
let to = to.i();
let step = step.i();
let mut ret = VVal::None;
#[allow(unused_must_use)]
while from <= to {
ret = VVal::None;
env.push(VVal::Int(from));
match f.call_internal(env, 1) {
Ok(v) => { ret = v; },
Err(StackAction::Break(v)) => { env.popn(1); return Ok(*v); },
Err(StackAction::Next) => { },
Err(e) => { env.popn(1); return Err(e); }
}
from += step;
env.popn(1);
}
Ok(ret)
}
}, Some(4), Some(4), false);
st
}
fn systime_to_unix(syst: &std::time::SystemTime, unit: &str) -> Result<VVal, StackAction> {
match syst.duration_since(std::time::SystemTime::UNIX_EPOCH) {
Ok(n) => {
Ok(duration_to_vval(n, unit))
},
Err(_) =>
Err(StackAction::panic_msg(
"SystemTime before UNIX EPOCH!".to_string()))
}
}
fn duration_to_vval(dur: std::time::Duration, unit: &str) -> VVal {
match unit {
"s" => { VVal::Int(i64::try_from(dur.as_secs()) .unwrap_or(0)) },
"ms" => { VVal::Int(i64::try_from(dur.as_millis()).unwrap_or(0)) },
"us" => { VVal::Int(i64::try_from(dur.as_micros()).unwrap_or(0)) },
"ns" => { VVal::Int(i64::try_from(dur.as_nanos()) .unwrap_or(0)) },
_ => { VVal::Int(i64::try_from(dur.as_millis()).unwrap_or(0)) },
}
}
fn dir_entry_to_vval(env: &mut Env, path: &str, entry: Result<std::fs::DirEntry, std::io::Error>) -> Result<VVal, StackAction> {
let entry = match entry {
Ok(e) => e,
Err(e) => {
return Ok(env.new_err(
format!(
"Couldn't directory read entry '{}': {}",
path, e)))
},
};
let metadata = match entry.metadata() {
Ok(m) => m,
Err(e) => {
return Ok(env.new_err(
format!(
"Couldn't read entry metadata '{}': {}",
entry.path().to_string_lossy(), e)))
},
};
let ve = VVal::map3(
"name", VVal::new_str_mv(entry.file_name().to_string_lossy().to_string()),
"path", VVal::new_str_mv(entry.path().to_string_lossy().to_string()),
"type", if metadata.file_type().is_dir() {
VVal::sym("d")
} else if metadata.file_type().is_symlink() {
VVal::sym("l")
} else {
VVal::sym("f")
});
ve.set_key_str("len", VVal::Int(metadata.len() as i64))
.expect("single use");
if let Ok(atime) = metadata.accessed() {
ve.set_key_str(
"atime", systime_to_unix(&atime, "s")?)
.expect("single use");
}
if let Ok(mtime) = metadata.modified() {
ve.set_key_str(
"mtime", systime_to_unix(&mtime, "s")?)
.expect("single use");
}
if let Ok(ctime) = metadata.created() {
ve.set_key_str(
"ctime", systime_to_unix(&ctime, "s")?)
.expect("single use");
}
ve.set_key_str(
"read_only",
VVal::Bol(metadata.permissions().readonly()))
.expect("single use");
Ok(ve)
}
/// Returns a SymbolTable with all WLambda standard library language symbols.
#[allow(clippy::question_mark)]
pub fn std_symbol_table() -> SymbolTable {
let mut st = SymbolTable::new();
func!(st, "not_i64",
|env: &mut Env, _argc: usize| {
Ok(VVal::Int(!env.arg(0).i()))
}, Some(1), Some(1), false);
func!(st, "neg_i64",
|env: &mut Env, _argc: usize| {
Ok(VVal::Int(env.arg(0).i().wrapping_neg()))
}, Some(1), Some(1), false);
func!(st, "not_u32",
|env: &mut Env, _argc: usize| {
Ok(VVal::Int(i64::from(!(env.arg(0).i() as u32))))
}, Some(1), Some(1), false);
func!(st, "neg_u32",
|env: &mut Env, _argc: usize| {
Ok(VVal::Int(i64::from((env.arg(0).i() as u32).wrapping_neg())))
}, Some(1), Some(1), false);
func!(st, "unshift",
|env: &mut Env, _argc: usize| {
let v = env.arg(0);
v.unshift(env.arg(1));
Ok(v)
}, Some(2), Some(2), false);
func!(st, "push",
|env: &mut Env, _argc: usize| {
let v = env.arg(0);
v.push(env.arg(1));
Ok(v)
}, Some(2), Some(2), false);
func!(st, "reverse",
|env: &mut Env, _argc: usize| {
Ok(env.arg(0).reverse())
}, Some(1), Some(1), false);
func!(st, "keys",
|env: &mut Env, _argc: usize| {
let v = env.arg(0);
Ok(v.keys())
}, Some(1), Some(1), false);
func!(st, "values",
|env: &mut Env, _argc: usize| {
let v = env.arg(0);
Ok(v.values())
}, Some(1), Some(1), false);
func!(st, "accum",
|env: &mut Env, argc: usize| {
let mut v = env.arg(0);
for i in 1..argc {
v.accum(&env.arg(i));
}
Ok(v)
}, Some(2), None, false);
func!(st, "delete",
|env: &mut Env, _argc: usize| {
let v = env.arg(0);
let key = env.arg(1);
v.delete_key(&key)
}, Some(2), Some(2), false);
func!(st, "prepend",
|env: &mut Env, argc: usize| {
let v = env.arg(0);
let v =
if v.is_vec() { v }
else {
let r = VVal::vec();
r.push(v);
r
};
for i in 1..argc {
match env.arg(i) {
VVal::Lst(b) => {
for item in b.borrow().iter() {
v.list_operation(|r: &mut std::cell::RefMut<Vec<VVal>>| {
r.insert(0, item.clone());
})?;
}
},
item => {
v.list_operation(|r: &mut std::cell::RefMut<Vec<VVal>>| {
r.insert(0, item.clone());
})?;
},
}
}
Ok(v)
}, Some(1), None, false);
func!(st, "append",
|env: &mut Env, argc: usize| {
let v = env.arg(0);
let v =
if v.is_vec() { v }
else {
let r = VVal::vec();
r.push(v);
r
};
for i in 1..argc {
match env.arg(i) {
VVal::Lst(b) => {
for item in b.borrow().iter() {
v.list_operation(|r: &mut std::cell::RefMut<Vec<VVal>>| {
r.push(item.clone());
})?;
}
},
item => {
v.list_operation(|r: &mut std::cell::RefMut<Vec<VVal>>| {
r.push(item.clone());
})?;
}
}
}
Ok(v)
}, Some(1), None, false);
func!(st, "pop",
|env: &mut Env, _argc: usize| {
let v = env.arg(0);
Ok(v.pop())
}, Some(1), Some(1), false);
func!(st, "take",
|env: &mut Env, _argc: usize| {
let cnt = env.arg(0).i() as usize;
let lst = env.arg(1);
lst.list_operation(|r: &mut std::cell::RefMut<Vec<VVal>>| {
let svec : Vec<VVal> =
r.iter().take(cnt).cloned().collect();
VVal::vec_mv(svec)
})
}, Some(2), Some(2), false);
func!(st, "drop",
|env: &mut Env, _argc: usize| {
let cnt = env.arg(0).i() as usize;
let lst = env.arg(1);
lst.list_operation(|r: &mut std::cell::RefMut<Vec<VVal>>| {
let svec : Vec<VVal> =
r.iter().skip(cnt).cloned().collect();
VVal::vec_mv(svec)
})
}, Some(2), Some(2), false);
func!(st, "error_to_str",
|env: &mut Env, _argc: usize| {
match env.arg(0) {
VVal::Err(_) => {
Ok(VVal::new_str_mv(env.arg(0).s()))
},
v => {
Err(StackAction::panic_msg(
format!("std:error_to_str on non error value: {}", v.s())))
},
}
}, Some(1), Some(1), true);
func!(st, "ser:wlambda",
|env: &mut Env, _argc: usize| { Ok(VVal::new_str_mv(env.arg(0).s())) },
Some(1), Some(1), true);
func!(st, "str:len",
|env: &mut Env, _argc: usize| { Ok(VVal::Int(env.arg(0).s_len() as i64)) },
Some(1), Some(1), false);
func!(st, "str:to_lowercase",
|env: &mut Env, _argc: usize| {
Ok(VVal::new_str_mv(
env.arg_ref(0).unwrap().with_s_ref(|s: &str| s.to_lowercase()))) },
Some(1), Some(1), false);
func!(st, "str:to_uppercase",
|env: &mut Env, _argc: usize| {
Ok(VVal::new_str_mv(
env.arg_ref(0).unwrap().with_s_ref(|s: &str| s.to_uppercase()))) },
Some(1), Some(1), false);
func!(st, "str:trim",
|env: &mut Env, _argc: usize| {
Ok(VVal::new_str_mv(
env.arg_ref(0).unwrap().with_s_ref(|s: &str| s.trim().to_string()))) },
Some(1), Some(1), false);
func!(st, "str:trim_start",
|env: &mut Env, _argc: usize| {
Ok(VVal::new_str_mv(
env.arg_ref(0).unwrap().with_s_ref(|s: &str| s.trim_start().to_string()))) },
Some(1), Some(1), false);
func!(st, "str:trim_end",
|env: &mut Env, _argc: usize| {
Ok(VVal::new_str_mv(
env.arg_ref(0).unwrap().with_s_ref(|s: &str| s.trim_end().to_string()))) },
Some(1), Some(1), false);
func!(st, "str:find",
|env: &mut Env, _argc: usize| {
Ok(env.arg_ref(1).unwrap()
.find(env.arg_ref(0).unwrap(),
env.arg_ref(2).unwrap_or(&VVal::Int(0)).i() as usize,
false))
}, Some(2), Some(3), false);
func!(st, "bytes:find",
|env: &mut Env, _argc: usize| {
Ok(env.arg_ref(1).unwrap()
.find(env.arg_ref(0).unwrap(),
env.arg_ref(2).unwrap_or(&VVal::Int(0)).i() as usize,
true))
}, Some(2), Some(3), false);
func!(st, "str:pad_start",
|env: &mut Env, _argc: usize| {
let len = env.arg(0).i() as usize;
let pads = env.arg(1).s_raw();
let mut s = env.arg(2).s_raw();
let mut src_len = s.chars().count();
let pad_len = pads.chars().count();
if pad_len == 0 {
return Ok(VVal::new_str_mv(s));
}
while src_len < len {
if len - src_len < pad_len {
for c in pads.chars().rev() {
s.insert(0, c);
src_len += 1;
if src_len >= len {
break;
}
}
} else {
s.insert_str(0, &pads);
}
src_len += pad_len;
}
Ok(VVal::new_str_mv(s))
}, Some(3), Some(3), false);
func!(st, "str:pad_end",
|env: &mut Env, _argc: usize| {
let len = env.arg(0).i() as usize;
let pads = env.arg(1).s_raw();
let mut s = env.arg(2).s_raw();
let mut src_len = s.chars().count();
let pad_len = pads.chars().count();
if pad_len == 0 {
return Ok(VVal::new_str_mv(s));
}
while src_len < len {
if len - src_len < pad_len {
for c in pads.chars() {
s.push(c);
src_len += 1;
if src_len >= len {
break;
}
}
} else {
s.push_str(&pads);
}
src_len += pad_len;
}
Ok(VVal::new_str_mv(s))
}, Some(3), Some(3), false);
func!(st, "str:cat",
|env: &mut Env, argc: usize| {
let mut s = String::from("");
for i in 0..argc {
let aref = env.arg_ref(i).unwrap();
if let VVal::Lst(l) = aref {
for v in l.borrow().iter() {
v.with_s_ref(|vs: &str| s.push_str(vs));
}
} else {
env.arg_ref(i).unwrap()
.with_s_ref(|vs: &str| s.push_str(vs))
}
}
Ok(VVal::new_str_mv(s))
}, None, None, false);
func!(st, "str:replace_n",
|env: &mut Env, _argc: usize| {
let cnt = env.arg(2).i() as usize;
env.arg_ref(0).unwrap().with_s_ref(|pat: &str|
env.arg_ref(1).unwrap().with_s_ref(|to: &str|
env.arg_ref(3).unwrap().with_s_ref(|data: &str|
Ok(VVal::new_str_mv(data.replacen(pat, to, cnt))))))
}, Some(4), Some(4), false);
func!(st, "str:replace",
|env: &mut Env, _argc: usize| {
env.arg_ref(0).unwrap().with_s_ref(|pat: &str|
env.arg_ref(1).unwrap().with_s_ref(|to: &str|
env.arg_ref(2).unwrap().with_s_ref(|data: &str|
Ok(VVal::new_str_mv(data.replace(pat, to))))))
}, Some(3), Some(3), false);
func!(st, "str:join",
|env: &mut Env, _argc: usize| {
let sep = env.arg(0);
let lst = env.arg(1);
if let VVal::Lst(l) = lst {
let mut s = VVal::new_str("");
let mut first = true;
for item in l.borrow().iter() {
if !first {
s.accum(&sep);
} else {
first = false;
}
s.accum(item);
}
Ok(s)
} else {
Ok(env.new_err(
format!(
"str:join only works with lists as second argument, got '{}'",
lst.s())))
}
}, Some(2), Some(2), false);
func!(st, "str:from_utf8_lossy",
|env: &mut Env, _argc: usize| {
let b = env.arg(0);
Ok(
if let VVal::Byt(u) = b {
VVal::new_str_mv(String::from_utf8_lossy(u.as_ref()).to_string())
} else {
VVal::None
})
}, Some(1), Some(1), false);
func!(st, "str:from_utf8",
|env: &mut Env, _argc: usize| {
let b = env.arg(0);
if let VVal::Byt(u) = b {
match String::from_utf8(u.to_vec()) {
Ok(s) => Ok(VVal::new_str_mv(s)),
Err(e) => {
Ok(env.new_err(
format!("str:from_utf8 decoding error: {}", e)))
}
}
} else {
Ok(VVal::None)
}
}, Some(1), Some(1), false);
func!(st, "str:from_latin1",
|env: &mut Env, _argc: usize| {
let b = env.arg(0);
if let VVal::Byt(u) = b {
let new_str =
u.iter()
.map(|byte| std::char::from_u32(*byte as u32).unwrap_or('?'))
.collect();
Ok(VVal::new_str_mv(new_str))
} else {
Ok(VVal::None)
}
}, Some(1), Some(1), false);
func!(st, "str:to_char_vec",
|env: &mut Env, _argc: usize| {
Ok(VVal::vec_mv(
env.arg_ref(0).unwrap().with_s_ref(|arg: &str|
arg.chars()
.map(|c| VVal::Int(i64::from(c as u32)))
.collect())))
}, Some(1), Some(1), false);
func!(st, "str:from_char_vec",
|env: &mut Env, _argc: usize| {
env.arg(0).with_iter(move |iter| {
let mut s = String::new();
for (vc, _) in iter {
s.push(std::char::from_u32(vc.i() as u32).unwrap_or('?'));
}
Ok(VVal::new_str_mv(s))
})
}, Some(1), Some(1), false);
func!(st, "str:to_bytes_latin1",
|env: &mut Env, _argc: usize| {
env.arg(0).with_s_ref(|s| {
Ok(VVal::new_byt(
s.chars()
.map(|c| {
let c = c as u32;
if c > 0xFF { '?' as u32 as u8 } else { c as u8 }
})
.collect()))
})
}, Some(1), Some(1), false);
func!(st, "str:to_bytes",
|env: &mut Env, _argc: usize| {
Ok(VVal::new_byt(env.arg(0).as_bytes()))
}, Some(1), Some(1), false);
func!(st, "str:edit_distance",
|env: &mut Env, _argc: usize| {
env.arg_ref(0).unwrap().with_s_ref(|a: &str| {
env.arg_ref(1).unwrap().with_s_ref(|b: &str| {
Ok(VVal::Int(util::edit_distance(a, b) as i64))
})
})
}, Some(2), Some(2), false);
func!(st, "char:to_lowercase",
|env: &mut Env, _argc: usize| {
Ok(VVal::Chr(VValChr::Char(
env.arg(0).c().to_lowercase().next().unwrap_or('\0'))))
}, Some(1), Some(1), false);
func!(st, "char:to_uppercase",
|env: &mut Env, _argc: usize| {
Ok(VVal::Chr(VValChr::Char(
env.arg(0).c().to_uppercase().next().unwrap_or('\0'))))
}, Some(1), Some(1), false);
func!(st, "bytes:replace",
|env: &mut Env, _argc: usize| {
let bv = env.arg(0);
let pat = env.arg(1);
let repl = env.arg(2);
Ok(bv.bytes_replace(&pat, &repl))
}, Some(3), Some(3), false);
func!(st, "bytes:from_vec",
|env: &mut Env, _argc: usize| {
if let VVal::Lst(u) = env.arg(0) {
Ok(VVal::new_byt(u.borrow().iter().map(|v| v.i() as u8).collect()))
} else {
Ok(VVal::None)
}
}, Some(1), Some(1), false);
func!(st, "bytes:to_vec",
|env: &mut Env, _argc: usize| {
if let VVal::Byt(u) = env.arg(0) {
Ok(VVal::vec_mv(
u.iter()
.map(|u| VVal::Int(i64::from(*u)))
.collect()))
} else {
Ok(VVal::vec_mv(
env.arg(0).as_bytes().iter()
.map(|u| VVal::Int(i64::from(*u)))
.collect()))
}
}, Some(1), Some(1), false);
func!(st, "bytes:pack",
|env: &mut Env, _argc: usize| {
use crate::packer;
let data = env.arg(1);
env.arg_ref(0).unwrap().with_s_ref(|s: &str| {
match packer::do_pack(s, &data) {
Ok(v) => Ok(v),
Err(e) => {
Ok(env.new_err(
format!("Bad pack '{}': {}", s, e)))
},
}
})
}, Some(2), Some(2), false);
func!(st, "bytes:unpack",
|env: &mut Env, _argc: usize| {
use crate::packer;
let data = env.arg(1);
env.arg_ref(0).unwrap().with_s_ref(|s: &str| {
match packer::do_unpack(s, &data) {
Ok(v) => Ok(v),
Err(e) => {
Ok(env.new_err(
format!("Bad (un)pack '{}': {}", s, e)))
},
}
})
}, Some(2), Some(2), false);
func!(st, "bytes:from_hex",
|env: &mut Env, _argc: usize| {
env.arg_ref(0).unwrap().with_s_ref(|s: &str| {
let out : Vec<u8> = Vec::with_capacity((s.len() + 1) / 2);
Ok(VVal::new_byt(
s.chars()
.map(|c|
match c { '0'..='9' => i16::from( 9 - (b'9' - (c as u8))),
'a'..='f' => i16::from(15 - (b'f' - (c as u8))),
'A'..='F' => i16::from(15 - (b'F' - (c as u8))),
_ => -1 })
.fold((256, out), |(last, mut out), c: i16|
if c == -1 { (last, out) }
else if last == 256 { (c, out) }
else {
out.push((((last << 4) | (c & 0x0F)) & 0xFF) as u8);
(256, out)
}).1))
})
}, Some(1), Some(1), false);
func!(st, "bytes:to_hex",
|env: &mut Env, argc: usize| {
static HEXCHARS : &[char] =
&['0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'];
if let VVal::Byt(u) = env.arg(0) {
let mut out : String =
String::with_capacity(u.len() * 2);
if argc == 1 {
for (a, b) in u.iter().map(|u|
(HEXCHARS[(u >> 4) as usize],
HEXCHARS[(u & 0x0F) as usize])) {
out.push(a);
out.push(b);
}
} else {
let group_len = env.arg(1).i();
let group_sep =
if env.arg(2).is_none() { String::from(" ") }
else { env.arg(2).s_raw() };
let mut len_counter = 0;
for (a, b) in u.iter().map(|u|
(HEXCHARS[(u >> 4) as usize],
HEXCHARS[(u & 0x0F) as usize])) {
if len_counter >= group_len { out.push_str(&group_sep); len_counter = 0; }
out.push(a);
len_counter += 1;
if len_counter >= group_len { out.push_str(&group_sep); len_counter = 0; }
out.push(b);
len_counter += 1;
}
}
Ok(VVal::new_str_mv(out))
} else {
Ok(VVal::None)
}
}, Some(1), Some(3), false);
func!(st, "to_no_arity",
|env: &mut Env, _argc: usize| {
let v = env.arg(0);
Ok(v.disable_function_arity())
}, Some(1), Some(1), false);
func!(st, "to_drop",
|env: &mut Env, _argc: usize| {
let fun = env.arg(0).disable_function_arity();
Ok(VVal::DropFun(Rc::new(DropFun { fun })))
}, Some(1), Some(1), false);
func!(st, "fold",
|env: &mut Env, _argc: usize| {
let f = env.arg(1);
let lst = env.arg(2);
lst.with_iter(move |iter| {
let mut acc = env.arg(0);
for (i, _) in iter {
env.push(i);
env.push(acc.clone());
let rv = f.call_internal(env, 2);
env.popn(2);
match rv {
Ok(v) => { acc = v; },
Err(StackAction::Break(v)) => { acc = *v; break; },
Err(StackAction::Next) => { },
Err(e) => { return Err(e); },
}
}
Ok(acc)
})
}, Some(3), Some(3), false);
func!(st, "enumerate",
|env: &mut Env, _argc: usize| {
let f = env.arg(0);
let i = Rc::new(std::cell::RefCell::new(0));
Ok(VValFun::new_fun(
move |env: &mut Env, argc: usize| {
env.push(VVal::Int(*i.borrow() as i64));
let r = f.call_internal(env, 1 + argc);
*i.borrow_mut() += 1;
env.popn(1);
r
}, None, None, false))
}, Some(1), Some(1), false);
func!(st, "zip",
|env: &mut Env, _argc: usize| {
let o = env.arg(0);
let f = env.arg(1);
let i = Rc::new(std::cell::RefCell::new(0));
Ok(VValFun::new_fun(
move |env: &mut Env, argc: usize| {
env.push(o.at(*i.borrow()).unwrap_or(VVal::None));
let r = f.call_internal(env, 1 + argc);
*i.borrow_mut() += 1;
env.popn(1);
r
}, None, None, false))
}, Some(2), Some(2), false);
add_num_fun_flt!(st, "num:ceil", ceil);
add_num_fun_flt!(st, "num:sqrt", sqrt);
add_num_fun_flt!(st, "num:cbrt", cbrt);
add_num_fun_flt!(st, "num:floor", floor);
add_num_fun_flt!(st, "num:round", round);
add_num_fun_flt!(st, "num:abs", abs);
add_num_fun_flt!(st, "num:trunc", trunc);
add_num_fun_flt!(st, "num:to_degrees", to_degrees);
add_num_fun_flt!(st, "num:to_radians", to_radians);
add_num_fun_flt!(st, "num:tan", tan);
add_num_fun_flt!(st, "num:tanh", tanh);
add_num_fun_flt!(st, "num:sin", sin);
add_num_fun_flt!(st, "num:sinh", sinh);
add_num_fun_flt!(st, "num:cos", cos);
add_num_fun_flt!(st, "num:cosh", cosh);
add_num_fun_flt!(st, "num:asin", asin);
add_num_fun_flt!(st, "num:asinh", asinh);
add_num_fun_flt!(st, "num:acos", acos);
add_num_fun_flt!(st, "num:acosh", acosh);
add_num_fun_flt!(st, "num:recip", recip);
add_num_fun_flt!(st, "num:log2", log2);
add_num_fun_flt!(st, "num:log10", log10);
add_num_fun_flt!(st, "num:ln", ln);
add_num_fun_flt!(st, "num:exp_m1", exp_m1);
add_num_fun_flt!(st, "num:exp", exp);
add_num_fun_flt!(st, "num:exp2", exp2);
add_num_fun_flt!(st, "num:atan", atan);
add_num_fun_flt!(st, "num:atanh", atanh);
add_num_fun!(st, "num:signum", signum);
add_num_fun_flt2!(st, "num:log", log);
add_num_fun_flt2!(st, "num:atan2", atan2);
add_num_fun_flt2!(st, "num:hypot", hypot);
add_num_fun_flt2!(st, "num:pow", powf);
func!(st, "num:abs",
|env: &mut Env, _argc: usize| {
Ok(match env.arg(0) {
VVal::Int(i) => VVal::Int(i.abs()),
VVal::Flt(i) => VVal::Flt(i.abs()),
_ => VVal::Int(env.arg(0).i().abs())
})
}, Some(1), Some(1), false);
func!(st, "num:fract",
|env: &mut Env, _argc: usize| {
Ok(VVal::Flt(env.arg(0).f().fract()))
}, Some(1), Some(1), false);
func!(st, "num:lerp",
|env: &mut Env, _argc: usize| {
let a = env.arg(0).f();
let b = env.arg(1).f();
let x = env.arg(2).f();
Ok(VVal::Flt(a * (1.0 - x) + b * x))
}, Some(3), Some(3), false);
func!(st, "num:smoothstep",
|env: &mut Env, _argc: usize| {
let a = env.arg(0).f();
let b = env.arg(1).f();
let x = env.arg(2).f();
let x = (x - a) / (b - a);
let x = x.max(0.0).min(1.0);
Ok(VVal::Flt(x * x * (3.0 - (2.0 * x))))
}, Some(3), Some(3), false);
func!(st, "fs:read_dir",
|env: &mut Env, _argc: usize| {
let path = env.arg(0);
let f = env.arg(1);
path.with_s_ref(|path| {
let mut ret = VVal::None;
let mut stack = vec![path.to_string()];
while !stack.is_empty() {
let path = stack.pop().unwrap();
match std::fs::read_dir(&path) {
Ok(iter) => {
for entry in iter {
let ve = dir_entry_to_vval(env, &path, entry)?;
let is_dir =
ve.get_key("type")
.unwrap_or(VVal::None)
.with_s_ref(|s| s == "d");
let entry_path =
if is_dir {
Some(
ve.get_key("path")
.unwrap_or(VVal::None)
.s_raw())
} else { None };
env.push(ve);
match f.call_internal(env, 1) {
Ok(v) => { ret = v; },
Err(StackAction::Break(v)) => { env.popn(1); return Ok(*v); },
Err(StackAction::Next) => { },
Err(e) => { env.popn(1); return Err(e); }
}
env.popn(1);
if is_dir && ret.b() {
if let Some(ep) = entry_path {
stack.push(ep);
}
}
}
},
Err(e) => {
return Ok(env.new_err(
format!(
"Couldn't read directory '{}': {}",
path, e)))
},
}
}
Ok(ret)
})
}, Some(2), Some(2), false);
func!(st, "fs:copy",
|env: &mut Env, _argc: usize| {
let from = env.arg(0).s_raw();
let to = env.arg(1).s_raw();
use std::path::Path;
match std::fs::copy(Path::new(&from), Path::new(&to)) {
Ok(_) => Ok(VVal::Bol(true)),
Err(e) => {
Ok(env.new_err(
format!(
"Couldn't copy file '{}' to file '{}': {}",
from, to, e)))
},
}
}, Some(2), Some(2), false);
func!(st, "fs:rename",
|env: &mut Env, _argc: usize| {
let from = env.arg(0);
let to = env.arg(1);
from.with_s_ref(|from| to.with_s_ref(|to| {
if let Err(e) = std::fs::rename(&from, &to) {
return Ok(env.new_err(
format!(
"Couldn't rename file '{}' to file '{}': {}",
from, to, e)));
}
Ok(VVal::Bol(true))
}))
}, Some(2), Some(2), false);
func!(st, "fs:remove_file",
|env: &mut Env, _argc: usize| {
let path = env.arg(0);
path.with_s_ref(|path| {
if let Err(e) = std::fs::remove_file(&path) {
return Ok(env.new_err(
format!(
"Couldn't remove file '{}': {}",
path, e)));
}
Ok(VVal::Bol(true))
})
}, Some(1), Some(1), false);
func!(st, "fs:remove_dir",
|env: &mut Env, _argc: usize| {
let path = env.arg(0);
path.with_s_ref(|path| {
if let Err(e) = std::fs::remove_dir(&path) {
return Ok(env.new_err(
format!(
"Couldn't remove dir '{}': {}",
path, e)));
}
Ok(VVal::Bol(true))
})
}, Some(1), Some(1), false);
func!(st, "fs:remove_dir_all",
|env: &mut Env, _argc: usize| {
let path = env.arg(0);
path.with_s_ref(|path| {
if let Err(e) = std::fs::remove_dir_all(&path) {
return Ok(env.new_err(
format!(
"Couldn't remove dir (recursively) '{}': {}",
path, e)));
}
Ok(VVal::Bol(true))
})
}, Some(1), Some(1), false);
func!(st, "io:line",
|env: &mut Env, _argc: usize| {
let mut line = String::new();
let mut read = env.stdio.read.borrow_mut();
match read.read_line(&mut line) {
Ok(n) => {
if n == 0 {
return Ok(VVal::None);
}
},
Err(e) => {
return Ok(env.new_err(
format!("IO-Error on std:io:line: {}", e)))
},
}
Ok(VVal::new_str_mv(line))
}, Some(0), Some(0), false);
func!(st, "io:lines",
|env: &mut Env, argc: usize| {
let (f, mut ret) =
if argc == 0 {
let vec = VVal::vec();
(vec.clone(), vec)
} else {
(env.arg(0), VVal::None)
};
loop {
let mut line = String::new();
{
let mut read = env.stdio.read.borrow_mut();
match read.read_line(&mut line) {
Ok(n) => { if n == 0 { break; } },
Err(e) => {
return Ok(env.new_err(
format!("IO-Error on std:io:lines: {}", e)))
},
}
}
if f.is_vec() {
f.push(VVal::new_str_mv(line));
} else {
env.push(VVal::new_str_mv(line));
match f.call_internal(env, 1) {
Ok(v) => { ret = v; },
Err(StackAction::Break(v)) => { env.popn(1); return Ok(*v); },
Err(StackAction::Next) => { },
Err(e) => { env.popn(1); return Err(e); }
}
env.popn(1);
}
}
Ok(ret)
}, Some(0), Some(1), false);
func!(st, "io:stdout:flush",
|env: &mut Env, _argc: usize| {
if let Err(e) = env.stdio.write.borrow_mut().flush() {
Ok(env.new_err(
format!("IO-Error on std:io:stdout:flush: {}", e)))
} else {
Ok(VVal::Bol(true))
}
}, Some(0), Some(0), false);
func!(st, "io:stdout:newline",
|env: &mut Env, _argc: usize| {
if let Err(e) = writeln!(*env.stdio.write.borrow_mut(), "") {
Ok(env.new_err(
format!("IO-Error on std:io:stdout:newline: {}", e)))
} else {
Ok(VVal::Bol(true))
}
}, Some(0), Some(0), false);
func!(st, "io:stdout:write",
|env: &mut Env, _argc: usize| {
let v = env.arg(0);
if let Err(e) = write!(*env.stdio.write.borrow_mut(), "{}", v.s()) {
Ok(env.new_err(
format!("IO-Error on std:io:stdout:write: {}", e)))
} else {
Ok(v)
}
}, Some(1), Some(1), false);
func!(st, "io:stdout:print",
|env: &mut Env, _argc: usize| {
let v = env.arg(0);
env.arg_ref(0).unwrap().with_s_ref(|vs: &str| {
if let Err(e) = write!(*env.stdio.write.borrow_mut(), "{}", vs) {
Ok(env.new_err(
format!("IO-Error on std:io:stdout:print: {}", e)))
} else {
Ok(v)
}
})
}, Some(1), Some(1), false);
func!(st, "io:file:read_text",
|env: &mut Env, _argc: usize| {
let filename = env.arg(0).s_raw();
use std::io::prelude::*;
use std::fs::OpenOptions;
let file =
OpenOptions::new()
.write(false)
.create(false)
.read(true)
.open(&filename);
match file {
Err(e) => {
Ok(env.new_err(
format!("Couldn't open file '{}': {}", filename, e)))
},
Ok(mut f) => {
let mut contents = String::new();
if let Err(e) = f.read_to_string(&mut contents) {
Ok(env.new_err(
format!(
"Couldn't read text from file '{}': {}",
filename, e)))
} else {
Ok(VVal::new_str_mv(contents))
}
},
}
}, Some(1), Some(1), false);
func!(st, "io:file:read",
|env: &mut Env, _argc: usize| {
let filename = env.arg(0).s_raw();
use std::io::prelude::*;
use std::fs::OpenOptions;
let file =
OpenOptions::new()
.write(false)
.create(false)
.read(true)
.open(&filename);
match file {
Err(e) => {
Ok(env.new_err(
format!("Couldn't open file '{}': {}", filename, e)))
},
Ok(mut f) => {
let mut contents : Vec<u8> = Vec::new();
if let Err(e) = f.read_to_end(&mut contents) {
Ok(env.new_err(
format!(
"Couldn't read text from file '{}': {}",
filename, e)))
} else {
Ok(VVal::new_byt(contents))
}
},
}
}, Some(1), Some(1), false);
func!(st, "io:file:write_safe",
|env: &mut Env, _argc: usize| {
let filename = env.arg(0).s_raw();
let tmp_filename = format!("{}~", filename);
let contents = env.arg(1);
let buf = match contents {
VVal::Byt(b) => b.as_ref().clone(), // TODO: Remove clone
v => v.with_s_ref(|v: &str| v.as_bytes().to_vec()),
};
use std::io::prelude::*;
use std::fs::OpenOptions;
let file =
OpenOptions::new()
.create(true)
.write(true)
.truncate(true)
.open(&tmp_filename);
match file {
Err(e) => {
Ok(env.new_err(
format!(
"Couldn't open file '{}': {}",
filename, e)))
},
Ok(mut f) => {
if let Err(e) = f.write_all(&buf) {
return Ok(env.new_err(
format!(
"Couldn't write to file '{}': {}",
tmp_filename, e)));
}
if let Err(e) = std::fs::rename(&tmp_filename, &filename) {
return Ok(env.new_err(
format!(
"Couldn't rename file '{}' to file '{}': {}",
tmp_filename, filename, e)));
}
Ok(VVal::Bol(true))
},
}
}, Some(2), Some(2), false);
func!(st, "io:file:append",
|env: &mut Env, _argc: usize| {
let filename = env.arg(0).s_raw();
let contents = env.arg(1);
let buf = match contents {
VVal::Byt(b) => b.as_ref().clone(), // TODO: Remove clone
v => v.with_s_ref(|v: &str| v.as_bytes().to_vec()),
};
use std::io::prelude::*;
use std::fs::OpenOptions;
let file =
OpenOptions::new()
.create(true)
.write(true)
.append(true)
.open(&filename);
match file {
Err(e) => {
Ok(env.new_err(
format!(
"Couldn't open file '{}': {}",
filename, e)))
},
Ok(mut f) => {
if let Err(e) = f.write_all(&buf) {
Ok(env.new_err(
format!(
"Couldn't write to file '{}': {}",
filename, e)))
} else {
Ok(VVal::Bol(true))
}
},
}
}, Some(2), Some(2), false);
func!(st, "writeln",
|env: &mut Env, argc: usize| {
print_value(env, argc, false)
}, None, None, false);
func!(st, "displayln",
|env: &mut Env, argc: usize| {
print_value(env, argc, true)
}, None, None, false);
func!(st, "write_str",
|env: &mut Env, _argc: usize| {
Ok(VVal::new_str_mv(env.arg_ref(0).unwrap().s()))
}, Some(1), Some(1), false);
func!(st, "dump_upvals",
|env: &mut Env, _argc: usize| {
if let VVal::Fun(f) = env.arg(0).deref() {
return Ok(f.dump_upvals());
}
Ok(VVal::None)
}, Some(1), Some(1), false);
func!(st, "wlambda:sizes",
|env: &mut Env, _argc: usize| {
let mut write = env.stdio.write.borrow_mut();
use crate::compiler::*;
use crate::ops::*;
use crate::vval::*;
use crate::str_int::*;
sizeof_writeln!(write, VVal);
sizeof_writeln!(write, Op);
sizeof_writeln!(write, Builtin);
sizeof_writeln!(write, NVecPos);
sizeof_writeln!(write, Prog);
sizeof_writeln!(write, ResPos);
sizeof_writeln!(write, (ResPos, Box<Symbol>, ResPos));
sizeof_writeln!(write, (ResPos, Box<String>, u16, ResPos));
sizeof_writeln!(write, (ResPos, ResPos, u16, ResPos));
sizeof_writeln!(write, (ResPos, ResPos, ResPos, ResPos));
sizeof_writeln!(write, (ResPos, DirectFun, ResPos));
sizeof_writeln!(write, (DirectFun, ResPos, ResPos));
sizeof_writeln!(write, (Box<String>, ResPos, ResPos, u16));
sizeof_writeln!(write, SynPos);
sizeof_writeln!(write, Symbol);
sizeof_writeln!(write, Option<Rc<VVal>>);
sizeof_writeln!(write, crate::nvec::NVec<f64>);
sizeof_writeln!(write, Box<crate::nvec::NVec<f64>>);
sizeof_writeln!(write, Result<VVal, StackAction>);
sizeof_writeln!(write, StackAction);
sizeof_writeln!(write, Box<String>);
sizeof_writeln!(write, Box<Vec<VVal>>);
sizeof_writeln!(write, Vec<VVal>);
sizeof_writeln!(write, Box<dyn VValUserData>);
sizeof_writeln!(write, std::rc::Weak<std::cell::RefCell<VVal>>);
Ok(VVal::None)
}, Some(0), Some(0), false);
//// println!("sizeof OP:{} bytes", std::mem::size_of::<(ResPos, Box<String>, Box<String>, Box<String>, ResPos)>());
func!(st, "wlambda:version",
|_env: &mut Env, _argc: usize| {
Ok(VVal::new_str(VERSION))
}, Some(0), Some(0), false);
func!(st, "wlambda:parse",
|env: &mut Env, argc: usize| {
let filename =
if argc == 2 { env.arg(1).s_raw() }
else { "<wlambda:parse:input>".to_string() };
let res =
env.arg(0).with_s_ref(|s| { crate::parser::parse(s, &filename) });
match res {
Ok(ast) => Ok(ast),
Err(e) => Ok(env.new_err(e))
}
}, Some(1), Some(2), false);
func!(st, "syn:type",
|env: &mut Env, _argc: usize| {
Ok(VVal::new_sym(env.arg(0).syntax_type()))
}, Some(1), Some(1), false);
func!(st, "syn:pos",
|env: &mut Env, _argc: usize| {
if let VVal::Syn(sp) = env.arg(0) {
if let Some(name) = sp.info.name.as_ref() {
Ok(VVal::vec4(
VVal::new_str(sp.info.file.s()),
VVal::Int(sp.info.line as i64),
VVal::Int(sp.info.col as i64),
VVal::new_str(name)))
} else {
Ok(VVal::vec3(
VVal::new_str(sp.info.file.s()),
VVal::Int(sp.info.line as i64),
VVal::Int(sp.info.col as i64)))
}
} else {
Ok(VVal::None)
}
}, Some(1), Some(1), false);
func!(st, "measure_time",
|env: &mut Env, _argc: usize| {
let t = std::time::Instant::now();
let unit = env.arg(0).s_raw();
match env.arg(1).call_no_args(env) {
Ok(v) => {
let ret = VVal::vec();
ret.push(duration_to_vval(t.elapsed(), &unit[..]));
ret.push(v);
Ok(ret)
},
Err(e) => Err(e),
}
}, Some(2), Some(2), false);
func!(st, "assert_eq",
|env: &mut Env, _argc: usize| {
let a = env.arg(0);
let b = env.arg(1);
if !a.eqv(&b) {
if env.arg(2).is_none() {
Err(StackAction::panic_msg(
format!(
"assertion failed: expected: '{}', got: '{}'",
b.s(), a.s())))
} else {
Err(StackAction::panic_msg(
format!(
"assertion '{}' failed: expected: '{}', got: '{}'",
env.arg(2).s_raw(), b.s(), a.s())))
}
} else {
Ok(VVal::Bol(true))
}
}, Some(2), Some(3), true);
func!(st, "assert_str_eq",
|env: &mut Env, _argc: usize| {
let a = env.arg(0).s();
let b = env.arg(1).s();
if a != b {
if env.arg(2).is_none() {
Err(StackAction::panic_msg(
format!(
"assertion failed: expected: '{}', got: '{}'",
b, a)))
} else {
Err(StackAction::panic_msg(
format!(
"assertion '{}' failed: expected: '{}', got: '{}'",
env.arg(2).s_raw(), b, a)))
}
} else {
Ok(VVal::Bol(true))
}
}, Some(2), Some(3), true);
func!(st, "assert_rel_eq",
|env: &mut Env, _argc: usize| {
let l = env.arg(0);
let r = env.arg(1);
let epsilon = env.arg(2).f();
let delta = (l.f() - r.f()).abs();
if delta < epsilon {
Ok(VVal::Bol(true))
} else {
Err(StackAction::panic_msg(format!(
"assertion{}failed: delta[{}] was more than epsilon[{}],\
left['{}', f:'{}'], right['{}', f:'{}']",
if env.arg(3).is_none() {
" ".to_string()
} else {
format!(" '{}' ", env.arg(3).s_raw())
},
delta, epsilon, l.s(), l.f(), r.s(), r.f()
)))
}
}, Some(3), Some(4), true);
func!(st, "to_ref",
|env: &mut Env, _argc: usize| {
Ok(env.arg(0).to_ref())
}, Some(1), Some(1), true);
func!(st, "ref_id",
|env: &mut Env, _argc: usize| {
if let Some(id) = env.arg_ref(0).unwrap_or(&VVal::None).ref_id() {
Ok(VVal::Int(id))
} else {
Ok(VVal::None)
}
}, Some(1), Some(1), true);
func!(st, "ref:set",
|env: &mut Env, _argc: usize| {
Ok(env.arg(0).set_ref(env.arg(1)))
}, Some(2), Some(2), false);
func!(st, "ref:strengthen",
|env: &mut Env, _argc: usize| {
Ok(env.arg(0).upgrade())
}, Some(1), Some(1), false);
func!(st, "ref:hide",
|env: &mut Env, _argc: usize| {
Ok(env.arg(0).hide_ref())
}, Some(1), Some(1), false);
func!(st, "ref:weaken",
|env: &mut Env, _argc: usize| {
Ok(env.arg(0).downgrade())
}, Some(1), Some(1), false);
func!(st, "assert",
|env: &mut Env, _argc: usize| {
if !env.arg(0).b() {
if env.arg(1).is_none() {
Err(StackAction::panic_msg("assertion failed".to_string()))
} else {
Err(StackAction::panic_msg(format!("assertion failed '{}'", env.arg(1).s_raw())))
}
} else {
Ok(env.arg(0))
}
}, Some(1), Some(2), true);
func!(st, "pattern",
|env: &mut Env, _argc: usize| {
let pat_src = env.arg_ref(0).cloned().unwrap_or(VVal::None);
let res_ref =
env.global.borrow_mut()
.get_var_ref("\\")
.unwrap_or(VVal::None);
let mode = env.arg(1).with_s_ref(RegexMode::from_str);
pat_src.with_s_ref(|pat_src|
match create_regex_find_function(pat_src, res_ref, mode) {
Ok(fun) => Ok(fun),
Err(e) => {
Ok(env.new_err(
format!("bad pattern: {}, pattern was: /{}/",
e, pat_src)))
}
})
}, Some(1), Some(2), false);
func!(st, "selector",
|env: &mut Env, _argc: usize| {
let pat_src = env.arg_ref(0).cloned().unwrap_or(VVal::None);
let res_ref =
env.global.borrow_mut()
.get_var_ref("\\")
.unwrap_or(VVal::None);
pat_src.with_s_ref(|sel_src|
match create_selector_function(sel_src, res_ref) {
Ok(fun) => Ok(fun),
Err(e) => {
Ok(env.new_err(
format!("bad selector: {}, selector was: /{}/",
e, sel_src)))
}
})
}, Some(1), Some(1), false);
func!(st, "formatter",
|env: &mut Env, _argc: usize| {
let fmt_src = env.arg(0);
match create_formatter_fun(&fmt_src) {
Ok(fun) => Ok(fun),
Err(e) => {
Ok(env.new_err(
format!("bad formatter: {}, formatter was: /{}/",
e, fmt_src.s_raw())))
}
}
}, Some(1), Some(1), false);
// func!(st, "tree_select",
// |env: &mut Env, _argc: usize| {
// let slct = env.arg(0);
// let tree = env.arg(1);
// Ok(util::tree_select(&slct, &tree))
// }, Some(2), Some(2), false);
func!(st, "ser:csv",
|env: &mut Env, _argc: usize| {
use crate::csv;
let delim =
if env.arg(0).is_none() {
",".to_string()
} else {
env.arg(0).s_raw()
};
let row_sep =
if env.arg(1).is_none() {
"\r\n".to_string()
} else {
env.arg(1).s_raw()
};
let escape_all = env.arg(2).b();
let val = env.arg(3);
Ok(VVal::new_str_mv(csv::to_csv(
delim.chars().next().unwrap_or(','),
&row_sep,
escape_all,
val)))
}, Some(4), Some(4), false);
func!(st, "deser:csv",
|env: &mut Env, _argc: usize| {
use crate::csv;
let delim =
if env.arg(0).is_none() {
",".to_string()
} else {
env.arg(0).s_raw()
};
let row_sep =
if env.arg(1).is_none() {
"\r\n".to_string()
} else {
env.arg(1).s_raw()
};
env.arg_ref(2).unwrap().with_s_ref(|data: &str| {
match csv::parse_csv(
delim.chars().next().unwrap_or(','),
&row_sep,
data)
{
Ok(v) => Ok(v),
Err(e) => Ok(env.new_err(e)),
}
})
}, Some(3), Some(3), false);
#[cfg(feature="regex")]
func!(st, "re:replace_all",
|env: &mut Env, _argc: usize| {
use regex::Regex;
let re = env.arg(0);
let f = env.arg(1);
let text = env.arg(2);
let rx = re.with_s_ref(Regex::new);
if let Err(e) = rx {
return Ok(env.new_err(
format!("Regex '{}' did not compile: {}", re.s_raw(), e)));
}
let rx = rx.unwrap();
let mut finished = false;
let mut ret = Ok(VVal::None);
let ret_str = text.with_s_ref(|text: &str| {
VVal::new_str_mv(String::from(
rx.replace_all(text, |capts: ®ex::Captures| {
let captures = VVal::vec();
for cap in capts.iter() {
match cap {
None => { captures.push(VVal::None); },
Some(c) => {
captures.push(VVal::new_str(c.as_str()));
}
}
}
let repl = captures.at(0).unwrap_or(VVal::None).s_raw();
if finished { return repl; }
if f.is_fun() {
env.push(captures);
let rv = f.call_internal(env, 1);
env.popn(1);
match rv {
Ok(v) => v.s_raw(),
Err(StackAction::Break(v)) => { finished = true; v.s_raw() },
Err(StackAction::Next) => { repl },
Err(e) => { finished = true; ret = Err(e); repl },
}
} else {
f.s_raw()
}
})))
});
if ret.is_err() { return ret; }
Ok(ret_str)
}, Some(3), Some(3), false);
#[cfg(feature="regex")]
func!(st, "re:match_compile",
|env: &mut Env, _argc: usize| {
use regex::Regex;
let re = env.arg(0);
let rx = re.with_s_ref(Regex::new);
if let Err(e) = rx {
return Ok(env.new_err(
format!("Regex '{}' did not compile: {}", re.s_raw(), e)));
}
let rx = rx.unwrap();
Ok(VValFun::new_fun(
move |env: &mut Env, _argc: usize| {
let text = env.arg(0);
let f = env.arg(1);
text.with_s_ref(|text: &str| {
match rx.captures(text) {
Some(c) => {
let captures = VVal::vec();
for cap in c.iter() {
match cap {
None => { captures.push(VVal::None); },
Some(c) => {
captures.push(VVal::new_str(c.as_str()));
}
}
}
env.push(captures);
let rv = f.call_internal(env, 1);
env.popn(1);
rv
},
None => {
Ok(VVal::None)
}
}
})
}, Some(2), Some(2), false))
}, Some(1), Some(1), false);
#[cfg(feature="regex")]
func!(st, "re:match",
|env: &mut Env, _argc: usize| {
use regex::Regex;
let re = env.arg(0);
let text = env.arg(1);
let f = env.arg(2);
let rx = re.with_s_ref(Regex::new);
if let Err(e) = rx {
return Ok(env.new_err(
format!("Regex '{}' did not compile: {}", re.s_raw(), e)));
}
let rx = rx.unwrap();
text.with_s_ref(|text: &str| {
match rx.captures(text) {
Some(c) => {
let captures = VVal::vec();
for cap in c.iter() {
match cap {
None => { captures.push(VVal::None); },
Some(c) => {
captures.push(VVal::new_str(c.as_str()));
}
}
}
env.push(captures);
let rv = f.call_internal(env, 1);
env.popn(1);
rv
},
None => {
Ok(VVal::None)
}
}
})
}, Some(3), Some(3), false);
#[cfg(feature="regex")]
func!(st, "re:map",
|env: &mut Env, _argc: usize| {
use regex::Regex;
let re = env.arg(0);
let f = env.arg(1);
let text = env.arg(2);
let rx = re.with_s_ref(Regex::new);
if let Err(e) = rx {
return Ok(env.new_err(
format!("Regex '{}' did not compile: {}", re.s_raw(), e)));
}
let rx = rx.unwrap();
let mut ret = VVal::None;
text.with_s_ref(|text: &str| {
for capts in rx.captures_iter(text) {
let captures = VVal::vec();
for cap in capts.iter() {
match cap {
None => { captures.push(VVal::None); },
Some(c) => {
captures.push(VVal::new_str(c.as_str()));
}
}
}
env.push(captures);
let rv = f.call_internal(env, 1);
env.popn(1);
match rv {
Ok(v) => { ret = v; },
Err(StackAction::Break(v)) => { ret = *v; break; },
Err(StackAction::Next) => { },
Err(e) => { return Err(e); },
}
}
Ok(ret)
})
}, Some(3), Some(3), false);
func!(st, "srand",
|env: &mut Env, argc: usize| {
if argc == 0 {
use std::time::SystemTime;
match SystemTime::now().duration_since(SystemTime::UNIX_EPOCH) {
Ok(n) => util::srand(n.as_nanos() as i64),
Err(_) => util::srand(1_234_567_890),
}
} else {
util::srand(env.arg(0).i());
}
Ok(VVal::None)
}, Some(0), Some(1), false);
func!(st, "rand",
|env: &mut Env, argc: usize| {
if argc == 0 {
Ok(VVal::Flt(util::rand_closed_open01()))
} else {
match env.arg(0).deref() {
VVal::Flt(f)
=> Ok(VVal::Flt(util::rand_closed_open01() * f)),
VVal::Int(i)
=> Ok(VVal::Int(util::rand_i(i as u64))),
v => {
if v.with_s_ref(|s| s == "i64") {
Ok(VVal::Int(util::rand_full_i()))
} else {
Ok(VVal::Int(util::rand_i(v.i() as u64)))
}
}
}
}
}, Some(0), Some(1), false);
func!(st, "sys:os",
|_env: &mut Env, _argc: usize| {
Ok(VVal::new_str(std::env::consts::OS))
}, Some(0), Some(0), false);
func!(st, "time:now",
|env: &mut Env, _argc: usize| {
use std::time::SystemTime;
match SystemTime::now().duration_since(SystemTime::UNIX_EPOCH) {
Ok(n) => {
Ok(env.arg(0).with_s_ref(|unit|
duration_to_vval(n, unit)))
},
Err(_) =>
Err(StackAction::panic_msg(
"SystemTime before UNIX EPOCH!".to_string()))
}
}, Some(0), Some(1), false);
#[cfg(feature="chrono")]
func!(st, "chrono:timestamp",
|env: &mut Env, _argc: usize| {
use chrono::prelude::*;
let dt = Local::now();
let fmt = env.arg(0);
if fmt.is_str() {
fmt.with_s_ref(|fmt: &str|
Ok(VVal::new_str_mv(dt.format(fmt).to_string())))
} else {
Ok(VVal::new_str_mv(dt.format("%Y-%m-%d %H:%M:%S.%f").to_string()))
}
}, Some(0), Some(1), false);
#[cfg(feature="chrono")]
func!(st, "chrono:format_utc",
|env: &mut Env, _argc: usize| {
use chrono::prelude::*;
let dt = Utc.timestamp(env.arg(0).i(), 0);
let fmt = env.arg(1);
if fmt.is_str() {
fmt.with_s_ref(|fmt: &str|
Ok(VVal::new_str_mv(dt.format(fmt).to_string())))
} else {
Ok(VVal::new_str_mv(dt.format("%Y-%m-%d %H:%M:%S.%f").to_string()))
}
}, Some(1), Some(2), false);
#[cfg(feature="chrono")]
func!(st, "chrono:format_local",
|env: &mut Env, _argc: usize| {
use chrono::prelude::*;
let dt = Local.timestamp(env.arg(0).i(), 0);
let fmt = env.arg(1);
if fmt.is_str() {
fmt.with_s_ref(|fmt: &str|
Ok(VVal::new_str_mv(dt.format(fmt).to_string())))
} else {
Ok(VVal::new_str_mv(dt.format("%Y-%m-%d %H:%M:%S.%f").to_string()))
}
}, Some(1), Some(2), false);
#[cfg(feature="serde_json")]
func!(st, "ser:json",
|env: &mut Env, _argc: usize| {
let v = env.arg(0);
let pp = env.arg(1).b();
match v.to_json(pp) {
Ok(s) => Ok(VVal::new_str_mv(s)),
Err(e) => Ok(env.new_err(e)),
}
}, Some(1), Some(2), false);
#[cfg(feature="serde_json")]
func!(st, "deser:json",
|env: &mut Env, _argc: usize| {
env.arg_ref(0).unwrap().with_s_ref(
|json_txt: &str|
match VVal::from_json(json_txt) {
Ok(v) => Ok(v),
Err(e) => Ok(env.new_err(e)),
})
}, Some(1), Some(1), false);
#[cfg(feature="rmp-serde")]
func!(st, "ser:msgpack",
|env: &mut Env, _argc: usize| {
let v = env.arg(0);
match v.to_msgpack() {
Ok(s) => Ok(VVal::new_byt(s)),
Err(e) => Ok(env.new_err(e)),
}
}, Some(1), Some(1), false);
#[cfg(feature="rmp-serde")]
func!(st, "deser:msgpack",
|env: &mut Env, _argc: usize| {
if let VVal::Byt(u) = env.arg(0) {
match VVal::from_msgpack(&u[..]) {
Ok(v) => Ok(v),
Err(e) => Ok(env.new_err(e)),
}
} else {
Ok(env.new_err("deser:msgpack expects bytes".to_string()))
}
}, Some(1), Some(1), false);
#[cfg(feature="base64")]
func!(st, "bytes:to_base64",
|env: &mut Env, _argc: usize| {
let mut cfg = base64::STANDARD;
let config = env.arg(1);
if config.is_some() {
cfg = config.with_s_ref(|s| match s {
"url" => base64::URL_SAFE,
"url_no_pad" => base64::URL_SAFE_NO_PAD,
"std" => base64::STANDARD,
"std_no_pad" => base64::STANDARD_NO_PAD,
_ => base64::STANDARD,
})
}
env.arg(0).with_bv_ref(|bytes| {
Ok(VVal::new_str_mv(base64::encode_config(bytes, cfg)))
})
}, Some(1), Some(2), false);
#[cfg(feature="base64")]
func!(st, "bytes:from_base64",
|env: &mut Env, _argc: usize| {
let mut cfg = base64::STANDARD;
let config = env.arg(1);
if config.is_some() {
cfg = config.with_s_ref(|s| match s {
"url" => base64::URL_SAFE,
"url_no_pad" => base64::URL_SAFE_NO_PAD,
"std" => base64::STANDARD,
"std_no_pad" => base64::STANDARD_NO_PAD,
_ => base64::STANDARD,
})
}
env.arg(0).with_bv_ref(|bytes| {
match base64::decode_config(bytes, cfg) {
Ok(bytes) => Ok(VVal::new_byt(bytes)),
Err(e) =>
Ok(env.new_err(
format!("bytes:from_base64: {}", e)))
}
})
}, Some(1), Some(2), false);
func!(st, "copy",
|env: &mut Env, _argc: usize| {
Ok(env.arg(0).shallow_clone())
}, Some(1), Some(1), false);
func!(st, "cmp:num:asc",
|env: &mut Env, _argc: usize| {
match env.arg(0).compare_num(&env.arg(1)) {
std::cmp::Ordering::Greater => Ok(VVal::Int(-1)),
std::cmp::Ordering::Less => Ok(VVal::Int(1)),
std::cmp::Ordering::Equal => Ok(VVal::Int(0)),
}
}, Some(2), Some(2), false);
func!(st, "cmp:num:desc",
|env: &mut Env, _argc: usize| {
match env.arg(0).compare_num(&env.arg(1)) {
std::cmp::Ordering::Greater => Ok(VVal::Int(1)),
std::cmp::Ordering::Less => Ok(VVal::Int(-1)),
std::cmp::Ordering::Equal => Ok(VVal::Int(0)),
}
}, Some(2), Some(2), false);
func!(st, "cmp:str:asc",
|env: &mut Env, _argc: usize| {
match env.arg(0).compare_str(&env.arg(1)) {
std::cmp::Ordering::Greater => Ok(VVal::Int(-1)),
std::cmp::Ordering::Less => Ok(VVal::Int(1)),
std::cmp::Ordering::Equal => Ok(VVal::Int(0)),
}
}, Some(2), Some(2), false);
func!(st, "cmp:str:desc",
|env: &mut Env, _argc: usize| {
match env.arg(0).compare_str(&env.arg(1)) {
std::cmp::Ordering::Greater => Ok(VVal::Int(1)),
std::cmp::Ordering::Less => Ok(VVal::Int(-1)),
std::cmp::Ordering::Equal => Ok(VVal::Int(0)),
}
}, Some(2), Some(2), false);
func!(st, "v:dims",
|env: &mut Env, _argc: usize| {
Ok(VVal::Int(match env.arg(0) {
VVal::FVec(fv) => fv.dims(),
v => v.nvec::<i64>().dims(),
} as i64))
}, Some(1), Some(1), false);
func!(st, "v:mag2",
|env: &mut Env, _argc: usize| {
Ok(VVal::Flt(match env.arg(0) {
VVal::FVec(fv) => fv.mag2(),
v => v.nvec::<i64>().mag2(),
}))
}, Some(1), Some(1), false);
func!(st, "v:mag",
|env: &mut Env, _argc: usize| {
Ok(VVal::Flt(match env.arg(0) {
VVal::FVec(fv) => fv.mag(),
v => v.nvec::<i64>().mag(),
}))
}, Some(1), Some(1), false);
func!(st, "v:norm",
|env: &mut Env, _argc: usize| {
Ok(match env.arg(0) {
VVal::FVec(fv) => VVal::FVec(Box::new(fv.norm())),
v => VVal::IVec(Box::new(v.nvec::<i64>().norm())),
})
}, Some(1), Some(1), false);
func!(st, "v:dot",
|env: &mut Env, _argc: usize| {
Ok(match env.arg(0) {
VVal::FVec(fv) => VVal::Flt(fv.dot(env.arg(1).nvec())),
v => VVal::Int(v.nvec::<i64>().dot(env.arg(1).nvec())),
})
}, Some(2), Some(2), false);
func!(st, "v:cross",
|env: &mut Env, _argc: usize| {
Ok(match env.arg(0) {
VVal::FVec(fv) => VVal::FVec(Box::new(fv.cross(env.arg(1).nvec()))),
v => VVal::IVec(Box::new(v.nvec::<i64>().cross(env.arg(1).nvec()))),
})
}, Some(2), Some(2), false);
func!(st, "v:lerp",
|env: &mut Env, _argc: usize| {
Ok(match env.arg(0) {
VVal::FVec(fv) => VVal::FVec(Box::new(fv.lerp(env.arg(1).nvec(), env.arg(2).f()))),
v => VVal::IVec(Box::new(v.nvec::<i64>().lerp(env.arg(1).nvec(), env.arg(2).f()))),
})
}, Some(3), Some(3), false);
func!(st, "v:slerp",
|env: &mut Env, _argc: usize| {
Ok(match env.arg(0) {
VVal::FVec(fv) => VVal::FVec(Box::new(fv.slerp(env.arg(1).nvec(), env.arg(2).f()))),
v => VVal::IVec(Box::new(v.nvec::<i64>().slerp(env.arg(1).nvec(), env.arg(2).f()))),
})
}, Some(3), Some(3), false);
func!(st, "v:vec2rad",
|env: &mut Env, _argc: usize| {
Ok(VVal::Flt(match env.arg(0) {
VVal::FVec(fv) => fv.vec2rad(),
v => v.nvec::<i64>().vec2rad(),
}))
}, Some(1), Some(1), false);
func!(st, "v:rad2vec",
|env: &mut Env, _argc: usize| {
Ok(VVal::FVec(Box::new(crate::nvec::NVec::rad2vec(env.arg(0).f()))))
}, Some(1), Some(1), false);
func!(st, "v:hex2hsva_i",
|env: &mut Env, _argc: usize| {
let hsvaf =
env.arg_ref(0)
.unwrap()
.with_s_ref(util::hex2hsvaf);
Ok(VVal::ivec4(
hsvaf.0.round() as i64,
hsvaf.1.round() as i64,
hsvaf.2.round() as i64,
hsvaf.3.round() as i64))
}, Some(1), Some(1), false);
func!(st, "v:hex2hsva_f",
|env: &mut Env, _argc: usize| {
let hsvaf =
env.arg_ref(0)
.unwrap()
.with_s_ref(util::hex2hsvaf);
Ok(VVal::fvec4(
hsvaf.0,
hsvaf.1,
hsvaf.2,
hsvaf.3))
}, Some(1), Some(1), false);
func!(st, "v:hex2rgba_i",
|env: &mut Env, _argc: usize| {
let (r, g, b, a) =
env.arg(0).with_s_ref(util::hex2rgba);
Ok(VVal::ivec_from_tpl4((r as i64, g as i64, b as i64, a as i64)))
}, Some(1), Some(1), false);
func!(st, "v:hex2rgba_f",
|env: &mut Env, _argc: usize| {
let (r, g, b, a) =
env.arg(0).with_s_ref(util::hex2rgbaf);
Ok(VVal::fvec_from_tpl4((r, g, b, a)))
}, Some(1), Some(1), false);
func!(st, "v:rgba2hex",
|env: &mut Env, _argc: usize| {
let arg = env.arg_ref(0).unwrap().deref();
process_vec_input!(env, arg, v, x, y, z, w, {
Ok(VVal::new_str_mv(util::rgba2hex( (x as u8, y as u8, z as u8, 255))))
}, {
Ok(VVal::new_str_mv(util::rgba2hexf((x, y, z, 1.0))))
}, {
Ok(VVal::new_str_mv(util::rgba2hex( (x as u8, y as u8, z as u8, w as u8))))
}, {
Ok(VVal::new_str_mv(util::rgba2hexf((x, y, z, w))))
})
}, Some(1), Some(1), false);
func!(st, "v:hsv2rgb",
|env: &mut Env, _argc: usize| {
let arg = env.arg_ref(0).unwrap().deref();
process_vec_input!(env, arg, v, x, y, z, w, {
let c =
util::hsv2rgb(
x as f64,
y as f64 / 100.0,
z as f64 / 100.0);
Ok(VVal::ivec_from_tpl3((
(c.0 * 255.0).round() as i64,
(c.1 * 255.0).round() as i64,
(c.2 * 255.0).round() as i64)))
}, {
let c = util::hsv2rgb(x, y, z);
Ok(VVal::fvec_from_tpl3(c))
}, {
let c =
util::hsva2rgba((
x as f64,
(y as f64 / 100.0),
(z as f64 / 100.0),
(w as f64 / 100.0)));
Ok(VVal::ivec_from_tpl4((
(c.0 * 255.0).round() as i64,
(c.1 * 255.0).round() as i64,
(c.2 * 255.0).round() as i64,
(c.3 * 255.0).round() as i64)))
}, {
let c = util::hsva2rgba((x, y, z, w));
Ok(VVal::fvec_from_tpl4(c))
})
}, Some(1), Some(1), false);
func!(st, "v:rgb2hsv",
|env: &mut Env, _argc: usize| {
let arg = env.arg_ref(0).unwrap().deref();
process_vec_input!(env, arg, v, x, y, z, w, {
let c =
util::rgb2hsv(
(x as f64) / 255.0,
(y as f64) / 255.0,
(z as f64) / 255.0);
Ok(VVal::ivec_from_tpl3((
c.0.round() as i64,
(c.1 * 100.0).round() as i64,
(c.2 * 100.0).round() as i64)))
}, {
let c = util::rgb2hsv(x, y, z);
Ok(VVal::fvec_from_tpl3(c))
}, {
let c =
util::rgb2hsv(
(x as f64) / 255.0,
(y as f64) / 255.0,
(z as f64) / 255.0);
Ok(VVal::ivec_from_tpl4((
c.0.round() as i64,
(c.1 * 100.0).round() as i64,
(c.2 * 100.0).round() as i64,
((w as f64 / 255.0) * 100.0).round() as i64)))
}, {
let c = util::rgba2hsvaf((x, y, z, w));
Ok(VVal::fvec_from_tpl4(c))
})
}, Some(1), Some(1), false);
func!(st, "sort",
|env: &mut Env, argc: usize| {
if argc == 1 {
let list = env.arg(0);
list.sort(|a: &VVal, b: &VVal| {
if a.is_int() || a.is_float() {
a.compare_num(b)
} else {
a.compare_str(b)
}
});
Ok(list)
} else {
let fun = env.arg(0);
let list = env.arg(1);
let mut ret = Ok(VVal::None);
list.sort(|a: &VVal, b: &VVal| {
env.push(b.clone());
env.push(a.clone());
let i =
match fun.call_internal(env, 2) {
Ok(v) => { v.i() },
Err(e) => { ret = Err(e); 1 },
};
env.popn(2);
match i {
_ if i == 0 => std::cmp::Ordering::Equal,
_ if i > 0 => std::cmp::Ordering::Greater,
_ => std::cmp::Ordering::Less,
}
});
if ret.is_ok() { ret = Ok(list); }
ret
}
}, Some(1), Some(2), false);
func!(st, "shuffle",
|env: &mut Env, _argc: usize| {
let fun = env.arg(0);
let mut list = env.arg(1);
list.fisher_yates_shuffle(|| {
fun.call_no_args(env).unwrap_or(VVal::None).i()
});
Ok(list)
}, Some(2), Some(2), false);
func!(st, "hash:fnv1a",
|env: &mut Env, argc: usize| {
let mut hash = util::FnvHasher::default();
for i in 0..argc {
match env.arg(i) {
VVal::Int(i) => hash.write_i64(i),
VVal::Flt(f) => hash.write_f64(f),
_ => {
env.arg(i).with_s_ref(|s: &str|
hash.write(s.as_bytes()));
}
}
}
Ok(VVal::Int(hash.finish_i64()))
}, Some(1), None, false);
func!(st, "rand:split_mix64_new",
|_env: &mut Env, _argc: usize| {
let v = VVal::vec();
v.push(VVal::Int(util::now_timestamp() as i64));
Ok(v)
}, Some(0), Some(0), false);
func!(st, "rand:split_mix64_new_from",
|env: &mut Env, _argc: usize| {
let v = VVal::vec();
v.push(VVal::Int(env.arg(0).i()));
Ok(v)
}, Some(1), Some(1), false);
func!(st, "rand:split_mix64_next",
|env: &mut Env, argc: usize| {
let mut sm =
util::SplitMix64::new_from_i64(
env.arg(0).at(0).unwrap_or(VVal::Int(0)).i());
let ret =
if argc == 2 {
let v = VVal::vec();
for _i in 0..env.arg(1).i() {
v.push(VVal::Int(sm.next_i64()));
}
v
} else {
VVal::Int(sm.next_i64())
};
env.arg(0).set_at(0,
VVal::Int(i64::from_be_bytes(sm.0.to_be_bytes())));
Ok(ret)
}, Some(1), Some(2), false);
func!(st, "num:int_to_open01", |env: &mut Env, _argc: usize| {
Ok(VVal::Flt(util::u64_to_open01(env.arg(0).i() as u64)))
}, Some(1), Some(1), false);
func!(st, "num:int_to_open_closed01", |env: &mut Env, _argc: usize| {
Ok(VVal::Flt(util::u64_to_open_closed01(env.arg(0).i() as u64)))
}, Some(1), Some(1), false);
func!(st, "num:int_to_closed_open01", |env: &mut Env, _argc: usize| {
Ok(VVal::Flt(util::u64_to_closed_open01(env.arg(0).i() as u64)))
}, Some(1), Some(1), false);
func!(st, "symbols:collect", |_env: &mut Env, _argc: usize| {
Ok(VVal::Int(crate::str_int::string_interner_collect()))
}, Some(0), Some(0), false);
func!(st, "rand:split_mix64_next_closed_open01",
|env: &mut Env, argc: usize| {
let mut sm =
util::SplitMix64::new_from_i64(
env.arg(0).at(0).unwrap_or(VVal::Int(0)).i());
let ret =
if argc == 2 {
let v = VVal::vec();
for _i in 0..env.arg(1).i() {
v.push(VVal::Flt(util::u64_to_closed_open01(sm.next_u64())));
}
v
} else {
VVal::Flt(util::u64_to_closed_open01(sm.next_u64()))
};
env.arg(0).set_at(0,
VVal::Int(i64::from_be_bytes(sm.0.to_be_bytes())));
Ok(ret)
}, Some(1), Some(2), false);
func!(st, "rand:split_mix64_next_open_closed01",
|env: &mut Env, argc: usize| {
let mut sm =
util::SplitMix64::new_from_i64(
env.arg(0).at(0).unwrap_or(VVal::Int(0)).i());
let ret =
if argc == 2 {
let v = VVal::vec();
for _i in 0..env.arg(1).i() {
v.push(VVal::Flt(util::u64_to_open_closed01(sm.next_u64())));
}
v
} else {
VVal::Flt(util::u64_to_open_closed01(sm.next_u64()))
};
env.arg(0).set_at(0,
VVal::Int(i64::from_be_bytes(sm.0.to_be_bytes())));
Ok(ret)
}, Some(1), Some(2), false);
func!(st, "rand:split_mix64_next_open01",
|env: &mut Env, argc: usize| {
let mut sm =
util::SplitMix64::new_from_i64(
env.arg(0).at(0).unwrap_or(VVal::Int(0)).i());
let ret =
if argc == 2 {
let v = VVal::vec();
for _i in 0..env.arg(1).i() {
v.push(VVal::Flt(util::u64_to_open01(sm.next_u64())));
}
v
} else {
VVal::Flt(util::u64_to_open01(sm.next_u64()))
};
env.arg(0).set_at(0,
VVal::Int(i64::from_be_bytes(sm.0.to_be_bytes())));
Ok(ret)
}, Some(1), Some(2), false);
func!(st, "sync:atom:new",
|env: &mut Env, _argc: usize| {
let v = env.arg(0);
let av = AtomicAVal::new();
av.write(&v);
Ok(VVal::Usr(Box::new(av)))
}, Some(1), Some(1), false);
func!(st, "sync:mpsc:new",
|_env: &mut Env, _argc: usize| {
Ok(AValChannel::new_vval())
}, Some(0), Some(0), false);
func!(st, "sync:slot:new",
|_env: &mut Env, _argc: usize| {
Ok(VVal::Usr(Box::new(AtomicAValSlot::new())))
}, Some(0), Some(0), false);
func!(st, "thread:sleep",
|env: &mut Env, _argc: usize| {
match env.arg(0).to_duration() {
Ok(dur) => std::thread::sleep(dur),
Err(v) => { return Ok(v); },
}
Ok(VVal::Bol(true))
}, Some(1), Some(1), false);
func!(st, "thread:spawn",
move |env: &mut Env, argc: usize| {
let avs =
if argc > 1 {
let mut avs = vec![];
for (i, (v, k)) in env.arg(1).iter().enumerate() {
let av = AtomicAVal::new();
av.write(&v);
if let Some(k) = k {
avs.push((k.s_raw(), av));
} else {
avs.push((format!("THREAD_ARG{}", i), av));
}
}
Some(avs)
} else {
None
};
let tc = env.global.borrow().get_thread_creator();
if let Some(tc) = &tc {
let ntc = tc.clone();
match tc.lock() {
Ok(mut tcg) => {
env.arg(0).with_s_ref(|code: &str|
Ok(tcg.spawn(ntc, code.to_string(), avs)))
},
Err(e) => {
Err(StackAction::panic_str(
format!("Couldn't create thread: {}", e),
None,
env.argv()))
},
}
} else {
Err(StackAction::panic_str(
"This global environment does not provide threads.".to_string(),
None,
env.argv()))
}
}, Some(1), Some(2), false);
crate::stdlib::add_to_symtable(&mut st);
st
}