Skip to content

Commit 8a7aec9

Browse files
corona10ncw
authored andcommitted
builtin: Implement builtin function any, all.
1 parent c0de7de commit 8a7aec9

File tree

3 files changed

+95
-2
lines changed

3 files changed

+95
-2
lines changed

‎builtin/builtin.go‎

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ func init(){
1919
py.MustNewMethod("__build_class__", builtin___build_class__, 0, build_class_doc),
2020
py.MustNewMethod("__import__", py.InternalMethodImport, 0, import_doc),
2121
py.MustNewMethod("abs", builtin_abs, 0, abs_doc),
22-
// py.MustNewMethod("all", builtin_all, 0, all_doc),
23-
// py.MustNewMethod("any", builtin_any, 0, any_doc),
22+
py.MustNewMethod("all", builtin_all, 0, all_doc),
23+
py.MustNewMethod("any", builtin_any, 0, any_doc),
2424
// py.MustNewMethod("ascii", builtin_ascii, 0, ascii_doc),
2525
// py.MustNewMethod("bin", builtin_bin, 0, bin_doc),
2626
// py.MustNewMethod("callable", builtin_callable, 0, callable_doc),
@@ -225,6 +225,63 @@ func builtin_abs(self, v py.Object) (py.Object, error){
225225
returnpy.Abs(v)
226226
}
227227

228+
constall_doc=`all(iterable) -> bool
229+
230+
Return True if bool(x) is True for all values x in the iterable.
231+
If the iterable is empty, return True.
232+
`
233+
234+
funcbuiltin_all(self, seq py.Object) (py.Object, error){
235+
iter, err:=py.Iter(seq)
236+
res:=false
237+
iferr!=nil{
238+
returnnil, err
239+
}
240+
for{
241+
item, err:=py.Next(iter)
242+
iferr!=nil{
243+
ifpy.IsException(py.StopIteration, err){
244+
break
245+
}
246+
returnnil, err
247+
}
248+
ifpy.ObjectIsTrue(item){
249+
res=true
250+
} else{
251+
res=false
252+
break
253+
}
254+
}
255+
returnpy.NewBool(res), nil
256+
}
257+
258+
constany_doc=`any(iterable) -> bool
259+
260+
Return True if bool(x) is True for any x in the iterable.
261+
If the iterable is empty, Py_RETURN_FALSE."`
262+
263+
funcbuiltin_any(self, seq py.Object) (py.Object, error){
264+
iter, err:=py.Iter(seq)
265+
res:=false
266+
iferr!=nil{
267+
returnnil, err
268+
}
269+
for{
270+
item, err:=py.Next(iter)
271+
iferr!=nil{
272+
ifpy.IsException(py.StopIteration, err){
273+
break
274+
}
275+
returnnil, err
276+
}
277+
ifpy.ObjectIsTrue(item){
278+
res=true
279+
break
280+
}
281+
}
282+
returnpy.NewBool(res), nil
283+
}
284+
228285
constround_doc=`round(number[, ndigits]) -> number
229286
230287
Round a number to a given precision in decimal digits (default 0 digits).

‎builtin/tests/builtin.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@
33
assertabs(10) ==10
44
assertabs(-10) ==10
55

6+
doc="all"
7+
assertall((0,0,0)) ==False
8+
assertall((1,1,0)) ==False
9+
assertall(["hello", "world"]) ==True
10+
assertall([]) ==False
11+
12+
doc="any"
13+
assertany((0,0,0)) ==False
14+
assertany((1,1,0)) ==True
15+
assertany(["hello", "world"]) ==True
16+
assertany([]) ==False
17+
618
doc="chr"
719
assertchr(65) =="A"
820
assertchr(163) =="£"

‎py/object.go‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,27 @@ func ObjectRepr(o Object) Object{
1717
// FIXME
1818
returnString(fmt.Sprintf("<%s %v>", o.Type().Name, o))
1919
}
20+
21+
// Return whether the object is True or not
22+
funcObjectIsTrue(oObject) bool{
23+
ifo==True{
24+
returntrue
25+
}
26+
ifo==False{
27+
returnfalse
28+
}
29+
30+
ifo==None{
31+
returnfalse
32+
}
33+
34+
ifI, ok:=o.(I__bool__); ok{
35+
cmp, err:=I.M__bool__()
36+
iferr==nil&&cmp==True{
37+
returntrue
38+
} elseiferr==nil&&cmp==False{
39+
returnfalse
40+
}
41+
}
42+
returnfalse
43+
}

0 commit comments

Comments
(0)