Skip to content

Commit 8837d5a

Browse files
committed
Compiled versions of 'any' and 'all'.
1 parent 0d9e724 commit 8837d5a

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

typed_python/compiler/python_object_representation.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
from typed_python.compiler.type_wrappers.bytes_wrapper import BytesWrapper, BytesMaketransWrapper
7373
from typed_python.compiler.type_wrappers.python_object_of_type_wrapper import PythonObjectOfTypeWrapper
7474
from typed_python.compiler.type_wrappers.abs_wrapper import AbsWrapper
75+
from typed_python.compiler.type_wrappers.all_any_wrapper import AllWrapper, AnyWrapper
7576
from typed_python.compiler.type_wrappers.min_max_wrapper import MinWrapper, MaxWrapper
7677
from typed_python.compiler.type_wrappers.repr_wrapper import ReprWrapper
7778
from types import ModuleType
@@ -262,6 +263,12 @@ def pythonObjectRepresentation(context, f, owningGlobalScopeAndName=None):
262263
if f is max:
263264
return TypedExpression(context, native_ast.nullExpr, MaxWrapper(), False)
264265

266+
if f is any:
267+
return TypedExpression(context, native_ast.nullExpr, AnyWrapper(), False)
268+
269+
if f is all:
270+
return TypedExpression(context, native_ast.nullExpr, AllWrapper(), False)
271+
265272
if f is repr:
266273
return TypedExpression(context, native_ast.nullExpr, ReprWrapper(), False)
267274

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Copyright 2017-2023 typed_python Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from typed_python import Entrypoint, TupleOf
16+
17+
18+
def test_compiles_any_and_all():
19+
@Entrypoint
20+
def callAny(x):
21+
return any(x)
22+
23+
@Entrypoint
24+
def callAll(x):
25+
return all(x)
26+
27+
assert callAll.resultTypeFor(list).typeRepresentation is bool
28+
assert callAny.resultTypeFor(list).typeRepresentation is bool
29+
30+
for T in [list, TupleOf(int)]:
31+
assert callAny(T([1, 2, 3])) == any(T([1, 2, 3]))
32+
assert callAll(T([1, 2, 3])) == all(T([1, 2, 3]))
33+
assert callAny(T([0, 0, 0])) == any(T([0, 0, 0]))
34+
assert callAll(T([0, 0, 0])) == all(T([0, 0, 0]))
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Copyright 2017-2019 typed_python Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from typed_python.compiler.type_wrappers.wrapper import Wrapper
16+
import typed_python.compiler.native_ast as native_ast
17+
18+
19+
def all(iterable):
20+
for a in iterable:
21+
if not a:
22+
return False
23+
24+
return True
25+
26+
27+
def any(iterable):
28+
for a in iterable:
29+
if a:
30+
return True
31+
32+
return False
33+
34+
35+
class AllWrapper(Wrapper):
36+
is_pod = True
37+
is_empty = False
38+
is_pass_by_ref = False
39+
40+
def __init__(self):
41+
super().__init__(print)
42+
43+
def getNativeLayoutType(self):
44+
return native_ast.Type.Void()
45+
46+
def convert_call(self, context, expr, args, kwargs):
47+
return context.call_py_function(all, args, kwargs)
48+
49+
50+
class AnyWrapper(Wrapper):
51+
is_pod = True
52+
is_empty = False
53+
is_pass_by_ref = False
54+
55+
def __init__(self):
56+
super().__init__(print)
57+
58+
def getNativeLayoutType(self):
59+
return native_ast.Type.Void()
60+
61+
def convert_call(self, context, expr, args, kwargs):
62+
return context.call_py_function(all, args, kwargs)

0 commit comments

Comments
 (0)