Week 2 Day 2 – Io craziness!

Io, you mad sexy beast! What kind of craziness is going on here?!

You don’t hide any secrets, do you? I can see all the operators by typing OperatorTable. I see their order of precedence. I can add an operator anywhere i like!

But i’m afraid i’m lost with your message targets and receivers. I’m looking at the example in the book but i’m really not sure what it’s getting at. I think i’m going to have to be guided to try my own example that needs message reflection before i really get this.

Still, cool that i can can implement an ‘unless’ … i see why they say that everything in Io is a message, even things that look like keywords.

Ah, i understand object reflection much more easily. Nice bit of recursion going on there, going up through the prototype hierarchy.


Well, it’s fair to say that day 2 is really stretching me. I spent ages on the fibonacci calculator last night, and still wasn’t happy with my output. I did a recursive calculator which i understood well but it was slow. I then did an iterative calculator which i couldn’t comprehend so well but at least it was faster. I wanted to do some clever algorithm using Phi, but i was too tired!

Tonight i got a bit further. I was stuck by the division by zero problem until i realised that you can use getSlot(“/”) to pull out the current functionality to save for later.

Adding up all the numbers in a 2D array was nice. I first did it as a stand-alone method, like this:

sum2d := method(array,
  total := 0
  array foreach(i, innerArray, total = total + innerArray sum)
  total
)

sum2d(array)

I later had the idea of adding a sum2d slot on the List prototype, like this:

List sum2d := method(
  total := 0
  self foreach(i, innerArray, total = total + innerArray sum)
  total
)

array sum2d

I got the idea from the myAverage slot exercise, which i also enjoyed. I found it quite fascinating that you can extend List so easily like this:

List myAverage := method(
  self sum / self size
)

Later on i went into more detail so as to perform an average only on numeric values in the list:

List myAverage := method(
  sum := 0
  size := 0
  self foreach(i, value,
    if(value type == "Number", sum = sum + value; size = size + 1)
  )
  if(size == 0, return 0)
  sum / size
)

I was very pleased to learn today that, like Ruby, Io has a return option to get out of a method early. That saves a lot of code.

I’m really liking what i’ve seen so far of Io. The hardest thing is getting to know the syntax and idioms.

I still have more to do on Day 2, but that’ll do for tonight!

Update

P.S. Tomorrow i want to make a simple testing framework. I want to be able to do this:

list(1, 2, "only", "average", "numbers", 3) myAverage should == 2

With what i’ve learned so far in Io i think i can do it.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s