Open parzhitsky opened 4 years ago
This would require saving start
, stop
, and step
of the range:
const { start, stop, step } = range;
let lower = false;
let upper = false;
if (step > 0)
[ lower, upper ] = [ start <= x, x < stop ];
if (step < 0)
[ lower, upper ] = [ stop < x, x <= start ];
if (!lower || !upper)
return false; // out of bounds
return ((x - start) % step) === 0;
Blocked by #59
Though possible via Proxy
, this won't be implemented as in
operator, since such usage of the operator would be very confusing. Instead, .has()
and/or .includes()
method will be implemented.
The name .includes()
(as in String.prototype.includes
or Array.prototype.includes
) seems more natural for such kind of task, but it creates false impression of under-the-hood iteration (i.e., O(n) computational complexity). On the other hand, .has()
(as in Set.prototype.has
) is a fast method (O(1), similar to "prop" in object
); but doesn't seem so natural.
With this in mind, .has()
will be the primary name for the method, and .includes()
will be an alias for it:
xrange(1, 17, 3).has(4); // true
Example:
Functional implementation is just too unpredictable to have this functionality: