List myAverage := method(
if(self isNotEmpty, testType(self), 0)
)
testType := method(
list,
if(list select(type == "Number") == list,
list average,
Exception raise(
"Exception: there are non-numeric items in the list"
))
)
我的思路:
- 空list返回0
- 非空list就用select得到一个原列表的是数字的项组成的list,如果该list的size和原list的size相对,那么原来的list就是纯数字list, 反之则产生一个Io异常