Skip to content

Commit 7c4b61a

Browse files
committed
Make NewBool and use it to simplify code
1 parent f18c29d commit 7c4b61a

File tree

6 files changed

+26
-86
lines changed

6 files changed

+26
-86
lines changed

py/bool.go

+8
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ func (s Bool) Type() *Type {
1616
return BoolType
1717
}
1818

19+
// Make a new bool - returns the canonical True and False values
20+
func NewBool(t bool) Bool {
21+
if t {
22+
return True
23+
}
24+
return False
25+
}
26+
1927
func (a Bool) M__bool__() Object {
2028
return a
2129
}

py/complex.go

+2-10
Original file line numberDiff line numberDiff line change
@@ -224,22 +224,14 @@ func (a Complex) M__le__(other Object) Object {
224224

225225
func (a Complex) M__eq__(other Object) Object {
226226
if b, ok := convertToComplex(other); ok {
227-
if a == b {
228-
return True
229-
} else {
230-
return False
231-
}
227+
return NewBool(a == b)
232228
}
233229
return NotImplemented
234230
}
235231

236232
func (a Complex) M__ne__(other Object) Object {
237233
if b, ok := convertToComplex(other); ok {
238-
if a != b {
239-
return True
240-
} else {
241-
return False
242-
}
234+
return NewBool(a != b)
243235
}
244236
return NotImplemented
245237
}

py/float.go

+7-34
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,7 @@ func (a Float) M__ipow__(other, modulus Object) Object {
195195
}
196196

197197
func (a Float) M__bool__() Object {
198-
if a == 0 {
199-
return False
200-
}
201-
return True
198+
return NewBool(a != 0)
202199
}
203200

204201
func (a Float) M__int__() Object {
@@ -229,66 +226,42 @@ func (a Float) M__round__(digitsObj Object) Object {
229226

230227
func (a Float) M__lt__(other Object) Object {
231228
if b, ok := convertToFloat(other); ok {
232-
if a < b {
233-
return True
234-
} else {
235-
return False
236-
}
229+
return NewBool(a < b)
237230
}
238231
return NotImplemented
239232
}
240233

241234
func (a Float) M__le__(other Object) Object {
242235
if b, ok := convertToFloat(other); ok {
243-
if a <= b {
244-
return True
245-
} else {
246-
return False
247-
}
236+
return NewBool(a <= b)
248237
}
249238
return NotImplemented
250239
}
251240

252241
func (a Float) M__eq__(other Object) Object {
253242
if b, ok := convertToFloat(other); ok {
254-
if a == b {
255-
return True
256-
} else {
257-
return False
258-
}
243+
return NewBool(a == b)
259244
}
260245
return NotImplemented
261246
}
262247

263248
func (a Float) M__ne__(other Object) Object {
264249
if b, ok := convertToFloat(other); ok {
265-
if a != b {
266-
return True
267-
} else {
268-
return False
269-
}
250+
return NewBool(a != b)
270251
}
271252
return NotImplemented
272253
}
273254

274255
func (a Float) M__gt__(other Object) Object {
275256
if b, ok := convertToFloat(other); ok {
276-
if a > b {
277-
return True
278-
} else {
279-
return False
280-
}
257+
return NewBool(a > b)
281258
}
282259
return NotImplemented
283260
}
284261

285262
func (a Float) M__ge__(other Object) Object {
286263
if b, ok := convertToFloat(other); ok {
287-
if a >= b {
288-
return True
289-
} else {
290-
return False
291-
}
264+
return NewBool(a >= b)
292265
}
293266
return NotImplemented
294267
}

py/int.go

+7-34
Original file line numberDiff line numberDiff line change
@@ -307,10 +307,7 @@ func (a Int) M__ior__(other Object) Object {
307307
}
308308

309309
func (a Int) M__bool__() Object {
310-
if a == 0 {
311-
return False
312-
}
313-
return True
310+
return NewBool(a != 0)
314311
}
315312

316313
func (a Int) M__index__() Int {
@@ -343,66 +340,42 @@ func (a Int) M__round__(digits Object) Object {
343340

344341
func (a Int) M__lt__(other Object) Object {
345342
if b, ok := convertToInt(other); ok {
346-
if a < b {
347-
return True
348-
} else {
349-
return False
350-
}
343+
return NewBool(a < b)
351344
}
352345
return NotImplemented
353346
}
354347

355348
func (a Int) M__le__(other Object) Object {
356349
if b, ok := convertToInt(other); ok {
357-
if a <= b {
358-
return True
359-
} else {
360-
return False
361-
}
350+
return NewBool(a <= b)
362351
}
363352
return NotImplemented
364353
}
365354

366355
func (a Int) M__eq__(other Object) Object {
367356
if b, ok := convertToInt(other); ok {
368-
if a == b {
369-
return True
370-
} else {
371-
return False
372-
}
357+
return NewBool(a == b)
373358
}
374359
return NotImplemented
375360
}
376361

377362
func (a Int) M__ne__(other Object) Object {
378363
if b, ok := convertToInt(other); ok {
379-
if a != b {
380-
return True
381-
} else {
382-
return False
383-
}
364+
return NewBool(a != b)
384365
}
385366
return NotImplemented
386367
}
387368

388369
func (a Int) M__gt__(other Object) Object {
389370
if b, ok := convertToInt(other); ok {
390-
if a > b {
391-
return True
392-
} else {
393-
return False
394-
}
371+
return NewBool(a > b)
395372
}
396373
return NotImplemented
397374
}
398375

399376
func (a Int) M__ge__(other Object) Object {
400377
if b, ok := convertToInt(other); ok {
401-
if a >= b {
402-
return True
403-
} else {
404-
return False
405-
}
378+
return NewBool(a >= b)
406379
}
407380
return NotImplemented
408381
}

py/list.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,7 @@ func (t List) M__len__() Object {
2626
}
2727

2828
func (t List) M__bool__() Object {
29-
if len(t) > 0 {
30-
return True
31-
}
32-
return False
29+
return NewBool(len(t) > 0)
3330
}
3431

3532
func (t List) M__iter__() Object {

py/tuple.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,7 @@ func (t Tuple) M__len__() Object {
2424
}
2525

2626
func (t Tuple) M__bool__() Object {
27-
if len(t) > 0 {
28-
return True
29-
}
30-
return False
27+
return NewBool(len(t) > 0)
3128
}
3229

3330
func (t Tuple) M__iter__() Object {

0 commit comments

Comments
 (0)