Skip to content

Commit e9fec44

Browse files
committed
Add __eq__, __ne__ of range
__eq__ compare length, start, step of range
1 parent 43d93e3 commit e9fec44

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

py/range.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,3 +194,58 @@ func computeRangeLength(start, stop, step Int) Int {
194194
var _ I__getitem__ = (*Range)(nil)
195195
var _ I__iter__ = (*Range)(nil)
196196
var _ I_iterator = (*RangeIterator)(nil)
197+
198+
199+
func (a *Range) M__eq__(other Object) (Object, error) {
200+
b, ok := other.(*Range)
201+
if !ok {
202+
return NotImplemented, nil
203+
}
204+
205+
if a.Length != b.Length {
206+
return False, nil
207+
}
208+
209+
if a.Length == 0 {
210+
return True, nil
211+
}
212+
if a.Start != b.Start {
213+
return False, nil
214+
}
215+
216+
if a.Step == 1 {
217+
return True, nil
218+
}
219+
if a.Step != b.Step {
220+
return False, nil
221+
}
222+
223+
return True, nil
224+
}
225+
226+
func (a *Range) M__ne__(other Object) (Object, error) {
227+
b, ok := other.(*Range)
228+
if !ok {
229+
return NotImplemented, nil
230+
}
231+
232+
if a.Length != b.Length {
233+
return True, nil
234+
}
235+
236+
if a.Length == 0 {
237+
return False, nil
238+
}
239+
if a.Start != b.Start {
240+
return True, nil
241+
}
242+
243+
if a.Step == 1 {
244+
return False, nil
245+
}
246+
if a.Step != b.Step {
247+
return True, nil
248+
}
249+
250+
return False, nil
251+
}

0 commit comments

Comments
 (0)