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
|
\input texinfo @c -*- texinfo -*-
@c %**start of header
@setfilename dash.info
@settitle dash
@documentencoding UTF-8
@documentlanguage en
@syncodeindex fn cp
@dircategory Emacs
@direntry
* Dash: (dash.info). A modern list library for GNU Emacs
@end direntry
@c %**end of header
@copying
This manual is for @code{dash.el} version 2.12.1.
Copyright © 2012-2015 Magnar Sveen
@quotation
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see @uref{http://www.gnu.org/licenses/}.
@end quotation
@end copying
@finalout
@titlepage
@title Dash
@author Magnar Sveen
@page
@vskip 0pt plus 1filll
@insertcopying
@end titlepage
@contents
@ifnottex
@node Top
@top dash
@insertcopying
@end ifnottex
@menu
* Installation::
* Functions::
* Development::
* Index::
@detailmenu
--- The Detailed Node Listing ---
Installation
* Using in a package::
* Syntax highlighting of dash functions::
Functions
* Maps::
* Sublist selection::
* List to list::
* Reductions::
* Unfolding::
* Predicates::
* Partitioning::
* Indexing::
* Set operations::
* Other list operations::
* Tree operations::
* Threading macros::
* Binding::
* Side-effects::
* Destructive operations::
* Function combinators::
Development
* Contribute:: How to contribute
* Changes:: List of significant changes by version
* Contributors:: List of contributors
@end detailmenu
@end menu
@node Installation
@chapter Installation
It's available on @uref{http://marmalade-repo.org/,marmalade} and
@uref{https://melpa.org/,Melpa}; use @code{M-x
package-install}:
@table @kbd
@item M-x package-install @key{RET} dash
Install the dash library.
@end table
@table @kbd
@item M-x package-install @key{RET} dash-functional
Optional, if you want the function combinators.
@end table
Alternatively, you can just dump @verb{~dash.el~} or
@verb{~dash-functional.el~} in your load path somewhere.
@menu
* Using in a package::
* Syntax highlighting of dash functions::
@end menu
@node Using in a package
@section Using in a package
Add this to the big comment block at the top:
@lisp
;; Package-Requires: ((dash "2.12.1"))
@end lisp
@noindent To get function combinators:
@lisp
;; Package-Requires: ((dash "2.12.1") (dash-functional "1.2.0") (emacs "24"))
@end lisp
@node Syntax highlighting of dash functions
@section Syntax highlighting of dash functions
Font lock of dash functions in emacs lisp buffers is now optional.
Include this in your emacs settings to get syntax highlighting:
@lisp
(eval-after-load "dash" '(dash-enable-font-lock))
@end lisp
@node Functions
@chapter Functions
This chapter contains reference documentation for the dash
@abbr{application programming interface,API}. All functions and
constructs in the library are prefixed with a dash (-).
There are also anaphoric versions of functions where that makes sense,
prefixed with two dashes instead of one.
For instance, while @code{-map} takes a function to map over the list,
one can also use the anaphoric form with double dashes - which will
then be executed with @code{it} exposed as the list item. Here's an
example:
@lisp
(-map (lambda (n) (* n n)) '(1 2 3 4)) ;; normal version
(--map (* it it) '(1 2 3 4)) ;; anaphoric version
@end lisp
@noindent Of course, the original can also be written like
@lisp
(defun square (n) (* n n))
(-map 'square '(1 2 3 4))
@end lisp
@noindent which demonstrates the usefulness of both versions.
@menu
* Maps::
* Sublist selection::
* List to list::
* Reductions::
* Unfolding::
* Predicates::
* Partitioning::
* Indexing::
* Set operations::
* Other list operations::
* Tree operations::
* Threading macros::
* Binding::
* Side-effects::
* Destructive operations::
* Function combinators::
@end menu
@node Maps
@section Maps
Functions in this category take a transforming function, which
is then applied sequentially to each or selected elements of the
input list. The results are collected in order and returned as
new list.
@anchor{-map}
@defun -map (fn list)
Return a new list consisting of the result of applying @var{fn} to the items in @var{list}.
@example
@group
(-map (lambda (num) (* num num)) '(1 2 3 4))
@result{} '(1 4 9 16)
@end group
@group
(-map 'square '(1 2 3 4))
@result{} '(1 4 9 16)
@end group
@group
(--map (* it it) '(1 2 3 4))
@result{} '(1 4 9 16)
@end group
@end example
@end defun
@anchor{-map-when}
@defun -map-when (pred rep list)
Return a new list where the elements in @var{list} that does not match the @var{pred} function
are unchanged, and where the elements in @var{list} that do match the @var{pred} function are mapped
through the @var{rep} function.
Alias: @code{-replace-where}
See also: @code{-update-at} (@pxref{-update-at})
@example
@group
(-map-when 'even? 'square '(1 2 3 4))
@result{} '(1 4 3 16)
@end group
@group
(--map-when (> it 2) (* it it) '(1 2 3 4))
@result{} '(1 2 9 16)
@end group
@group
(--map-when (= it 2) 17 '(1 2 3 4))
@result{} '(1 17 3 4)
@end group
@end example
@end defun
@anchor{-map-first}
@defun -map-first (pred rep list)
Replace first item in @var{list} satisfying @var{pred} with result of @var{rep} called on this item.
See also: @code{-map-when} (@pxref{-map-when}), @code{-replace-first} (@pxref{-replace-first})
@example
@group
(-map-first 'even? 'square '(1 2 3 4))
@result{} '(1 4 3 4)
@end group
@group
(--map-first (> it 2) (* it it) '(1 2 3 4))
@result{} '(1 2 9 4)
@end group
@group
(--map-first (= it 2) 17 '(1 2 3 2))
@result{} '(1 17 3 2)
@end group
@end example
@end defun
@anchor{-map-last}
@defun -map-last (pred rep list)
Replace first item in @var{list} satisfying @var{pred} with result of @var{rep} called on this item.
See also: @code{-map-when} (@pxref{-map-when}), @code{-replace-last} (@pxref{-replace-last})
@example
@group
(-map-last 'even? 'square '(1 2 3 4))
@result{} '(1 2 3 16)
@end group
@group
(--map-last (> it 2) (* it it) '(1 2 3 4))
@result{} '(1 2 3 16)
@end group
@group
(--map-last (= it 2) 17 '(1 2 3 2))
@result{} '(1 2 3 17)
@end group
@end example
@end defun
@anchor{-map-indexed}
@defun -map-indexed (fn list)
Return a new list consisting of the result of (@var{fn} index item) for each item in @var{list}.
In the anaphoric form @code{--map-indexed}, the index is exposed as `it-index`.
See also: @code{-each-indexed} (@pxref{-each-indexed}).
@example
@group
(-map-indexed (lambda (index item) (- item index)) '(1 2 3 4))
@result{} '(1 1 1 1)
@end group
@group
(--map-indexed (- it it-index) '(1 2 3 4))
@result{} '(1 1 1 1)
@end group
@end example
@end defun
@anchor{-annotate}
@defun -annotate (fn list)
Return a list of cons cells where each cell is @var{fn} applied to each
element of @var{list} paired with the unmodified element of @var{list}.
@example
@group
(-annotate '1+ '(1 2 3))
@result{} '((2 . 1) (3 . 2) (4 . 3))
@end group
@group
(-annotate 'length '(("h" "e" "l" "l" "o") ("hello" "world")))
@result{} '((5 "h" "e" "l" "l" "o") (2 "hello" "world"))
@end group
@group
(--annotate (< 1 it) '(0 1 2 3))
@result{} '((nil . 0) (nil . 1) (t . 2) (t . 3))
@end group
@end example
@end defun
@anchor{-splice}
@defun -splice (pred fun list)
Splice lists generated by @var{fun} in place of elements matching @var{pred} in @var{list}.
@var{fun} takes the element matching @var{pred} as input.
This function can be used as replacement for @code{,@@} in case you
need to splice several lists at marked positions (for example
with keywords).
See also: @code{-splice-list} (@pxref{-splice-list}), @code{-insert-at} (@pxref{-insert-at})
@example
@group
(-splice 'even? (lambda (x) (list x x)) '(1 2 3 4))
@result{} '(1 2 2 3 4 4)
@end group
@group
(--splice 't (list it it) '(1 2 3 4))
@result{} '(1 1 2 2 3 3 4 4)
@end group
@group
(--splice (equal it :magic) '((list of) (magical) (code)) '((foo) (bar) :magic (baz)))
@result{} '((foo) (bar) (list of) (magical) (code) (baz))
@end group
@end example
@end defun
@anchor{-splice-list}
@defun -splice-list (pred new-list list)
Splice @var{new-list} in place of elements matching @var{pred} in @var{list}.
See also: @code{-splice} (@pxref{-splice}), @code{-insert-at} (@pxref{-insert-at})
@example
@group
(-splice-list 'keywordp '(a b c) '(1 :foo 2))
@result{} '(1 a b c 2)
@end group
@group
(-splice-list 'keywordp nil '(1 :foo 2))
@result{} '(1 2)
@end group
@group
(--splice-list (keywordp it) '(a b c) '(1 :foo 2))
@result{} '(1 a b c 2)
@end group
@end example
@end defun
@anchor{-mapcat}
@defun -mapcat (fn list)
Return the concatenation of the result of mapping @var{fn} over @var{list}.
Thus function @var{fn} should return a list.
@example
@group
(-mapcat 'list '(1 2 3))
@result{} '(1 2 3)
@end group
@group
(-mapcat (lambda (item) (list 0 item)) '(1 2 3))
@result{} '(0 1 0 2 0 3)
@end group
@group
(--mapcat (list 0 it) '(1 2 3))
@result{} '(0 1 0 2 0 3)
@end group
@end example
@end defun
@anchor{-copy}
@defun -copy (arg)
Create a shallow copy of @var{list}.
@example
@group
(-copy '(1 2 3))
@result{} '(1 2 3)
@end group
@group
(let ((a '(1 2 3))) (eq a (-copy a)))
@result{} nil
@end group
@end example
@end defun
@node Sublist selection
@section Sublist selection
Functions returning a sublist of the original list.
@anchor{-filter}
@defun -filter (pred list)
Return a new list of the items in @var{list} for which @var{pred} returns a non-nil value.
Alias: @code{-select}
See also: @code{-keep} (@pxref{-keep})
@example
@group
(-filter (lambda (num) (= 0 (% num 2))) '(1 2 3 4))
@result{} '(2 4)
@end group
@group
(-filter 'even? '(1 2 3 4))
@result{} '(2 4)
@end group
@group
(--filter (= 0 (% it 2)) '(1 2 3 4))
@result{} '(2 4)
@end group
@end example
@end defun
@anchor{-remove}
@defun -remove (pred list)
Return a new list of the items in @var{list} for which @var{pred} returns nil.
Alias: @code{-reject}
@example
@group
(-remove (lambda (num) (= 0 (% num 2))) '(1 2 3 4))
@result{} '(1 3)
@end group
@group
(-remove 'even? '(1 2 3 4))
@result{} '(1 3)
@end group
@group
(--remove (= 0 (% it 2)) '(1 2 3 4))
@result{} '(1 3)
@end group
@end example
@end defun
@anchor{-remove-first}
@defun -remove-first (pred list)
Return a new list with the first item matching @var{pred} removed.
Alias: @code{-reject-first}
See also: @code{-remove} (@pxref{-remove}), @code{-map-first} (@pxref{-map-first})
@example
@group
(-remove-first 'even? '(1 3 5 4 7 8 10))
@result{} '(1 3 5 7 8 10)
@end group
@group
(-remove-first 'stringp '(1 2 "first" "second" "third"))
@result{} '(1 2 "second" "third")
@end group
@group
(--remove-first (> it 3) '(1 2 3 4 5 6 7 8 9 10))
@result{} '(1 2 3 5 6 7 8 9 10)
@end group
@end example
@end defun
@anchor{-remove-last}
@defun -remove-last (pred list)
Return a new list with the last item matching @var{pred} removed.
Alias: @code{-reject-last}
See also: @code{-remove} (@pxref{-remove}), @code{-map-last} (@pxref{-map-last})
@example
@group
(-remove-last 'even? '(1 3 5 4 7 8 10 11))
@result{} '(1 3 5 4 7 8 11)
@end group
@group
(-remove-last 'stringp '(1 2 "last" "second" "third"))
@result{} '(1 2 "last" "second")
@end group
@group
(--remove-last (> it 3) '(1 2 3 4 5 6 7 8 9 10))
@result{} '(1 2 3 4 5 6 7 8 9)
@end group
@end example
@end defun
@anchor{-remove-item}
@defun -remove-item (item list)
Remove all occurences of @var{item} from @var{list}.
Comparison is done with @code{equal}.
@example
@group
(-remove-item 3 '(1 2 3 2 3 4 5 3))
@result{} '(1 2 2 4 5)
@end group
@group
(-remove-item 'foo '(foo bar baz foo))
@result{} '(bar baz)
@end group
@group
(-remove-item "bob" '("alice" "bob" "eve" "bob" "dave"))
@result{} '("alice" "eve" "dave")
@end group
@end example
@end defun
@anchor{-non-nil}
@defun -non-nil (list)
Return all non-nil elements of @var{list}.
@example
@group
(-non-nil '(1 nil 2 nil nil 3 4 nil 5 nil))
@result{} '(1 2 3 4 5)
@end group
@end example
@end defun
@anchor{-slice}
@defun -slice (list from &optional to step)
Return copy of @var{list}, starting from index @var{from} to index @var{to}.
@var{from} or @var{to} may be negative. These values are then interpreted
modulo the length of the list.
If @var{step} is a number, only each STEPth item in the resulting
section is returned. Defaults to 1.
@example
@group
(-slice '(1 2 3 4 5) 1)
@result{} '(2 3 4 5)
@end group
@group
(-slice '(1 2 3 4 5) 0 3)
@result{} '(1 2 3)
@end group
@group
(-slice '(1 2 3 4 5 6 7 8 9) 1 -1 2)
@result{} '(2 4 6 8)
@end group
@end example
@end defun
@anchor{-take}
@defun -take (n list)
Return a new list of the first @var{n} items in @var{list}, or all items if there are fewer than @var{n}.
See also: @code{-take-last} (@pxref{-take-last})
@example
@group
(-take 3 '(1 2 3 4 5))
@result{} '(1 2 3)
@end group
@group
(-take 17 '(1 2 3 4 5))
@result{} '(1 2 3 4 5)
@end group
@end example
@end defun
@anchor{-take-last}
@defun -take-last (n list)
Return the last @var{n} items of @var{list} in order.
See also: @code{-take} (@pxref{-take})
@example
@group
(-take-last 3 '(1 2 3 4 5))
@result{} '(3 4 5)
@end group
@group
(-take-last 17 '(1 2 3 4 5))
@result{} '(1 2 3 4 5)
@end group
@group
(-take-last 1 '(1 2 3 4 5))
@result{} '(5)
@end group
@end example
@end defun
@anchor{-drop}
@defun -drop (n list)
Return the tail of @var{list} without the first @var{n} items.
See also: @code{-drop-last} (@pxref{-drop-last})
@example
@group
(-drop 3 '(1 2 3 4 5))
@result{} '(4 5)
@end group
@group
(-drop 17 '(1 2 3 4 5))
@result{} '()
@end group
@end example
@end defun
@anchor{-drop-last}
@defun -drop-last (n list)
Remove the last @var{n} items of @var{list} and return a copy.
See also: @code{-drop} (@pxref{-drop})
@example
@group
(-drop-last 3 '(1 2 3 4 5))
@result{} '(1 2)
@end group
@group
(-drop-last 17 '(1 2 3 4 5))
@result{} '()
@end group
@end example
@end defun
@anchor{-take-while}
@defun -take-while (pred list)
Return a new list of successive items from @var{list} while (@var{pred} item) returns a non-nil value.
@example
@group
(-take-while 'even? '(1 2 3 4))
@result{} '()
@end group
@group
(-take-while 'even? '(2 4 5 6))
@result{} '(2 4)
@end group
@group
(--take-while (< it 4) '(1 2 3 4 3 2 1))
@result{} '(1 2 3)
@end group
@end example
@end defun
@anchor{-drop-while}
@defun -drop-while (pred list)
Return the tail of @var{list} starting from the first item for which (@var{pred} item) returns nil.
@example
@group
(-drop-while 'even? '(1 2 3 4))
@result{} '(1 2 3 4)
@end group
@group
(-drop-while 'even? '(2 4 5 6))
@result{} '(5 6)
@end group
@group
(--drop-while (< it 4) '(1 2 3 4 3 2 1))
@result{} '(4 3 2 1)
@end group
@end example
@end defun
@anchor{-select-by-indices}
@defun -select-by-indices (indices list)
Return a list whose elements are elements from @var{list} selected
as `(nth i list)` for all i from @var{indices}.
@example
@group
(-select-by-indices '(4 10 2 3 6) '("v" "e" "l" "o" "c" "i" "r" "a" "p" "t" "o" "r"))
@result{} '("c" "o" "l" "o" "r")
@end group
@group
(-select-by-indices '(2 1 0) '("a" "b" "c"))
@result{} '("c" "b" "a")
@end group
@group
(-select-by-indices '(0 1 2 0 1 3 3 1) '("f" "a" "r" "l"))
@result{} '("f" "a" "r" "f" "a" "l" "l" "a")
@end group
@end example
@end defun
@anchor{-select-columns}
@defun -select-columns (columns table)
Select @var{columns} from @var{table}.
@var{table} is a list of lists where each element represents one row.
It is assumed each row has the same length.
Each row is transformed such that only the specified @var{columns} are
selected.
See also: @code{-select-column} (@pxref{-select-column}), @code{-select-by-indices} (@pxref{-select-by-indices})
@example
@group
(-select-columns '(0 2) '((1 2 3) (a b c) (:a :b :c)))
@result{} '((1 3) (a c) (:a :c))
@end group
@group
(-select-columns '(1) '((1 2 3) (a b c) (:a :b :c)))
@result{} '((2) (b) (:b))
@end group
@group
(-select-columns nil '((1 2 3) (a b c) (:a :b :c)))
@result{} '(nil nil nil)
@end group
@end example
@end defun
@anchor{-select-column}
@defun -select-column (column table)
Select @var{column} from @var{table}.
@var{table} is a list of lists where each element represents one row.
It is assumed each row has the same length.
The single selected column is returned as a list.
See also: @code{-select-columns} (@pxref{-select-columns}), @code{-select-by-indices} (@pxref{-select-by-indices})
@example
@group
(-select-column 1 '((1 2 3) (a b c) (:a :b :c)))
@result{} '(2 b :b)
@end group
@end example
@end defun
@node List to list
@section List to list
Bag of various functions which modify input list.
@anchor{-keep}
@defun -keep (fn list)
Return a new list of the non-nil results of applying @var{fn} to the items in @var{list}.
If you want to select the original items satisfying a predicate use @code{-filter} (@pxref{-filter}).
@example
@group
(-keep 'cdr '((1 2 3) (4 5) (6)))
@result{} '((2 3) (5))
@end group
@group
(-keep (lambda (num) (when (> num 3) (* 10 num))) '(1 2 3 4 5 6))
@result{} '(40 50 60)
@end group
@group
(--keep (when (> it 3) (* 10 it)) '(1 2 3 4 5 6))
@result{} '(40 50 60)
@end group
@end example
@end defun
@anchor{-concat}
@defun -concat (&rest lists)
Return a new list with the concatenation of the elements in the supplied @var{lists}.
@example
@group
(-concat '(1))
@result{} '(1)
@end group
@group
(-concat '(1) '(2))
@result{} '(1 2)
@end group
@group
(-concat '(1) '(2 3) '(4))
@result{} '(1 2 3 4)
@end group
@end example
@end defun
@anchor{-flatten}
@defun -flatten (l)
Take a nested list @var{l} and return its contents as a single, flat list.
Note that because @code{nil} represents a list of zero elements (an
empty list), any mention of nil in @var{l} will disappear after
flattening. If you need to preserve nils, consider @code{-flatten-n} (@pxref{-flatten-n})
or map them to some unique symbol and then map them back.
Conses of two atoms are considered "terminals", that is, they
aren't flattened further.
See also: @code{-flatten-n} (@pxref{-flatten-n})
@example
@group
(-flatten '((1)))
@result{} '(1)
@end group
@group
(-flatten '((1 (2 3) (((4 (5)))))))
@result{} '(1 2 3 4 5)
@end group
@group
(-flatten '(1 2 (3 . 4)))
@result{} '(1 2 (3 . 4))
@end group
@end example
@end defun
@anchor{-flatten-n}
@defun -flatten-n (num list)
Flatten @var{num} levels of a nested @var{list}.
See also: @code{-flatten} (@pxref{-flatten})
@example
@group
(-flatten-n 1 '((1 2) ((3 4) ((5 6)))))
@result{} '(1 2 (3 4) ((5 6)))
@end group
@group
(-flatten-n 2 '((1 2) ((3 4) ((5 6)))))
@result{} '(1 2 3 4 (5 6))
@end group
@group
(-flatten-n 3 '((1 2) ((3 4) ((5 6)))))
@result{} '(1 2 3 4 5 6)
@end group
@end example
@end defun
@anchor{-replace}
@defun -replace (old new list)
Replace all @var{old} items in @var{list} with @var{new}.
Elements are compared using @code{equal}.
See also: @code{-replace-at} (@pxref{-replace-at})
@example
@group
(-replace 1 "1" '(1 2 3 4 3 2 1))
@result{} '("1" 2 3 4 3 2 "1")
@end group
@group
(-replace "foo" "bar" '("a" "nice" "foo" "sentence" "about" "foo"))
@result{} '("a" "nice" "bar" "sentence" "about" "bar")
@end group
@group
(-replace 1 2 nil)
@result{} nil
@end group
@end example
@end defun
@anchor{-replace-first}
@defun -replace-first (old new list)
Replace the first occurence of @var{old} with @var{new} in @var{list}.
Elements are compared using @code{equal}.
See also: @code{-map-first} (@pxref{-map-first})
@example
@group
(-replace-first 1 "1" '(1 2 3 4 3 2 1))
@result{} '("1" 2 3 4 3 2 1)
@end group
@group
(-replace-first "foo" "bar" '("a" "nice" "foo" "sentence" "about" "foo"))
@result{} '("a" "nice" "bar" "sentence" "about" "foo")
@end group
@group
(-replace-first 1 2 nil)
@result{} nil
@end group
@end example
@end defun
@anchor{-replace-last}
@defun -replace-last (old new list)
Replace the last occurence of @var{old} with @var{new} in @var{list}.
Elements are compared using @code{equal}.
See also: @code{-map-last} (@pxref{-map-last})
@example
@group
(-replace-last 1 "1" '(1 2 3 4 3 2 1))
@result{} '(1 2 3 4 3 2 "1")
@end group
@group
(-replace-last "foo" "bar" '("a" "nice" "foo" "sentence" "about" "foo"))
@result{} '("a" "nice" "foo" "sentence" "about" "bar")
@end group
@group
(-replace-last 1 2 nil)
@result{} nil
@end group
@end example
@end defun
@anchor{-insert-at}
@defun -insert-at (n x list)
Return a list with @var{x} inserted into @var{list} at position @var{n}.
See also: @code{-splice} (@pxref{-splice}), @code{-splice-list} (@pxref{-splice-list})
@example
@group
(-insert-at 1 'x '(a b c))
@result{} '(a x b c)
@end group
@group
(-insert-at 12 'x '(a b c))
@result{} '(a b c x)
@end group
@end example
@end defun
@anchor{-replace-at}
@defun -replace-at (n x list)
Return a list with element at Nth position in @var{list} replaced with @var{x}.
See also: @code{-replace} (@pxref{-replace})
@example
@group
(-replace-at 0 9 '(0 1 2 3 4 5))
@result{} '(9 1 2 3 4 5)
@end group
@group
(-replace-at 1 9 '(0 1 2 3 4 5))
@result{} '(0 9 2 3 4 5)
@end group
@group
(-replace-at 4 9 '(0 1 2 3 4 5))
@result{} '(0 1 2 3 9 5)
@end group
@end example
@end defun
@anchor{-update-at}
@defun -update-at (n func list)
Return a list with element at Nth position in @var{list} replaced with `(func (nth n list))`.
See also: @code{-map-when} (@pxref{-map-when})
@example
@group
(-update-at 0 (lambda (x) (+ x 9)) '(0 1 2 3 4 5))
@result{} '(9 1 2 3 4 5)
@end group
@group
(-update-at 1 (lambda (x) (+ x 8)) '(0 1 2 3 4 5))
@result{} '(0 9 2 3 4 5)
@end group
@group
(--update-at 2 (length it) '("foo" "bar" "baz" "quux"))
@result{} '("foo" "bar" 3 "quux")
@end group
@end example
@end defun
@anchor{-remove-at}
@defun -remove-at (n list)
Return a list with element at Nth position in @var{list} removed.
See also: @code{-remove-at-indices} (@pxref{-remove-at-indices}), @code{-remove} (@pxref{-remove})
@example
@group
(-remove-at 0 '("0" "1" "2" "3" "4" "5"))
@result{} '("1" "2" "3" "4" "5")
@end group
@group
(-remove-at 1 '("0" "1" "2" "3" "4" "5"))
@result{} '("0" "2" "3" "4" "5")
@end group
@group
(-remove-at 2 '("0" "1" "2" "3" "4" "5"))
@result{} '("0" "1" "3" "4" "5")
@end group
@end example
@end defun
@anchor{-remove-at-indices}
@defun -remove-at-indices (indices list)
Return a list whose elements are elements from @var{list} without
elements selected as `(nth i list)` for all i
from @var{indices}.
See also: @code{-remove-at} (@pxref{-remove-at}), @code{-remove} (@pxref{-remove})
@example
@group
(-remove-at-indices '(0) '("0" "1" "2" "3" "4" "5"))
@result{} '("1" "2" "3" "4" "5")
@end group
@group
(-remove-at-indices '(0 2 4) '("0" "1" "2" "3" "4" "5"))
@result{} '("1" "3" "5")
@end group
@group
(-remove-at-indices '(0 5) '("0" "1" "2" "3" "4" "5"))
@result{} '("1" "2" "3" "4")
@end group
@end example
@end defun
@node Reductions
@section Reductions
Functions reducing lists into single value.
@anchor{-reduce-from}
@defun -reduce-from (fn initial-value list)
Return the result of applying @var{fn} to @var{initial-value} and the
first item in @var{list}, then applying @var{fn} to that result and the 2nd
item, etc. If @var{list} contains no items, return @var{initial-value} and
@var{fn} is not called.
In the anaphoric form @code{--reduce-from}, the accumulated value is
exposed as `acc`.
See also: @code{-reduce} (@pxref{-reduce}), @code{-reduce-r} (@pxref{-reduce-r})
@example
@group
(-reduce-from '- 10 '(1 2 3))
@result{} 4
@end group
@group
(-reduce-from (lambda (memo item) (concat "(" memo " - " (int-to-string item) ")")) "10" '(1 2 3))
@result{} "(((10 - 1) - 2) - 3)"
@end group
@group
(--reduce-from (concat acc " " it) "START" '("a" "b" "c"))
@result{} "START a b c"
@end group
@end example
@end defun
@anchor{-reduce-r-from}
@defun -reduce-r-from (fn initial-value list)
Replace conses with @var{fn}, nil with @var{initial-value} and evaluate
the resulting expression. If @var{list} is empty, @var{initial-value} is
returned and @var{fn} is not called.
Note: this function works the same as @code{-reduce-from} (@pxref{-reduce-from}) but the
operation associates from right instead of from left.
See also: @code{-reduce-r} (@pxref{-reduce-r}), @code{-reduce} (@pxref{-reduce})
@example
@group
(-reduce-r-from '- 10 '(1 2 3))
@result{} -8
@end group
@group
(-reduce-r-from (lambda (item memo) (concat "(" (int-to-string item) " - " memo ")")) "10" '(1 2 3))
@result{} "(1 - (2 - (3 - 10)))"
@end group
@group
(--reduce-r-from (concat it " " acc) "END" '("a" "b" "c"))
@result{} "a b c END"
@end group
@end example
@end defun
@anchor{-reduce}
@defun -reduce (fn list)
Return the result of applying @var{fn} to the first 2 items in @var{list},
then applying @var{fn} to that result and the 3rd item, etc. If @var{list}
contains no items, @var{fn} must accept no arguments as well, and
reduce return the result of calling @var{fn} with no arguments. If
@var{list} has only 1 item, it is returned and @var{fn} is not called.
In the anaphoric form @code{--reduce}, the accumulated value is
exposed as `acc`.
See also: @code{-reduce-from} (@pxref{-reduce-from}), @code{-reduce-r} (@pxref{-reduce-r})
@example
@group
(-reduce '- '(1 2 3 4))
@result{} -8
@end group
@group
(-reduce (lambda (memo item) (format "%s-%s" memo item)) '(1 2 3))
@result{} "1-2-3"
@end group
@group
(--reduce (format "%s-%s" acc it) '(1 2 3))
@result{} "1-2-3"
@end group
@end example
@end defun
@anchor{-reduce-r}
@defun -reduce-r (fn list)
Replace conses with @var{fn} and evaluate the resulting expression.
The final nil is ignored. If @var{list} contains no items, @var{fn} must
accept no arguments as well, and reduce return the result of
calling @var{fn} with no arguments. If @var{list} has only 1 item, it is
returned and @var{fn} is not called.
The first argument of @var{fn} is the new item, the second is the
accumulated value.
Note: this function works the same as @code{-reduce} (@pxref{-reduce}) but the operation
associates from right instead of from left.
See also: @code{-reduce-r-from} (@pxref{-reduce-r-from}), @code{-reduce} (@pxref{-reduce})
@example
@group
(-reduce-r '- '(1 2 3 4))
@result{} -2
@end group
@group
(-reduce-r (lambda (item memo) (format "%s-%s" memo item)) '(1 2 3))
@result{} "3-2-1"
@end group
@group
(--reduce-r (format "%s-%s" acc it) '(1 2 3))
@result{} "3-2-1"
@end group
@end example
@end defun
@anchor{-count}
@defun -count (pred list)
Counts the number of items in @var{list} where (@var{pred} item) is non-nil.
@example
@group
(-count 'even? '(1 2 3 4 5))
@result{} 2
@end group
@group
(--count (< it 4) '(1 2 3 4))
@result{} 3
@end group
@end example
@end defun
@anchor{-sum}
@defun -sum (list)
Return the sum of @var{list}.
@example
@group
(-sum '())
@result{} 0
@end group
@group
(-sum '(1))
@result{} 1
@end group
@group
(-sum '(1 2 3 4))
@result{} 10
@end group
@end example
@end defun
@anchor{-product}
@defun -product (list)
Return the product of @var{list}.
@example
@group
(-product '())
@result{} 1
@end group
@group
(-product '(1))
@result{} 1
@end group
@group
(-product '(1 2 3 4))
@result{} 24
@end group
@end example
@end defun
@anchor{-min}
@defun -min (list)
Return the smallest value from @var{list} of numbers or markers.
@example
@group
(-min '(0))
@result{} 0
@end group
@group
(-min '(3 2 1))
@result{} 1
@end group
@group
(-min '(1 2 3))
@result{} 1
@end group
@end example
@end defun
@anchor{-min-by}
@defun -min-by (comparator list)
Take a comparison function @var{comparator} and a @var{list} and return
the least element of the list by the comparison function.
See also combinator @code{-on} (@pxref{-on}) which can transform the values before
comparing them.
@example
@group
(-min-by '> '(4 3 6 1))
@result{} 1
@end group
@group
(--min-by (> (car it) (car other)) '((1 2 3) (2) (3 2)))
@result{} '(1 2 3)
@end group
@group
(--min-by (> (length it) (length other)) '((1 2 3) (2) (3 2)))
@result{} '(2)
@end group
@end example
@end defun
@anchor{-max}
@defun -max (list)
Return the largest value from @var{list} of numbers or markers.
@example
@group
(-max '(0))
@result{} 0
@end group
@group
(-max '(3 2 1))
@result{} 3
@end group
@group
(-max '(1 2 3))
@result{} 3
@end group
@end example
@end defun
@anchor{-max-by}
@defun -max-by (comparator list)
Take a comparison function @var{comparator} and a @var{list} and return
the greatest element of the list by the comparison function.
See also combinator @code{-on} (@pxref{-on}) which can transform the values before
comparing them.
@example
@group
(-max-by '> '(4 3 6 1))
@result{} 6
@end group
@group
(--max-by (> (car it) (car other)) '((1 2 3) (2) (3 2)))
@result{} '(3 2)
@end group
@group
(--max-by (> (length it) (length other)) '((1 2 3) (2) (3 2)))
@result{} '(1 2 3)
@end group
@end example
@end defun
@node Unfolding
@section Unfolding
Operations dual to reductions, building lists from seed value rather than consuming a list to produce a single value.
@anchor{-iterate}
@defun -iterate (fun init n)
Return a list of iterated applications of @var{fun} to @var{init}.
This means a list of form:
(init (fun init) (fun (fun init)) ...)
@var{n} is the length of the returned list.
@example
@group
(-iterate '1+ 1 10)
@result{} '(1 2 3 4 5 6 7 8 9 10)
@end group
@group
(-iterate (lambda (x) (+ x x)) 2 5)
@result{} '(2 4 8 16 32)
@end group
@group
(--iterate (* it it) 2 5)
@result{} '(2 4 16 256 65536)
@end group
@end example
@end defun
@anchor{-unfold}
@defun -unfold (fun seed)
Build a list from @var{seed} using @var{fun}.
This is "dual" operation to @code{-reduce-r} (@pxref{-reduce-r}): while -reduce-r
consumes a list to produce a single value, @code{-unfold} (@pxref{-unfold}) takes a
seed value and builds a (potentially infinite!) list.
@var{fun} should return @code{nil} to stop the generating process, or a
cons (@var{a} . @var{b}), where @var{a} will be prepended to the result and @var{b} is
the new seed.
@example
@group
(-unfold (lambda (x) (unless (= x 0) (cons x (1- x)))) 10)
@result{} '(10 9 8 7 6 5 4 3 2 1)
@end group
@group
(--unfold (when it (cons it (cdr it))) '(1 2 3 4))
@result{} '((1 2 3 4) (2 3 4) (3 4) (4))
@end group
@group
(--unfold (when it (cons it (butlast it))) '(1 2 3 4))
@result{} '((1 2 3 4) (1 2 3) (1 2) (1))
@end group
@end example
@end defun
@node Predicates
@section Predicates
@anchor{-any?}
@defun -any? (pred list)
Return t if (@var{pred} x) is non-nil for any x in @var{list}, else nil.
Alias: @code{-any-p}, @code{-some?}, @code{-some-p}
@example
@group
(-any? 'even? '(1 2 3))
@result{} t
@end group
@group
(-any? 'even? '(1 3 5))
@result{} nil
@end group
@group
(--any? (= 0 (% it 2)) '(1 2 3))
@result{} t
@end group
@end example
@end defun
@anchor{-all?}
@defun -all? (pred list)
Return t if (@var{pred} x) is non-nil for all x in @var{list}, else nil.
Alias: @code{-all-p}, @code{-every?}, @code{-every-p}
@example
@group
(-all? 'even? '(1 2 3))
@result{} nil
@end group
@group
(-all? 'even? '(2 4 6))
@result{} t
@end group
@group
(--all? (= 0 (% it 2)) '(2 4 6))
@result{} t
@end group
@end example
@end defun
@anchor{-none?}
@defun -none? (pred list)
Return t if (@var{pred} x) is nil for all x in @var{list}, else nil.
Alias: @code{-none-p}
@example
@group
(-none? 'even? '(1 2 3))
@result{} nil
@end group
@group
(-none? 'even? '(1 3 5))
@result{} t
@end group
@group
(--none? (= 0 (% it 2)) '(1 2 3))
@result{} nil
@end group
@end example
@end defun
@anchor{-only-some?}
@defun -only-some? (pred list)
Return `t` if at least one item of @var{list} matches @var{pred} and at least one item of @var{list} does not match @var{pred}.
Return `nil` both if all items match the predicate or if none of the items match the predicate.
Alias: @code{-only-some-p}
@example
@group
(-only-some? 'even? '(1 2 3))
@result{} t
@end group
@group
(-only-some? 'even? '(1 3 5))
@result{} nil
@end group
@group
(-only-some? 'even? '(2 4 6))
@result{} nil
@end group
@end example
@end defun
@anchor{-contains?}
@defun -contains? (list element)
Return non-nil if @var{list} contains @var{element}.
The test for equality is done with @code{equal}, or with @code{-compare-fn}
if that's non-nil.
Alias: @code{-contains-p}
@example
@group
(-contains? '(1 2 3) 1)
@result{} t
@end group
@group
(-contains? '(1 2 3) 2)
@result{} t
@end group
@group
(-contains? '(1 2 3) 4)
@result{} nil
@end group
@end example
@end defun
@anchor{-same-items?}
@defun -same-items? (list list2)
Return true if @var{list} and @var{list2} has the same items.
The order of the elements in the lists does not matter.
Alias: @code{-same-items-p}
@example
@group
(-same-items? '(1 2 3) '(1 2 3))
@result{} t
@end group
@group
(-same-items? '(1 2 3) '(3 2 1))
@result{} t
@end group
@group
(-same-items? '(1 2 3) '(1 2 3 4))
@result{} nil
@end group
@end example
@end defun
@anchor{-is-prefix?}
@defun -is-prefix? (prefix list)
Return non-nil if @var{prefix} is prefix of @var{list}.
Alias: @code{-is-prefix-p}
@example
@group
(-is-prefix? '(1 2 3) '(1 2 3 4 5))
@result{} t
@end group
@group
(-is-prefix? '(1 2 3 4 5) '(1 2 3))
@result{} nil
@end group
@group
(-is-prefix? '(1 3) '(1 2 3 4 5))
@result{} nil
@end group
@end example
@end defun
@anchor{-is-suffix?}
@defun -is-suffix? (suffix list)
Return non-nil if @var{suffix} is suffix of @var{list}.
Alias: @code{-is-suffix-p}
@example
@group
(-is-suffix? '(3 4 5) '(1 2 3 4 5))
@result{} t
@end group
@group
(-is-suffix? '(1 2 3 4 5) '(3 4 5))
@result{} nil
@end group
@group
(-is-suffix? '(3 5) '(1 2 3 4 5))
@result{} nil
@end group
@end example
@end defun
@anchor{-is-infix?}
@defun -is-infix? (infix list)
Return non-nil if @var{infix} is infix of @var{list}.
This operation runs in @var{o}(n^2) time
Alias: @code{-is-infix-p}
@example
@group
(-is-infix? '(1 2 3) '(1 2 3 4 5))
@result{} t
@end group
@group
(-is-infix? '(2 3 4) '(1 2 3 4 5))
@result{} t
@end group
@group
(-is-infix? '(3 4 5) '(1 2 3 4 5))
@result{} t
@end group
@end example
@end defun
@node Partitioning
@section Partitioning
Functions partitioning the input list into a list of lists.
@anchor{-split-at}
@defun -split-at (n list)
Return a list of ((-take @var{n} @var{list}) (-drop @var{n} @var{list})), in no more than one pass through the list.
@example
@group
(-split-at 3 '(1 2 3 4 5))
@result{} '((1 2 3) (4 5))
@end group
@group
(-split-at 17 '(1 2 3 4 5))
@result{} '((1 2 3 4 5) nil)
@end group
@end example
@end defun
@anchor{-split-with}
@defun -split-with (pred list)
Return a list of ((-take-while @var{pred} @var{list}) (-drop-while @var{pred} @var{list})), in no more than one pass through the list.
@example
@group
(-split-with 'even? '(1 2 3 4))
@result{} '(nil (1 2 3 4))
@end group
@group
(-split-with 'even? '(2 4 5 6))
@result{} '((2 4) (5 6))
@end group
@group
(--split-with (< it 4) '(1 2 3 4 3 2 1))
@result{} '((1 2 3) (4 3 2 1))
@end group
@end example
@end defun
@anchor{-split-on}
@defmac -split-on (item list)
Split the @var{list} each time @var{item} is found.
Unlike @code{-partition-by} (@pxref{-partition-by}), the @var{item} is discarded from the results.
Empty lists are also removed from the result.
Comparison is done by @code{equal}.
See also @code{-split-when} (@pxref{-split-when})
@example
@group
(-split-on '| '(Nil | Leaf a | Node [Tree a]))
@result{} '((Nil) (Leaf a) (Node [Tree a]))
@end group
@group
(-split-on ':endgroup '("a" "b" :endgroup "c" :endgroup "d" "e"))
@result{} '(("a" "b") ("c") ("d" "e"))
@end group
@group
(-split-on ':endgroup '("a" "b" :endgroup :endgroup "d" "e"))
@result{} '(("a" "b") ("d" "e"))
@end group
@end example
@end defmac
@anchor{-split-when}
@defun -split-when (fn list)
Split the @var{list} on each element where @var{fn} returns non-nil.
Unlike @code{-partition-by} (@pxref{-partition-by}), the "matched" element is discarded from
the results. Empty lists are also removed from the result.
This function can be thought of as a generalization of
@code{split-string}.
@example
@group
(-split-when 'even? '(1 2 3 4 5 6))
@result{} '((1) (3) (5))
@end group
@group
(-split-when 'even? '(1 2 3 4 6 8 9))
@result{} '((1) (3) (9))
@end group
@group
(--split-when (memq it '(&optional &rest)) '(a b &optional c d &rest args))
@result{} '((a b) (c d) (args))
@end group
@end example
@end defun
@anchor{-separate}
@defun -separate (pred list)
Return a list of ((-filter @var{pred} @var{list}) (-remove @var{pred} @var{list})), in one pass through the list.
@example
@group
(-separate (lambda (num) (= 0 (% num 2))) '(1 2 3 4 5 6 7))
@result{} '((2 4 6) (1 3 5 7))
@end group
@group
(--separate (< it 5) '(3 7 5 9 3 2 1 4 6))
@result{} '((3 3 2 1 4) (7 5 9 6))
@end group
@group
(-separate 'cdr '((1 2) (1) (1 2 3) (4)))
@result{} '(((1 2) (1 2 3)) ((1) (4)))
@end group
@end example
@end defun
@anchor{-partition}
@defun -partition (n list)
Return a new list with the items in @var{list} grouped into @var{n-}sized sublists.
If there are not enough items to make the last group @var{n-}sized,
those items are discarded.
@example
@group
(-partition 2 '(1 2 3 4 5 6))
@result{} '((1 2) (3 4) (5 6))
@end group
@group
(-partition 2 '(1 2 3 4 5 6 7))
@result{} '((1 2) (3 4) (5 6))
@end group
@group
(-partition 3 '(1 2 3 4 5 6 7))
@result{} '((1 2 3) (4 5 6))
@end group
@end example
@end defun
@anchor{-partition-all}
@defun -partition-all (n list)
Return a new list with the items in @var{list} grouped into @var{n-}sized sublists.
The last group may contain less than @var{n} items.
@example
@group
(-partition-all 2 '(1 2 3 4 5 6))
@result{} '((1 2) (3 4) (5 6))
@end group
@group
(-partition-all 2 '(1 2 3 4 5 6 7))
@result{} '((1 2) (3 4) (5 6) (7))
@end group
@group
(-partition-all 3 '(1 2 3 4 5 6 7))
@result{} '((1 2 3) (4 5 6) (7))
@end group
@end example
@end defun
@anchor{-partition-in-steps}
@defun -partition-in-steps (n step list)
Return a new list with the items in @var{list} grouped into @var{n-}sized sublists at offsets @var{step} apart.
If there are not enough items to make the last group @var{n-}sized,
those items are discarded.
@example
@group
(-partition-in-steps 2 1 '(1 2 3 4))
@result{} '((1 2) (2 3) (3 4))
@end group
@group
(-partition-in-steps 3 2 '(1 2 3 4))
@result{} '((1 2 3))
@end group
@group
(-partition-in-steps 3 2 '(1 2 3 4 5))
@result{} '((1 2 3) (3 4 5))
@end group
@end example
@end defun
@anchor{-partition-all-in-steps}
@defun -partition-all-in-steps (n step list)
Return a new list with the items in @var{list} grouped into @var{n-}sized sublists at offsets @var{step} apart.
The last groups may contain less than @var{n} items.
@example
@group
(-partition-all-in-steps 2 1 '(1 2 3 4))
@result{} '((1 2) (2 3) (3 4) (4))
@end group
@group
(-partition-all-in-steps 3 2 '(1 2 3 4))
@result{} '((1 2 3) (3 4))
@end group
@group
(-partition-all-in-steps 3 2 '(1 2 3 4 5))
@result{} '((1 2 3) (3 4 5) (5))
@end group
@end example
@end defun
@anchor{-partition-by}
@defun -partition-by (fn list)
Apply @var{fn} to each item in @var{list}, splitting it each time @var{fn} returns a new value.
@example
@group
(-partition-by 'even? '())
@result{} '()
@end group
@group
(-partition-by 'even? '(1 1 2 2 2 3 4 6 8))
@result{} '((1 1) (2 2 2) (3) (4 6 8))
@end group
@group
(--partition-by (< it 3) '(1 2 3 4 3 2 1))
@result{} '((1 2) (3 4 3) (2 1))
@end group
@end example
@end defun
@anchor{-partition-by-header}
@defun -partition-by-header (fn list)
Apply @var{fn} to the first item in @var{list}. That is the header
value. Apply @var{fn} to each item in @var{list}, splitting it each time @var{fn}
returns the header value, but only after seeing at least one
other value (the body).
@example
@group
(--partition-by-header (= it 1) '(1 2 3 1 2 1 2 3 4))
@result{} '((1 2 3) (1 2) (1 2 3 4))
@end group
@group
(--partition-by-header (> it 0) '(1 2 0 1 0 1 2 3 0))
@result{} '((1 2 0) (1 0) (1 2 3 0))
@end group
@group
(-partition-by-header 'even? '(2 1 1 1 4 1 3 5 6 6 1))
@result{} '((2 1 1 1) (4 1 3 5) (6 6 1))
@end group
@end example
@end defun
@anchor{-group-by}
@defun -group-by (fn list)
Separate @var{list} into an alist whose keys are @var{fn} applied to the
elements of @var{list}. Keys are compared by @code{equal}.
@example
@group
(-group-by 'even? '())
@result{} '()
@end group
@group
(-group-by 'even? '(1 1 2 2 2 3 4 6 8))
@result{} '((nil 1 1 3) (t 2 2 2 4 6 8))
@end group
@group
(--group-by (car (split-string it "/")) '("a/b" "c/d" "a/e"))
@result{} '(("a" "a/b" "a/e") ("c" "c/d"))
@end group
@end example
@end defun
@node Indexing
@section Indexing
Return indices of elements based on predicates, sort elements by indices etc.
@anchor{-elem-index}
@defun -elem-index (elem list)
Return the index of the first element in the given @var{list} which
is equal to the query element @var{elem}, or nil if there is no
such element.
@example
@group
(-elem-index 2 '(6 7 8 2 3 4))
@result{} 3
@end group
@group
(-elem-index "bar" '("foo" "bar" "baz"))
@result{} 1
@end group
@group
(-elem-index '(1 2) '((3) (5 6) (1 2) nil))
@result{} 2
@end group
@end example
@end defun
@anchor{-elem-indices}
@defun -elem-indices (elem list)
Return the indices of all elements in @var{list} equal to the query
element @var{elem}, in ascending order.
@example
@group
(-elem-indices 2 '(6 7 8 2 3 4 2 1))
@result{} '(3 6)
@end group
@group
(-elem-indices "bar" '("foo" "bar" "baz"))
@result{} '(1)
@end group
@group
(-elem-indices '(1 2) '((3) (1 2) (5 6) (1 2) nil))
@result{} '(1 3)
@end group
@end example
@end defun
@anchor{-find-index}
@defun -find-index (pred list)
Take a predicate @var{pred} and a @var{list} and return the index of the
first element in the list satisfying the predicate, or nil if
there is no such element.
See also @code{-first} (@pxref{-first}).
@example
@group
(-find-index 'even? '(2 4 1 6 3 3 5 8))
@result{} 0
@end group
@group
(--find-index (< 5 it) '(2 4 1 6 3 3 5 8))
@result{} 3
@end group
@group
(-find-index (-partial 'string-lessp "baz") '("bar" "foo" "baz"))
@result{} 1
@end group
@end example
@end defun
@anchor{-find-last-index}
@defun -find-last-index (pred list)
Take a predicate @var{pred} and a @var{list} and return the index of the
last element in the list satisfying the predicate, or nil if
there is no such element.
See also @code{-last} (@pxref{-last}).
@example
@group
(-find-last-index 'even? '(2 4 1 6 3 3 5 8))
@result{} 7
@end group
@group
(--find-last-index (< 5 it) '(2 7 1 6 3 8 5 2))
@result{} 5
@end group
@group
(-find-last-index (-partial 'string-lessp "baz") '("q" "foo" "baz"))
@result{} 1
@end group
@end example
@end defun
@anchor{-find-indices}
@defun -find-indices (pred list)
Return the indices of all elements in @var{list} satisfying the
predicate @var{pred}, in ascending order.
@example
@group
(-find-indices 'even? '(2 4 1 6 3 3 5 8))
@result{} '(0 1 3 7)
@end group
@group
(--find-indices (< 5 it) '(2 4 1 6 3 3 5 8))
@result{} '(3 7)
@end group
@group
(-find-indices (-partial 'string-lessp "baz") '("bar" "foo" "baz"))
@result{} '(1)
@end group
@end example
@end defun
@anchor{-grade-up}
@defun -grade-up (comparator list)
Grade elements of @var{list} using @var{comparator} relation, yielding a
permutation vector such that applying this permutation to @var{list}
sorts it in ascending order.
@example
@group
(-grade-up '< '(3 1 4 2 1 3 3))
@result{} '(1 4 3 0 5 6 2)
@end group
@group
(let ((l '(3 1 4 2 1 3 3))) (-select-by-indices (-grade-up '< l) l))
@result{} '(1 1 2 3 3 3 4)
@end group
@end example
@end defun
@anchor{-grade-down}
@defun -grade-down (comparator list)
Grade elements of @var{list} using @var{comparator} relation, yielding a
permutation vector such that applying this permutation to @var{list}
sorts it in descending order.
@example
@group
(-grade-down '< '(3 1 4 2 1 3 3))
@result{} '(2 0 5 6 3 1 4)
@end group
@group
(let ((l '(3 1 4 2 1 3 3))) (-select-by-indices (-grade-down '< l) l))
@result{} '(4 3 3 3 2 1 1)
@end group
@end example
@end defun
@node Set operations
@section Set operations
Operations pretending lists are sets.
@anchor{-union}
@defun -union (list list2)
Return a new list containing the elements of @var{list} and elements of @var{list2} that are not in @var{list}.
The test for equality is done with @code{equal},
or with @code{-compare-fn} if that's non-nil.
@example
@group
(-union '(1 2 3) '(3 4 5))
@result{} '(1 2 3 4 5)
@end group
@group
(-union '(1 2 3 4) '())
@result{} '(1 2 3 4)
@end group
@group
(-union '(1 1 2 2) '(3 2 1))
@result{} '(1 1 2 2 3)
@end group
@end example
@end defun
@anchor{-difference}
@defun -difference (list list2)
Return a new list with only the members of @var{list} that are not in @var{list2}.
The test for equality is done with @code{equal},
or with @code{-compare-fn} if that's non-nil.
@example
@group
(-difference '() '())
@result{} '()
@end group
@group
(-difference '(1 2 3) '(4 5 6))
@result{} '(1 2 3)
@end group
@group
(-difference '(1 2 3 4) '(3 4 5 6))
@result{} '(1 2)
@end group
@end example
@end defun
@anchor{-intersection}
@defun -intersection (list list2)
Return a new list containing only the elements that are members of both @var{list} and @var{list2}.
The test for equality is done with @code{equal},
or with @code{-compare-fn} if that's non-nil.
@example
@group
(-intersection '() '())
@result{} '()
@end group
@group
(-intersection '(1 2 3) '(4 5 6))
@result{} '()
@end group
@group
(-intersection '(1 2 3 4) '(3 4 5 6))
@result{} '(3 4)
@end group
@end example
@end defun
@anchor{-distinct}
@defun -distinct (list)
Return a new list with all duplicates removed.
The test for equality is done with @code{equal},
or with @code{-compare-fn} if that's non-nil.
Alias: @code{-uniq}
@example
@group
(-distinct '())
@result{} '()
@end group
@group
(-distinct '(1 2 2 4))
@result{} '(1 2 4)
@end group
@end example
@end defun
@node Other list operations
@section Other list operations
Other list functions not fit to be classified elsewhere.
@anchor{-rotate}
@defun -rotate (n list)
Rotate @var{list} @var{n} places to the right. With @var{n} negative, rotate to the left.
The time complexity is @var{o}(n).
@example
@group
(-rotate 3 '(1 2 3 4 5 6 7))
@result{} '(5 6 7 1 2 3 4)
@end group
@group
(-rotate -3 '(1 2 3 4 5 6 7))
@result{} '(4 5 6 7 1 2 3)
@end group
@end example
@end defun
@anchor{-repeat}
@defun -repeat (n x)
Return a list with @var{x} repeated @var{n} times.
Return nil if @var{n} is less than 1.
@example
@group
(-repeat 3 :a)
@result{} '(:a :a :a)
@end group
@group
(-repeat 1 :a)
@result{} '(:a)
@end group
@group
(-repeat 0 :a)
@result{} nil
@end group
@end example
@end defun
@anchor{-cons*}
@defun -cons* (&rest args)
Make a new list from the elements of @var{args}.
The last 2 members of @var{args} are used as the final cons of the
result so if the final member of @var{args} is not a list the result is
a dotted list.
@example
@group
(-cons* 1 2)
@result{} '(1 . 2)
@end group
@group
(-cons* 1 2 3)
@result{} '(1 2 . 3)
@end group
@group
(-cons* 1)
@result{} 1
@end group
@end example
@end defun
@anchor{-snoc}
@defun -snoc (list elem &rest elements)
Append @var{elem} to the end of the list.
This is like @code{cons}, but operates on the end of list.
If @var{elements} is non nil, append these to the list as well.
@example
@group
(-snoc '(1 2 3) 4)
@result{} '(1 2 3 4)
@end group
@group
(-snoc '(1 2 3) 4 5 6)
@result{} '(1 2 3 4 5 6)
@end group
@group
(-snoc '(1 2 3) '(4 5 6))
@result{} '(1 2 3 (4 5 6))
@end group
@end example
@end defun
@anchor{-interpose}
@defun -interpose (sep list)
Return a new list of all elements in @var{list} separated by @var{sep}.
@example
@group
(-interpose "-" '())
@result{} '()
@end group
@group
(-interpose "-" '("a"))
@result{} '("a")
@end group
@group
(-interpose "-" '("a" "b" "c"))
@result{} '("a" "-" "b" "-" "c")
@end group
@end example
@end defun
@anchor{-interleave}
@defun -interleave (&rest lists)
Return a new list of the first item in each list, then the second etc.
@example
@group
(-interleave '(1 2) '("a" "b"))
@result{} '(1 "a" 2 "b")
@end group
@group
(-interleave '(1 2) '("a" "b") '("A" "B"))
@result{} '(1 "a" "A" 2 "b" "B")
@end group
@group
(-interleave '(1 2 3) '("a" "b"))
@result{} '(1 "a" 2 "b")
@end group
@end example
@end defun
@anchor{-zip-with}
@defun -zip-with (fn list1 list2)
Zip the two lists @var{list1} and @var{list2} using a function @var{fn}. This
function is applied pairwise taking as first argument element of
@var{list1} and as second argument element of @var{list2} at corresponding
position.
The anaphoric form @code{--zip-with} binds the elements from @var{list1} as `it`,
and the elements from @var{list2} as `other`.
@example
@group
(-zip-with '+ '(1 2 3) '(4 5 6))
@result{} '(5 7 9)
@end group
@group
(-zip-with 'cons '(1 2 3) '(4 5 6))
@result{} '((1 . 4) (2 . 5) (3 . 6))
@end group
@group
(--zip-with (concat it " and " other) '("Batman" "Jekyll") '("Robin" "Hyde"))
@result{} '("Batman and Robin" "Jekyll and Hyde")
@end group
@end example
@end defun
@anchor{-zip}
@defun -zip (&rest lists)
Zip @var{lists} together. Group the head of each list, followed by the
second elements of each list, and so on. The lengths of the returned
groupings are equal to the length of the shortest input list.
If two lists are provided as arguments, return the groupings as a list
of cons cells. Otherwise, return the groupings as a list of lists.
Please note! This distinction is being removed in an upcoming 2.0
release of Dash. If you rely on this behavior, use -zip-pair instead.
@example
@group
(-zip '(1 2 3) '(4 5 6))
@result{} '((1 . 4) (2 . 5) (3 . 6))
@end group
@group
(-zip '(1 2 3) '(4 5 6 7))
@result{} '((1 . 4) (2 . 5) (3 . 6))
@end group
@group
(-zip '(1 2 3 4) '(4 5 6))
@result{} '((1 . 4) (2 . 5) (3 . 6))
@end group
@end example
@end defun
@anchor{-zip-fill}
@defun -zip-fill (fill-value &rest lists)
Zip @var{lists}, with @var{fill-value} padded onto the shorter lists. The
lengths of the returned groupings are equal to the length of the
longest input list.
@example
@group
(-zip-fill 0 '(1 2 3 4 5) '(6 7 8 9))
@result{} '((1 . 6) (2 . 7) (3 . 8) (4 . 9) (5 . 0))
@end group
@end example
@end defun
@anchor{-cycle}
@defun -cycle (list)
Return an infinite copy of @var{list} that will cycle through the
elements and repeat from the beginning.
@example
@group
(-take 5 (-cycle '(1 2 3)))
@result{} '(1 2 3 1 2)
@end group
@group
(-take 7 (-cycle '(1 "and" 3)))
@result{} '(1 "and" 3 1 "and" 3 1)
@end group
@group
(-zip (-cycle '(1 2 3)) '(1 2))
@result{} '((1 . 1) (2 . 2))
@end group
@end example
@end defun
@anchor{-pad}
@defun -pad (fill-value &rest lists)
Appends @var{fill-value} to the end of each list in @var{lists} such that they
will all have the same length.
@example
@group
(-pad 0 '())
@result{} '(nil)
@end group
@group
(-pad 0 '(1))
@result{} '((1))
@end group
@group
(-pad 0 '(1 2 3) '(4 5))
@result{} '((1 2 3) (4 5 0))
@end group
@end example
@end defun
@anchor{-table}
@defun -table (fn &rest lists)
Compute outer product of @var{lists} using function @var{fn}.
The function @var{fn} should have the same arity as the number of
supplied lists.
The outer product is computed by applying fn to all possible
combinations created by taking one element from each list in
order. The dimension of the result is (length lists).
See also: @code{-table-flat} (@pxref{-table-flat})
@example
@group
(-table '* '(1 2 3) '(1 2 3))
@result{} '((1 2 3) (2 4 6) (3 6 9))
@end group
@group
(-table (lambda (a b) (-sum (-zip-with '* a b))) '((1 2) (3 4)) '((1 3) (2 4)))
@result{} '((7 15) (10 22))
@end group
@group
(apply '-table 'list (-repeat 3 '(1 2)))
@result{} '((((1 1 1) (2 1 1)) ((1 2 1) (2 2 1))) (((1 1 2) (2 1 2)) ((1 2 2) (2 2 2))))
@end group
@end example
@end defun
@anchor{-table-flat}
@defun -table-flat (fn &rest lists)
Compute flat outer product of @var{lists} using function @var{fn}.
The function @var{fn} should have the same arity as the number of
supplied lists.
The outer product is computed by applying fn to all possible
combinations created by taking one element from each list in
order. The results are flattened, ignoring the tensor structure
of the result. This is equivalent to calling:
(-flatten-n (1- (length lists)) (-table fn lists))
but the implementation here is much more efficient.
See also: @code{-flatten-n} (@pxref{-flatten-n}), @code{-table} (@pxref{-table})
@example
@group
(-table-flat 'list '(1 2 3) '(a b c))
@result{} '((1 a) (2 a) (3 a) (1 b) (2 b) (3 b) (1 c) (2 c) (3 c))
@end group
@group
(-table-flat '* '(1 2 3) '(1 2 3))
@result{} '(1 2 3 2 4 6 3 6 9)
@end group
@group
(apply '-table-flat 'list (-repeat 3 '(1 2)))
@result{} '((1 1 1) (2 1 1) (1 2 1) (2 2 1) (1 1 2) (2 1 2) (1 2 2) (2 2 2))
@end group
@end example
@end defun
@anchor{-first}
@defun -first (pred list)
Return the first x in @var{list} where (@var{pred} x) is non-nil, else nil.
To get the first item in the list no questions asked, use @code{car}.
Alias: @code{-find}
@example
@group
(-first 'even? '(1 2 3))
@result{} 2
@end group
@group
(-first 'even? '(1 3 5))
@result{} nil
@end group
@group
(--first (> it 2) '(1 2 3))
@result{} 3
@end group
@end example
@end defun
@anchor{-some}
@defun -some (pred list)
Return (@var{pred} x) for the first @var{list} item where (@var{pred} x) is non-nil, else nil.
Alias: @code{-any}
@example
@group
(-some 'even? '(1 2 3))
@result{} t
@end group
@group
(--some (member 'foo it) '((foo bar) (baz)))
@result{} '(foo bar)
@end group
@group
(--some (plist-get it :bar) '((:foo 1 :bar 2) (:baz 3)))
@result{} 2
@end group
@end example
@end defun
@anchor{-last}
@defun -last (pred list)
Return the last x in @var{list} where (@var{pred} x) is non-nil, else nil.
@example
@group
(-last 'even? '(1 2 3 4 5 6 3 3 3))
@result{} 6
@end group
@group
(-last 'even? '(1 3 7 5 9))
@result{} nil
@end group
@group
(--last (> (length it) 3) '("a" "looong" "word" "and" "short" "one"))
@result{} "short"
@end group
@end example
@end defun
@anchor{-first-item}
@defun -first-item (list)
Return the first item of @var{list}, or nil on an empty list.
@example
@group
(-first-item '(1 2 3))
@result{} 1
@end group
@group
(-first-item nil)
@result{} nil
@end group
@end example
@end defun
@anchor{-last-item}
@defun -last-item (list)
Return the last item of @var{list}, or nil on an empty list.
@example
@group
(-last-item '(1 2 3))
@result{} 3
@end group
@group
(-last-item nil)
@result{} nil
@end group
@end example
@end defun
@anchor{-butlast}
@defun -butlast (list)
Return a list of all items in list except for the last.
@example
@group
(-butlast '(1 2 3))
@result{} '(1 2)
@end group
@group
(-butlast '(1 2))
@result{} '(1)
@end group
@group
(-butlast '(1))
@result{} nil
@end group
@end example
@end defun
@anchor{-sort}
@defun -sort (comparator list)
Sort @var{list}, stably, comparing elements using @var{comparator}.
Return the sorted list. @var{list} is @var{not} modified by side effects.
@var{comparator} is called with two elements of @var{list}, and should return non-nil
if the first element should sort before the second.
@example
@group
(-sort '< '(3 1 2))
@result{} '(1 2 3)
@end group
@group
(-sort '> '(3 1 2))
@result{} '(3 2 1)
@end group
@group
(--sort (< it other) '(3 1 2))
@result{} '(1 2 3)
@end group
@end example
@end defun
@anchor{-list}
@defun -list (&rest args)
Return a list with @var{args}.
If first item of @var{args} is already a list, simply return @var{args}. If
not, return a list with @var{args} as elements.
@example
@group
(-list 1)
@result{} '(1)
@end group
@group
(-list 1 2 3)
@result{} '(1 2 3)
@end group
@group
(-list '(1 2 3))
@result{} '(1 2 3)
@end group
@end example
@end defun
@anchor{-fix}
@defun -fix (fn list)
Compute the (least) fixpoint of @var{fn} with initial input @var{list}.
@var{fn} is called at least once, results are compared with @code{equal}.
@example
@group
(-fix (lambda (l) (-non-nil (--mapcat (-split-at (/ (length it) 2) it) l))) '((1 2 3 4 5 6)))
@result{} '((1) (2) (3) (4) (5) (6))
@end group
@group
(let ((data '(("starwars" "scifi") ("jedi" "starwars" "warrior")))) (--fix (-uniq (--mapcat (cons it (cdr (assoc it data))) it)) '("jedi" "book")))
@result{} '("jedi" "starwars" "warrior" "scifi" "book")
@end group
@end example
@end defun
@node Tree operations
@section Tree operations
Functions pretending lists are trees.
@anchor{-tree-seq}
@defun -tree-seq (branch children tree)
Return a sequence of the nodes in @var{tree}, in depth-first search order.
@var{branch} is a predicate of one argument that returns non-nil if the
passed argument is a branch, that is, a node that can have children.
@var{children} is a function of one argument that returns the children
of the passed branch node.
Non-branch nodes are simply copied.
@example
@group
(-tree-seq 'listp 'identity '(1 (2 3) 4 (5 (6 7))))
@result{} '((1 (2 3) 4 (5 (6 7))) 1 (2 3) 2 3 4 (5 (6 7)) 5 (6 7) 6 7)
@end group
@group
(-tree-seq 'listp 'reverse '(1 (2 3) 4 (5 (6 7))))
@result{} '((1 (2 3) 4 (5 (6 7))) (5 (6 7)) (6 7) 7 6 5 4 (2 3) 3 2 1)
@end group
@group
(--tree-seq (vectorp it) (append it nil) [1 [2 3] 4 [5 [6 7]]])
@result{} '([1 [2 3] 4 [5 [6 7]]] 1 [2 3] 2 3 4 [5 [6 7]] 5 [6 7] 6 7)
@end group
@end example
@end defun
@anchor{-tree-map}
@defun -tree-map (fn tree)
Apply @var{fn} to each element of @var{tree} while preserving the tree structure.
@example
@group
(-tree-map '1+ '(1 (2 3) (4 (5 6) 7)))
@result{} '(2 (3 4) (5 (6 7) 8))
@end group
@group
(-tree-map '(lambda (x) (cons x (expt 2 x))) '(1 (2 3) 4))
@result{} '((1 . 2) ((2 . 4) (3 . 8)) (4 . 16))
@end group
@group
(--tree-map (length it) '("<body>" ("<p>" "text" "</p>") "</body>"))
@result{} '(6 (3 4 4) 7)
@end group
@end example
@end defun
@anchor{-tree-map-nodes}
@defun -tree-map-nodes (pred fun tree)
Call @var{fun} on each node of @var{tree} that satisfies @var{pred}.
If @var{pred} returns nil, continue descending down this node. If @var{pred}
returns non-nil, apply @var{fun} to this node and do not descend
further.
@example
@group
(-tree-map-nodes 'vectorp (lambda (x) (-sum (append x nil))) '(1 [2 3] 4 (5 [6 7] 8)))
@result{} '(1 5 4 (5 13 8))
@end group
@group
(-tree-map-nodes 'keywordp (lambda (x) (symbol-name x)) '(1 :foo 4 ((5 6 :bar) :baz 8)))
@result{} '(1 ":foo" 4 ((5 6 ":bar") ":baz" 8))
@end group
@group
(--tree-map-nodes (eq (car-safe it) 'add-mode) (-concat it (list :mode 'emacs-lisp-mode)) '(with-mode emacs-lisp-mode (foo bar) (add-mode a b) (baz (add-mode c d))))
@result{} '(with-mode emacs-lisp-mode (foo bar) (add-mode a b :mode emacs-lisp-mode) (baz (add-mode c d :mode emacs-lisp-mode)))
@end group
@end example
@end defun
@anchor{-tree-reduce}
@defun -tree-reduce (fn tree)
Use @var{fn} to reduce elements of list @var{tree}.
If elements of @var{tree} are lists themselves, apply the reduction recursively.
@var{fn} is first applied to first element of the list and second
element, then on this result and third element from the list etc.
See @code{-reduce-r} (@pxref{-reduce-r}) for how exactly are lists of zero or one element handled.
@example
@group
(-tree-reduce '+ '(1 (2 3) (4 5)))
@result{} 15
@end group
@group
(-tree-reduce 'concat '("strings" (" on" " various") ((" levels"))))
@result{} "strings on various levels"
@end group
@group
(--tree-reduce (cond ((stringp it) (concat it " " acc)) (t (let ((sn (symbol-name it))) (concat "<" sn ">" acc "</" sn ">")))) '(body (p "some words") (div "more" (b "bold") "words")))
@result{} "<body><p>some words</p> <div>more <b>bold</b> words</div></body>"
@end group
@end example
@end defun
@anchor{-tree-reduce-from}
@defun -tree-reduce-from (fn init-value tree)
Use @var{fn} to reduce elements of list @var{tree}.
If elements of @var{tree} are lists themselves, apply the reduction recursively.
@var{fn} is first applied to @var{init-value} and first element of the list,
then on this result and second element from the list etc.
The initial value is ignored on cons pairs as they always contain
two elements.
@example
@group
(-tree-reduce-from '+ 1 '(1 (1 1) ((1))))
@result{} 8
@end group
@group
(--tree-reduce-from (-concat acc (list it)) nil '(1 (2 3 (4 5)) (6 7)))
@result{} '((7 6) ((5 4) 3 2) 1)
@end group
@end example
@end defun
@anchor{-tree-mapreduce}
@defun -tree-mapreduce (fn folder tree)
Apply @var{fn} to each element of @var{tree}, and make a list of the results.
If elements of @var{tree} are lists themselves, apply @var{fn} recursively to
elements of these nested lists.
Then reduce the resulting lists using @var{folder} and initial value
@var{init-value}. See @code{-reduce-r-from} (@pxref{-reduce-r-from}).
This is the same as calling @code{-tree-reduce} (@pxref{-tree-reduce}) after @code{-tree-map} (@pxref{-tree-map})
but is twice as fast as it only traverse the structure once.
@example
@group
(-tree-mapreduce 'list 'append '(1 (2 (3 4) (5 6)) (7 (8 9))))
@result{} '(1 2 3 4 5 6 7 8 9)
@end group
@group
(--tree-mapreduce 1 (+ it acc) '(1 (2 (4 9) (2 1)) (7 (4 3))))
@result{} 9
@end group
@group
(--tree-mapreduce 0 (max acc (1+ it)) '(1 (2 (4 9) (2 1)) (7 (4 3))))
@result{} 3
@end group
@end example
@end defun
@anchor{-tree-mapreduce-from}
@defun -tree-mapreduce-from (fn folder init-value tree)
Apply @var{fn} to each element of @var{tree}, and make a list of the results.
If elements of @var{tree} are lists themselves, apply @var{fn} recursively to
elements of these nested lists.
Then reduce the resulting lists using @var{folder} and initial value
@var{init-value}. See @code{-reduce-r-from} (@pxref{-reduce-r-from}).
This is the same as calling @code{-tree-reduce-from} (@pxref{-tree-reduce-from}) after @code{-tree-map} (@pxref{-tree-map})
but is twice as fast as it only traverse the structure once.
@example
@group
(-tree-mapreduce-from 'identity '* 1 '(1 (2 (3 4) (5 6)) (7 (8 9))))
@result{} 362880
@end group
@group
(--tree-mapreduce-from (+ it it) (cons it acc) nil '(1 (2 (4 9) (2 1)) (7 (4 3))))
@result{} '(2 (4 (8 18) (4 2)) (14 (8 6)))
@end group
@group
(concat "@{" (--tree-mapreduce-from (cond ((-cons-pair? it) (concat (symbol-name (car it)) " -> " (symbol-name (cdr it)))) (t (concat (symbol-name it) " : @{"))) (concat it (unless (or (equal acc "@}") (equal (substring it (1- (length it))) "@{")) ", ") acc) "@}" '((elips-mode (foo (bar . booze)) (baz . qux)) (c-mode (foo . bla) (bum . bam)))))
@result{} "@{elips-mode : @{foo : @{bar -> booze@{, baz -> qux@{, c-mode : @{foo -> bla, bum -> bam@}@}"
@end group
@end example
@end defun
@anchor{-clone}
@defun -clone (list)
Create a deep copy of @var{list}.
The new list has the same elements and structure but all cons are
replaced with new ones. This is useful when you need to clone a
structure such as plist or alist.
@example
@group
(let* ((a '(1 2 3)) (b (-clone a))) (nreverse a) b)
@result{} '(1 2 3)
@end group
@end example
@end defun
@node Threading macros
@section Threading macros
@anchor{->}
@defmac -> (x &optional form &rest more)
Thread the expr through the forms. Insert @var{x} as the second item
in the first form, making a list of it if it is not a list
already. If there are more forms, insert the first form as the
second item in second form, etc.
@example
@group
(-> '(2 3 5))
@result{} '(2 3 5)
@end group
@group
(-> '(2 3 5) (append '(8 13)))
@result{} '(2 3 5 8 13)
@end group
@group
(-> '(2 3 5) (append '(8 13)) (-slice 1 -1))
@result{} '(3 5 8)
@end group
@end example
@end defmac
@anchor{->>}
@defmac ->> (x &optional form &rest more)
Thread the expr through the forms. Insert @var{x} as the last item
in the first form, making a list of it if it is not a list
already. If there are more forms, insert the first form as the
last item in second form, etc.
@example
@group
(->> '(1 2 3) (-map 'square))
@result{} '(1 4 9)
@end group
@group
(->> '(1 2 3) (-map 'square) (-remove 'even?))
@result{} '(1 9)
@end group
@group
(->> '(1 2 3) (-map 'square) (-reduce '+))
@result{} 14
@end group
@end example
@end defmac
@anchor{-->}
@defmac --> (x form &rest more)
Thread the expr through the forms. Insert @var{x} at the position
signified by the token @code{it} in the first form. If there are more
forms, insert the first form at the position signified by @code{it} in
in second form, etc.
@example
@group
(--> "def" (concat "abc" it "ghi"))
@result{} "abcdefghi"
@end group
@group
(--> "def" (concat "abc" it "ghi") (upcase it))
@result{} "ABCDEFGHI"
@end group
@group
(--> "def" (concat "abc" it "ghi") upcase)
@result{} "ABCDEFGHI"
@end group
@end example
@end defmac
@anchor{-some->}
@defmac -some-> (x &optional form &rest more)
When expr is non-nil, thread it through the first form (via @code{->} (@pxref{->})),
and when that result is non-nil, through the next form, etc.
@example
@group
(-some-> '(2 3 5))
@result{} '(2 3 5)
@end group
@group
(-some-> 5 square)
@result{} 25
@end group
@group
(-some-> 5 even? square)
@result{} nil
@end group
@end example
@end defmac
@anchor{-some->>}
@defmac -some->> (x &optional form &rest more)
When expr is non-nil, thread it through the first form (via @code{->>} (@pxref{->>})),
and when that result is non-nil, through the next form, etc.
@example
@group
(-some->> '(1 2 3) (-map 'square))
@result{} '(1 4 9)
@end group
@group
(-some->> '(1 3 5) (-last 'even?) (+ 100))
@result{} nil
@end group
@group
(-some->> '(2 4 6) (-last 'even?) (+ 100))
@result{} 106
@end group
@end example
@end defmac
@anchor{-some-->}
@defmac -some--> (x &optional form &rest more)
When expr in non-nil, thread it through the first form (via @code{-->} (@pxref{-->})),
and when that result is non-nil, through the next form, etc.
@example
@group
(-some--> "def" (concat "abc" it "ghi"))
@result{} "abcdefghi"
@end group
@group
(-some--> nil (concat "abc" it "ghi"))
@result{} nil
@end group
@group
(-some--> '(1 3 5) (-filter 'even? it) (append it it) (-map 'square it))
@result{} nil
@end group
@end example
@end defmac
@node Binding
@section Binding
Convenient versions of `let` and `let*` constructs combined with flow control.
@anchor{-when-let}
@defmac -when-let (var-val &rest body)
If @var{val} evaluates to non-nil, bind it to @var{var} and execute body.
@var{var-val} should be a (@var{var} @var{val}) pair.
Note: binding is done according to @code{-let} (@pxref{-let}).
@example
@group
(-when-let (match-index (string-match "d" "abcd")) (+ match-index 2))
@result{} 5
@end group
@group
(-when-let ((&plist :foo foo) (list :foo "foo")) foo)
@result{} "foo"
@end group
@group
(-when-let ((&plist :foo foo) (list :bar "bar")) foo)
@result{} nil
@end group
@end example
@end defmac
@anchor{-when-let*}
@defmac -when-let* (vars-vals &rest body)
If all @var{vals} evaluate to true, bind them to their corresponding
@var{vars} and execute body. @var{vars-vals} should be a list of (@var{var} @var{val})
pairs.
Note: binding is done according to @code{-let*} (@pxref{-let*}). @var{vals} are evaluated
sequentially, and evaluation stops after the first nil @var{val} is
encountered.
@example
@group
(-when-let* ((x 5) (y 3) (z (+ y 4))) (+ x y z))
@result{} 15
@end group
@group
(-when-let* ((x 5) (y nil) (z 7)) (+ x y z))
@result{} nil
@end group
@end example
@end defmac
@anchor{-if-let}
@defmac -if-let (var-val then &rest else)
If @var{val} evaluates to non-nil, bind it to @var{var} and do @var{then},
otherwise do @var{else}. @var{var-val} should be a (@var{var} @var{val}) pair.
Note: binding is done according to @code{-let} (@pxref{-let}).
@example
@group
(-if-let (match-index (string-match "d" "abc")) (+ match-index 3) 7)
@result{} 7
@end group
@group
(--if-let (even? 4) it nil)
@result{} t
@end group
@end example
@end defmac
@anchor{-if-let*}
@defmac -if-let* (vars-vals then &rest else)
If all @var{vals} evaluate to true, bind them to their corresponding
@var{vars} and do @var{then}, otherwise do @var{else}. @var{vars-vals} should be a list
of (@var{var} @var{val}) pairs.
Note: binding is done according to @code{-let*} (@pxref{-let*}). @var{vals} are evaluated
sequentially, and evaluation stops after the first nil @var{val} is
encountered.
@example
@group
(-if-let* ((x 5) (y 3) (z 7)) (+ x y z) "foo")
@result{} 15
@end group
@group
(-if-let* ((x 5) (y nil) (z 7)) (+ x y z) "foo")
@result{} "foo"
@end group
@group
(-if-let* (((_ _ x) '(nil nil 7))) x)
@result{} 7
@end group
@end example
@end defmac
@anchor{-let}
@defmac -let (varlist &rest body)
Bind variables according to @var{varlist} then eval @var{body}.
@var{varlist} is a list of lists of the form (@var{pattern} @var{source}). Each
@var{pattern} is matched against the @var{source} "structurally". @var{source}
is only evaluated once for each @var{pattern}. Each @var{pattern} is matched
recursively, and can therefore contain sub-patterns which are
matched against corresponding sub-expressions of @var{source}.
All the SOURCEs are evalled before any symbols are
bound (i.e. "in parallel").
If @var{varlist} only contains one (@var{pattern} @var{source}) element, you can
optionally specify it using a vector and discarding the
outer-most parens. Thus
(-let ((@var{pattern} @var{source})) ..)
becomes
(-let [@var{pattern} @var{source}] ..).
@code{-let} (@pxref{-let}) uses a convention of not binding places (symbols) starting
with _ whenever it's possible. You can use this to skip over
entries you don't care about. However, this is not *always*
possible (as a result of implementation) and these symbols might
get bound to undefined values.
Following is the overview of supported patterns. Remember that
patterns can be matched recursively, so every a, b, aK in the
following can be a matching construct and not necessarily a
symbol/variable.
Symbol:
a - bind the @var{source} to @var{a}. This is just like regular @code{let}.
Conses and lists:
(a) - bind @code{car} of cons/list to @var{a}
(a . b) - bind car of cons to @var{a} and @code{cdr} to @var{b}
(a b) - bind car of list to @var{a} and @code{cadr} to @var{b}
(a1 a2 a3 ...) - bind 0th car of list to @var{a1}, 1st to @var{a2}, 2nd to @var{a3} ...
(a1 a2 a3 ... aN . rest) - as above, but bind the Nth cdr to @var{rest}.
Vectors:
[a] - bind 0th element of a non-list sequence to @var{a} (works with
vectors, strings, bit arrays...)
[a1 a2 a3 ...] - bind 0th element of non-list sequence to @var{a0}, 1st to
@var{a1}, 2nd to @var{a2}, ...
If the @var{pattern} is shorter than @var{source}, the values at
places not in @var{pattern} are ignored.
If the @var{pattern} is longer than @var{source}, an @code{error} is
thrown.
[a1 a2 a3 ... &rest rest] - as above, but bind the rest of
the sequence to @var{rest}. This is
conceptually the same as improper list
matching (a1 a2 ... aN . rest)
Key/value stores:
(&plist key0 a0 ... keyN aN) - bind value mapped by keyK in the
@var{source} plist to aK. If the
value is not found, aK is nil.
(&alist key0 a0 ... keyN aN) - bind value mapped by keyK in the
@var{source} alist to aK. If the
value is not found, aK is nil.
(&hash key0 a0 ... keyN aN) - bind value mapped by keyK in the
@var{source} hash table to aK. If the
value is not found, aK is nil.
Further, special keyword &keys supports "inline" matching of
plist-like key-value pairs, similarly to &keys keyword of
@code{cl-defun}.
(a1 a2 ... aN &keys key1 b1 ... keyN bK)
This binds @var{n} values from the list to a1 ... aN, then interprets
the cdr as a plist (see key/value matching above).
You can name the source using the syntax @var{symbol} &as @var{pattern}.
This syntax works with lists (proper or improper), vectors and
all types of maps.
(list &as a b c) (list 1 2 3)
binds @var{a} to 1, @var{b} to 2, @var{c} to 3 and @var{list} to (1 2 3).
Similarly:
(bounds &as beg . end) (cons 1 2)
binds @var{beg} to 1, @var{end} to 2 and @var{bounds} to (1 . 2).
(items &as first . rest) (list 1 2 3)
binds @var{first} to 1, @var{rest} to (2 3) and @var{items} to (1 2 3)
[vect &as _ b c] [1 2 3]
binds @var{b} to 2, @var{c} to 3 and @var{vect} to [1 2 3] (_ avoids binding as usual).
(plist &as &plist :b b) (list :a 1 :b 2 :c 3)
binds @var{b} to 2 and @var{plist} to (:a 1 :b 2 :c 3). Same for &alist and &hash.
This is especially useful when we want to capture the result of a
computation and destructure at the same time. Consider the
form (function-returning-complex-structure) returning a list of
two vectors with two items each. We want to capture this entire
result and pass it to another computation, but at the same time
we want to get the second item from each vector. We can achieve
it with pattern
(result &as [_ a] [_ b]) (function-returning-complex-structure)
Note: Clojure programmers may know this feature as the ":as
binding". The difference is that we put the &as at the front
because we need to support improper list binding.
@example
@group
(-let (([a (b c) d] [1 (2 3) 4])) (list a b c d))
@result{} '(1 2 3 4)
@end group
@group
(-let [(a b c . d) (list 1 2 3 4 5 6)] (list a b c d))
@result{} '(1 2 3 (4 5 6))
@end group
@group
(-let [(&plist :foo foo :bar bar) (list :baz 3 :foo 1 :qux 4 :bar 2)] (list foo bar))
@result{} '(1 2)
@end group
@end example
@end defmac
@anchor{-let*}
@defmac -let* (varlist &rest body)
Bind variables according to @var{varlist} then eval @var{body}.
@var{varlist} is a list of lists of the form (@var{pattern} @var{source}). Each
@var{pattern} is matched against the @var{source} structurally. @var{source} is
only evaluated once for each @var{pattern}.
Each @var{source} can refer to the symbols already bound by this
@var{varlist}. This is useful if you want to destructure @var{source}
recursively but also want to name the intermediate structures.
See @code{-let} (@pxref{-let}) for the list of all possible patterns.
@example
@group
(-let* (((a . b) (cons 1 2)) ((c . d) (cons 3 4))) (list a b c d))
@result{} '(1 2 3 4)
@end group
@group
(-let* (((a . b) (cons 1 (cons 2 3))) ((c . d) b)) (list a b c d))
@result{} '(1 (2 . 3) 2 3)
@end group
@group
(-let* (((&alist "foo" foo "bar" bar) (list (cons "foo" 1) (cons "bar" (list 'a 'b 'c)))) ((a b c) bar)) (list foo a b c bar))
@result{} '(1 a b c (a b c))
@end group
@end example
@end defmac
@anchor{-lambda}
@defmac -lambda (match-form &rest body)
Return a lambda which destructures its input as @var{match-form} and executes @var{body}.
Note that you have to enclose the @var{match-form} in a pair of parens,
such that:
(-lambda (x) body)
(-lambda (x y ...) body)
has the usual semantics of @code{lambda}. Furthermore, these get
translated into normal lambda, so there is no performance
penalty.
See @code{-let} (@pxref{-let}) for the description of destructuring mechanism.
@example
@group
(-map (-lambda ((x y)) (+ x y)) '((1 2) (3 4) (5 6)))
@result{} '(3 7 11)
@end group
@group
(-map (-lambda ([x y]) (+ x y)) '([1 2] [3 4] [5 6]))
@result{} '(3 7 11)
@end group
@group
(funcall (-lambda ((_ . a) (_ . b)) (-concat a b)) '(1 2 3) '(4 5 6))
@result{} '(2 3 5 6)
@end group
@end example
@end defmac
@node Side-effects
@section Side-effects
Functions iterating over lists for side-effect only.
@anchor{-each}
@defun -each (list fn)
Call @var{fn} with every item in @var{list}. Return nil, used for side-effects only.
@example
@group
(let (s) (-each '(1 2 3) (lambda (item) (setq s (cons item s)))))
@result{} nil
@end group
@group
(let (s) (-each '(1 2 3) (lambda (item) (setq s (cons item s)))) s)
@result{} '(3 2 1)
@end group
@group
(let (s) (--each '(1 2 3) (setq s (cons it s))) s)
@result{} '(3 2 1)
@end group
@end example
@end defun
@anchor{-each-while}
@defun -each-while (list pred fn)
Call @var{fn} with every item in @var{list} while (@var{pred} item) is non-nil.
Return nil, used for side-effects only.
@example
@group
(let (s) (-each-while '(2 4 5 6) 'even? (lambda (item) (!cons item s))) s)
@result{} '(4 2)
@end group
@group
(let (s) (--each-while '(1 2 3 4) (< it 3) (!cons it s)) s)
@result{} '(2 1)
@end group
@end example
@end defun
@anchor{-each-indexed}
@defun -each-indexed (list fn)
Call (@var{fn} index item) for each item in @var{list}.
In the anaphoric form @code{--each-indexed}, the index is exposed as `it-index`.
See also: @code{-map-indexed} (@pxref{-map-indexed}).
@example
@group
(let (s) (-each-indexed '(a b c) (lambda (index item) (setq s (cons (list item index) s)))) s)
@result{} '((c 2) (b 1) (a 0))
@end group
@group
(let (s) (--each-indexed '(a b c) (setq s (cons (list it it-index) s))) s)
@result{} '((c 2) (b 1) (a 0))
@end group
@end example
@end defun
@anchor{-dotimes}
@defun -dotimes (num fn)
Repeatedly calls @var{fn} (presumably for side-effects) passing in integers from 0 through @var{num-1}.
@example
@group
(let (s) (-dotimes 3 (lambda (n) (!cons n s))) s)
@result{} '(2 1 0)
@end group
@group
(let (s) (--dotimes 5 (!cons it s)) s)
@result{} '(4 3 2 1 0)
@end group
@end example
@end defun
@anchor{-doto}
@defmac -doto (eval-initial-value &rest forms)
Eval a form, then insert that form as the 2nd argument to other forms.
The @var{eval-initial-value} form is evaluated once. Its result is
passed to @var{forms}, which are then evaluated sequentially. Returns
the target form.
@example
@group
(-doto '(1 2 3) (!cdr) (!cdr))
@result{} '(3)
@end group
@group
(-doto '(1 . 2) (setcar 3) (setcdr 4))
@result{} '(3 . 4)
@end group
@end example
@end defmac
@node Destructive operations
@section Destructive operations
@anchor{!cons}
@defmac !cons (car cdr)
Destructive: Set @var{cdr} to the cons of @var{car} and @var{cdr}.
@example
@group
(let (l) (!cons 5 l) l)
@result{} '(5)
@end group
@group
(let ((l '(3))) (!cons 5 l) l)
@result{} '(5 3)
@end group
@end example
@end defmac
@anchor{!cdr}
@defmac !cdr (list)
Destructive: Set @var{list} to the cdr of @var{list}.
@example
@group
(let ((l '(3))) (!cdr l) l)
@result{} '()
@end group
@group
(let ((l '(3 5))) (!cdr l) l)
@result{} '(5)
@end group
@end example
@end defmac
@node Function combinators
@section Function combinators
These combinators require Emacs 24 for its lexical scope. So they are offered in a separate package: `dash-functional`.
@anchor{-partial}
@defun -partial (fn &rest args)
Takes a function @var{fn} and fewer than the normal arguments to @var{fn},
and returns a fn that takes a variable number of additional @var{args}.
When called, the returned function calls @var{fn} with @var{args} first and
then additional args.
@example
@group
(funcall (-partial '- 5) 3)
@result{} 2
@end group
@group
(funcall (-partial '+ 5 2) 3)
@result{} 10
@end group
@end example
@end defun
@anchor{-rpartial}
@defun -rpartial (fn &rest args)
Takes a function @var{fn} and fewer than the normal arguments to @var{fn},
and returns a fn that takes a variable number of additional @var{args}.
When called, the returned function calls @var{fn} with the additional
args first and then @var{args}.
@example
@group
(funcall (-rpartial '- 5) 8)
@result{} 3
@end group
@group
(funcall (-rpartial '- 5 2) 10)
@result{} 3
@end group
@end example
@end defun
@anchor{-juxt}
@defun -juxt (&rest fns)
Takes a list of functions and returns a fn that is the
juxtaposition of those fns. The returned fn takes a variable
number of args, and returns a list containing the result of
applying each fn to the args (left-to-right).
@example
@group
(funcall (-juxt '+ '-) 3 5)
@result{} '(8 -2)
@end group
@group
(-map (-juxt 'identity 'square) '(1 2 3))
@result{} '((1 1) (2 4) (3 9))
@end group
@end example
@end defun
@anchor{-compose}
@defun -compose (&rest fns)
Takes a list of functions and returns a fn that is the
composition of those fns. The returned fn takes a variable
number of arguments, and returns the result of applying
each fn to the result of applying the previous fn to
the arguments (right-to-left).
@example
@group
(funcall (-compose 'square '+) 2 3)
@result{} (square (+ 2 3))
@end group
@group
(funcall (-compose 'identity 'square) 3)
@result{} (square 3)
@end group
@group
(funcall (-compose 'square 'identity) 3)
@result{} (square 3)
@end group
@end example
@end defun
@anchor{-applify}
@defun -applify (fn)
Changes an n-arity function @var{fn} to a 1-arity function that
expects a list with n items as arguments
@example
@group
(-map (-applify '+) '((1 1 1) (1 2 3) (5 5 5)))
@result{} '(3 6 15)
@end group
@group
(-map (-applify (lambda (a b c) (\` ((\, a) ((\, b) ((\, c))))))) '((1 1 1) (1 2 3) (5 5 5)))
@result{} '((1 (1 (1))) (1 (2 (3))) (5 (5 (5))))
@end group
@group
(funcall (-applify '<) '(3 6))
@result{} t
@end group
@end example
@end defun
@anchor{-on}
@defun -on (operator transformer)
Return a function of two arguments that first applies
@var{transformer} to each of them and then applies @var{operator} on the
results (in the same order).
In types: (b -> b -> c) -> (a -> b) -> a -> a -> c
@example
@group
(-sort (-on '< 'length) '((1 2 3) (1) (1 2)))
@result{} '((1) (1 2) (1 2 3))
@end group
@group
(-min-by (-on '> 'length) '((1 2 3) (4) (1 2)))
@result{} '(4)
@end group
@group
(-min-by (-on 'string-lessp 'int-to-string) '(2 100 22))
@result{} 22
@end group
@end example
@end defun
@anchor{-flip}
@defun -flip (func)
Swap the order of arguments for binary function @var{func}.
In types: (a -> b -> c) -> b -> a -> c
@example
@group
(funcall (-flip '<) 2 1)
@result{} t
@end group
@group
(funcall (-flip '-) 3 8)
@result{} 5
@end group
@group
(-sort (-flip '<) '(4 3 6 1))
@result{} '(6 4 3 1)
@end group
@end example
@end defun
@anchor{-const}
@defun -const (c)
Return a function that returns @var{c} ignoring any additional arguments.
In types: a -> b -> a
@example
@group
(funcall (-const 2) 1 3 "foo")
@result{} 2
@end group
@group
(-map (-const 1) '("a" "b" "c" "d"))
@result{} '(1 1 1 1)
@end group
@group
(-sum (-map (-const 1) '("a" "b" "c" "d")))
@result{} 4
@end group
@end example
@end defun
@anchor{-cut}
@defmac -cut (&rest params)
Take n-ary function and n arguments and specialize some of them.
Arguments denoted by <> will be left unspecialized.
See @var{srfi-26} for detailed description.
@example
@group
(funcall (-cut list 1 <> 3 <> 5) 2 4)
@result{} '(1 2 3 4 5)
@end group
@group
(-map (-cut funcall <> 5) '(1+ 1- (lambda (x) (/ 1.0 x))))
@result{} '(6 4 0.2)
@end group
@group
(-filter (-cut < <> 5) '(1 3 5 7 9))
@result{} '(1 3)
@end group
@end example
@end defmac
@anchor{-not}
@defun -not (pred)
Take a unary predicate @var{pred} and return a unary predicate
that returns t if @var{pred} returns nil and nil if @var{pred} returns
non-nil.
@example
@group
(funcall (-not 'even?) 5)
@result{} t
@end group
@group
(-filter (-not (-partial '< 4)) '(1 2 3 4 5 6 7 8))
@result{} '(1 2 3 4)
@end group
@end example
@end defun
@anchor{-orfn}
@defun -orfn (&rest preds)
Take list of unary predicates @var{preds} and return a unary
predicate with argument x that returns non-nil if at least one of
the @var{preds} returns non-nil on x.
In types: [a -> Bool] -> a -> Bool
@example
@group
(-filter (-orfn 'even? (-partial (-flip '<) 5)) '(1 2 3 4 5 6 7 8 9 10))
@result{} '(1 2 3 4 6 8 10)
@end group
@group
(funcall (-orfn 'stringp 'even?) "foo")
@result{} t
@end group
@end example
@end defun
@anchor{-andfn}
@defun -andfn (&rest preds)
Take list of unary predicates @var{preds} and return a unary
predicate with argument x that returns non-nil if all of the
@var{preds} returns non-nil on x.
In types: [a -> Bool] -> a -> Bool
@example
@group
(funcall (-andfn (-cut < <> 10) 'even?) 6)
@result{} t
@end group
@group
(funcall (-andfn (-cut < <> 10) 'even?) 12)
@result{} nil
@end group
@group
(-filter (-andfn (-not 'even?) (-cut >= 5 <>)) '(1 2 3 4 5 6 7 8 9 10))
@result{} '(1 3 5)
@end group
@end example
@end defun
@anchor{-iteratefn}
@defun -iteratefn (fn n)
Return a function @var{fn} composed @var{n} times with itself.
@var{fn} is a unary function. If you need to use a function of higher
arity, use @code{-applify} (@pxref{-applify}) first to turn it into a unary function.
With n = 0, this acts as identity function.
In types: (a -> a) -> Int -> a -> a.
This function satisfies the following law:
(funcall (-iteratefn fn n) init) = (-last-item (-iterate fn init (1+ n))).
@example
@group
(funcall (-iteratefn (lambda (x) (* x x)) 3) 2)
@result{} 256
@end group
@group
(funcall (-iteratefn '1+ 3) 1)
@result{} 4
@end group
@group
(funcall (-iteratefn 'cdr 3) '(1 2 3 4 5))
@result{} '(4 5)
@end group
@end example
@end defun
@anchor{-fixfn}
@defun -fixfn (fn &optional equal-test halt-test)
Return a function that computes the (least) fixpoint of @var{fn}.
@var{fn} must be a unary function. The returned lambda takes a single
argument, @var{x}, the initial value for the fixpoint iteration. The
iteration halts when either of the following conditions is satisified:
1. Iteration converges to the fixpoint, with equality being
tested using @var{equal-test}. If @var{equal-test} is not specified,
@code{equal} is used. For functions over the floating point
numbers, it may be necessary to provide an appropriate
appoximate comparsion test.
2. @var{halt-test} returns a non-nil value. @var{halt-test} defaults to a
simple counter that returns t after @code{-fixfn-max-iterations},
to guard against infinite iteration. Otherwise, @var{halt-test}
must be a function that accepts a single argument, the
current value of @var{x}, and returns non-nil as long as iteration
should continue. In this way, a more sophisticated
convergence test may be supplied by the caller.
The return value of the lambda is either the fixpoint or, if
iteration halted before converging, a cons with car @code{halted} and
cdr the final output from @var{halt-test}.
In types: (a -> a) -> a -> a.
@example
@group
(funcall (-fixfn 'cos 'approx-equal) 0.7)
@result{} 0.7390851332151607
@end group
@group
(funcall (-fixfn (lambda (x) (expt (+ x 10) 0.25))) 2.0)
@result{} 1.8555845286409378
@end group
@group
(funcall (-fixfn 'sin 'approx-equal) 0.1)
@result{} '(halted . t)
@end group
@end example
@end defun
@anchor{-prodfn}
@defun -prodfn (&rest fns)
Take a list of n functions and return a function that takes a
list of length n, applying i-th function to i-th element of the
input list. Returns a list of length n.
In types (for n=2): ((a -> b), (c -> d)) -> (a, c) -> (b, d)
This function satisfies the following laws:
(-compose (-prodfn f g ...) (-prodfn f' g' ...)) = (-prodfn (-compose f f') (-compose g g') ...)
(-prodfn f g ...) = (-juxt (-compose f (-partial 'nth 0)) (-compose g (-partial 'nth 1)) ...)
(-compose (-prodfn f g ...) (-juxt f' g' ...)) = (-juxt (-compose f f') (-compose g g') ...)
(-compose (-partial 'nth n) (-prod f1 f2 ...)) = (-compose fn (-partial 'nth n))
@example
@group
(funcall (-prodfn '1+ '1- 'int-to-string) '(1 2 3))
@result{} '(2 1 "3")
@end group
@group
(-map (-prodfn '1+ '1-) '((1 2) (3 4) (5 6) (7 8)))
@result{} '((2 1) (4 3) (6 5) (8 7))
@end group
@group
(apply '+ (funcall (-prodfn 'length 'string-to-int) '((1 2 3) "15")))
@result{} 18
@end group
@end example
@end defun
@node Development
@chapter Development
The dash repository is hosted on GitHub:
@uref{https://github.com/magnars/dash.el}
@menu
* Contribute:: How to contribute
* Changes:: List of significant changes by version
* Contributors:: List of contributors
@end menu
@node Contribute
@section Contribute
Yes, please do. Pure functions in the list manipulation realm only,
please. There's a suite of tests in @verb{~dev/examples.el~}, so remember to add
tests for your function, or it might get broken later.
Run the tests with @code{./run-tests.sh}. Create the docs with
@code{./create-docs.sh}. I highly recommend that you install these as a
pre-commit hook, so that the tests are always running and the docs are
always in sync:
@verbatim
cp pre-commit.sh .git/hooks/pre-commit
@end verbatim
Oh, and don't edit @file{README.md} directly, it is auto-generated.
Change @file{readme-template.md} or @file{examples-to-docs.el}
instead. The same goes for the info manual.
@node Changes
@section Changes
@noindent Changes in 2.10:
@itemize
@item
Add @code{-let} destructuring to @code{-if-let} and @code{-when-let}
(Fredrik Bergroth)
@end itemize
@noindent Changes in 2.9:
@itemize
@item
Add @code{-let}, @code{-let*} and @code{-lambda} with destructuring
@item
Add @code{-tree-seq} and @code{-tree-map-nodes}
@item
Add @code{-non-nil}
@item
Add @code{-fix}
@item
Add @code{-fixfn} (dash-functional 1.2)
@item
Add @code{-copy} (Wilfred Hughes)
@end itemize
@noindent Changes in 2.8:
@itemize
@item
Add @code{-butlast}
@end itemize
@noindent Changes in 2.7:
@itemize
@item
@code{-zip} now supports more than two lists (Steve Lamb)
@item
Add @code{-cycle}, @code{-pad}, @code{-annotate}, @code{-zip-fill}
(Steve Lamb)
@item
Add @code{-table}, @code{-table-flat} (finite cartesian product)
@item
Add @code{-flatten-n}
@item
@code{-slice} now supports "step" argument
@item
Add functional combinators @code{-iteratefn}, @code{-prodfn}
@item
Add @code{-replace}, @code{-splice}, @code{-splice-list} which
generalize @code{-replace-at} and @code{-insert-at}
@item
Add @code{-compose}, @code{-iteratefn} and @code{-prodfn}
(dash-functional 1.1)
@end itemize
@noindent Changes in 2.6:
@itemize
@item
Add @code{-is-prefix-p}, @code{-is-suffix-p}, @code{-is-infix-p}
(Matus Goljer)
@item
Add @code{-iterate}, @code{-unfold} (Matus Goljer)
@item
Add @code{-split-on}, @code{-split-when} (Matus Goljer)
@item
Add @code{-find-last-index} (Matus Goljer)
@item
Add @code{-list} (Johan Andersson)
@end itemize
@noindent Changes in 2.5:
@itemize
@item
Add @code{-same-items?} (Johan Andersson)
@item
A few bugfixes
@end itemize
@noindent Changes in 2.4:
@itemize
@item
Add @code{-snoc} (Matus Goljer)
@item
Add @code{-replace-at}, @code{-update-at}, @code{-remove-at}, and
@code{-remove-at-indices} (Matus Goljer)
@end itemize
@noindent Changes in 2.3:
@itemize
@item
Add tree operations (Matus Goljer)
@item
Make font-lock optional
@end itemize
@noindent Changes in 2.2:
@itemize
@item
Add @code{-compose} (Christina Whyte)
@end itemize
@noindent Changes in 2.1:
@itemize
@item
Add indexing operations (Matus Goljer)
@end itemize
@noindent Changes in 2.0:
@itemize
@item
Split out @code{dash-functional.el} (Matus Goljer)
@item
Add @code{-andfn}, @code{-orfn}, @code{-not}, @code{-cut},
@code{-const}, @code{-flip} and @code{-on}. (Matus Goljer)
@item
Fix @code{-min}, @code{-max}, @code{-min-by} and @code{-max-by} (Matus
Goljer)
@end itemize
@noindent Changes in 1.8:
@itemize
@item
Add @code{-first-item} and @code{-last-item} (Wilfred Hughes)
@end itemize
@noindent Changes in 1.7:
@itemize
@item
Add @code{-rotate} (Matus Goljer)
@end itemize
@noindent Changes in 1.6:
@itemize
@item
Add @code{-min}, @code{-max}, @code{-min-by} and @code{-max-by} (Johan
Andersson)
@end itemize
@noindent Changes in 1.5:
@itemize
@item
Add @code{-sum} and @code{-product} (Johan Andersson)
@end itemize
@noindent Changes in 1.4:
@itemize
@item
Add @code{-sort}
@item
Add @code{-reduce-r} (Matus Goljer)
@item
Add @code{-reduce-r-from} (Matus Goljer)
@end itemize
@noindent Changes in 1.3:
@itemize
@item
Add @code{-partition-in-steps}
@item
Add @code{-partition-all-in-steps}
@end itemize
@noindent Changes in 1.2:
@itemize
@item
Add @code{-last} (Matus Goljer)
@item
Add @code{-insert-at} (Emanuel Evans)
@item
Add @code{-when-let} and @code{-if-let} (Emanuel Evans)
@item
Add @code{-when-let*} and @code{-if-let*} (Emanuel Evans)
@item
Some bugfixes
@end itemize
@node Contributors
@section Contributors
@itemize
@item
@uref{https://github.com/Fuco1,Matus Goljer} contributed lots of
features and functions.
@item
@uref{https://github.com/tkf,Takafumi Arakaki} contributed
@code{-group-by}.
@item
@uref{https://github.com/tali713,tali713} is the author of
@code{-applify}.
@item
@uref{https://github.com/vemv,VĂctor M. Valenzuela} contributed
@code{-repeat}.
@item
@uref{https://github.com/nicferrier,Nic Ferrier} contributed
@code{-cons*}.
@item
@uref{https://github.com/Wilfred,Wilfred Hughes} contributed
@code{-slice}, @code{-first-item} and @code{-last-item}.
@item
@uref{https://github.com/shosti,Emanuel Evans} contributed
@code{-if-let}, @code{-when-let} and @code{-insert-at}.
@item
@uref{https://github.com/rejeep,Johan Andersson} contributed
@code{-sum}, @code{-product} and @code{-same-items?}
@item
@uref{https://github.com/kurisuwhyte,Christina Whyte} contributed
@code{-compose}
@item
@uref{https://github.com/steventlamb,Steve Lamb} contributed
@code{-cycle}, @code{-pad}, @code{-annotate}, @code{-zip-fill} and an
n-ary version of @code{-zip}.
@item
@uref{https://github.com/fbergroth,Fredrik Bergroth} made the
@code{-if-let} family use @code{-let} destructuring and improved
script for generating documentation.
@item
@uref{https://github.com/holomorph,Mark Oteiza} contributed the
script to create an info manual.
@item
@uref{https://github.com/wasamasa,Vasilij Schneidermann} contributed
@code{-some}.
@item
@uref{https://github.com/occidens,William West} made @code{-fixfn}
more robust at handling floats.
@end itemize
Thanks!
@node Index
@unnumbered Index
@printindex cp
@bye
|