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

Add a method to get the size(length) of interval instance. #42

Closed
hthayko opened this issue Oct 7, 2020 · 5 comments
Closed

Add a method to get the size(length) of interval instance. #42

hthayko opened this issue Oct 7, 2020 · 5 comments
Labels
enhancement New feature or request wontfix This will not be worked on

Comments

@hthayko
Copy link

hthayko commented Oct 7, 2020

e.g.
P.open(1, 2).size() == 1
P.closed(1, 2).size() == 1
(P.open(1, 2) | P.closed(4, 5)).size() == 2

@hthayko
Copy link
Author

hthayko commented Oct 7, 2020

smth like:

def intervalSize(p):
	ret = 0
	for i in p:
		ret += i.upper - i.lower
	return ret

@AlexandreDecan
Copy link
Owner

I understand that this would be a nice, simple feature to add to portion. However, it assumes that objects being used as bounds of an interval can be subtracted, which is not an assumption that holds in all possible cases. Therefore, I'm afraid I cannot add this to portion.

Also, I do not agree that P.open(1, 2).size() should return 1 :-) For integers, it should be 0, and for floats, it should be the highest possible float whose value is strictly less than 1.

@AlexandreDecan AlexandreDecan added enhancement New feature or request invalid This doesn't seem right labels Oct 7, 2020
@hthayko
Copy link
Author

hthayko commented Oct 7, 2020

Quoting documentation: "Note that discrete intervals are not supported by portion" so I assume the numbered intervals represent sets of real numbers, in which case, the length of an interval is defined as upper - lower, no matter if the ends are open or closed.

Re subtraction being a prerequisite: I agree, it breaks the consistency, but imho the most use cases of portion will be dealing with numbers, and the benefit of having this nice function will make it worth it.

@AlexandreDecan AlexandreDecan added wontfix This will not be worked on and removed invalid This doesn't seem right labels Oct 7, 2020
@AlexandreDecan
Copy link
Owner

Discrete domains are implicitly supported, but not for numbers, since we cannot (always) distinguish between floats and integers (i.e. given [1, 2], does this correspond to integers or real numbers?). For instance, portion works very well with characters, dates, versions (the use case that made me developing this library), etc.

However, if the general definition of "length of an interval" for real numbers is the one you provided, I expect that anyone needing to compute the length of an interval will implement the corresponding function by herself on top of portion. I think it's not a good idea to "break the generalizability" of portion for such a simple function (a potential one-liner would be sum((x.upper - x.lower for x in interval)).

@AlexandreDecan
Copy link
Owner

I close this issue, but I'll definitely keep the idea in mind for a potential next major release of portion (I'm currently thinking of allowing to create "specialized"/typed intervals in complement to the generic ones, allowing users to provide custom domains/comparisons/operations/infinities).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

2 participants