diff options
author | Yonaba <roland.yonaba@gmail.com> | 2017-04-14 12:05:21 +0300 |
---|---|---|
committer | Yonaba <roland.yonaba@gmail.com> | 2017-04-14 12:05:21 +0300 |
commit | 22805dae6fcd9ed9513038c9a94e3e17ca5639f4 (patch) | |
tree | 17c8bb417a030d7168c8cc9d2a8d6c0136812a94 | |
parent | 6b251d6cc18c8ae144af515ca093664706b53f6a (diff) |
Added _.partialRight
-rw-r--r-- | CHANGELOG.md | 1 | ||||
-rw-r--r-- | doc/index.html | 47 | ||||
-rw-r--r-- | doc/topics/tutorial.md.html | 37 | ||||
-rw-r--r-- | doc/tutorial.md | 33 | ||||
-rw-r--r-- | moses.lua | 23 | ||||
-rw-r--r-- | spec/func_spec.lua | 18 |
6 files changed, 151 insertions, 8 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 359df92..e28af67 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ * Added `_.toObj` * Added `_.noop` +* Added `_.partialRight` ## 1.5.1 (04/13/17) diff --git a/doc/index.html b/doc/index.html index 27bbb34..6ef18b5 100644 --- a/doc/index.html +++ b/doc/index.html @@ -455,6 +455,10 @@ <td class="summary">Partially apply a function by filling in any number of its arguments.</td> </tr> <tr> + <td class="name" nowrap><a href="#partialRight">partialRight (f, ...)</a></td> + <td class="summary">Similar to <a href="index.html#partial">partial</a>, but from the right.</td> + </tr> + <tr> <td class="name" nowrap><a href="#curry">curry (f[, n_args])</a></td> <td class="summary">Curries a function.</td> </tr> @@ -3762,13 +3766,47 @@ <h3>Returns:</h3> <ol> - a new version of function f where having some of it original arguments filled + a new version of function f having some of it original arguments filled + </ol> + + + <h3>See also:</h3> + <ul> + <li><a href="index.html#partialRight">partialRight</a></li> + <li><a href="index.html#curry">curry</a></li> + </ul> + + +</dd> + <dt> + <a name = "partialRight"></a> + <strong>partialRight (f, ...)</strong> + </dt> + <dd> + Similar to <a href="index.html#partial">partial</a>, but from the right. + + + <h3>Parameters:</h3> + <ul> + <li><span class="parameter">f</span> + a function + </li> + <li><span class="parameter">...</span> + a list of partial arguments to <code>f</code> + </li> + </ul> + + <h3>Returns:</h3> + <ol> + + a new version of function f having some of it original arguments filled </ol> <h3>See also:</h3> <ul> - <a href="index.html#curry">curry</a> + <li><a href="index.html#partialRight">partialRight</a></li> + <li><a href="index.html#curry">curry</a></li> </ul> @@ -3803,7 +3841,8 @@ <h3>See also:</h3> <ul> - <a href="index.html#partial">partial</a> + <li><a href="index.html#partial">partial</a></li> + <li><a href="index.html#partialRight">partialRight</a></li> </ul> @@ -4732,7 +4771,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-14 08:14:37 </i> +<i style="float:right;">Last updated 2017-04-14 09:04:58 </i> </div> <!-- id="about" --> </div> <!-- id="container" --> </body> diff --git a/doc/topics/tutorial.md.html b/doc/topics/tutorial.md.html index 87bc9ca..e3d07ff 100644 --- a/doc/topics/tutorial.md.html +++ b/doc/topics/tutorial.md.html @@ -1665,7 +1665,7 @@ iter_po2() <span class="comment">-- => 2 </pre> -<p>The string <code>'_'</code> can be used 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.</p> +<p>The string <code>'_'</code> can be used as a placeholder in the list of arguments to specify an argument that should not be pre-filled, but is rather left open to be supplied at call-time.</p> <pre> <span class="keyword">local</span> <span class="keyword">function</span> diff(a, b) <span class="keyword">return</span> a - b <span class="keyword">end</span> @@ -1674,6 +1674,39 @@ iter_po2() <span class="comment">-- => 2 </pre> +<h3>partialRight (f, …)</h3> + +<p>Like <code>_.partial</code>, it partially applies a function by filling in any number of its arguments, but from the right.</p> + +<pre> +<span class="keyword">local</span> <span class="keyword">function</span> concat(...) <span class="keyword">return</span> <span class="global">table</span>.concat({...},<span class="string">','</span>) <span class="keyword">end</span> +<span class="keyword">local</span> concat_right = _.partialRight(concat,<span class="string">'a'</span>,<span class="string">'b'</span>,<span class="string">'c'</span>) +concat_right(<span class="string">'d'</span>) <span class="comment">-- => d,a,b,c +</span> +concat_right = _.partialRight(concat,<span class="string">'a'</span>,<span class="string">'b'</span>) +concat_right(<span class="string">'c'</span>,<span class="string">'d'</span>) <span class="comment">-- => c,d,a,b +</span> +concat_right = _.partialRight(concat,<span class="string">'a'</span>) +concat_right(<span class="string">'b'</span>,<span class="string">'c'</span>,<span class="string">'d'</span>) <span class="comment">-- => b,c,d,a</span> +</pre> + + +<p>The string <code>'_'</code>, as always, can be used as a placeholder in the list of arguments to specify an argument that should not be pre-filled, but is rather left open to be supplied at call-time. +In that case, the first args supplied at runtime will be used to fill the initial list of args while the remaining will be prepended.</p> + +<pre> +<span class="keyword">local</span> <span class="keyword">function</span> concat(...) <span class="keyword">return</span> <span class="global">table</span>.concat({...},<span class="string">','</span>) <span class="keyword">end</span> +<span class="keyword">local</span> concat_right = _.partialRight(concat,<span class="string">'a'</span>,<span class="string">'_'</span>,<span class="string">'c'</span>) +concat_right(<span class="string">'d'</span>,<span class="string">'b'</span>) <span class="comment">-- => b,a,d,c +</span> +concat_right = _.partialRight(concat,<span class="string">'a'</span>,<span class="string">'b'</span>,<span class="string">'_'</span>) +concat_right(<span class="string">'c'</span>,<span class="string">'d'</span>) <span class="comment">-- => d,a,b,c +</span> +concat_right = _.partialRight(concat,<span class="string">'_'</span>,<span class="string">'a'</span>) +concat_right(<span class="string">'b'</span>,<span class="string">'c'</span>,<span class="string">'d'</span>) <span class="comment">-- => c,d,b,a</span> +</pre> + + <h3>curry (f, n_args)</h3> <p>Curries a function. If the given function <code>f</code> takes multiple arguments, it returns another version of <code>f</code> that takes a single argument @@ -2154,7 +2187,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-14 08:14:37 </i> +<i style="float:right;">Last updated 2017-04-14 09:04:58 </i> </div> <!-- id="about" --> </div> <!-- id="container" --> </body> diff --git a/doc/tutorial.md b/doc/tutorial.md index cfb6c46..31906a2 100644 --- a/doc/tutorial.md +++ b/doc/tutorial.md @@ -1436,7 +1436,7 @@ local diffFrom20 = _.partial(diff, 20) -- arg 'a' will be 20 by default diffFrom20(5) -- => 15 ```` -The string `'_'` can be used 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. +The string `'_'` can be used as a placeholder in the list of arguments to specify an argument that should not be pre-filled, but is rather left open to be supplied at call-time. ```lua local function diff(a, b) return a - b end @@ -1444,6 +1444,37 @@ local remove5 = _.partial(diff, '_', 5) -- arg 'a' will be given at call-time, b remove5(20) -- => 15 ```` +### partialRight (f, ...) + +Like `_.partial`, it partially applies a function by filling in any number of its arguments, but from the right. + +```lua +local function concat(...) return table.concat({...},',') end +local concat_right = _.partialRight(concat,'a','b','c') +concat_right('d') -- => d,a,b,c + +concat_right = _.partialRight(concat,'a','b') +concat_right('c','d') -- => c,d,a,b + +concat_right = _.partialRight(concat,'a') +concat_right('b','c','d') -- => b,c,d,a +``` + +The string `'_'`, as always, can be used as a placeholder in the list of arguments to specify an argument that should not be pre-filled, but is rather left open to be supplied at call-time. +In that case, the first args supplied at runtime will be used to fill the initial list of args while the remaining will be prepended. + +```lua +local function concat(...) return table.concat({...},',') end +local concat_right = _.partialRight(concat,'a','_','c') +concat_right('d','b') -- => b,a,d,c + +concat_right = _.partialRight(concat,'a','b','_') +concat_right('c','d') -- => d,a,b,c + +concat_right = _.partialRight(concat,'_','a') +concat_right('b','c','d') -- => c,d,b,a +```` + ### curry (f, n_args) Curries a function. If the given function `f` takes multiple arguments, it returns another version of `f` that takes a single argument @@ -1516,7 +1516,8 @@ end -- @name partial -- @param f a function -- @param ... a list of partial arguments to `f` --- @return a new version of function f where having some of it original arguments filled +-- @return a new version of function f having some of it original arguments filled +-- @see partialRight -- @see curry function _.partial(f,...) local partial_args = {...} @@ -1530,6 +1531,25 @@ function _.partial(f,...) end end +--- Similar to @{partial}, but from the right. +-- @name partialRight +-- @param f a function +-- @param ... a list of partial arguments to `f` +-- @return a new version of function f having some of it original arguments filled +-- @see partialRight +-- @see curry +function _.partialRight(f,...) + local partial_args = {...} + return function (...) + local n_args = {...} + local f_args = {} + for k = 1,#partial_args do + f_args[k] = (partial_args[k] == '_') and _.pop(n_args) or partial_args[k] + end + return f(unpack(_.append(n_args, f_args))) + end +end + --- Curries a function. If the given function `f` takes multiple arguments, it returns another version of -- `f` that takes a single argument (the first of the arguments to the original function) and returns a new -- function that takes the remainder of the arguments and returns the result. @@ -1538,6 +1558,7 @@ end -- @param[opt] n_args the number of arguments expected for `f`. Defaults to 2. -- @return a curried version of `f` -- @see partial +-- @see partialRight function _.curry(f, n_args) n_args = n_args or 2 local _args = {} diff --git a/spec/func_spec.lua b/spec/func_spec.lua index a46763c..0d3ea97 100644 --- a/spec/func_spec.lua +++ b/spec/func_spec.lua @@ -337,6 +337,24 @@ context('Utility functions specs', function() end) + context('partialRight', function() + + test('applies partial but from the right',function() + local function concat(a,b,c,d) return a..b..c..d end + assert_equal(_.partialRight(concat,'a','b','c')('d'), 'dabc') + assert_equal(_.partialRight(concat,'a','b')('c','d'), 'cdab') + assert_equal(_.partialRight(concat,'a')('b','c','d'), 'bcda') + end) + + test('\'_\' can be used as a placeholder',function() + local function concat(a,b,c,d) return a..b..c..d end + assert_equal(_.partialRight(concat,'a','_','c')('d','b'), 'badc') + assert_equal(_.partialRight(concat,'a','b','_')('c','d'), 'dabc') + assert_equal(_.partialRight(concat,'_','a')('b','c','d'), 'cdba') + end) + + end) + context('curry', function() test('curries a function for a specific number of args',function() |