You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
origDiv := Number getSlot("/")
Number / = method(denominator,
if(denominator==0, 0, origDiv(denominator))
)
22/0 # --> 0
22/2 # --> 11
3 Sum two-dimensional array
List sum2d := method(
total := 0
self foreach(r,
r foreach(c, total = total + c))
return total
)
2dArray := list(list(1, 2, 3), list(1, 2, 3), list(1, 2, 3))
2dArray sum2d # --> 18
5 Raise exception if list is not number only
NotNumberOnlyListError := Error clone
List sum2d := method(
total := 0
self foreach(r,
r foreach(c,
if(c type == Number,
total = total + c,
Exception raise(NotNumberOnlyListError)
)
)
)
return total
)
l := list("junk", "oh ho")
l sum2d # --> NotNumberOnlyListError
6 Create type for two-dimensional matrix
Matrix := List clone
Matrix dim := method(x, y,
for(i, 0, y-1,
self append(List clone)
for(j, 0, x-1,
self at(i) append(0)
)
)
)
Matrix set := method(x, y, value,
self at(y) atPut(x, value)
)
Matrix get := method(x, y,
return self at(y) at(x)
)
myMatrix := Matrix clone
myMatrix dim(4,4)
myMatrix set(1,1, "ding")
myMatrix set(1,2, "dong")
myMatrix get(1,1) println # --> "ding"
myMatrix get(1,2) println # --> "dong"
Question: How to check if something is a Number? E.g.