Skip to content

restrict

Subhajit Sahu edited this page Aug 7, 2022 · 1 revision

Generate restricted-use version of a function.

Alternatives: restrict, restrictOnce, restrictBefore, restrictAfter.


function restrict(x, start, end)
// x:     a function
// start: usable from
// end:   usable till (excluding) [-1 ⇒ end]

const xasyncfn = require('extra-async-function');


var sum = 0;
var add = (x: number) => sum += x;
var fn  = xasyncfn.restrict(add, 0, 4);
for (var i=0; i<10; ++i)
  fn(i);
sum;  // 0 + 1 + 2 + 3
// → 6


var sum = 0;
var add = (x: number) => sum += x;
var fn  = xasyncfn.restrict(add, 4, 8);
for (var i=0; i<10; ++i)
  fn(i);
sum;  // 4 + 5 + 6 + 7
// → 22


References

Clone this wiki locally