Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement .has / .includes method #25

Open
parzhitsky opened this issue Apr 22, 2020 · 3 comments
Open

Implement .has / .includes method #25

parzhitsky opened this issue Apr 22, 2020 · 3 comments
Labels
Change: minor [Issue / PR] describes a non-breaking change, such as adding a new functionality Domain: main [Issue / PR] describes change in the functionality, its optimization good first issue [Issue] can be addressed by a first-time contributor Pending: blocked [Issue / PR] cannot be addressed until another issue is resolved Priority: low [Issue / PR] could be addressed at any convenient time Type: improvement [Issue / PR] addresses lack of a functionality or an open possibility of enhancement

Comments

@parzhitsky
Copy link
Member

parzhitsky commented Apr 22, 2020

Example:

xrange(10).has(7); // true
xrange(2, -3).has(5); // false
const range = xrange(0, 10, 2);

range.has(6); // true
range.has(7); // false
range.has(8); // true

Functional implementation is just too unpredictable to have this functionality:

xrange(0, () => Math.random() > 0.5, () => 0).has(0);
// Error: cannot probe items of a non-deterministic range
// (even if `0` is clearly there, such predicate might make the whole range empty)
xrange(0, () => true, ([ last ]) => last === 6 ? CONSTANT : last + 1).has(7);
// Error: cannot probe items of a non-deterministic range
// (whether this range has 7 depends on the value of `CONSTANT`)
@parzhitsky parzhitsky added Change: minor [Issue / PR] describes a non-breaking change, such as adding a new functionality Domain: main [Issue / PR] describes change in the functionality, its optimization Priority: low [Issue / PR] could be addressed at any convenient time Type: improvement [Issue / PR] addresses lack of a functionality or an open possibility of enhancement good first issue [Issue] can be addressed by a first-time contributor labels Apr 22, 2020
@parzhitsky parzhitsky added this to To be considered in xrange@2.2 via automation Apr 22, 2020
@parzhitsky
Copy link
Member Author

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;

@parzhitsky parzhitsky added the Pending: blocked [Issue / PR] cannot be addressed until another issue is resolved label May 13, 2020
@parzhitsky
Copy link
Member Author

Blocked by #59

@parzhitsky
Copy link
Member Author

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

@parzhitsky parzhitsky changed the title Allow using 'in' operator with numeric xrange as its right operand Implement .has / .includes method May 13, 2020
@parzhitsky parzhitsky added this to To be considered in xrange@2.3 via automation Oct 25, 2021
@parzhitsky parzhitsky moved this from To be considered to Rejected in xrange@2.2 Oct 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Change: minor [Issue / PR] describes a non-breaking change, such as adding a new functionality Domain: main [Issue / PR] describes change in the functionality, its optimization good first issue [Issue] can be addressed by a first-time contributor Pending: blocked [Issue / PR] cannot be addressed until another issue is resolved Priority: low [Issue / PR] could be addressed at any convenient time Type: improvement [Issue / PR] addresses lack of a functionality or an open possibility of enhancement
Projects
xrange@2.2
  
Rejected
xrange@2.3
To be considered
Development

No branches or pull requests

1 participant