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
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<head>
<title>Penlight Documentation</title>
<link rel="stylesheet" href="../ldoc_fixed.css" type="text/css" />
</head>
<body>
<div id="container">
<div id="product">
<div id="product_logo"></div>
<div id="product_name"><big><b></b></big></div>
<div id="product_description"></div>
</div> <!-- id="product" -->
<div id="main">
<!-- Menu -->
<div id="navigation">
<br/>
<h1>Penlight</h1>
<ul>
<li><a href="https://github.com/lunarmodules/Penlight">GitHub Project</a></li>
<li><a href="../index.html">Documentation</a></li>
</ul>
<h2>Contents</h2>
<ul>
<li><a href="#Functions">Functions</a></li>
<li><a href="#Tables">Tables</a></li>
<li><a href="#Error_handling">Error handling </a></li>
<li><a href="#File_handling">File handling </a></li>
<li><a href="#OS_functions">OS functions </a></li>
<li><a href="#String_functions">String functions </a></li>
<li><a href="#Functional">Functional </a></li>
<li><a href="#Deprecation">Deprecation </a></li>
</ul>
<h2>Libraries</h2>
<ul class="nowrap">
<li><a href="../libraries/pl.html">pl</a></li>
<li><a href="../libraries/pl.app.html">pl.app</a></li>
<li><a href="../libraries/pl.array2d.html">pl.array2d</a></li>
<li><a href="../libraries/pl.class.html">pl.class</a></li>
<li><a href="../libraries/pl.compat.html">pl.compat</a></li>
<li><a href="../libraries/pl.comprehension.html">pl.comprehension</a></li>
<li><a href="../libraries/pl.config.html">pl.config</a></li>
<li><a href="../libraries/pl.data.html">pl.data</a></li>
<li><a href="../libraries/pl.dir.html">pl.dir</a></li>
<li><a href="../libraries/pl.file.html">pl.file</a></li>
<li><a href="../libraries/pl.func.html">pl.func</a></li>
<li><a href="../libraries/pl.import_into.html">pl.import_into</a></li>
<li><a href="../libraries/pl.input.html">pl.input</a></li>
<li><a href="../libraries/pl.lapp.html">pl.lapp</a></li>
<li><a href="../libraries/pl.lexer.html">pl.lexer</a></li>
<li><a href="../libraries/pl.luabalanced.html">pl.luabalanced</a></li>
<li><a href="../libraries/pl.operator.html">pl.operator</a></li>
<li><a href="../libraries/pl.path.html">pl.path</a></li>
<li><a href="../libraries/pl.permute.html">pl.permute</a></li>
<li><a href="../libraries/pl.pretty.html">pl.pretty</a></li>
<li><a href="../libraries/pl.seq.html">pl.seq</a></li>
<li><a href="../libraries/pl.sip.html">pl.sip</a></li>
<li><a href="../libraries/pl.strict.html">pl.strict</a></li>
<li><a href="../libraries/pl.stringio.html">pl.stringio</a></li>
<li><a href="../libraries/pl.stringx.html">pl.stringx</a></li>
<li><a href="../libraries/pl.tablex.html">pl.tablex</a></li>
<li><a href="../libraries/pl.template.html">pl.template</a></li>
<li><a href="../libraries/pl.test.html">pl.test</a></li>
<li><a href="../libraries/pl.text.html">pl.text</a></li>
<li><a href="../libraries/pl.types.html">pl.types</a></li>
<li><a href="../libraries/pl.url.html">pl.url</a></li>
<li><strong>pl.utils</strong></li>
<li><a href="../libraries/pl.xml.html">pl.xml</a></li>
</ul>
<h2>Classes</h2>
<ul class="nowrap">
<li><a href="../classes/pl.Date.html">pl.Date</a></li>
<li><a href="../classes/pl.List.html">pl.List</a></li>
<li><a href="../classes/pl.Map.html">pl.Map</a></li>
<li><a href="../classes/pl.MultiMap.html">pl.MultiMap</a></li>
<li><a href="../classes/pl.OrderedMap.html">pl.OrderedMap</a></li>
<li><a href="../classes/pl.Set.html">pl.Set</a></li>
</ul>
<h2>Manual</h2>
<ul class="nowrap">
<li><a href="../manual/01-introduction.md.html">Introduction</a></li>
<li><a href="../manual/02-arrays.md.html">Tables and Arrays</a></li>
<li><a href="../manual/03-strings.md.html">Strings. Higher-level operations on strings.</a></li>
<li><a href="../manual/04-paths.md.html">Paths and Directories</a></li>
<li><a href="../manual/05-dates.md.html">Date and Time</a></li>
<li><a href="../manual/06-data.md.html">Data</a></li>
<li><a href="../manual/07-functional.md.html">Functional Programming</a></li>
<li><a href="../manual/08-additional.md.html">Additional Libraries</a></li>
<li><a href="../manual/09-discussion.md.html">Technical Choices</a></li>
</ul>
<h2>Examples</h2>
<ul class="nowrap">
<li><a href="../examples/seesubst.lua.html">seesubst.lua</a></li>
<li><a href="../examples/sipscan.lua.html">sipscan.lua</a></li>
<li><a href="../examples/symbols.lua.html">symbols.lua</a></li>
<li><a href="../examples/test-cmp.lua.html">test-cmp.lua</a></li>
<li><a href="../examples/test-data.lua.html">test-data.lua</a></li>
<li><a href="../examples/test-listcallbacks.lua.html">test-listcallbacks.lua</a></li>
<li><a href="../examples/test-pretty.lua.html">test-pretty.lua</a></li>
<li><a href="../examples/test-symbols.lua.html">test-symbols.lua</a></li>
<li><a href="../examples/testclone.lua.html">testclone.lua</a></li>
<li><a href="../examples/testconfig.lua.html">testconfig.lua</a></li>
<li><a href="../examples/testglobal.lua.html">testglobal.lua</a></li>
<li><a href="../examples/testinputfields.lua.html">testinputfields.lua</a></li>
<li><a href="../examples/testinputfields2.lua.html">testinputfields2.lua</a></li>
<li><a href="../examples/testxml.lua.html">testxml.lua</a></li>
<li><a href="../examples/which.lua.html">which.lua</a></li>
</ul>
</div>
<div id="content">
<h1>Module <code>pl.utils</code></h1>
<p>Generally useful routines.</p>
<p> See <a href="../manual/01-introduction.md.html#Generally_useful_functions_">the Guide</a>.</p>
<p> Dependencies: <a href="../libraries/pl.compat.html#">pl.compat</a>, all exported fields and functions from
<a href="../libraries/pl.compat.html#">pl.compat</a> are also available in this module.</p>
<h2><a href="#Functions">Functions</a></h2>
<table class="function_list">
<tr>
<td class="name" nowrap><a href="#pack">pack (...)</a></td>
<td class="summary">pack an argument list into a table.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#unpack">unpack (t[, i[, j]])</a></td>
<td class="summary">unpack a table and return its contents.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#printf">printf (fmt, ...)</a></td>
<td class="summary">print an arbitrary number of arguments using a format.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#fprintf">fprintf (f, fmt, ...)</a></td>
<td class="summary">write an arbitrary number of arguments to a file using a format.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#import">import (t, T)</a></td>
<td class="summary">take a table and 'inject' it into the local namespace.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#choose">choose (cond, value1, value2)</a></td>
<td class="summary">return either of two values, depending on a condition.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#array_tostring">array_tostring (t[, temp[, tostr]])</a></td>
<td class="summary">convert an array of values to strings.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#is_type">is_type (obj, tp)</a></td>
<td class="summary">is the object of the specified type?</td>
</tr>
<tr>
<td class="name" nowrap><a href="#npairs">npairs (t[, i_start=1[, i_end=t.n or #t[, step=1]]])</a></td>
<td class="summary">an iterator with indices, similar to <a href="https://www.lua.org/manual/5.4/manual.html#pdf-ipairs">ipairs</a>, but with a range.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#kpairs">kpairs (t)</a></td>
<td class="summary">an iterator over all non-integer keys (inverse of <a href="https://www.lua.org/manual/5.4/manual.html#pdf-ipairs">ipairs</a>).</td>
</tr>
</table>
<h2><a href="#Tables">Tables</a></h2>
<table class="function_list">
<tr>
<td class="name" nowrap><a href="#patterns">patterns</a></td>
<td class="summary">Some standard patterns</td>
</tr>
<tr>
<td class="name" nowrap><a href="#stdmt">stdmt</a></td>
<td class="summary">Standard meta-tables as used by other Penlight modules</td>
</tr>
</table>
<h2><a href="#Error_handling">Error handling </a></h2>
<table class="function_list">
<tr>
<td class="name" nowrap><a href="#assert_arg">assert_arg (n, val, tp, verify, msg, lev)</a></td>
<td class="summary">assert that the given argument is in fact of the correct type.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#enum">enum (...)</a></td>
<td class="summary">creates an Enum or constants lookup table for improved error handling.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#function_arg">function_arg (idx, f, msg)</a></td>
<td class="summary">process a function argument.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#assert_string">assert_string (n, val)</a></td>
<td class="summary">assert the common case that the argument is a string.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#on_error">on_error (mode)</a></td>
<td class="summary">control the error strategy used by Penlight.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#raise">raise (err)</a></td>
<td class="summary">used by Penlight functions to return errors.</td>
</tr>
</table>
<h2><a href="#File_handling">File handling </a></h2>
<table class="function_list">
<tr>
<td class="name" nowrap><a href="#readfile">readfile (filename, is_bin)</a></td>
<td class="summary">return the contents of a file as a string</td>
</tr>
<tr>
<td class="name" nowrap><a href="#writefile">writefile (filename, str, is_bin)</a></td>
<td class="summary">write a string to a file</td>
</tr>
<tr>
<td class="name" nowrap><a href="#readlines">readlines (filename)</a></td>
<td class="summary">return the contents of a file as a list of lines</td>
</tr>
</table>
<h2><a href="#OS_functions">OS functions </a></h2>
<table class="function_list">
<tr>
<td class="name" nowrap><a href="#execute">execute ()</a></td>
<td class="summary">Execute a shell command.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#executeex">executeex (cmd, bin)</a></td>
<td class="summary">execute a shell command and return the output.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#quote_arg">quote_arg (argument)</a></td>
<td class="summary">Quote and escape an argument of a command.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#quit">quit ([code], msg, ...)</a></td>
<td class="summary">error out of this program gracefully.</td>
</tr>
</table>
<h2><a href="#String_functions">String functions </a></h2>
<table class="function_list">
<tr>
<td class="name" nowrap><a href="#escape">escape (s)</a></td>
<td class="summary">escape any Lua 'magic' characters in a string</td>
</tr>
<tr>
<td class="name" nowrap><a href="#split">split (s, re, plain, n)</a></td>
<td class="summary">split a string into a list of strings separated by a delimiter.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#splitv">splitv (s, re, plain, n)</a></td>
<td class="summary">split a string into a number of return values.</td>
</tr>
</table>
<h2><a href="#Functional">Functional </a></h2>
<table class="function_list">
<tr>
<td class="name" nowrap><a href="#memoize">memoize (func)</a></td>
<td class="summary">'memoize' a function (cache returned value for next call).</td>
</tr>
<tr>
<td class="name" nowrap><a href="#add_function_factory">add_function_factory (mt, fun)</a></td>
<td class="summary">associate a function factory with a type.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#string_lambda">string_lambda (lf)</a></td>
<td class="summary">an anonymous function as a string.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#bind1">bind1 (fn, p)</a></td>
<td class="summary">bind the first argument of the function to a value.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#bind2">bind2 (fn, p)</a></td>
<td class="summary">bind the second argument of the function to a value.</td>
</tr>
</table>
<h2><a href="#Deprecation">Deprecation </a></h2>
<table class="function_list">
<tr>
<td class="name" nowrap><a href="#set_deprecation_func">set_deprecation_func (func)</a></td>
<td class="summary">Sets a deprecation warning function.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#raise_deprecation">raise_deprecation (opts)</a></td>
<td class="summary">raises a deprecation warning.</td>
</tr>
</table>
<br/>
<br/>
<h2 class="section-header "><a name="Functions"></a>Functions</h2>
<dl class="function">
<dt>
<a name = "pack"></a>
<strong>pack (...)</strong>
</dt>
<dd>
pack an argument list into a table.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">...</span>
any arguments
</li>
</ul>
<h3>Returns:</h3>
<ol>
a table with field <code>n</code> set to the length
</ol>
<h3>See also:</h3>
<ul>
<li><a href="../libraries/pl.compat.html#table.pack">compat.pack</a></li>
<li><a href="../libraries/pl.utils.html#npairs">utils.npairs</a></li>
<li><a href="../libraries/pl.utils.html#unpack">utils.unpack</a></li>
</ul>
</dd>
<dt>
<a name = "unpack"></a>
<strong>unpack (t[, i[, j]])</strong>
</dt>
<dd>
unpack a table and return its contents. </p>
<p> NOTE: this implementation differs from the Lua implementation in the way
that this one DOES honor the <code>n</code> field in the table <code>t</code>, such that it is 'nil-safe'.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">t</span>
table to unpack
</li>
<li><span class="parameter">i</span>
index from which to start unpacking, defaults to 1
(<em>optional</em>)
</li>
<li><span class="parameter">j</span>
index of the last element to unpack, defaults to <code>t.n</code> or else <code>#t</code>
(<em>optional</em>)
</li>
</ul>
<h3>Returns:</h3>
<ol>
multiple return values from the table
</ol>
<h3>See also:</h3>
<ul>
<li><a href="../libraries/pl.compat.html#table.unpack">compat.unpack</a></li>
<li><a href="../libraries/pl.utils.html#pack">utils.pack</a></li>
<li><a href="../libraries/pl.utils.html#npairs">utils.npairs</a></li>
</ul>
<h3>Usage:</h3>
<ul>
<pre class="example"><span class="keyword">local</span> t = <span class="global">table</span>.<span class="function-name">pack</span>(<span class="keyword">nil</span>, <span class="keyword">nil</span>, <span class="keyword">nil</span>, <span class="number">4</span>)
<span class="keyword">local</span> a, b, c, d = <span class="global">table</span>.<span class="function-name">unpack</span>(t) <span class="comment">-- this <a href="../libraries/pl.utils.html#unpack">unpack</a> is NOT nil-safe, so d == nil
</span>
<span class="keyword">local</span> a, b, c, d = utils.<span class="function-name">unpack</span>(t) <span class="comment">-- this is nil-safe, so d == 4</span></pre>
</ul>
</dd>
<dt>
<a name = "printf"></a>
<strong>printf (fmt, ...)</strong>
</dt>
<dd>
print an arbitrary number of arguments using a format.
Output will be sent to <code>stdout</code>.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">fmt</span>
The format (see <a href="https://www.lua.org/manual/5.4/manual.html#pdf-string.format">string.format</a>)
</li>
<li><span class="parameter">...</span>
Extra arguments for format
</li>
</ul>
</dd>
<dt>
<a name = "fprintf"></a>
<strong>fprintf (f, fmt, ...)</strong>
</dt>
<dd>
write an arbitrary number of arguments to a file using a format.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">f</span>
File handle to write to.
</li>
<li><span class="parameter">fmt</span>
The format (see <a href="https://www.lua.org/manual/5.4/manual.html#pdf-string.format">string.format</a>).
</li>
<li><span class="parameter">...</span>
Extra arguments for format
</li>
</ul>
</dd>
<dt>
<a name = "import"></a>
<strong>import (t, T)</strong>
</dt>
<dd>
take a table and 'inject' it into the local namespace.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">t</span>
The table (table), or module name (string), defaults to this <a href="../libraries/pl.utils.html">utils</a> module table
</li>
<li><span class="parameter">T</span>
An optional destination table (defaults to callers environment)
</li>
</ul>
</dd>
<dt>
<a name = "choose"></a>
<strong>choose (cond, value1, value2)</strong>
</dt>
<dd>
return either of two values, depending on a condition.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">cond</span>
A condition
</li>
<li><span class="parameter">value1</span>
Value returned if cond is truthy
</li>
<li><span class="parameter">value2</span>
Value returned if cond is falsy
</li>
</ul>
</dd>
<dt>
<a name = "array_tostring"></a>
<strong>array_tostring (t[, temp[, tostr]])</strong>
</dt>
<dd>
convert an array of values to strings.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">t</span>
a list-like table
</li>
<li><span class="parameter">temp</span>
(table) buffer to use, otherwise allocate
(<em>optional</em>)
</li>
<li><span class="parameter">tostr</span>
custom tostring function, called with (value,index). Defaults to <a href="https://www.lua.org/manual/5.4/manual.html#pdf-tostring">tostring</a>.
(<em>optional</em>)
</li>
</ul>
<h3>Returns:</h3>
<ol>
the converted buffer
</ol>
</dd>
<dt>
<a name = "is_type"></a>
<strong>is_type (obj, tp)</strong>
</dt>
<dd>
is the object of the specified type?
If the type is a string, then use type, otherwise compare with metatable
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">obj</span>
An object to check
</li>
<li><span class="parameter">tp</span>
String of what type it should be
</li>
</ul>
<h3>Returns:</h3>
<ol>
boolean
</ol>
<h3>Usage:</h3>
<ul>
<pre class="example">utils.<span class="function-name">is_type</span>(<span class="string">"hello world"</span>, <span class="string">"string"</span>) <span class="comment">--> true
</span><span class="comment">-- or check metatable
</span><span class="keyword">local</span> my_mt = {}
<span class="keyword">local</span> my_obj = <span class="global">setmetatable</span>(my_obj, my_mt)
utils.<span class="function-name">is_type</span>(my_obj, my_mt) <span class="comment">--> true</span></pre>
</ul>
</dd>
<dt>
<a name = "npairs"></a>
<strong>npairs (t[, i_start=1[, i_end=t.n or #t[, step=1]]])</strong>
</dt>
<dd>
an iterator with indices, similar to <a href="https://www.lua.org/manual/5.4/manual.html#pdf-ipairs">ipairs</a>, but with a range.
This is a nil-safe index based iterator that will return <code>nil</code> when there
is a hole in a list. To be safe ensure that table <code>t.n</code> contains the length.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">t</span>
<span class="types"><a class="type" href="https://www.lua.org/manual/5.4/manual.html#6.6">table</a></span>
the table to iterate over
</li>
<li><span class="parameter">i_start</span>
<span class="types"><span class="type">integer</span></span>
start index
(<em>default</em> 1)
</li>
<li><span class="parameter">i_end</span>
<span class="types"><span class="type">integer</span></span>
end index
(<em>default</em> t.n or #t)
</li>
<li><span class="parameter">step</span>
<span class="types"><span class="type">integer</span></span>
step size
(<em>default</em> 1)
</li>
</ul>
<h3>Returns:</h3>
<ol>
<li>
<span class="types"><span class="type">integer</span></span>
index</li>
<li>
<span class="types"><span class="type">any</span></span>
value at index (which can be <code>nil</code>!)</li>
</ol>
<h3>See also:</h3>
<ul>
<li><a href="../libraries/pl.utils.html#pack">utils.pack</a></li>
<li><a href="../libraries/pl.utils.html#unpack">utils.unpack</a></li>
</ul>
<h3>Usage:</h3>
<ul>
<pre class="example"><span class="keyword">local</span> t = utils.<span class="function-name">pack</span>(<span class="keyword">nil</span>, <span class="number">123</span>, <span class="keyword">nil</span>) <span class="comment">-- adds an <code>n</code> field when packing
</span>
<span class="keyword">for</span> i, v <span class="keyword">in</span> utils.<span class="function-name">npairs</span>(t, <span class="number">2</span>) <span class="keyword">do</span> <span class="comment">-- start at index 2
</span> t[i] = <span class="global">tostring</span>(t[i])
<span class="keyword">end</span>
<span class="comment">-- t = { n = 3, [2] = "123", [3] = "nil" }</span></pre>
</ul>
</dd>
<dt>
<a name = "kpairs"></a>
<strong>kpairs (t)</strong>
</dt>
<dd>
an iterator over all non-integer keys (inverse of <a href="https://www.lua.org/manual/5.4/manual.html#pdf-ipairs">ipairs</a>).
It will skip any key that is an integer number, so negative indices or an
array with holes will not return those either (so it returns slightly less than
'the inverse of <a href="https://www.lua.org/manual/5.4/manual.html#pdf-ipairs">ipairs</a>').</p>
<p> This uses <a href="https://www.lua.org/manual/5.4/manual.html#pdf-pairs">pairs</a> under the hood, so any value that is iterable using <a href="https://www.lua.org/manual/5.4/manual.html#pdf-pairs">pairs</a>
will work with this function.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">t</span>
<span class="types"><a class="type" href="https://www.lua.org/manual/5.4/manual.html#6.6">table</a></span>
the table to iterate over
</li>
</ul>
<h3>Returns:</h3>
<ol>
<li>
<span class="types"><span class="type">key</span></span>
</li>
<li>
<span class="types"><span class="type">value</span></span>
</li>
</ol>
<h3>Usage:</h3>
<ul>
<pre class="example"><span class="keyword">local</span> t = {
<span class="string">"hello"</span>,
<span class="string">"world"</span>,
hello = <span class="string">"hallo"</span>,
world = <span class="string">"Welt"</span>,
}
<span class="keyword">for</span> k, v <span class="keyword">in</span> utils.<span class="function-name">kpairs</span>(t) <span class="keyword">do</span>
<span class="global">print</span>(<span class="string">"German: "</span>, v)
<span class="keyword">end</span>
<span class="comment">-- output;
</span><span class="comment">-- German: hallo
</span><span class="comment">-- German: Welt</span></pre>
</ul>
</dd>
</dl>
<h2 class="section-header "><a name="Tables"></a>Tables</h2>
<dl class="function">
<dt>
<a name = "patterns"></a>
<strong>patterns</strong>
</dt>
<dd>
Some standard patterns
<h3>Fields:</h3>
<ul>
<li><span class="parameter">FLOAT</span>
floating point number
</li>
<li><span class="parameter">INTEGER</span>
integer number
</li>
<li><span class="parameter">IDEN</span>
identifier
</li>
<li><span class="parameter">FILE</span>
file
</li>
</ul>
</dd>
<dt>
<a name = "stdmt"></a>
<strong>stdmt</strong>
</dt>
<dd>
Standard meta-tables as used by other Penlight modules
<h3>Fields:</h3>
<ul>
<li><span class="parameter">List</span>
the List metatable
</li>
<li><span class="parameter">Map</span>
the Map metatable
</li>
<li><span class="parameter">Set</span>
the Set metatable
</li>
<li><span class="parameter">MultiMap</span>
the MultiMap metatable
</li>
</ul>
</dd>
</dl>
<h2 class="section-header "><a name="Error_handling"></a>Error handling </h2>
<dl class="function">
<dt>
<a name = "assert_arg"></a>
<strong>assert_arg (n, val, tp, verify, msg, lev)</strong>
</dt>
<dd>
assert that the given argument is in fact of the correct type.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">n</span>
argument index
</li>
<li><span class="parameter">val</span>
the value
</li>
<li><span class="parameter">tp</span>
the type
</li>
<li><span class="parameter">verify</span>
an optional verification function
</li>
<li><span class="parameter">msg</span>
an optional custom message
</li>
<li><span class="parameter">lev</span>
optional stack position for trace, default 2
</li>
</ul>
<h3>Returns:</h3>
<ol>
the validated value
</ol>
<h3>Raises:</h3>
if <code>val</code> is not the correct type
<h3>Usage:</h3>
<ul>
<pre class="example"><span class="keyword">local</span> param1 = <span class="function-name">assert_arg</span>(<span class="number">1</span>,<span class="string">"hello"</span>,<span class="string">'table'</span>) <span class="comment">--> error: argument 1 expected a 'table', got a 'string'
</span><span class="keyword">local</span> param4 = <span class="function-name">assert_arg</span>(<span class="number">4</span>,<span class="string">'!@#$%^&*'</span>,<span class="string">'string'</span>,path.isdir,<span class="string">'not a directory'</span>)
<span class="comment">--> error: argument 4: '!@#$%^&*' not a directory</span></pre>
</ul>
</dd>
<dt>
<a name = "enum"></a>
<strong>enum (...)</strong>
</dt>
<dd>
creates an Enum or constants lookup table for improved error handling.
This helps prevent magic strings in code by throwing errors for accessing
non-existing values, and/or converting strings/identifiers to other values.</p>
<p> Calling on the object does the same, but returns a soft error; <code>nil + err</code>, if
the call is successful (the key exists), it will return the value.</p>
<p> When calling with varargs or an array the values will be equal to the keys.
The enum object is read-only.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">...</span>
<span class="types"><a class="type" href="https://www.lua.org/manual/5.4/manual.html#6.6">table</a> or <span class="type">vararg</span></span>
the input for the Enum. If varargs or an array then the
values in the Enum will be equal to the names (must be strings), if a hash-table
then values remain (any type), and the keys must be strings.
</li>
</ul>
<h3>Returns:</h3>
<ol>
Enum object (read-only table/object)
</ol>
<h3>Usage:</h3>
<ul>
<li><pre class="example"><span class="comment">-- Enum access at runtime
</span><span class="keyword">local</span> obj = {}
obj.MOVEMENT = utils.<span class="function-name">enum</span>(<span class="string">"FORWARD"</span>, <span class="string">"REVERSE"</span>, <span class="string">"LEFT"</span>, <span class="string">"RIGHT"</span>)
<span class="keyword">if</span> current_movement == obj.MOVEMENT.FORWARD <span class="keyword">then</span>
<span class="comment">-- do something
</span>
<span class="keyword">elseif</span> current_movement == obj.MOVEMENT.REVERES <span class="keyword">then</span>
<span class="comment">-- throws error due to typo 'REVERES', so a silent mistake becomes a hard error
</span> <span class="comment">-- "'REVERES' is not a valid value (expected one of: 'FORWARD', 'REVERSE', 'LEFT', 'RIGHT')"
</span>
<span class="keyword">end</span></pre></li>
<li><pre class="example"><span class="comment">-- standardized error codes
</span><span class="keyword">local</span> obj = {
ERR = utils.<span class="function-name">enum</span> {
NOT_FOUND = <span class="string">"the item was not found"</span>,
OUT_OF_BOUNDS = <span class="string">"the index is outside the allowed range"</span>
},
some_method = <span class="keyword">function</span>(self)
<span class="keyword">return</span> <span class="keyword">nil</span>, self.ERR.OUT_OF_BOUNDS
<span class="keyword">end</span>,
}
<span class="keyword">local</span> result, err = obj:<span class="function-name">some_method</span>()
<span class="keyword">if</span> <span class="keyword">not</span> result <span class="keyword">then</span>
<span class="keyword">if</span> err == obj.ERR.NOT_FOUND <span class="keyword">then</span>
<span class="comment">-- check on error code, not magic strings
</span>
<span class="keyword">else</span>
<span class="comment">-- return the error description, contained in the constant
</span> <span class="keyword">return</span> <span class="keyword">nil</span>, <span class="string">"error: "</span>..err <span class="comment">-- "error: the index is outside the allowed range"
</span> <span class="keyword">end</span>
<span class="keyword">end</span></pre></li>
<li><pre class="example"><span class="comment">-- validating/converting user-input
</span><span class="keyword">local</span> color = <span class="string">"purple"</span>
<span class="keyword">local</span> ansi_colors = utils.<span class="function-name">enum</span> {
black = <span class="number">30</span>,
red = <span class="number">31</span>,
green = <span class="number">32</span>,
}
<span class="keyword">local</span> color_code, err = <span class="function-name">ansi_colors</span>(color) <span class="comment">-- calling on the object, returns the value from the enum
</span><span class="keyword">if</span> <span class="keyword">not</span> color_code <span class="keyword">then</span>
<span class="global">print</span>(<span class="string">"bad 'color', "</span> .. err)
<span class="comment">-- "bad 'color', 'purple' is not a valid value (expected one of: 'black', 'red', 'green')"
</span> <span class="global">os</span>.<span class="function-name">exit</span>(<span class="number">1</span>)
<span class="keyword">end</span></pre></li>
</ul>
</dd>
<dt>
<a name = "function_arg"></a>
<strong>function_arg (idx, f, msg)</strong>
</dt>
<dd>
process a function argument.
This is used throughout Penlight and defines what is meant by a function:
Something that is callable, or an operator string as defined by <code>pl.operator</code>,
such as '>' or '#'. If a function factory has been registered for the type, it will
be called to get the function.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">idx</span>
argument index
</li>
<li><span class="parameter">f</span>
a function, operator string, or callable object
</li>
<li><span class="parameter">msg</span>
optional error message
</li>
</ul>
<h3>Returns:</h3>
<ol>
a callable
</ol>
<h3>Raises:</h3>
if idx is not a number or if f is not callable
</dd>
<dt>
<a name = "assert_string"></a>
<strong>assert_string (n, val)</strong>
</dt>
<dd>
assert the common case that the argument is a string.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">n</span>
argument index
</li>
<li><span class="parameter">val</span>
a value that must be a string
</li>
</ul>
<h3>Returns:</h3>
<ol>
the validated value
</ol>
<h3>Raises:</h3>
val must be a string
<h3>Usage:</h3>
<ul>
<pre class="example"><span class="keyword">local</span> val = <span class="number">42</span>
<span class="keyword">local</span> param2 = utils.<span class="function-name">assert_string</span>(<span class="number">2</span>, val) <span class="comment">--> error: argument 2 expected a 'string', got a 'number'</span></pre>
</ul>
</dd>
<dt>
<a name = "on_error"></a>
<strong>on_error (mode)</strong>
</dt>
<dd>
<p>control the error strategy used by Penlight.
This is a global setting that controls how <a href="../libraries/pl.utils.html#raise">utils.raise</a> behaves:</p>
<ul>
<li>'default': return <code>nil + error</code> (this is the default)</li>
<li>'error': throw a Lua error</li>
<li>'quit': exit the program</li>
</ul>
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">mode</span>
either 'default', 'quit' or 'error'
</li>
</ul>
<h3>See also:</h3>
<ul>
<a href="../libraries/pl.utils.html#raise">utils.raise</a>
</ul>
</dd>
<dt>
<a name = "raise"></a>
<strong>raise (err)</strong>
</dt>
<dd>
used by Penlight functions to return errors. Its global behaviour is controlled
by <a href="../libraries/pl.utils.html#on_error">utils.on_error</a>.
To use this function you MUST use it in conjunction with <code>return</code>, since it might
return <code>nil + error</code>.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">err</span>
the error string.
</li>
</ul>
<h3>See also:</h3>
<ul>
<a href="../libraries/pl.utils.html#on_error">utils.on_error</a>
</ul>
<h3>Usage:</h3>
<ul>
<pre class="example"><span class="keyword">if</span> some_condition <span class="keyword">then</span>
<span class="keyword">return</span> utils.<span class="function-name">raise</span>(<span class="string">"some condition was not met"</span>) <span class="comment">-- MUST use 'return'!
</span><span class="keyword">end</span></pre>
</ul>
</dd>
</dl>
<h2 class="section-header "><a name="File_handling"></a>File handling </h2>
<dl class="function">
<dt>
<a name = "readfile"></a>
<strong>readfile (filename, is_bin)</strong>
</dt>
<dd>
return the contents of a file as a string
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">filename</span>
The file path
</li>
<li><span class="parameter">is_bin</span>
open in binary mode
</li>
</ul>
<h3>Returns:</h3>
<ol>
file contents
</ol>
</dd>
<dt>
<a name = "writefile"></a>
<strong>writefile (filename, str, is_bin)</strong>
</dt>
<dd>
write a string to a file
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">filename</span>
The file path
</li>
<li><span class="parameter">str</span>
The string
</li>
<li><span class="parameter">is_bin</span>
open in binary mode
</li>
</ul>
<h3>Returns:</h3>
<ol>
<li>
true or nil</li>
<li>
error message</li>
</ol>
<h3>Raises:</h3>
error if filename or str aren't strings
</dd>
<dt>
<a name = "readlines"></a>
<strong>readlines (filename)</strong>
</dt>
<dd>
return the contents of a file as a list of lines
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">filename</span>
The file path
</li>
</ul>
<h3>Returns:</h3>
<ol>
file contents as a table
</ol>
<h3>Raises:</h3>
error if filename is not a string
</dd>
</dl>
<h2 class="section-header "><a name="OS_functions"></a>OS functions </h2>
<dl class="function">
<dt>
<a name = "execute"></a>
<strong>execute ()</strong>
</dt>
<dd>
Execute a shell command.
This function is a copy of <a href="../libraries/pl.compat.html#execute">compat.execute</a>.
</dd>
<dt>
<a name = "executeex"></a>
<strong>executeex (cmd, bin)</strong>
</dt>
<dd>
execute a shell command and return the output.
This function redirects the output to tempfiles and returns the content of those files.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">cmd</span>
a shell command
</li>
<li><span class="parameter">bin</span>
boolean, if true, read output as binary file
</li>
</ul>
<h3>Returns:</h3>
<ol>
<li>
true if successful</li>
<li>
actual return code</li>
<li>
stdout output (string)</li>
<li>
errout output (string)</li>
</ol>
</dd>
<dt>
<a name = "quote_arg"></a>
<strong>quote_arg (argument)</strong>
</dt>
<dd>
Quote and escape an argument of a command.
Quotes a single (or list of) argument(s) of a command to be passed
to <a href="https://www.lua.org/manual/5.4/manual.html#pdf-os.execute">os.execute</a>, <a href="../libraries/pl.utils.html#execute">pl.utils.execute</a> or <a href="../libraries/pl.utils.html#executeex">pl.utils.executeex</a>.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">argument</span>
(string or table/list) the argument to quote. If a list then
all arguments in the list will be returned as a single string quoted.
</li>
</ul>
<h3>Returns:</h3>
<ol>
quoted and escaped argument.
</ol>
<h3>Usage:</h3>
<ul>
<pre class="example"><span class="keyword">local</span> options = utils.<span class="function-name">quote_arg</span> {
<span class="string">"-lluacov"</span>,
<span class="string">"-e"</span>,
<span class="string">"utils = print(require('pl.utils')._VERSION"</span>,
}
<span class="comment">-- returns: -lluacov -e 'utils = print(require('\''pl.utils'\'')._VERSION'</span></pre>
</ul>
</dd>
<dt>
<a name = "quit"></a>
<strong>quit ([code], msg, ...)</strong>
</dt>
<dd>
error out of this program gracefully.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">code</span>
The exit code, defaults to -<code>1</code> if omitted
(<em>optional</em>)
</li>
<li><span class="parameter">msg</span>
The exit message will be sent to <code>stderr</code> (will be formatted with the extra parameters)
</li>
<li><span class="parameter">...</span>
extra arguments for message's format'
</li>
</ul>
<h3>See also:</h3>
<ul>
<a href="../libraries/pl.utils.html#fprintf">utils.fprintf</a>
</ul>
<h3>Usage:</h3>
<ul>
<pre class="example">utils.<span class="function-name">quit</span>(-<span class="number">1</span>, <span class="string">"Error '%s' happened"</span>, <span class="string">"42"</span>)
<span class="comment">-- is equivalent to
</span>utils.<span class="function-name">quit</span>(<span class="string">"Error '%s' happened"</span>, <span class="string">"42"</span>) <span class="comment">--> Error '42' happened</span></pre>
</ul>
</dd>
</dl>
<h2 class="section-header "><a name="String_functions"></a>String functions </h2>
<dl class="function">
<dt>
<a name = "escape"></a>
<strong>escape (s)</strong>
</dt>
<dd>
escape any Lua 'magic' characters in a string
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">s</span>
The input string
</li>
</ul>
</dd>
<dt>
<a name = "split"></a>
<strong>split (s, re, plain, n)</strong>
</dt>
<dd>
split a string into a list of strings separated by a delimiter.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">s</span>
The input string
</li>
<li><span class="parameter">re</span>
optional A Lua string pattern; defaults to '%s+'
</li>
<li><span class="parameter">plain</span>
optional If truthy don't use Lua patterns
</li>
<li><span class="parameter">n</span>
optional maximum number of elements (if there are more, the last will remain un-split)
</li>
</ul>
<h3>Returns:</h3>
<ol>
a list-like table
</ol>
<h3>Raises:</h3>
error if s is not a string
<h3>See also:</h3>
<ul>
<a href="../libraries/pl.utils.html#splitv">splitv</a>
</ul>
</dd>
<dt>
<a name = "splitv"></a>
<strong>splitv (s, re, plain, n)</strong>
</dt>
<dd>
split a string into a number of return values.
Identical to <a href="../libraries/pl.utils.html#split">split</a> but returns multiple sub-strings instead of
a single list of sub-strings.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">s</span>
the string
</li>
<li><span class="parameter">re</span>
A Lua string pattern; defaults to '%s+'
</li>
<li><span class="parameter">plain</span>
don't use Lua patterns
</li>
<li><span class="parameter">n</span>
optional maximum number of splits
</li>
</ul>
<h3>Returns:</h3>
<ol>
n values
</ol>
<h3>See also:</h3>
<ul>
<a href="../libraries/pl.utils.html#split">split</a>
</ul>
<h3>Usage:</h3>
<ul>
<pre class="example">first,<span class="global">next</span> = <span class="function-name">splitv</span>(<span class="string">'user=jane=doe'</span>,<span class="string">'='</span>, <span class="keyword">false</span>, <span class="number">2</span>)
<span class="global">assert</span>(first == <span class="string">"user"</span>)
<span class="global">assert</span>(<span class="global">next</span> == <span class="string">"jane=doe"</span>)</pre>
</ul>
</dd>
</dl>
<h2 class="section-header "><a name="Functional"></a>Functional </h2>
<dl class="function">
<dt>
<a name = "memoize"></a>
<strong>memoize (func)</strong>
</dt>
<dd>
'memoize' a function (cache returned value for next call).
This is useful if you have a function which is relatively expensive,
but you don't know in advance what values will be required, so
building a table upfront is wasteful/impossible.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">func</span>
a function that takes exactly one argument (which later serves as the cache key) and returns a single value
</li>
</ul>
<h3>Returns:</h3>
<ol>
a function taking one argument and returning a single value either from the cache or by running the original input function
</ol>
</dd>
<dt>
<a name = "add_function_factory"></a>
<strong>add_function_factory (mt, fun)</strong>
</dt>
<dd>
associate a function factory with a type.
A function factory takes an object of the given type and
returns a function for evaluating it
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">mt</span>
<span class="types"><a class="type" href="https://www.lua.org/manual/5.4/manual.html#6.6">table</a></span>
metatable
</li>
<li><span class="parameter">fun</span>
<span class="types"><span class="type">function</span></span>
a callable that returns a function
</li>
</ul>
</dd>
<dt>
<a name = "string_lambda"></a>
<strong>string_lambda (lf)</strong>
</dt>
<dd>
an anonymous function as a string. This string is either of the form
'|args| expression' or is a function of one argument, '_'
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">lf</span>
function as a string
</li>
</ul>
<h3>Returns:</h3>
<ol>
a function
</ol>
<h3>Usage:</h3>
<ul>
<pre class="example"><span class="function-name">string_lambda</span> <span class="string">'|x|x+1'</span> (<span class="number">2</span>) == <span class="number">3</span>
<span class="function-name">string_lambda</span> <span class="string">'_+1'</span> (<span class="number">2</span>) == <span class="number">3</span></pre>
</ul>
</dd>
<dt>
<a name = "bind1"></a>
<strong>bind1 (fn, p)</strong>
</dt>
<dd>
bind the first argument of the function to a value.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">fn</span>
a function of at least two values (may be an operator string)
</li>
<li><span class="parameter">p</span>
a value
</li>
</ul>
<h3>Returns:</h3>
<ol>
a function such that f(x) is fn(p,x)
</ol>
<h3>Raises:</h3>
same as <a href="../libraries/pl.utils.html#function_arg">function_arg</a>
<h3>See also:</h3>
<ul>
<a href="../libraries/pl.func.html#bind1">func.bind1</a>
</ul>
<h3>Usage:</h3>
<ul>
<pre class="example"><span class="keyword">local</span> <span class="keyword">function</span> <span class="function-name">f</span>(msg, name)
<span class="global">print</span>(msg .. <span class="string">" "</span> .. name)
<span class="keyword">end</span>
<span class="keyword">local</span> hello = utils.<span class="function-name">bind1</span>(f, <span class="string">"Hello"</span>)
<span class="global">print</span>(<span class="function-name">hello</span>(<span class="string">"world"</span>)) <span class="comment">--> "Hello world"
</span><span class="global">print</span>(<span class="function-name">hello</span>(<span class="string">"sunshine"</span>)) <span class="comment">--> "Hello sunshine"</span></pre>
</ul>
</dd>
<dt>
<a name = "bind2"></a>
<strong>bind2 (fn, p)</strong>
</dt>
<dd>
bind the second argument of the function to a value.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">fn</span>
a function of at least two values (may be an operator string)
</li>
<li><span class="parameter">p</span>
a value
</li>
</ul>
<h3>Returns:</h3>
<ol>
a function such that f(x) is fn(x,p)
</ol>
<h3>Raises:</h3>
same as <a href="../libraries/pl.utils.html#function_arg">function_arg</a>
<h3>Usage:</h3>
<ul>
<pre class="example"><span class="keyword">local</span> <span class="keyword">function</span> <span class="function-name">f</span>(a, b, c)
<span class="global">print</span>(a .. <span class="string">" "</span> .. b .. <span class="string">" "</span> .. c)
<span class="keyword">end</span>
<span class="keyword">local</span> hello = utils.<span class="function-name">bind1</span>(f, <span class="string">"world"</span>)
<span class="global">print</span>(<span class="function-name">hello</span>(<span class="string">"Hello"</span>, <span class="string">"!"</span>)) <span class="comment">--> "Hello world !"
</span><span class="global">print</span>(<span class="function-name">hello</span>(<span class="string">"Bye"</span>, <span class="string">"?"</span>)) <span class="comment">--> "Bye world ?"</span></pre>
</ul>
</dd>
</dl>
<h2 class="section-header "><a name="Deprecation"></a>Deprecation </h2>
<dl class="function">
<dt>
<a name = "set_deprecation_func"></a>
<strong>set_deprecation_func (func)</strong>
</dt>
<dd>
Sets a deprecation warning function.
An application can override this function to support proper output of
deprecation warnings. The warnings can be generated from libraries or
functions by calling <a href="../libraries/pl.utils.html#raise_deprecation">utils.raise_deprecation</a>. The default function
will write to the 'warn' system (introduced in Lua 5.4, or the compatibility
function from the <a href="../libraries/pl.compat.html">compat</a> module for earlier versions).</p>
<p> Note: only applications should set/change this function, libraries should not.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">func</span>
a callback with signature: <code>function(msg, trace)</code> both arguments are strings, the latter being optional.
</li>
</ul>
<h3>See also:</h3>
<ul>
<a href="../libraries/pl.utils.html#raise_deprecation">utils.raise_deprecation</a>
</ul>
<h3>Usage:</h3>
<ul>
<pre class="example"><span class="comment">-- write to the Nginx logs with OpenResty
</span>utils.<span class="function-name">set_deprecation_func</span>(<span class="keyword">function</span>(msg, trace)
ngx.<span class="function-name">log</span>(ngx.WARN, msg, (trace <span class="keyword">and</span> (<span class="string">" "</span> .. trace) <span class="keyword">or</span> <span class="keyword">nil</span>))
<span class="keyword">end</span>)
<span class="comment">-- disable deprecation warnings
</span>utils.<span class="function-name">set_deprecation_func</span>()</pre>
</ul>
</dd>
<dt>
<a name = "raise_deprecation"></a>
<strong>raise_deprecation (opts)</strong>
</dt>
<dd>
raises a deprecation warning.
For options see the usage example below.</p>
<p> Note: the <code>opts.deprecated_after</code> field is the last version in which
a feature or option was NOT YET deprecated! Because when writing the code it
is quite often not known in what version the code will land. But the last
released version is usually known.
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">opts</span>
options table
</li>
</ul>
<h3>See also:</h3>
<ul>
<a href="../libraries/pl.utils.html#set_deprecation_func">utils.set_deprecation_func</a>
</ul>
<h3>Usage:</h3>
<ul>
<pre class="example"><span class="global">warn</span>(<span class="string">"@on"</span>) <span class="comment">-- enable Lua warnings, they are usually off by default
</span>
<span class="keyword">function</span> stringx.<span class="function-name">islower</span>(str)
<span class="function-name">raise_deprecation</span> {
source = <span class="string">"Penlight "</span> .. utils._VERSION, <span class="comment">-- optional
</span> message = <span class="string">"function 'islower' was renamed to 'is_lower'"</span>, <span class="comment">-- required
</span> version_removed = <span class="string">"2.0.0"</span>, <span class="comment">-- optional
</span> deprecated_after = <span class="string">"1.2.3"</span>, <span class="comment">-- optional
</span> no_trace = <span class="keyword">true</span>, <span class="comment">-- optional
</span> }
<span class="keyword">return</span> stringx.<span class="function-name">is_lower</span>(str)
<span class="keyword">end</span>
<span class="comment">-- output: "[Penlight 1.9.2] function 'islower' was renamed to 'is_lower' (deprecated after 1.2.3, scheduled for removal in 2.0.0)"</span></pre>
</ul>
</dd>
</dl>
</div> <!-- id="content" -->
</div> <!-- id="main" -->
<div id="about">
<i>generated by <a href="http://github.com/stevedonovan/LDoc">LDoc 1.5.0</a></i>
</div> <!-- id="about" -->
</div> <!-- id="container" -->
</body>
</html>
|