Skip to content
This repository has been archived by the owner on Dec 29, 2022. It is now read-only.

Comparison of range objects #139

Closed
laurentlb opened this issue Oct 26, 2018 · 1 comment
Closed

Comparison of range objects #139

laurentlb opened this issue Oct 26, 2018 · 1 comment
Assignees

Comments

@laurentlb
Copy link
Contributor

laurentlb commented Oct 26, 2018

According to the specification:

Range values are comparable: two range values compare equal if they denote the same sequence of integers, even if they were created using different parameters.

The interpreter contains a bug: range(0, 5, 10) == range(0, 5, 11) returns False.
Bazel, Python2, and Python3 return True (Bazel's implementation is not necessarily the best, as it iterates over the values).

@alandonovan
Copy link
Contributor

Well spotted! The fix is that we should only compare the step if len > 1.

func rangeEqual(x, y rangeValue) bool {
	// Two ranges compare equal if they denote the same sequence.
	if x.len != y.len {
		return false // sequences differ in length
	}
	if x.len == 0 {
		return true // both sequences are empty
	}
	if x.start != y.start {
		return false // first element differs
	}
	return x.len == 1 || x.step == y.step
}

testdata/builtins.star

+assert.eq(range(0, 5, 10), range(0, 5, 11))

I won't fix it just yet because I'm hoping to cut over to the new repo any day now.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants