zzz6519003/blog

io workout

Opened this issue · 2 comments

Complex := Object clone do (
  setValues := method(real, imaginary, 
    self real := real; 
    self imaginary := imaginary; 
    self
  )
  + := method(other, 
    result := Complex clone setValues(self real + other real, self imaginary + other imaginary)
  ) 
)
 
c1 := Complex clone setValues(1, 2)
c2 := Complex clone setValues(3, 4)
 
c3 := c1 + c2
c3 println
 
/* 
 Complex_0x22ee90:
  imaginary        = 6
  real             = 4
*/
(1 / 0) println // inf
(1 / 2) println // 0.5
 
Number originalDivision := Number getSlot("/")
Number / := method(other, 
  if (other == 0, 0, self originalDivision(other))
)
 
(1 / 0) println // 0
(1 / 2) println // 0.5
Write a program to add up all the values in a 2-dimensional array.

sum2dArray := method(arr,
  arr flatten sum  
)
 
arr := list(list(1, 2), list(3, 4), 5, list(6, 7, 8), 9, 10)
sum2dArray(arr) println // 55