Skip to content

Commit

Permalink
Added _.before
Browse files Browse the repository at this point in the history
  • Loading branch information
Yonaba committed Apr 27, 2017
1 parent 09216c7 commit f64ffde
Show file tree
Hide file tree
Showing 7 changed files with 221 additions and 113 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Added `_.array`
* Added `_.clear`
* Added `_.time`
* Added `_.before`

## 1.6.0 (14/04/17)

Expand Down
68 changes: 54 additions & 14 deletions doc/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -395,12 +395,16 @@ <h2><a href="#Utility_functions">Utility functions </a></h2>
<td class="summary">Creates a constant function which returns the same output on every call.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#memoize">memoize (f[, hash])</a></td>
<td class="summary">Memoizes a given function by caching the computed result.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#once">once (f)</a></td>
<td class="summary">Returns a version of <code>f</code> that runs only once.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#memoize">memoize (f[, hash])</a></td>
<td class="summary">Memoizes a given function by caching the computed result.</td>
<td class="name" nowrap><a href="#before">before (f, count)</a></td>
<td class="summary">Returns a version of <code>f</code> that will run no more than <a href="index.html#count">count</a> times.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#after">after (f, count)</a></td>
Expand Down Expand Up @@ -3319,6 +3323,38 @@ <h3>Returns:</h3>



</dd>
<dt>
<a name = "memoize"></a>
<strong>memoize (f[, hash])</strong>
</dt>
<dd>
Memoizes a given function by caching the computed result.
Useful for speeding-up slow-running functions. If a <code>hash</code> function is passed,
it will be used to compute hash keys for a set of input values for caching.
<br/><em>Aliased as <code>cache</code></em>


<h3>Parameters:</h3>
<ul>
<li><span class="parameter">f</span>
a function
</li>
<li><span class="parameter">hash</span>
a hash function, defaults to <a href="index.html#identity">identity</a>
(<em>optional</em>)
</li>
</ul>

<h3>Returns:</h3>
<ol>

a new function
</ol>




</dd>
<dt>
<a name = "once"></a>
Expand Down Expand Up @@ -3346,30 +3382,28 @@ <h3>Returns:</h3>

<h3>See also:</h3>
<ul>
<a href="index.html#after">after</a>
<li><a href="index.html#before">before</a></li>
<li><a href="index.html#after">after</a></li>
</ul>


</dd>
<dt>
<a name = "memoize"></a>
<strong>memoize (f[, hash])</strong>
<a name = "before"></a>
<strong>before (f, count)</strong>
</dt>
<dd>
Memoizes a given function by caching the computed result.
Useful for speeding-up slow-running functions. If a <code>hash</code> function is passed,
it will be used to compute hash keys for a set of input values for caching.
<br/><em>Aliased as <code>cache</code></em>
Returns a version of <code>f</code> that will run no more than <a href="index.html#count">count</a> times. Next calls will
keep yielding the results of the count-th call.


<h3>Parameters:</h3>
<ul>
<li><span class="parameter">f</span>
a function
</li>
<li><span class="parameter">hash</span>
a hash function, defaults to <a href="index.html#identity">identity</a>
(<em>optional</em>)
<li><span class="parameter">count</span>
a count
</li>
</ul>

Expand All @@ -3380,6 +3414,11 @@ <h3>Returns:</h3>
</ol>


<h3>See also:</h3>
<ul>
<li><a href="index.html#once">once</a></li>
<li><a href="index.html#after">after</a></li>
</ul>


</dd>
Expand Down Expand Up @@ -3411,7 +3450,8 @@ <h3>Returns:</h3>

<h3>See also:</h3>
<ul>
<a href="index.html#once">once</a>
<li><a href="index.html#once">once</a></li>
<li><a href="index.html#before">before</a></li>
</ul>


Expand Down Expand Up @@ -5035,7 +5075,7 @@ <h3>Returns:</h3>
</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-27 15:04:19 </i>
<i style="float:right;">Last updated 2017-04-27 15:26:55 </i>
</div> <!-- id="about" -->
</div> <!-- id="container" -->
</body>
Expand Down
37 changes: 26 additions & 11 deletions doc/topics/tutorial.md.html
Original file line number Diff line number Diff line change
Expand Up @@ -1449,6 +1449,22 @@ <h3>constant (value)</h3>
</pre>


<h3>memoize (f, hash)</h3>

<p><em>Aliases: <code>_.cache</code></em>.</p>

<p>Memoizes a slow-running function. It caches the result for a specific input, so that the next time the function is called with the same input, it will lookup the result in its cache, instead of running again the function body.</p>

<pre>
<span class="keyword">local</span> <span class="keyword">function</span> fibonacci(n)
<span class="keyword">return</span> n &lt; <span class="number">2</span> <span class="keyword">and</span> n <span class="keyword">or</span> fibonacci(n-<span class="number">1</span>)+fibonacci(n-<span class="number">2</span>)
<span class="keyword">end</span>
<span class="keyword">local</span> mem_fibonacci = _.memoize(fibonacci)
fibonacci(<span class="number">20</span>) <span class="comment">-- =&gt; 6765 (but takes some time)
</span>mem_fibonacci(<span class="number">20</span>) <span class="comment">-- =&gt; 6765 (takes less time)</span>
</pre>


<h3>once (f)</h3>

<p>Produces a function that runs only once. Successive calls to this function will still yield the same input.</p>
Expand All @@ -1463,19 +1479,18 @@ <h3>once (f)</h3>
</pre>


<h3>memoize (f, hash)</h3>
<h3>before (f, count)</h3>

<p><em>Aliases: <code>_.cache</code></em>.</p>

<p>Memoizes a slow-running function. It caches the result for a specific input, so that the next time the function is called with the same input, it will lookup the result in its cache, instead of running again the function body.</p>
<p>Returns a version of <code>f</code> that will run no more than <a href="../index.html#count">count</a> times. Next calls will keep yielding the results of the (n-th)-1 call.</p>

<pre>
<span class="keyword">local</span> <span class="keyword">function</span> fibonacci(n)
<span class="keyword">return</span> n &lt; <span class="number">2</span> <span class="keyword">and</span> n <span class="keyword">or</span> fibonacci(n-<span class="number">1</span>)+fibonacci(n-<span class="number">2</span>)
<span class="keyword">end</span>
<span class="keyword">local</span> mem_fibonacci = _.memoize(fibonacci)
fibonacci(<span class="number">20</span>) <span class="comment">-- =&gt; 6765 (but takes some time)
</span>mem_fibonacci(<span class="number">20</span>) <span class="comment">-- =&gt; 6765 (takes less time)</span>
<span class="keyword">local</span> <span class="keyword">function</span> greet(someone) <span class="keyword">return</span> <span class="string">'hello '</span>..someone <span class="keyword">end</span>
<span class="keyword">local</span> greetOnly3people = _.before(greet, <span class="number">3</span>)
greetOnly3people(<span class="string">'John'</span>) <span class="comment">-- =&gt; 'hello John'
</span>greetOnly3people(<span class="string">'Moe'</span>) <span class="comment">-- =&gt; 'hello Moe'
</span>greetOnly3people(<span class="string">'James'</span>) <span class="comment">-- =&gt; 'hello James'
</span>greetOnly3people(<span class="string">'Joseph'</span>) <span class="comment">-- =&gt; 'hello James'
</span>greetOnly3people(<span class="string">'Allan'</span>) <span class="comment">-- =&gt; 'hello James'</span>
</pre>


Expand Down Expand Up @@ -2327,7 +2342,7 @@ <h2><a name='import'>Import</a></h2>
</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-27 15:04:19 </i>
<i style="float:right;">Last updated 2017-04-27 15:26:55 </i>
</div> <!-- id="about" -->
</div> <!-- id="container" -->
</body>
Expand Down
32 changes: 23 additions & 9 deletions doc/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -1240,6 +1240,20 @@ pi(2) -- => 3.1415926535898
pi(math.pi) -- => 3.1415926535898
````

### memoize (f, hash)
*Aliases: `_.cache`*.

Memoizes a slow-running function. It caches the result for a specific input, so that the next time the function is called with the same input, it will lookup the result in its cache, instead of running again the function body.

```lua
local function fibonacci(n)
return n < 2 and n or fibonacci(n-1)+fibonacci(n-2)
end
local mem_fibonacci = _.memoize(fibonacci)
fibonacci(20) -- => 6765 (but takes some time)
mem_fibonacci(20) -- => 6765 (takes less time)
````

### once (f)

Produces a function that runs only once. Successive calls to this function will still yield the same input.
Expand All @@ -1253,18 +1267,18 @@ sq(4) -- => 1
sq(5) -- => 1
````

### memoize (f, hash)
*Aliases: `_.cache`*.
### before (f, count)

Memoizes a slow-running function. It caches the result for a specific input, so that the next time the function is called with the same input, it will lookup the result in its cache, instead of running again the function body.
Returns a version of `f` that will run no more than `count` times. Next calls will keep yielding the results of the (n-th)-1 call.

```lua
local function fibonacci(n)
return n < 2 and n or fibonacci(n-1)+fibonacci(n-2)
end
local mem_fibonacci = _.memoize(fibonacci)
fibonacci(20) -- => 6765 (but takes some time)
mem_fibonacci(20) -- => 6765 (takes less time)
local function greet(someone) return 'hello '..someone end
local greetOnly3people = _.before(greet, 3)
greetOnly3people('John') -- => 'hello John'
greetOnly3people('Moe') -- => 'hello Moe'
greetOnly3people('James') -- => 'hello James'
greetOnly3people('Joseph') -- => 'hello James'
greetOnly3people('Allan') -- => 'hello James'
````

### after (f, count)
Expand Down
60 changes: 40 additions & 20 deletions moses.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1290,23 +1290,6 @@ function _.identity(value) return value end
-- @return a constant function
function _.constant(value) return function() return value end end

--- Returns a version of `f` that runs only once. Successive calls to `f`
-- will keep yielding the same output, no matter what the passed-in arguments are.
-- It can be used to initialize variables.
-- @name once
-- @param f a function
-- @return a new function
-- @see after
function _.once(f)
local _internal = 0
local _args = {}
return function(...)
_internal = _internal+1
if _internal<=1 then _args = {...} end
return f(unpack(_args))
end
end

--- Memoizes a given function by caching the computed result.
-- Useful for speeding-up slow-running functions. If a `hash` function is passed,
-- it will be used to compute hash keys for a set of input values for caching.
Expand All @@ -1326,19 +1309,56 @@ function _.memoize(f, hash)
end
end

--- Returns a version of `f` that runs only once. Successive calls to `f`
-- will keep yielding the same output, no matter what the passed-in arguments are.
-- It can be used to initialize variables.
-- @name once
-- @param f a function
-- @return a new function
-- @see before
-- @see after
function _.once(f)
local _internal = 0
local _args = {}
return function(...)
_internal = _internal+1
if _internal <= 1 then _args = {...} end
return f(unpack(_args))
end
end

--- Returns a version of `f` that will run no more than `count` times. Next calls will
-- keep yielding the results of the count-th call.
-- @name before
-- @param f a function
-- @param count a count
-- @return a new function
-- @see once
-- @see after
function _.before(f, count)
local _internal = 0
local _args = {}
return function(...)
_internal = _internal+1
if _internal <= count then _args = {...} end
return f(unpack(_args))
end
end

--- Returns a version of `f` that runs on the `count-th` call.
-- Useful when dealing with asynchronous tasks.
-- @name after
-- @param f a function
-- @param count the number of calls before `f` will start running.
-- @return a new function
-- @see once
-- @see before
function _.after(f, count)
local _limit,_internal = count, 0
return function(...)
_internal = _internal+1
if _internal >= _limit then return f(...) end
end
_internal = _internal+1
if _internal >= _limit then return f(...) end
end
end

--- Composes functions. Each passed-in function consumes the return value of the function that follows.
Expand Down
16 changes: 9 additions & 7 deletions moses_min.lua
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,15 @@ function bab.concat(dcb,_db,adb,bdb)
local cdb=bab.map(dcb,function(ddb,__c)return
tostring(__c)end)return _da(cdb,_db,adb or 1,bdb or#dcb)end;function bab.noop()return end;function bab.identity(dcb)return dcb end;function bab.constant(dcb)return
function()return dcb end end
function bab.once(dcb)local _db=0;local adb={}return
function(...)_db=_db+1;if
_db<=1 then adb={...}end;return dcb(c_b(adb))end end
function bab.memoize(dcb,_db)local adb=_ca({},{__mode='kv'})
local bdb=_db or bab.identity
return function(...)local cdb=bdb(...)local ddb=adb[cdb]
if not ddb then adb[cdb]=dcb(...)end;return adb[cdb]end end
function bab.memoize(dcb,_db)
local adb=_ca({},{__mode='kv'})local bdb=_db or bab.identity;return
function(...)local cdb=bdb(...)local ddb=adb[cdb]if not ddb then
adb[cdb]=dcb(...)end;return adb[cdb]end end;function bab.once(dcb)local _db=0;local adb={}
return function(...)_db=_db+1;if _db<=1 then adb={...}end
return dcb(c_b(adb))end end
function bab.before(dcb,_db)
local adb=0;local bdb={}return
function(...)adb=adb+1;if adb<=_db then bdb={...}end;return dcb(c_b(bdb))end end
function bab.after(dcb,_db)local adb,bdb=_db,0;return
function(...)bdb=bdb+1;if bdb>=adb then return dcb(...)end end end
function bab.compose(...)local dcb=bab.reverse{...}
Expand Down
Loading

0 comments on commit f64ffde

Please sign in to comment.