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异常
该博客介绍了如何处理一个列表,当列表为空时返回0,非空时通过select方法获取列表中仅包含数字的项,如果筛选后的列表大小与原列表相同,则原列表为纯数字列表,进而计算平均值;否则抛出Io异常。

529

被折叠的 条评论
为什么被折叠?



