Finishing up with Io

As I reached Day 2 with Io, I continued to struggle with the syntax, in particular temptation to write object.method instead of dropping the ‘.’. I felt that I was starting to get some of the concepts of the language, the ability to rewrite core methods and override Operators.

However, I have also struggled throughout Day 2 and Day 3 with the lack of easily available documentation – the reference and guide on the main IO website did not seem to explain many of the things I needed to know, including how to read input from the console. I resorted in some cases to “cheating” by reading other people’s solutions to the exercises (although it’s not really cheating, since the point is to learn :)).

As I wrote my solutions, particularly the longer ones, they felt slightly clumsy – as though I wasn’t taking advantage of Io’s strengths. In the middle of Day 2 and start of Day 3, I really wasn’t enjoying working with the language, although by the end of Day 3 when I introduced the new Xml_Element object, it felt more natural. I’m glad I finished the chapter, although I’m still not really sure where I would use Io.

Overall, the main thing I’ve got out of the book so far is a greater understanding of meta-programming, which I’m hoping will help me take better approaches to new programming problems.

All of my solutions are on Github Gists:

Here are some highlights from the difficulties I ran into …

I was baffled for Day 2, Exercise 2 as to how to keep the original division method, but a quick internet search (and a minor cheat) revealed the solution: I needed to store the original division method in another variable:

When I reached Exercise 7, I discovered from other solutions that serialization in IO is actually really, really easy. All objects have a serialized method that writes it out to a string. When reading the object back from a file, all that was needed was the assign the results of the doFile("filename.txt") method to a new object. Writing to files is easy too – here’s the full solution:

For Exercise 8, I spent a considerable amount of time trying to find out how to read input from the console – turns out you can use File standardInput readLine. This gives you a string, so you need to use asNumber to complete this exercise.

Day 3, Exercise 1, was a massive struggle – at first I could not figure out how to pass the right indent, and I ended up looking around at some other examples before I figured out that I needed to:

  1. Write the parent node
  2. Add one to the indent
  3. Process the child nodes
  4. Remove one from the indent
  5. Close the parent node

I also discovered that using indent := indent + 1 doesn’t work – I guess you lose the reference to indent, so you need to do indent = indent + 1. Here’s the finished code:

In Exercise 2, I discovered after some internet searching and experimenting that Io has two built in methods: curlyBrackets (as used in the book) and squareBrackets – these can both be overridden to allow lists or maps, or anything else, to be created using {} or []. I guess this might be obvious to some people, but I was quite confused by it!

Here’s the list code:

The final exercise, adding attributes to the XML Builder, had me stumped for some time – in fact, I took a break from it overnight and a solution came to me in the shower :) My difficulty was figuring out when to write the contents of the XML node, I didn’t want a separate method but couldn’t write out the contents inside the loop.

The final solution still feels slightly clumsy but it does work (indents and all):

IO – Day 1

The group as a whole seems to have slowed down a bit on IO, with only a few of us sharing our Day 1 experiences. Installation has bitten a few people. I was lucky in that I was able to install it painlessly on OSX using Homebrew.

My next challenge with IO was figuring out the syntax for comments – turns out it’s a double slash: //

The homework: there was a lot of questions to answer, so I figured it was better to complete it in a blog post rather than a gist.

Stuff to find

Example problems
I wasn’t sure if they are looking for puzzles to solve, or problems with the IO codebase. I couldn’t find the former, but here are some problems with the codebase: http://en.wikibooks.org/wiki/Io_Programming/Pitfalls

Community
Yahoo group: http://tech.groups.yahoo.com/group/iolanguage/

Style guide
http://en.wikibooks.org/wiki/Io_Programming/Io_Style_Guide

Questions to answer

  1. IO is strongly typed. The following commented out code causes an error because “one” is not a number.
  2. All are true:
  3. Use slotNames:
  4. ::= Creates slot, creates setter, assigns value
    := Creates slot, assigns value
    = Assigns value to slot if it exists, otherwise raises exception

    The output of the slotNames in the code below reveals that Elf has the following slots: type, weapon, setWeapon, name

Stuff to Do

  1. To run an IO program from a file, just open terminal and type io filename.io.
  2. To execute the code in a given slot, simply call the slot.

Note: for the last piece of homework, I was trying to think of what to write code about. I recently moved into a new flat, and my new housemate has a small, very cute dog, who has just discovered a new game: fling his smackos under the bed, and then bark at it until she comes to retrieve it for him. I decided to write a very simplified version of that :)