zzz6519003 / blog

My blog about coding
4 stars 1 forks source link

io workout #117

Open zzz6519003 opened 5 years ago

zzz6519003 commented 5 years ago
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
*/
zzz6519003 commented 5 years ago
(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
zzz6519003 commented 5 years ago
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