forked from mindspore-Ecosystem/mindspore
Construct supports the filter function.
This commit is contained in:
parent
dddea1803d
commit
9f68d43c32
|
@ -111,6 +111,7 @@ convert_object_map = {
|
|||
T.len: M.ms_len,
|
||||
T.bool_: M.bool_,
|
||||
T.map: C.Map(),
|
||||
T.filter: M.filter_,
|
||||
T.partial: F.partial,
|
||||
T.zip: C.zip_operation,
|
||||
T.enumerate: M.enumerate_,
|
||||
|
|
|
@ -1793,3 +1793,11 @@ def list_append(self_, item):
|
|||
def to_array(x):
|
||||
"""Implementation of `to_array`."""
|
||||
return x.__ms_to_array__()
|
||||
|
||||
def filter_(fun, iter_):
|
||||
"""Support the use of built-in function filter."""
|
||||
result = []
|
||||
for elem in iter_:
|
||||
if fun(elem):
|
||||
result.append(elem)
|
||||
return result
|
||||
|
|
|
@ -27,7 +27,8 @@ from operator import ( # noqa
|
|||
|
||||
# support system function call
|
||||
from builtins import ( # noqa
|
||||
bool, getattr, setattr, len, iter, next, pow, range, map, zip, print, enumerate, isinstance
|
||||
bool, getattr, setattr, len, iter, next, pow, range, map, zip,
|
||||
print, enumerate, isinstance, filter
|
||||
)
|
||||
|
||||
# support functools
|
||||
|
@ -45,7 +46,7 @@ __all__ = ['add', 'sub', 'mul', 'truediv', 'floordiv', 'mod', 'eq', 'ne', 'lt',
|
|||
'matmul', 'getitem', 'setitem',
|
||||
'bool', 'getattr', 'setattr', 'len', 'iter', 'next', 'pow', 'range', 'map', 'zip',
|
||||
'partial', 'print', 'enumerate', 'isinstance',
|
||||
'exp', 'log', 'sin', 'cos', 'tan']
|
||||
'exp', 'log', 'sin', 'cos', 'tan', 'filter']
|
||||
|
||||
|
||||
def MakeTuple(*elts): # pragma: no cover
|
||||
|
|
|
@ -0,0 +1,47 @@
|
|||
# Copyright 2021 Huawei Technologies Co., Ltd
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ============================================================================
|
||||
""" test_filter """
|
||||
|
||||
from mindspore.nn import Cell
|
||||
from mindspore import context
|
||||
|
||||
context.set_context(mode=context.GRAPH_MODE, save_graphs=True)
|
||||
|
||||
|
||||
def is_odd(x):
|
||||
""" Judge whether the parameter is odd """
|
||||
|
||||
if x % 2:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
class NetWork(Cell):
|
||||
""" NetWork definition """
|
||||
|
||||
def __init__(self):
|
||||
super(NetWork, self).__init__()
|
||||
self.func = is_odd
|
||||
|
||||
def construct(self, list_):
|
||||
set_func = filter
|
||||
ret = set_func(self.func, list_)
|
||||
return ret
|
||||
|
||||
|
||||
list1 = [1, 2, 3]
|
||||
net1 = NetWork()
|
||||
result = net1(list1)
|
||||
assert result == (1, 3)
|
Loading…
Reference in New Issue