diff options
author | Yonaba <roland.yonaba@gmail.com> | 2017-04-27 17:16:10 +0300 |
---|---|---|
committer | Yonaba <roland.yonaba@gmail.com> | 2017-04-27 17:16:52 +0300 |
commit | 997ec36d8ac9f7167f1b0e69ec308536769e2f2b (patch) | |
tree | 81cf97b60f72ec2c65c1a9938b2a2d10ad061936 | |
parent | 590c069c44a343c7eca2ddd939d27250397dc008 (diff) |
Added _.array
Fixed proper name of _.iterator in docs
-rw-r--r-- | CHANGELOG.md | 4 | ||||
-rw-r--r-- | doc/index.html | 51 | ||||
-rw-r--r-- | doc/topics/tutorial.md.html | 13 | ||||
-rw-r--r-- | doc/tutorial.md | 10 | ||||
-rw-r--r-- | moses.lua | 16 | ||||
-rw-r--r-- | moses_min.lua | 5 | ||||
-rw-r--r-- | spec/func_spec.lua | 12 |
7 files changed, 94 insertions, 17 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index b9ffa13..77723a3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Version history +## 1.6.1 (04/27/17) + +* Added `_.array` + ## 1.6.0 (14/04/17) * Added `_.toObj` diff --git a/doc/index.html b/doc/index.html index e9466a1..86dca38 100644 --- a/doc/index.html +++ b/doc/index.html @@ -56,7 +56,7 @@ <h3>Info:</h3> <ul> <li><strong>Copyright</strong>: 2012-2017</li> - <li><strong>Release</strong>: 1.6.0</li> + <li><strong>Release</strong>: 1.6.1</li> <li><strong>License</strong>: <a href="http://www.opensource.org/licenses/mit-license.php">MIT</a></li> <li><strong>Author</strong>: <a href="http://github.com/Yonaba">Roland Yonaba</a></li> </ul> @@ -337,7 +337,7 @@ </tr> <tr> <td class="name" nowrap><a href="#append">append (array, other)</a></td> - <td class="summary">Clones <code>array</code> and appends <code>other</code> values.</td> + <td class="summary">Clones <a href="index.html#array">array</a> and appends <code>other</code> values.</td> </tr> <tr> <td class="name" nowrap><a href="#interleave">interleave (...)</a></td> @@ -345,7 +345,7 @@ </tr> <tr> <td class="name" nowrap><a href="#interpose">interpose (value, array)</a></td> - <td class="summary">Interposes value in-between consecutive pair of values in <code>array</code>.</td> + <td class="summary">Interposes value in-between consecutive pair of values in <a href="index.html#array">array</a>.</td> </tr> <tr> <td class="name" nowrap><a href="#range">range ([from[, to[, step]]])</a></td> @@ -447,10 +447,14 @@ <td class="summary">Generates an unique ID for the current session.</td> </tr> <tr> - <td class="name" nowrap><a href="#iterate">iterate (f, x)</a></td> + <td class="name" nowrap><a href="#iterator">iterator (f, x)</a></td> <td class="summary">Produces an iterator which repeatedly apply a function <code>f</code> onto an input.</td> </tr> <tr> + <td class="name" nowrap><a href="#array">array (...)</a></td> + <td class="summary">Iterates an iterator and returns its values in an array.</td> + </tr> + <tr> <td class="name" nowrap><a href="#flip">flip (f)</a></td> <td class="summary">Creates a function of <code>f</code> with arguments flipped in reverse order.</td> </tr> @@ -2773,8 +2777,8 @@ <strong>symmetricDifference (array, array2)</strong> </dt> <dd> - Performs a symmetric difference. Returns values from <code>array</code> not present in <code>array2</code> and also values - from <code>array2</code> not present in <code>array</code>. + Performs a symmetric difference. Returns values from <a href="index.html#array">array</a> not present in <code>array2</code> and also values + from <code>array2</code> not present in <a href="index.html#array">array</a>. <br/><em>Aliased as <code>symdiff</code></em> @@ -2897,7 +2901,7 @@ <strong>append (array, other)</strong> </dt> <dd> - Clones <code>array</code> and appends <code>other</code> values. + Clones <a href="index.html#array">array</a> and appends <code>other</code> values. <h3>Parameters:</h3> @@ -2955,7 +2959,7 @@ <strong>interpose (value, array)</strong> </dt> <dd> - Interposes value in-between consecutive pair of values in <code>array</code>. + Interposes value in-between consecutive pair of values in <a href="index.html#array">array</a>. <h3>Parameters:</h3> @@ -3732,8 +3736,8 @@ </dd> <dt> - <a name = "iterate"></a> - <strong>iterate (f, x)</strong> + <a name = "iterator"></a> + <strong>iterator (f, x)</strong> </dt> <dd> Produces an iterator which repeatedly apply a function <code>f</code> onto an input.<br/> @@ -3762,6 +3766,31 @@ </dd> <dt> + <a name = "array"></a> + <strong>array (...)</strong> + </dt> + <dd> + Iterates an iterator and returns its values in an array. + + + <h3>Parameters:</h3> + <ul> + <li><span class="parameter">...</span> + an iterator (a function, a table and a value) + </li> + </ul> + + <h3>Returns:</h3> + <ol> + + an array of results + </ol> + + + + +</dd> + <dt> <a name = "flip"></a> <strong>flip (f)</strong> </dt> @@ -4944,7 +4973,7 @@ </div> <!-- id="main" --> <div id="about"> <i>generated by <a href="http://github.com/stevedonovan/LDoc">LDoc 1.4.6</a></i> -<i style="float:right;">Last updated 2017-04-15 17:26:06 </i> +<i style="float:right;">Last updated 2017-04-27 14:14:42 </i> </div> <!-- id="about" --> </div> <!-- id="container" --> </body> diff --git a/doc/topics/tutorial.md.html b/doc/topics/tutorial.md.html index 155c5b4..29b356d 100644 --- a/doc/topics/tutorial.md.html +++ b/doc/topics/tutorial.md.html @@ -1654,6 +1654,17 @@ iter_po2() <span class="comment">-- => 2 </pre> +<h3>array (…)</h3> + +<p>Iterates a given iterator function and returns its values packed in an array.</p> + +<pre> +<span class="keyword">local</span> text = <span class="string">'letters'</span> +<span class="keyword">local</span> chars = <span class="global">string</span>.gmatch(text, <span class="string">'.'</span>) +<span class="keyword">local</span> letters = _.array(chars) <span class="comment">-- => {'l','e','t','t','e','r','s'}</span> +</pre> + + <h3>flip (f)</h3> <p>Creates a function of <code>f</code> with arguments flipped in reverse order.</p> @@ -2291,7 +2302,7 @@ _.import(context, <span class="keyword">true</span>) </div> <!-- id="main" --> <div id="about"> <i>generated by <a href="http://github.com/stevedonovan/LDoc">LDoc 1.4.6</a></i> -<i style="float:right;">Last updated 2017-04-15 17:26:06 </i> +<i style="float:right;">Last updated 2017-04-27 14:14:42 </i> </div> <!-- id="about" --> </div> <!-- id="container" --> </body> diff --git a/doc/tutorial.md b/doc/tutorial.md index 5a1cf2e..aa8c636 100644 --- a/doc/tutorial.md +++ b/doc/tutorial.md @@ -1426,6 +1426,16 @@ iter_po2() -- => 4 iter_po2() -- => 8 ```` +### array (...) + +Iterates a given iterator function and returns its values packed in an array. + +```lua +local text = 'letters' +local chars = string.gmatch(text, '.') +local letters = _.array(chars) -- => {'l','e','t','t','e','r','s'} +```` + ### flip (f) Creates a function of `f` with arguments flipped in reverse order. @@ -2,10 +2,10 @@ -- @author [Roland Yonaba](http://github.com/Yonaba) -- @copyright 2012-2017 -- @license [MIT](http://www.opensource.org/licenses/mit-license.php) --- @release 1.6.0 +-- @release 1.6.1 -- @module moses -local _MODULEVERSION = '1.6.0' +local _MODULEVERSION = '1.6.1' -- Internalisation local next, type, select, pcall = next, type, select, pcall @@ -1498,7 +1498,7 @@ end --- Produces an iterator which repeatedly apply a function `f` onto an input. -- Yields x, then f(x), then f(f(x)), continuously. --- @name iterate +-- @name iterator -- @param f a function -- @param x an initial input to `f` -- @return an iterator fnction @@ -1510,6 +1510,16 @@ function _.iterator(f, x) end end +--- Iterates an iterator and returns its values in an array. +-- @name array +-- @param ... an iterator (a function, a table and a value) +-- @return an array of results +function _.array(...) + local r = {} + for v in ... do r[#r+1] = v end + return r +end + --- Creates a function of `f` with arguments flipped in reverse order. -- @name flip -- @param f a function diff --git a/moses_min.lua b/moses_min.lua index ae2f4bd..5469ee1 100644 --- a/moses_min.lua +++ b/moses_min.lua @@ -1,4 +1,4 @@ -local daa='1.6.0'local _ba,aba,bba,cba=next,type,select,pcall;local dba,_ca=setmetatable,getmetatable +local daa='1.6.1'local _ba,aba,bba,cba=next,type,select,pcall;local dba,_ca=setmetatable,getmetatable local aca,bca=table.insert,table.sort;local cca,dca=table.remove,table.concat local _da,ada,bda=math.randomseed,math.random,math.huge;local cda,dda,__b=math.floor,math.max,math.min;local a_b=rawget local b_b=table.unpack or unpack;local c_b,d_b=pairs,ipairs;local _ab={}local function aab(bcb,ccb)return bcb>ccb end @@ -233,7 +233,8 @@ d_b(ccb)do local adb=bcb[_db]if adb then bcb[_db]=_ab.bind(adb,bcb)end end;retur function _ab.uniqueId(bcb,...)acb=acb+1 if bcb then if _ab.isString(bcb)then return bcb:format(acb)elseif _ab.isFunction(bcb)then return bcb(acb,...)end end;return acb end -function _ab.iterator(bcb,ccb)return function()ccb=bcb(ccb)return ccb end end;function _ab.flip(bcb)return +function _ab.iterator(bcb,ccb)return function()ccb=bcb(ccb)return ccb end end +function _ab.array(...)local bcb={}for ccb in...do bcb[#bcb+1]=ccb end;return bcb end;function _ab.flip(bcb)return function(...)return bcb(b_b(_ab.reverse({...})))end end;function _ab.over(...) local bcb={...} return function(...)local ccb={}for dcb,_db in d_b(bcb)do ccb[#ccb+1]=_db(...)end diff --git a/spec/func_spec.lua b/spec/func_spec.lua index 2c9e3ce..3f2895a 100644 --- a/spec/func_spec.lua +++ b/spec/func_spec.lua @@ -317,6 +317,18 @@ context('Utility functions specs', function() end) + context('array', function() + + test('iterates a given iterator and returns its values in an array',function() + local letters = _.array(('Lua'):gmatch('.')) + assert_true(_.isEqual(letters,{'L','u','a'})) + + local numbers = _.array(pairs(_.range(1,10))) + assert_true(_.isEqual(numbers,_.range(1,10))) + end) + + end) + context('flip', function() test('creates a function which runs f with arguments flipped',function() |