{"id":64,"date":"2015-05-22T12:00:39","date_gmt":"2015-05-22T17:00:39","guid":{"rendered":"http:\/\/www.playwithlua.com\/?p=64"},"modified":"2015-05-19T23:44:35","modified_gmt":"2015-05-20T04:44:35","slug":"my-lua-init-file","status":"publish","type":"post","link":"http:\/\/www.playwithlua.com\/?p=64","title":{"rendered":"My Lua init file"},"content":{"rendered":"<p>I don&#8217;t think it&#8217;ll surprise anyone when I say that I think Lua is a great language. But, it does have a very small standard library. So, there are a few other utilities that I end up including or writing in almost everything I do. Luckily, Lua has a facility for loading them automatically when I open up the REPL.<br \/>\n<!--more--><\/p>\n<h2>How to invoke it<\/h2>\n<p>Lua, when the REPL starts up, reads an environment variable called <code>LUA_INIT<\/code>. If this variable contains Lua code, it runs it; otherwise, if it begins with an &#8220;@&#8221; symbol, it&#8217;ll read it as the name of a file to run. So, in my <code>.bashrc<\/code> I have this line:<\/p>\n<pre>\r\n# Lua init\r\nexport LUA_INIT=\"@$HOME\/.lua\/init.lua\"\r\n<\/pre>\n<h2>My .lua directory<\/h2>\n<p>There are three things I load every time: <a onclick=\"javascript:pageTracker._trackPageview('\/outgoing\/github.com\/kikito\/inspect.lua');\"  href=\"https:\/\/github.com\/kikito\/inspect.lua\">the &#8220;inspect&#8221; library<\/a> and <a onclick=\"javascript:pageTracker._trackPageview('\/outgoing\/github.com\/kikito\/middleclass');\"  href=\"https:\/\/github.com\/kikito\/middleclass\">the &#8220;middleclass&#8221; library<\/a>, both by Enrique Garcia, and <a onclick=\"javascript:pageTracker._trackPageview('\/outgoing\/www.inf.puc-rio.br\/~roberto\/lpeg\/');\"  href=\"http:\/\/www.inf.puc-rio.br\/~roberto\/lpeg\/\">LPeg<\/a>, by Roberto Ierusalimschy. So, here&#8217;s the complete listing of what I have:<\/p>\n<pre>\r\n-rw-rw-r-- 1 randrews randrews  1465 May 17 22:52 init.lua\r\n-rw-rw-r-- 1 randrews randrews  9183 May 10 12:36 inspect.lua\r\n-rwxrwxr-x 1 randrews randrews 52048 May 17 22:49 lpeg.so\r\n-rw-rw-r-- 1 randrews randrews  6116 May 10 11:45 middleclass.lua\r\n-r--r--r-- 1 randrews randrews  6286 May 17 22:49 re.lua\r\n<\/pre>\n<p>(<code>re<\/code> is a regular expression library that comes with LPeg)<\/p>\n<p>The <code>init.lua<\/code> file itself starts out by including the three libraries:<\/p>\n\n<div class=\"my_syntax_box\"><div class=\"my_syntax\"><div class=\"code\"><pre class=\"lua\" style=\"font-family:monospace;\">package<span style=\"color: #66cc66;\">.<\/span>path <span style=\"color: #66cc66;\">=<\/span> package<span style=\"color: #66cc66;\">.<\/span>path <span style=\"color: #66cc66;\">..<\/span> <span style=\"color: #ff6666;\">&quot;;&quot;<\/span> <span style=\"color: #66cc66;\">..<\/span> <span style=\"color: #0000aa;\">os.getenv<\/span><span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #ff6666;\">&quot;HOME&quot;<\/span><span style=\"color: #66cc66;\">&#41;<\/span> <span style=\"color: #66cc66;\">..<\/span> <span style=\"color: #ff6666;\">&quot;\/.lua\/?.lua&quot;<\/span>\npackage<span style=\"color: #66cc66;\">.<\/span>cpath <span style=\"color: #66cc66;\">=<\/span> package<span style=\"color: #66cc66;\">.<\/span>cpath <span style=\"color: #66cc66;\">..<\/span> <span style=\"color: #ff6666;\">&quot;;&quot;<\/span> <span style=\"color: #66cc66;\">..<\/span> <span style=\"color: #0000aa;\">os.getenv<\/span><span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #ff6666;\">&quot;HOME&quot;<\/span><span style=\"color: #66cc66;\">&#41;<\/span> <span style=\"color: #66cc66;\">..<\/span> <span style=\"color: #ff6666;\">&quot;\/.lua\/?.so&quot;<\/span>\ninspect <span style=\"color: #66cc66;\">=<\/span> <span style=\"color: #0000aa;\">require<\/span> <span style=\"color: #ff6666;\">'inspect'<\/span>\nclass <span style=\"color: #66cc66;\">=<\/span> <span style=\"color: #0000aa;\">require<\/span> <span style=\"color: #ff6666;\">'middleclass'<\/span>\nlpeg <span style=\"color: #66cc66;\">=<\/span> <span style=\"color: #0000aa;\">require<\/span> <span style=\"color: #ff6666;\">'lpeg'<\/span>\nre <span style=\"color: #66cc66;\">=<\/span> <span style=\"color: #0000aa;\">require<\/span> <span style=\"color: #ff6666;\">'re'<\/span><\/pre><\/div><\/div><\/div>\n\n<p>Note that I have to stick the <code>.lua<\/code> directory on to both <code>package.path<\/code> and <code>package.cpath<\/code>, because in all likelihood I&#8217;m not running the REPL from the <code>.lua<\/code> directory itself: the normal path element of <code>\"?.lua\"<\/code> won&#8217;t find these.<\/p>\n<h2>Table utilities<\/h2>\n<p>After that, I put in a few things that I think make using tables easier. To begin with, I noticed that most of the standard library functions in the <code>table<\/code> table take a table as the first parameter, meaning it would be very convenient to call them as methods. So, I make a shortcut to create a table that has <code>table<\/code> as its metatable:<\/p>\n\n<div class=\"my_syntax_box\"><div class=\"my_syntax\"><div class=\"code\"><pre class=\"lua\" style=\"font-family:monospace;\"><span style=\"color: #0000aa;\">setmetatable<\/span><span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #0000aa;\">table<\/span><span style=\"color: #66cc66;\">,<\/span>\n             <span style=\"color: #66cc66;\">&#123;<\/span> __call <span style=\"color: #66cc66;\">=<\/span> <span style=\"color: #aa9900; font-weight: bold;\">function<\/span><span style=\"color: #66cc66;\">&#40;<\/span>_<span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #66cc66;\">...<\/span><span style=\"color: #66cc66;\">&#41;<\/span>\n                   <span style=\"color: #aa9900; font-weight: bold;\">return<\/span> <span style=\"color: #0000aa;\">setmetatable<\/span><span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #66cc66;\">&#123;<\/span><span style=\"color: #66cc66;\">...<\/span><span style=\"color: #66cc66;\">&#125;<\/span><span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #0000aa;\">table<\/span><span style=\"color: #66cc66;\">.<\/span>mt<span style=\"color: #66cc66;\">&#41;<\/span> <span style=\"color: #aa9900; font-weight: bold;\">end<\/span> <span style=\"color: #66cc66;\">&#125;<\/span><span style=\"color: #66cc66;\">&#41;<\/span>\n&nbsp;\n<span style=\"color: #0000aa;\">table<\/span><span style=\"color: #66cc66;\">.<\/span>mt <span style=\"color: #66cc66;\">=<\/span> <span style=\"color: #66cc66;\">&#123;<\/span>\n    __index <span style=\"color: #66cc66;\">=<\/span> <span style=\"color: #0000aa;\">table<\/span><span style=\"color: #66cc66;\">,<\/span>\n    __tostring <span style=\"color: #66cc66;\">=<\/span> <span style=\"color: #aa9900; font-weight: bold;\">function<\/span><span style=\"color: #66cc66;\">&#40;<\/span>self<span style=\"color: #66cc66;\">&#41;<\/span>\n        <span style=\"color: #aa9900; font-weight: bold;\">return<\/span> <span style=\"color: #ff6666;\">&quot;{&quot;<\/span> <span style=\"color: #66cc66;\">..<\/span> self<span style=\"color: #66cc66;\">:<\/span>map<span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #0000aa;\">tostring<\/span><span style=\"color: #66cc66;\">&#41;<\/span><span style=\"color: #66cc66;\">:<\/span>concat<span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #ff6666;\">&quot;, &quot;<\/span><span style=\"color: #66cc66;\">&#41;<\/span> <span style=\"color: #66cc66;\">..<\/span> <span style=\"color: #ff6666;\">&quot;}&quot;<\/span>\n    <span style=\"color: #aa9900; font-weight: bold;\">end<\/span>\n<span style=\"color: #66cc66;\">&#125;<\/span><\/pre><\/div><\/div><\/div>\n\n<p>This means I can do something like this:<\/p>\n\n<div class=\"my_syntax_box\"><div class=\"my_syntax\"><div class=\"code\"><pre class=\"lua\" style=\"font-family:monospace;\">t <span style=\"color: #66cc66;\">=<\/span> <span style=\"color: #0000aa;\">table<\/span><span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #cc66cc;\">1<\/span><span style=\"color: #66cc66;\">,<\/span><span style=\"color: #cc66cc;\">2<\/span><span style=\"color: #66cc66;\">,<\/span><span style=\"color: #cc66cc;\">3<\/span><span style=\"color: #66cc66;\">&#41;<\/span>\n<span style=\"color: #0000aa;\">print<\/span><span style=\"color: #66cc66;\">&#40;<\/span>t<span style=\"color: #66cc66;\">&#41;<\/span> <span style=\"color: #808080; font-style: italic;\">-- prints &quot;{1, 2, 3}&quot;<\/span>\nt<span style=\"color: #66cc66;\">:<\/span>insert<span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #cc66cc;\">4<\/span><span style=\"color: #66cc66;\">&#41;<\/span>\ns <span style=\"color: #66cc66;\">=<\/span> t<span style=\"color: #66cc66;\">:<\/span>concat<span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #ff6666;\">&quot;:&quot;<\/span><span style=\"color: #66cc66;\">&#41;<\/span> <span style=\"color: #808080; font-style: italic;\">-- s is now &quot;1:2:3:4&quot;<\/span><\/pre><\/div><\/div><\/div>\n\n<p>It&#8217;s just a convenient shorthand, and having a real <code>tostring<\/code> is very helpful in the REPL.<\/p>\n<h2>Higher-order functions<\/h2>\n<p>You may have noticed that <code>map<\/code> function in there. I also defined a couple handy functions of my own in <code>table<\/code>. That&#8217;s the first, <code>map<\/code>, which transforms a table into another table using a function:<\/p>\n\n<div class=\"my_syntax_box\"><div class=\"my_syntax\"><div class=\"code\"><pre class=\"lua\" style=\"font-family:monospace;\">t <span style=\"color: #66cc66;\">=<\/span> <span style=\"color: #0000aa;\">table<\/span><span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #cc66cc;\">1<\/span><span style=\"color: #66cc66;\">,<\/span><span style=\"color: #cc66cc;\">2<\/span><span style=\"color: #66cc66;\">,<\/span><span style=\"color: #cc66cc;\">3<\/span><span style=\"color: #66cc66;\">,<\/span><span style=\"color: #cc66cc;\">4<\/span><span style=\"color: #66cc66;\">&#41;<\/span>\nt2 <span style=\"color: #66cc66;\">=<\/span> t<span style=\"color: #66cc66;\">:<\/span>map<span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #aa9900; font-weight: bold;\">function<\/span><span style=\"color: #66cc66;\">&#40;<\/span>x<span style=\"color: #66cc66;\">&#41;<\/span> <span style=\"color: #aa9900; font-weight: bold;\">return<\/span> x<span style=\"color: #66cc66;\">*<\/span>x <span style=\"color: #aa9900; font-weight: bold;\">end<\/span><span style=\"color: #66cc66;\">&#41;<\/span>\n<span style=\"color: #0000aa;\">print<\/span><span style=\"color: #66cc66;\">&#40;<\/span>t2<span style=\"color: #66cc66;\">&#41;<\/span> <span style=\"color: #808080; font-style: italic;\">-- prints &quot;{1, 4, 9, 16}&quot;<\/span><\/pre><\/div><\/div><\/div>\n\n<p>It&#8217;s much more terse than a <code>for<\/code> loop, and present in all functional languages. It&#8217;s defined like this:<\/p>\n\n<div class=\"my_syntax_box\"><div class=\"my_syntax\"><div class=\"code\"><pre class=\"lua\" style=\"font-family:monospace;\"><span style=\"color: #aa9900; font-weight: bold;\">function<\/span> <span style=\"color: #0000aa;\">table<\/span><span style=\"color: #66cc66;\">:<\/span>map<span style=\"color: #66cc66;\">&#40;<\/span>fn<span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #66cc66;\">...<\/span><span style=\"color: #66cc66;\">&#41;<\/span>\n    <span style=\"color: #aa9900; font-weight: bold;\">local<\/span> t <span style=\"color: #66cc66;\">=<\/span> <span style=\"color: #0000aa;\">table<\/span><span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #66cc66;\">&#41;<\/span>\n    <span style=\"color: #aa9900; font-weight: bold;\">for<\/span> _<span style=\"color: #66cc66;\">,<\/span> e <span style=\"color: #aa9900; font-weight: bold;\">in<\/span> <span style=\"color: #0000aa;\">ipairs<\/span><span style=\"color: #66cc66;\">&#40;<\/span>self<span style=\"color: #66cc66;\">&#41;<\/span> <span style=\"color: #aa9900; font-weight: bold;\">do<\/span>\n        <span style=\"color: #aa9900; font-weight: bold;\">local<\/span> _<span style=\"color: #66cc66;\">,<\/span> r <span style=\"color: #66cc66;\">=<\/span> fn<span style=\"color: #66cc66;\">&#40;<\/span>e<span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #66cc66;\">...<\/span><span style=\"color: #66cc66;\">&#41;<\/span>\n        t<span style=\"color: #66cc66;\">:<\/span>insert<span style=\"color: #66cc66;\">&#40;<\/span>r<span style=\"color: #66cc66;\">&#41;<\/span>\n    <span style=\"color: #aa9900; font-weight: bold;\">end<\/span>\n    <span style=\"color: #aa9900; font-weight: bold;\">return<\/span> t\n<span style=\"color: #aa9900; font-weight: bold;\">end<\/span><\/pre><\/div><\/div><\/div>\n\n<p>Any extra parameters you pass to <code>map<\/code> are passed through to the callback.<\/p>\n<p>The next function is another simple one, <code>select<\/code>. This builds a new table from all the elements for which a callback function returns true:<\/p>\n\n<div class=\"my_syntax_box\"><div class=\"my_syntax\"><div class=\"code\"><pre class=\"lua\" style=\"font-family:monospace;\">t <span style=\"color: #66cc66;\">=<\/span> <span style=\"color: #0000aa;\">table<\/span><span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #cc66cc;\">1<\/span><span style=\"color: #66cc66;\">,<\/span><span style=\"color: #cc66cc;\">2<\/span><span style=\"color: #66cc66;\">,<\/span><span style=\"color: #cc66cc;\">3<\/span><span style=\"color: #66cc66;\">,<\/span><span style=\"color: #cc66cc;\">4<\/span><span style=\"color: #66cc66;\">,<\/span><span style=\"color: #cc66cc;\">5<\/span><span style=\"color: #66cc66;\">&#41;<\/span>\nodds <span style=\"color: #66cc66;\">=<\/span> t<span style=\"color: #66cc66;\">:<\/span>select<span style=\"color: #66cc66;\">&#40;<\/span> <span style=\"color: #aa9900; font-weight: bold;\">function<\/span><span style=\"color: #66cc66;\">&#40;<\/span>n<span style=\"color: #66cc66;\">&#41;<\/span> <span style=\"color: #aa9900; font-weight: bold;\">return<\/span> n<span style=\"color: #66cc66;\">%<\/span><span style=\"color: #cc66cc;\">2<\/span> <span style=\"color: #66cc66;\">==<\/span> <span style=\"color: #cc66cc;\">1<\/span> <span style=\"color: #aa9900; font-weight: bold;\">end<\/span> <span style=\"color: #66cc66;\">&#41;<\/span><\/pre><\/div><\/div><\/div>\n\n<p>This is another straightforward but very handy function that can make a lot of things more concise:<\/p>\n\n<div class=\"my_syntax_box\"><div class=\"my_syntax\"><div class=\"code\"><pre class=\"lua\" style=\"font-family:monospace;\"><span style=\"color: #aa9900; font-weight: bold;\">function<\/span> <span style=\"color: #0000aa;\">table<\/span><span style=\"color: #66cc66;\">:<\/span>select<span style=\"color: #66cc66;\">&#40;<\/span>query<span style=\"color: #66cc66;\">&#41;<\/span>\n    <span style=\"color: #aa9900; font-weight: bold;\">local<\/span> t <span style=\"color: #66cc66;\">=<\/span> <span style=\"color: #0000aa;\">table<\/span><span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #66cc66;\">&#41;<\/span>\n&nbsp;\n    self<span style=\"color: #66cc66;\">:<\/span>map<span style=\"color: #66cc66;\">&#40;<\/span> <span style=\"color: #aa9900; font-weight: bold;\">function<\/span><span style=\"color: #66cc66;\">&#40;<\/span>el<span style=\"color: #66cc66;\">&#41;<\/span>\n            <span style=\"color: #aa9900; font-weight: bold;\">if<\/span> query<span style=\"color: #66cc66;\">&#40;<\/span>el<span style=\"color: #66cc66;\">&#41;<\/span> <span style=\"color: #aa9900; font-weight: bold;\">then<\/span> t<span style=\"color: #66cc66;\">:<\/span>insert<span style=\"color: #66cc66;\">&#40;<\/span>el<span style=\"color: #66cc66;\">&#41;<\/span> <span style=\"color: #aa9900; font-weight: bold;\">end<\/span>\n    <span style=\"color: #aa9900; font-weight: bold;\">end<\/span><span style=\"color: #66cc66;\">&#41;<\/span>\n&nbsp;\n    <span style=\"color: #aa9900; font-weight: bold;\">return<\/span> t\n<span style=\"color: #aa9900; font-weight: bold;\">end<\/span><\/pre><\/div><\/div><\/div>\n\n<h2>Reduce<\/h2>\n<p>The final utility I put in is <code>reduce<\/code>. This reduces a table down to one value, given a function (and an optional initial value). For example, here&#8217;s how to use <code>reduce<\/code> to find the maximum value of a table of numbers:<\/p>\n\n<div class=\"my_syntax_box\"><div class=\"my_syntax\"><div class=\"code\"><pre class=\"lua\" style=\"font-family:monospace;\">t <span style=\"color: #66cc66;\">=<\/span> <span style=\"color: #0000aa;\">table<\/span><span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #cc66cc;\">1<\/span><span style=\"color: #66cc66;\">,<\/span><span style=\"color: #cc66cc;\">7<\/span><span style=\"color: #66cc66;\">,<\/span><span style=\"color: #cc66cc;\">3<\/span><span style=\"color: #66cc66;\">,<\/span><span style=\"color: #cc66cc;\">19<\/span><span style=\"color: #66cc66;\">,<\/span><span style=\"color: #cc66cc;\">4<\/span><span style=\"color: #66cc66;\">&#41;<\/span>\n<span style=\"color: #0000aa;\">max<\/span> <span style=\"color: #66cc66;\">=<\/span> t<span style=\"color: #66cc66;\">:<\/span>reduce<span style=\"color: #66cc66;\">&#40;<\/span> <span style=\"color: #aa9900; font-weight: bold;\">function<\/span><span style=\"color: #66cc66;\">&#40;<\/span>a<span style=\"color: #66cc66;\">,<\/span>b<span style=\"color: #66cc66;\">&#41;<\/span>\n    <span style=\"color: #aa9900; font-weight: bold;\">if<\/span> a <span style=\"color: #66cc66;\">&gt;<\/span> b <span style=\"color: #aa9900; font-weight: bold;\">then<\/span> <span style=\"color: #aa9900; font-weight: bold;\">return<\/span> a\n    <span style=\"color: #aa9900; font-weight: bold;\">else<\/span> <span style=\"color: #aa9900; font-weight: bold;\">return<\/span> b\n<span style=\"color: #aa9900; font-weight: bold;\">end<\/span> <span style=\"color: #66cc66;\">&#41;<\/span><\/pre><\/div><\/div><\/div>\n\n<p>This will call the function on the first two elements, 1 and 7, returning 7. Then it will call it with the result of the first call and the third element, 3. Then the result of that and the next element, and so on:<\/p>\n<pre>\r\n1, 7  ==> 7\r\n7, 3  ==> 7\r\n7, 19 ==> 19\r\n19, 4 ==> 19\r\n<\/pre>\n<p>You can optionally pass in a second argument that will be used as the initial value; if you don&#8217;t then the first thing in the array is the initial value:<\/p>\n\n<div class=\"my_syntax_box\"><div class=\"my_syntax\"><div class=\"code\"><pre class=\"lua\" style=\"font-family:monospace;\"><span style=\"color: #aa9900; font-weight: bold;\">function<\/span> <span style=\"color: #0000aa;\">table<\/span><span style=\"color: #66cc66;\">:<\/span>reduce<span style=\"color: #66cc66;\">&#40;<\/span>fn<span style=\"color: #66cc66;\">,<\/span> init<span style=\"color: #66cc66;\">&#41;<\/span>\n    <span style=\"color: #aa9900; font-weight: bold;\">local<\/span> i <span style=\"color: #66cc66;\">=<\/span> <span style=\"color: #cc66cc;\">1<\/span>\n    <span style=\"color: #aa9900; font-weight: bold;\">local<\/span> accum <span style=\"color: #66cc66;\">=<\/span> init\n    <span style=\"color: #aa9900; font-weight: bold;\">if<\/span> init <span style=\"color: #66cc66;\">==<\/span> <span style=\"color: #aa9900;\">nil<\/span> <span style=\"color: #aa9900; font-weight: bold;\">then<\/span>\n        accum <span style=\"color: #66cc66;\">=<\/span> self<span style=\"color: #66cc66;\">&#91;<\/span><span style=\"color: #cc66cc;\">1<\/span><span style=\"color: #66cc66;\">&#93;<\/span>\n        i <span style=\"color: #66cc66;\">=<\/span> <span style=\"color: #cc66cc;\">2<\/span>\n    <span style=\"color: #aa9900; font-weight: bold;\">end<\/span>\n&nbsp;\n    <span style=\"color: #aa9900; font-weight: bold;\">while<\/span> i <span style=\"color: #66cc66;\">&lt;=<\/span> <span style=\"color: #66cc66;\">#<\/span>self <span style=\"color: #aa9900; font-weight: bold;\">do<\/span>\n        accum <span style=\"color: #66cc66;\">=<\/span> fn<span style=\"color: #66cc66;\">&#40;<\/span>accum<span style=\"color: #66cc66;\">,<\/span> self<span style=\"color: #66cc66;\">&#91;<\/span>i<span style=\"color: #66cc66;\">&#93;<\/span><span style=\"color: #66cc66;\">&#41;<\/span>\n        i <span style=\"color: #66cc66;\">=<\/span> i <span style=\"color: #66cc66;\">+<\/span> <span style=\"color: #cc66cc;\">1<\/span>\n    <span style=\"color: #aa9900; font-weight: bold;\">end<\/span>\n&nbsp;\n    <span style=\"color: #aa9900; font-weight: bold;\">return<\/span> accum\n<span style=\"color: #aa9900; font-weight: bold;\">end<\/span><\/pre><\/div><\/div><\/div>\n\n<p>There are a few short functions dealing with numeric arrays that are just calls to <code>reduce<\/code>:<\/p>\n\n<div class=\"my_syntax_box\"><div class=\"my_syntax\"><div class=\"code\"><pre class=\"lua\" style=\"font-family:monospace;\"><span style=\"color: #aa9900; font-weight: bold;\">function<\/span> <span style=\"color: #0000aa;\">table<\/span><span style=\"color: #66cc66;\">:<\/span>sum<span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #66cc66;\">&#41;<\/span>\n    <span style=\"color: #aa9900; font-weight: bold;\">return<\/span> self<span style=\"color: #66cc66;\">:<\/span>reduce<span style=\"color: #66cc66;\">&#40;<\/span> <span style=\"color: #aa9900; font-weight: bold;\">function<\/span><span style=\"color: #66cc66;\">&#40;<\/span>a<span style=\"color: #66cc66;\">,<\/span>b<span style=\"color: #66cc66;\">&#41;<\/span> <span style=\"color: #aa9900; font-weight: bold;\">return<\/span> a<span style=\"color: #66cc66;\">+<\/span>b <span style=\"color: #aa9900; font-weight: bold;\">end<\/span><span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #cc66cc;\">0<\/span> <span style=\"color: #66cc66;\">&#41;<\/span>\n<span style=\"color: #aa9900; font-weight: bold;\">end<\/span>\n&nbsp;\n<span style=\"color: #aa9900; font-weight: bold;\">function<\/span> <span style=\"color: #0000aa;\">table<\/span><span style=\"color: #66cc66;\">:<\/span><span style=\"color: #0000aa;\">max<\/span><span style=\"color: #66cc66;\">&#40;<\/span>comp<span style=\"color: #66cc66;\">&#41;<\/span>\n    <span style=\"color: #aa9900; font-weight: bold;\">return<\/span> self<span style=\"color: #66cc66;\">:<\/span>reduce<span style=\"color: #66cc66;\">&#40;<\/span> <span style=\"color: #aa9900; font-weight: bold;\">function<\/span><span style=\"color: #66cc66;\">&#40;<\/span>a<span style=\"color: #66cc66;\">,<\/span>b<span style=\"color: #66cc66;\">&#41;<\/span>\n            <span style=\"color: #aa9900; font-weight: bold;\">if<\/span> a <span style=\"color: #66cc66;\">&gt;<\/span> b <span style=\"color: #aa9900; font-weight: bold;\">then<\/span> <span style=\"color: #aa9900; font-weight: bold;\">return<\/span> a\n            <span style=\"color: #aa9900; font-weight: bold;\">else<\/span> <span style=\"color: #aa9900; font-weight: bold;\">return<\/span> b <span style=\"color: #aa9900; font-weight: bold;\">end<\/span> <span style=\"color: #aa9900; font-weight: bold;\">end<\/span> <span style=\"color: #66cc66;\">&#41;<\/span>\n<span style=\"color: #aa9900; font-weight: bold;\">end<\/span>\n&nbsp;\n<span style=\"color: #aa9900; font-weight: bold;\">function<\/span> <span style=\"color: #0000aa;\">table<\/span><span style=\"color: #66cc66;\">:<\/span><span style=\"color: #0000aa;\">min<\/span><span style=\"color: #66cc66;\">&#40;<\/span>comp<span style=\"color: #66cc66;\">&#41;<\/span>\n    <span style=\"color: #aa9900; font-weight: bold;\">return<\/span> self<span style=\"color: #66cc66;\">:<\/span>reduce<span style=\"color: #66cc66;\">&#40;<\/span> <span style=\"color: #aa9900; font-weight: bold;\">function<\/span><span style=\"color: #66cc66;\">&#40;<\/span>a<span style=\"color: #66cc66;\">,<\/span>b<span style=\"color: #66cc66;\">&#41;<\/span>\n            <span style=\"color: #aa9900; font-weight: bold;\">if<\/span> a <span style=\"color: #66cc66;\">&lt;<\/span> b <span style=\"color: #aa9900; font-weight: bold;\">then<\/span> <span style=\"color: #aa9900; font-weight: bold;\">return<\/span> a\n            <span style=\"color: #aa9900; font-weight: bold;\">else<\/span> <span style=\"color: #aa9900; font-weight: bold;\">return<\/span> b <span style=\"color: #aa9900; font-weight: bold;\">end<\/span> <span style=\"color: #aa9900; font-weight: bold;\">end<\/span> <span style=\"color: #66cc66;\">&#41;<\/span>\n<span style=\"color: #aa9900; font-weight: bold;\">end<\/span><\/pre><\/div><\/div><\/div>\n\n<p>Notice that I have to pass an initial value to the call in <code>sum<\/code>: this is because without it the sum of an empty table would be <code>nil<\/code>, and it&#8217;s more useful for it to be zero.<\/p>\n<h2>The code<\/h2>\n<p>As always, the code is available <a onclick=\"javascript:pageTracker._trackPageview('\/outgoing\/github.com\/randrews\/dotlua');\"  href=\"https:\/\/github.com\/randrews\/dotlua\">on Github<\/a>. To use it, you&#8217;ll need to build your own copy of LPeg since that binary is for my system (you can obtain it on <a onclick=\"javascript:pageTracker._trackPageview('\/outgoing\/www.inf.puc-rio.br\/~roberto\/lpeg\/');\"  href=\"http:\/\/www.inf.puc-rio.br\/~roberto\/lpeg\/\">their page<\/a>), and set the <code>LUA_INIT<\/code> environment variable, which is different depending on what system you&#8217;re on: for Unix (or Mac) you should put it in your login script, which is probably <code>.bashrc<\/code>, <code>.profile<\/code>, or something like that. For Windows you&#8217;ll have to go through the control panel.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I don&#8217;t think it&#8217;ll surprise anyone when I say that I think Lua is a great language. But, it does have a very small standard library. So, there are a few other utilities that I end up including or writing in almost everything I do. Luckily, Lua has a facility for loading them automatically when [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"http:\/\/www.playwithlua.com\/index.php?rest_route=\/wp\/v2\/posts\/64"}],"collection":[{"href":"http:\/\/www.playwithlua.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.playwithlua.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.playwithlua.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.playwithlua.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=64"}],"version-history":[{"count":0,"href":"http:\/\/www.playwithlua.com\/index.php?rest_route=\/wp\/v2\/posts\/64\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.playwithlua.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=64"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.playwithlua.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=64"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.playwithlua.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=64"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}