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‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ func (s Bool) Type() *Type{
1616
returnBoolType
1717
}
1818

19+
// Make a new bool - returns the canonical True and False values
20+
funcNewBool(tbool) Bool{
21+
ift{
22+
returnTrue
23+
}
24+
returnFalse
25+
}
26+
1927
func (aBool) M__bool__() Object{
2028
returna
2129
}

‎py/complex.go‎

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

225225
func (aComplex) M__eq__(otherObject) Object{
226226
ifb, ok:=convertToComplex(other); ok{
227-
ifa==b{
228-
returnTrue
229-
} else{
230-
returnFalse
231-
}
227+
returnNewBool(a==b)
232228
}
233229
returnNotImplemented
234230
}
235231

236232
func (aComplex) M__ne__(otherObject) Object{
237233
ifb, ok:=convertToComplex(other); ok{
238-
ifa!=b{
239-
returnTrue
240-
} else{
241-
returnFalse
242-
}
234+
returnNewBool(a!=b)
243235
}
244236
returnNotImplemented
245237
}

‎py/float.go‎

Lines changed: 7 additions & 34 deletions
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 (aFloat) M__bool__() Object{
198-
ifa==0{
199-
returnFalse
200-
}
201-
returnTrue
198+
returnNewBool(a!=0)
202199
}
203200

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

230227
func (aFloat) M__lt__(otherObject) Object{
231228
ifb, ok:=convertToFloat(other); ok{
232-
ifa<b{
233-
returnTrue
234-
} else{
235-
returnFalse
236-
}
229+
returnNewBool(a<b)
237230
}
238231
returnNotImplemented
239232
}
240233

241234
func (aFloat) M__le__(otherObject) Object{
242235
ifb, ok:=convertToFloat(other); ok{
243-
ifa<=b{
244-
returnTrue
245-
} else{
246-
returnFalse
247-
}
236+
returnNewBool(a<=b)
248237
}
249238
returnNotImplemented
250239
}
251240

252241
func (aFloat) M__eq__(otherObject) Object{
253242
ifb, ok:=convertToFloat(other); ok{
254-
ifa==b{
255-
returnTrue
256-
} else{
257-
returnFalse
258-
}
243+
returnNewBool(a==b)
259244
}
260245
returnNotImplemented
261246
}
262247

263248
func (aFloat) M__ne__(otherObject) Object{
264249
ifb, ok:=convertToFloat(other); ok{
265-
ifa!=b{
266-
returnTrue
267-
} else{
268-
returnFalse
269-
}
250+
returnNewBool(a!=b)
270251
}
271252
returnNotImplemented
272253
}
273254

274255
func (aFloat) M__gt__(otherObject) Object{
275256
ifb, ok:=convertToFloat(other); ok{
276-
ifa>b{
277-
returnTrue
278-
} else{
279-
returnFalse
280-
}
257+
returnNewBool(a>b)
281258
}
282259
returnNotImplemented
283260
}
284261

285262
func (aFloat) M__ge__(otherObject) Object{
286263
ifb, ok:=convertToFloat(other); ok{
287-
ifa>=b{
288-
returnTrue
289-
} else{
290-
returnFalse
291-
}
264+
returnNewBool(a>=b)
292265
}
293266
returnNotImplemented
294267
}

‎py/int.go‎

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

309309
func (aInt) M__bool__() Object{
310-
ifa==0{
311-
returnFalse
312-
}
313-
returnTrue
310+
returnNewBool(a!=0)
314311
}
315312

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

344341
func (aInt) M__lt__(otherObject) Object{
345342
ifb, ok:=convertToInt(other); ok{
346-
ifa<b{
347-
returnTrue
348-
} else{
349-
returnFalse
350-
}
343+
returnNewBool(a<b)
351344
}
352345
returnNotImplemented
353346
}
354347

355348
func (aInt) M__le__(otherObject) Object{
356349
ifb, ok:=convertToInt(other); ok{
357-
ifa<=b{
358-
returnTrue
359-
} else{
360-
returnFalse
361-
}
350+
returnNewBool(a<=b)
362351
}
363352
returnNotImplemented
364353
}
365354

366355
func (aInt) M__eq__(otherObject) Object{
367356
ifb, ok:=convertToInt(other); ok{
368-
ifa==b{
369-
returnTrue
370-
} else{
371-
returnFalse
372-
}
357+
returnNewBool(a==b)
373358
}
374359
returnNotImplemented
375360
}
376361

377362
func (aInt) M__ne__(otherObject) Object{
378363
ifb, ok:=convertToInt(other); ok{
379-
ifa!=b{
380-
returnTrue
381-
} else{
382-
returnFalse
383-
}
364+
returnNewBool(a!=b)
384365
}
385366
returnNotImplemented
386367
}
387368

388369
func (aInt) M__gt__(otherObject) Object{
389370
ifb, ok:=convertToInt(other); ok{
390-
ifa>b{
391-
returnTrue
392-
} else{
393-
returnFalse
394-
}
371+
returnNewBool(a>b)
395372
}
396373
returnNotImplemented
397374
}
398375

399376
func (aInt) M__ge__(otherObject) Object{
400377
ifb, ok:=convertToInt(other); ok{
401-
ifa>=b{
402-
returnTrue
403-
} else{
404-
returnFalse
405-
}
378+
returnNewBool(a>=b)
406379
}
407380
returnNotImplemented
408381
}

‎py/list.go‎

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

2828
func (tList) M__bool__() Object{
29-
iflen(t) >0{
30-
returnTrue
31-
}
32-
returnFalse
29+
returnNewBool(len(t) >0)
3330
}
3431

3532
func (tList) M__iter__() Object{

‎py/tuple.go‎

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

2626
func (tTuple) M__bool__() Object{
27-
iflen(t) >0{
28-
returnTrue
29-
}
30-
returnFalse
27+
returnNewBool(len(t) >0)
3128
}
3229

3330
func (tTuple) M__iter__() Object{

0 commit comments

Comments
(0)