计算列表所有数字的平均值

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异常
发布了40 篇原创文章 · 获赞 7 · 访问量 1077

猜你喜欢

转载自blog.csdn.net/BobDay/article/details/104133905