Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/Yonaba/Moses.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYonaba <roland.yonaba@gmail.com>2017-04-13 18:54:00 +0300
committerYonaba <roland.yonaba@gmail.com>2017-04-13 18:54:00 +0300
commitc322b82ab4e60bbb722792a6768b90900c53ef50 (patch)
tree8f2a76fd62099177f0362b9ee0a6582c1fec4d1f
parentc765f66bce75e27d0307f182dd0c4ab506cb0e5d (diff)
Added _.toObj
-rw-r--r--CHANGELOG.md4
-rw-r--r--doc/index.html45
-rw-r--r--doc/topics/tutorial.md.html14
-rw-r--r--doc/tutorial.md11
-rw-r--r--moses.lua18
-rw-r--r--spec/object_spec.lua11
6 files changed, 99 insertions, 4 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index d3363ff..8d654b0 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,9 @@
# Version history
+##
+
+* Added `_.toObj`
+
## 1.5.1 (04/13/17)
* Added `_.curry`
diff --git a/doc/index.html b/doc/index.html
index 3132bd5..792fd66 100644
--- a/doc/index.html
+++ b/doc/index.html
@@ -470,6 +470,12 @@
<td class="summary">Converts keys and values a an array-list of [k, v].</td>
</tr>
<tr>
+ <td class="name" nowrap><a href="#toObj">toObj (kvpairs)</a></td>
+ <td class="summary">Converts an array list of <a href="index.html#kvpairs">kvpairs</a> to an object where keys are taken
+ from the 1rst column in the <a href="index.html#kvpairs">kvpairs</a> sequence, associated with values in the 2nd
+ column</td>
+ </tr>
+ <tr>
<td class="name" nowrap><a href="#property">property (key)</a></td>
<td class="summary">Returns a function that will return the key property of any passed-in object.</td>
</tr>
@@ -3716,7 +3722,7 @@
</dt>
<dd>
Partially apply a function by filling in any number of its arguments.<br/>
- One may pass a string <code>&apos;_&apos;</code> as a placeholder in your list of arguments to specify an argument
+ One may pass a string <code>&apos;_&apos;</code> as a placeholder in the list of arguments to specify an argument
that should not be pre-filled, but left open to be supplied at call-time.
@@ -3855,6 +3861,41 @@
</ol>
+ <h3>See also:</h3>
+ <ul>
+ <a href="index.html#toObj">toObj</a>
+ </ul>
+
+
+</dd>
+ <dt>
+ <a name = "toObj"></a>
+ <strong>toObj (kvpairs)</strong>
+ </dt>
+ <dd>
+ Converts an array list of <a href="index.html#kvpairs">kvpairs</a> to an object where keys are taken
+ from the 1rst column in the <a href="index.html#kvpairs">kvpairs</a> sequence, associated with values in the 2nd
+ column
+
+
+ <h3>Parameters:</h3>
+ <ul>
+ <li><span class="parameter">kvpairs</span>
+ an array-list of <a href="index.html#kvpairs">kvpairs</a>
+ </li>
+ </ul>
+
+ <h3>Returns:</h3>
+ <ol>
+
+ an object
+ </ol>
+
+
+ <h3>See also:</h3>
+ <ul>
+ <a href="index.html#kvpairs">kvpairs</a>
+ </ul>
</dd>
@@ -4668,7 +4709,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-13 12:33:58 </i>
+<i style="float:right;">Last updated 2017-04-13 15:52:55 </i>
</div> <!-- id="about" -->
</div> <!-- id="container" -->
</body>
diff --git a/doc/topics/tutorial.md.html b/doc/topics/tutorial.md.html
index efd0968..b67a04a 100644
--- a/doc/topics/tutorial.md.html
+++ b/doc/topics/tutorial.md.html
@@ -1731,6 +1731,18 @@ _.each(_.kvpairs(obj), <span class="keyword">function</span>(k,v)
</pre>
+<h3>toObj</h3>
+
+<p>Converts an array list of <a href="../index.html#kvpairs">kvpairs</a> to an object where keys are taken from the 1rst column in the <a href="../index.html#kvpairs">kvpairs</a> sequence, associated with values in the 2nd column.</p>
+
+<pre>
+<span class="keyword">local</span> list_pairs = {{<span class="string">'x'</span>,<span class="number">1</span>},{<span class="string">'y'</span>,<span class="number">2</span>},{<span class="string">'z'</span>,<span class="number">3</span>}}
+obj = _.toObj(list_pairs)
+
+<span class="comment">-- =&gt; {x = 1, y = 2, z = 3}</span>
+</pre>
+
+
<h3>property (key)</h3>
<p>Returns a function that will return the key property of any passed-in object.</p>
@@ -2133,7 +2145,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-13 12:33:58 </i>
+<i style="float:right;">Last updated 2017-04-13 15:52:55 </i>
</div> <!-- id="about" -->
</div> <!-- id="container" -->
</body>
diff --git a/doc/tutorial.md b/doc/tutorial.md
index 21ccc19..34d36d0 100644
--- a/doc/tutorial.md
+++ b/doc/tutorial.md
@@ -1495,6 +1495,17 @@ end)
-- => 3 z,3
````
+### toObj
+
+Converts an array list of `kvpairs` to an object where keys are taken from the 1rst column in the `kvpairs` sequence, associated with values in the 2nd column.
+
+```lua
+local list_pairs = {{'x',1},{'y',2},{'z',3}}
+obj = _.toObj(list_pairs)
+
+-- => {x = 1, y = 2, z = 3}
+````
+
### property (key)
Returns a function that will return the key property of any passed-in object.
diff --git a/moses.lua b/moses.lua
index 5d73d8a..850249b 100644
--- a/moses.lua
+++ b/moses.lua
@@ -1506,7 +1506,7 @@ function _.iterator(f, x)
end
--- Partially apply a function by filling in any number of its arguments.
--- One may pass a string `'_'` as a placeholder in your list of arguments to specify an argument
+-- One may pass a string `'_'` as a placeholder in the list of arguments to specify an argument
-- that should not be pre-filled, but left open to be supplied at call-time.
-- @name partial
-- @param f a function
@@ -1577,12 +1577,28 @@ end
-- @name kvpairs
-- @param obj an object
-- @return an array list of key-values pairs
+-- @see toObj
function _.kvpairs(obj)
local t = {}
_.each(obj, function(k,v) t[#t+1] = {k,v} end)
return t
end
+--- Converts an array list of `kvpairs` to an object where keys are taken
+-- from the 1rst column in the `kvpairs` sequence, associated with values in the 2nd
+-- column
+-- @name toObj
+-- @param kvpairs an array-list of `kvpairs`
+-- @return an object
+-- @see kvpairs
+function _.toObj(kvpairs)
+ local obj = {}
+ for __, v in ipairs(kvpairs) do
+ obj[v[1]] = v[2]
+ end
+ return obj
+end
+
--- Returns a function that will return the key property of any passed-in object.
-- @name property
-- @param key a key property name
diff --git a/spec/object_spec.lua b/spec/object_spec.lua
index 44b2470..139083a 100644
--- a/spec/object_spec.lua
+++ b/spec/object_spec.lua
@@ -34,6 +34,17 @@ context('Object functions specs', function()
end)
end)
+
+ context('toObj', function()
+
+ test('converts an array-list of {k,v} pairs to an object',function()
+ local obj = _.toObj({{'x',1},{'y',2},{'z',3}})
+ assert_equal(obj.x,1)
+ assert_equal(obj.y,2)
+ assert_equal(obj.z,3)
+ end)
+
+ end)
context('property', function()