Skip to content

restrict

Subhajit Sahu edited this page Jul 29, 2022 · 2 revisions

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 xfunction = require('extra-function');


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


References

Clone this wiki locally