{"id":40,"date":"2011-07-24T00:35:23","date_gmt":"2011-07-24T05:35:23","guid":{"rendered":"http:\/\/www.playwithlua.com\/?p=40"},"modified":"2020-05-22T13:38:30","modified_gmt":"2020-05-22T18:38:30","slug":"a-short-trick-transparent-containers","status":"publish","type":"post","link":"http:\/\/www.playwithlua.com\/?p=40","title":{"rendered":"A short trick: transparent containers"},"content":{"rendered":"<p>I thought of this today, and I thought it might make an interesting short post. One of the small annoyances of Lua is that it doesn&#8217;t have a lot of convenient support for arrays. You can implement arrays with tables, of course, but they&#8217;re still tables: they don&#8217;t have default metatables (so you can&#8217;t do things like <code>join<\/code>), they don&#8217;t pretty-print (so it&#8217;s a hassle to see what you have), and there&#8217;s still only one iterator (using <code>ipairs<\/code> with a for loop). You can do it but it&#8217;s not exactly convenient. So I started thinking about how exactly I&#8217;d add all that stuff.<br \/>\n<!--more--><\/p>\n<h2>Creating an array<\/h2>\n<p>First, let&#8217;s recognize that not all tables are going to have this behavior, and that they actually shouldn&#8217;t. An array is a CS concept, an abstract data structure, that just happens to be a primitive in most languages and implementable with a table in Lua. We want to make a way to create arrays, and some handy default behaviors for arrays, and that&#8217;s it.<\/p>\n<p>So what I did was make a table to be used as the metatable for all arrays, called <code>_array<\/code>. Then I made an <code>array<\/code> function, global, that would make a table and set that as its metatable. Then, to save a little typing, I aliased that function to <code>A<\/code>:<\/p>\n\n<div class=\"my_syntax_box\"><div class=\"my_syntax\"><div class=\"code\"><pre class=\"lua\" style=\"font-family:monospace;\">_array <span style=\"color: #66cc66;\">=<\/span> <span style=\"color: #66cc66;\">&#123;<\/span>\n   <span style=\"color: #808080; font-style: italic;\">-- Functions go here<\/span>\n<span style=\"color: #66cc66;\">&#125;<\/span>\n&nbsp;\n<span style=\"color: #aa9900; font-weight: bold;\">function<\/span> array<span style=\"color: #66cc66;\">&#40;<\/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> _array<span style=\"color: #66cc66;\">&#41;<\/span>\n<span style=\"color: #aa9900; font-weight: bold;\">end<\/span>\n&nbsp;\nA <span style=\"color: #66cc66;\">=<\/span> array<\/pre><\/div><\/div><\/div>\n\n<p>So now I can make an array by saying <code>array(1, 4, 9)<\/code>, or more likely (because short is good for simple concepts), <code>A(1, 4, 9)<\/code>.<\/p>\n<h2>Pretty-printing<\/h2>\n<p>Making an array and then seeing <code>table: 0x100116290<\/code> when I print it out is a little less than useful. It&#8217;s really helpful for exploratory programming (not to mention debugging) if I don&#8217;t have to write a for loop by hand whenever I want to inspect an array. So, the first thing I&#8217;m going to add is a <code>__tostring<\/code> metamethod. This is so easy to do using the Lua standard library that I&#8217;m surprised I even have to, that it&#8217;s not built in:<\/p>\n\n<div class=\"my_syntax_box\"><div class=\"my_syntax\"><div class=\"code\"><pre class=\"lua\" style=\"font-family:monospace;\">__tostring <span style=\"color: #66cc66;\">=<\/span>\n   <span style=\"color: #aa9900; font-weight: bold;\">function<\/span><span style=\"color: #66cc66;\">&#40;<\/span>tbl<span style=\"color: #66cc66;\">&#41;<\/span>\n      <span style=\"color: #aa9900; font-weight: bold;\">local<\/span> strings <span style=\"color: #66cc66;\">=<\/span> <span style=\"color: #66cc66;\">&#123;<\/span><span style=\"color: #66cc66;\">&#125;<\/span>\n      <span style=\"color: #aa9900; font-weight: bold;\">for<\/span> k<span style=\"color: #66cc66;\">,<\/span> v <span style=\"color: #aa9900; font-weight: bold;\">in<\/span> <span style=\"color: #0000aa;\">ipairs<\/span><span style=\"color: #66cc66;\">&#40;<\/span>tbl<span style=\"color: #66cc66;\">&#41;<\/span> <span style=\"color: #aa9900; font-weight: bold;\">do<\/span> strings<span style=\"color: #66cc66;\">&#91;<\/span>k<span style=\"color: #66cc66;\">&#93;<\/span> <span style=\"color: #66cc66;\">=<\/span> <span style=\"color: #0000aa;\">tostring<\/span><span style=\"color: #66cc66;\">&#40;<\/span>v<span style=\"color: #66cc66;\">&#41;<\/span> <span style=\"color: #aa9900; font-weight: bold;\">end<\/span>\n      <span style=\"color: #aa9900; font-weight: bold;\">return<\/span> <span style=\"color: #ff6666;\">'['<\/span> <span style=\"color: #66cc66;\">..<\/span> <span style=\"color: #0000aa;\">table.concat<\/span><span style=\"color: #66cc66;\">&#40;<\/span>strings<span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #ff6666;\">', '<\/span><span style=\"color: #66cc66;\">&#41;<\/span> <span style=\"color: #66cc66;\">..<\/span> <span style=\"color: #ff6666;\">']'<\/span>\n   <span style=\"color: #aa9900; font-weight: bold;\">end<\/span><\/pre><\/div><\/div><\/div>\n\n<p>This metamethod gets called whenever something needs to become a string; notably, because <code>tostring<\/code> told it to. We feed everything in the array into <code>tostring<\/code>, then join it all up with commas and put it in brackets. This function will get a bit smaller shortly though.<\/p>\n<h2>Iteration<\/h2>\n<p>There are two things I want to provide for iteration. First, I want to have an <code>each<\/code> function that I pass a function to (along with some arguments) and it will invoke it on every element in the array. Pretty standard stuff:<\/p>\n\n<div class=\"my_syntax_box\"><div class=\"my_syntax\"><div class=\"code\"><pre class=\"lua\" style=\"font-family:monospace;\">   each <span style=\"color: #66cc66;\">=<\/span>\n      <span style=\"color: #aa9900; font-weight: bold;\">function<\/span><span style=\"color: #66cc66;\">&#40;<\/span>list<span style=\"color: #66cc66;\">,<\/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> r <span style=\"color: #66cc66;\">=<\/span> array<span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #66cc66;\">&#41;<\/span>\n         <span style=\"color: #aa9900; font-weight: bold;\">for<\/span> k<span style=\"color: #66cc66;\">,<\/span>v <span style=\"color: #aa9900; font-weight: bold;\">in<\/span> <span style=\"color: #0000aa;\">pairs<\/span><span style=\"color: #66cc66;\">&#40;<\/span>list<span style=\"color: #66cc66;\">&#41;<\/span> <span style=\"color: #aa9900; font-weight: bold;\">do<\/span>\n            r<span style=\"color: #66cc66;\">&#91;<\/span>k<span style=\"color: #66cc66;\">&#93;<\/span> <span style=\"color: #66cc66;\">=<\/span> fn<span style=\"color: #66cc66;\">&#40;<\/span>v<span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #66cc66;\">...<\/span><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> r\n      <span style=\"color: #aa9900; font-weight: bold;\">end<\/span><\/pre><\/div><\/div><\/div>\n\n<p>This returns another array containing all the return values of the invocations, so I can do stuff like this:<\/p>\n\n<div class=\"my_syntax_box\"><div class=\"my_syntax\"><div class=\"code\"><pre class=\"lua\" style=\"font-family:monospace;\">A<span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #cc66cc;\">1<\/span><span style=\"color: #66cc66;\">,<\/span><span style=\"color: #cc66cc;\">4<\/span><span style=\"color: #66cc66;\">,<\/span><span style=\"color: #cc66cc;\">9<\/span><span style=\"color: #66cc66;\">&#41;<\/span><span style=\"color: #66cc66;\">:<\/span>each<span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #aa9900; font-weight: bold;\">function<\/span><span style=\"color: #66cc66;\">&#40;<\/span>v<span style=\"color: #66cc66;\">&#41;<\/span> <span style=\"color: #aa9900; font-weight: bold;\">return<\/span> v<span style=\"color: #66cc66;\">*<\/span>v <span style=\"color: #aa9900; font-weight: bold;\">end<\/span><span style=\"color: #66cc66;\">&#41;<\/span> <span style=\"color: #808080; font-style: italic;\">-- ==&amp;gt; [1, 16, 81]<\/span><\/pre><\/div><\/div><\/div>\n\n<p>Since it&#8217;s created with <code>array<\/code>, I can chain them together; the return value will also support <code>each<\/code>. And of course, now that we can do this, we can refactor <code>__tostring<\/code> to this:<\/p>\n\n<div class=\"my_syntax_box\"><div class=\"my_syntax\"><div class=\"code\"><pre class=\"lua\" style=\"font-family:monospace;\">   __tostring <span style=\"color: #66cc66;\">=<\/span>\n      <span style=\"color: #aa9900; font-weight: bold;\">function<\/span><span style=\"color: #66cc66;\">&#40;<\/span>tbl<span style=\"color: #66cc66;\">&#41;<\/span>\n         <span style=\"color: #aa9900; font-weight: bold;\">local<\/span> strings <span style=\"color: #66cc66;\">=<\/span> tbl<span style=\"color: #66cc66;\">:<\/span>each<span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #0000aa;\">tostring<\/span><span style=\"color: #66cc66;\">&#41;<\/span>\n         <span style=\"color: #aa9900; font-weight: bold;\">return<\/span> <span style=\"color: #ff6666;\">'['<\/span> <span style=\"color: #66cc66;\">..<\/span> <span style=\"color: #0000aa;\">table.concat<\/span><span style=\"color: #66cc66;\">&#40;<\/span>strings<span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #ff6666;\">', '<\/span><span style=\"color: #66cc66;\">&#41;<\/span> <span style=\"color: #66cc66;\">..<\/span> <span style=\"color: #ff6666;\">']'<\/span>\n      <span style=\"color: #aa9900; font-weight: bold;\">end<\/span><\/pre><\/div><\/div><\/div>\n\n<h2>Transparency<\/h2>\n<p>The other kind of iteration I want to do is the neat trick: I want to be able to treat an array of objects just like an object. Suppose I have a bunch of objects, like strings, and I want to invoke a string method on them, like this one I shamelessly stole from Rails:<\/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;\">string<\/span><span style=\"color: #66cc66;\">.<\/span>ordinalize<span style=\"color: #66cc66;\">&#40;<\/span>str<span style=\"color: #66cc66;\">&#41;<\/span>\n   <span style=\"color: #aa9900; font-weight: bold;\">local<\/span> num <span style=\"color: #66cc66;\">=<\/span> <span style=\"color: #0000aa;\">tonumber<\/span><span style=\"color: #66cc66;\">&#40;<\/span>str<span style=\"color: #66cc66;\">&#41;<\/span>\n   <span style=\"color: #aa9900; font-weight: bold;\">local<\/span> endings <span style=\"color: #66cc66;\">=<\/span> <span style=\"color: #66cc66;\">&#123;<\/span><span style=\"color: #ff6666;\">&quot;st&quot;<\/span><span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #ff6666;\">&quot;nd&quot;<\/span><span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #ff6666;\">&quot;rd&quot;<\/span><span style=\"color: #66cc66;\">&#125;<\/span>\n   <span style=\"color: #aa9900; font-weight: bold;\">if<\/span> num &amp;gt<span style=\"color: #66cc66;\">;=<\/span> <span style=\"color: #cc66cc;\">11<\/span> <span style=\"color: #aa9900; font-weight: bold;\">and<\/span> num &amp;lt<span style=\"color: #66cc66;\">;=<\/span> <span style=\"color: #cc66cc;\">13<\/span> <span style=\"color: #aa9900; font-weight: bold;\">then<\/span>\n\t  <span style=\"color: #aa9900; font-weight: bold;\">return<\/span> num <span style=\"color: #66cc66;\">..<\/span> <span style=\"color: #ff6666;\">&quot;th&quot;<\/span>\n   <span style=\"color: #aa9900; font-weight: bold;\">elseif<\/span> num <span style=\"color: #66cc66;\">%<\/span> <span style=\"color: #cc66cc;\">10<\/span> &amp;gt<span style=\"color: #66cc66;\">;<\/span> <span style=\"color: #cc66cc;\">0<\/span> <span style=\"color: #aa9900; font-weight: bold;\">and<\/span> num <span style=\"color: #66cc66;\">%<\/span> <span style=\"color: #cc66cc;\">10<\/span> &amp;lt<span style=\"color: #66cc66;\">;=<\/span> <span style=\"color: #66cc66;\">#<\/span>endings <span style=\"color: #aa9900; font-weight: bold;\">then<\/span>\n\t  <span style=\"color: #aa9900; font-weight: bold;\">return<\/span> num <span style=\"color: #66cc66;\">..<\/span> endings<span style=\"color: #66cc66;\">&#91;<\/span>num <span style=\"color: #66cc66;\">%<\/span> <span style=\"color: #cc66cc;\">10<\/span><span style=\"color: #66cc66;\">&#93;<\/span>\n   <span style=\"color: #aa9900; font-weight: bold;\">else<\/span>\n\t  <span style=\"color: #aa9900; font-weight: bold;\">return<\/span> num <span style=\"color: #66cc66;\">..<\/span> <span style=\"color: #ff6666;\">&quot;th&quot;<\/span>\n   <span style=\"color: #aa9900; font-weight: bold;\">end<\/span>\n<span style=\"color: #aa9900; font-weight: bold;\">end<\/span><\/pre><\/div><\/div><\/div>\n\n<p>It would be neat if I could make the array take any message it doesn&#8217;t understand and pass it along to all its elements, so this:<\/p>\n\n<div class=\"my_syntax_box\"><div class=\"my_syntax\"><div class=\"code\"><pre class=\"lua\" style=\"font-family:monospace;\">foo<span style=\"color: #66cc66;\">:<\/span>ordinalize<span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #66cc66;\">&#41;<\/span><\/pre><\/div><\/div><\/div>\n\n<p>is the same as this:<\/p>\n\n<div class=\"my_syntax_box\"><div class=\"my_syntax\"><div class=\"code\"><pre class=\"lua\" style=\"font-family:monospace;\">A<span style=\"color: #66cc66;\">&#40;<\/span>foo<span style=\"color: #66cc66;\">&#91;<\/span><span style=\"color: #cc66cc;\">1<\/span><span style=\"color: #66cc66;\">&#93;<\/span><span style=\"color: #66cc66;\">:<\/span>ordinalize<span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #66cc66;\">&#41;<\/span><span style=\"color: #66cc66;\">,<\/span> foo<span style=\"color: #66cc66;\">&#91;<\/span><span style=\"color: #cc66cc;\">2<\/span><span style=\"color: #66cc66;\">&#93;<\/span><span style=\"color: #66cc66;\">:<\/span>ordinalize<span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #66cc66;\">&#41;<\/span><span style=\"color: #66cc66;\">,<\/span> foo<span style=\"color: #66cc66;\">&#91;<\/span><span style=\"color: #cc66cc;\">3<\/span><span style=\"color: #66cc66;\">&#93;<\/span><span style=\"color: #66cc66;\">:<\/span>ordinalize<span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #66cc66;\">&#41;<\/span><span style=\"color: #66cc66;\">&#41;<\/span><\/pre><\/div><\/div><\/div>\n\n<p>So let&#8217;s do that. This has a lot in common with <code>each<\/code>, so maybe we can write this as a special case of that. We&#8217;ll make a function that calls a method on an object given its name, pass that inte <code>each<\/code>, and return the result.<\/p>\n<p>A method call (using the colon) is just a lookup of a name followed by calling that function, with the object as the first argument, 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> send_msg<span style=\"color: #66cc66;\">&#40;<\/span>obj<span style=\"color: #66cc66;\">,<\/span> name<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: #66cc66;\">&#40;<\/span>obj<span style=\"color: #66cc66;\">&#91;<\/span>name<span style=\"color: #66cc66;\">&#93;<\/span><span style=\"color: #66cc66;\">&#41;<\/span><span style=\"color: #66cc66;\">&#40;<\/span>obj<span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #66cc66;\">...<\/span><span style=\"color: #66cc66;\">&#41;<\/span>\n<span style=\"color: #aa9900; font-weight: bold;\">end<\/span><\/pre><\/div><\/div><\/div>\n\n<p>So, our function to create a function like this and send it to <code>each<\/code> is this:<\/p>\n\n<div class=\"my_syntax_box\"><div class=\"my_syntax\"><div class=\"code\"><pre class=\"lua\" style=\"font-family:monospace;\">   send <span style=\"color: #66cc66;\">=<\/span>\n      <span style=\"color: #aa9900; font-weight: bold;\">function<\/span><span style=\"color: #66cc66;\">&#40;<\/span>msg<span style=\"color: #66cc66;\">&#41;<\/span>\n         <span style=\"color: #aa9900; font-weight: bold;\">return<\/span> <span style=\"color: #aa9900; font-weight: bold;\">function<\/span><span style=\"color: #66cc66;\">&#40;<\/span>list<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> <span style=\"color: #aa9900; font-weight: bold;\">function<\/span> fn<span style=\"color: #66cc66;\">&#40;<\/span>v<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: #66cc66;\">&#40;<\/span>v<span style=\"color: #66cc66;\">&#91;<\/span>msg<span style=\"color: #66cc66;\">&#93;<\/span><span style=\"color: #66cc66;\">&#41;<\/span><span style=\"color: #66cc66;\">&#40;<\/span>v<span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #66cc66;\">...<\/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;\">return<\/span> _array<span style=\"color: #66cc66;\">.<\/span>each<span style=\"color: #66cc66;\">&#40;<\/span>list<span style=\"color: #66cc66;\">,<\/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;\">end<\/span>\n      <span style=\"color: #aa9900; font-weight: bold;\">end<\/span><\/pre><\/div><\/div><\/div>\n\n<p>What we&#8217;ll do in <code>__index<\/code> is call this (with a name) to get a function (method) that takes the array and any extra arguments, and then return it (so that the caller can pass into it an array and some extra arguments). Here&#8217;s the finished <code>__index<\/code>, that handles that and <code>each<\/code>:<\/p>\n\n<div class=\"my_syntax_box\"><div class=\"my_syntax\"><div class=\"code\"><pre class=\"lua\" style=\"font-family:monospace;\">   __index <span style=\"color: #66cc66;\">=<\/span>\n      <span style=\"color: #aa9900; font-weight: bold;\">function<\/span><span style=\"color: #66cc66;\">&#40;<\/span>t<span style=\"color: #66cc66;\">,<\/span> name<span style=\"color: #66cc66;\">&#41;<\/span>\n         <span style=\"color: #aa9900; font-weight: bold;\">if<\/span> name <span style=\"color: #66cc66;\">==<\/span> <span style=\"color: #ff6666;\">'each'<\/span> <span style=\"color: #aa9900; font-weight: bold;\">then<\/span> <span style=\"color: #aa9900; font-weight: bold;\">return<\/span> _array<span style=\"color: #66cc66;\">.<\/span>each\n         <span style=\"color: #aa9900; font-weight: bold;\">else<\/span> <span style=\"color: #aa9900; font-weight: bold;\">return<\/span> _array<span style=\"color: #66cc66;\">.<\/span>send<span style=\"color: #66cc66;\">&#40;<\/span>name<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><\/pre><\/div><\/div><\/div>\n\n<p>Now, we can do stuff like this:<\/p>\n\n<div class=\"my_syntax_box\"><div class=\"my_syntax\"><div class=\"code\"><pre class=\"lua\" style=\"font-family:monospace;\">A<span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #cc66cc;\">1<\/span><span style=\"color: #66cc66;\">,<\/span><span style=\"color: #cc66cc;\">10<\/span><span style=\"color: #66cc66;\">,<\/span><span style=\"color: #cc66cc;\">33<\/span><span style=\"color: #66cc66;\">&#41;<\/span><span style=\"color: #66cc66;\">:<\/span>each<span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #0000aa;\">tostring<\/span><span style=\"color: #66cc66;\">&#41;<\/span><span style=\"color: #66cc66;\">:<\/span>ordinalize<span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #66cc66;\">&#41;<\/span> <span style=\"color: #808080; font-style: italic;\">-- ==&amp;gt; [1st, 10th, 33rd]<\/span>\nA<span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #ff6666;\">'foo'<\/span><span style=\"color: #66cc66;\">,<\/span><span style=\"color: #ff6666;\">'bar'<\/span><span style=\"color: #66cc66;\">,<\/span><span style=\"color: #ff6666;\">'baz'<\/span><span style=\"color: #66cc66;\">&#41;<\/span><span style=\"color: #66cc66;\">:<\/span>sub<span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #cc66cc;\">1<\/span><span style=\"color: #66cc66;\">,<\/span><span style=\"color: #cc66cc;\">1<\/span><span style=\"color: #66cc66;\">&#41;<\/span> <span style=\"color: #808080; font-style: italic;\">-- ==&amp;gt; [f, b, b]<\/span><\/pre><\/div><\/div><\/div>\n\n<h2>Next steps<\/h2>\n<p>There are a lot more functions that it would be handy to do to arrays other than just <code>each<\/code> and passing through method calls. We could want to reverse them, or partition them, or filter elements out. <code>inject<\/code> is handy in more places than you&#8217;d expect. Ruby has a really complete set; if you want to try adapting some methods from <a onclick=\"javascript:pageTracker._trackPageview('\/outgoing\/www.ruby-doc.org\/core\/classes\/Enumerable.html');\"  href=\"http:\/\/www.ruby-doc.org\/core\/classes\/Enumerable.html\">Ruby&#8217;s Enumerable library<\/a> into this, the code is <a onclick=\"javascript:pageTracker._trackPageview('\/outgoing\/gist.github.com\/1102289');\"  href=\"http:\/\/gist.github.com\/1102289\">here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I thought of this today, and I thought it might make an interesting short post. One of the small annoyances of Lua is that it doesn&#8217;t have a lot of convenient support for arrays. You can implement arrays with tables, of course, but they&#8217;re still tables: they don&#8217;t have default metatables (so you can&#8217;t do [&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\/40"}],"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=40"}],"version-history":[{"count":0,"href":"http:\/\/www.playwithlua.com\/index.php?rest_route=\/wp\/v2\/posts\/40\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.playwithlua.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=40"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.playwithlua.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=40"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.playwithlua.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=40"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}