Sunday, November 30, 2008

Adventures in Reia land, part 1: iteration

One of my current interests is the new Reia programming language, created by Tony Arcieri. The wiki contains most of the documentation, but but besides a simple "Hello, World!" and Fibonacci implementation, there is not much working code. Reia is a work in progress, so that makes sense.

Philipp Pirozhkov created Ryan, a web framework built on top of Reia and YAWS. For reasons I have yet to figure out, it does not want to build on my machine. At least there's more code to look at to learn the language. Ryan also has a RSpec-like syntax for writing tests, which look interesting.

Starting with web development might be a bit too ambitious for me. I actually managed to mess up a simple "Hello, world!" example, so I'm starting slow.
Note to self: methods in Reia have parentheses. It's not Ruby, where you can omit them.

Wrong hello world:
puts "Hello, world!"
Proper hello world:
puts("Hello, world!")
Sincy my brain still thinks in Ruby, let's start with a simple bit of Ruby code and convert it to Reia.

First, the Ruby code:
[1,2,3].each { |n| puts n }
This prints out three lines with 1, 2 and 3 on them.
Now the same code in Reia:
[1,2,3].each { |n| puts(n.to_s()) }
The two obvious differences:
  1. All method calls need their parentheses. So use "puts('String')" instead of "puts 'String'".
  2. The int has to be explicitly cast to a String.
Just like Ruby, Reia has multiple ways to write this code. Another Ruby-esque way to write it is by using the 'do' block notation instead of curly braces:
[1,2,3].each do |n|
puts(n.to_s())
Notice the puts() is indented and there is no "end": that is the Python-style indentation at work. A third way to write the code is by (ab)using List Comprehensions:
[puts(x.to_s()) | x in [1,2,3]]
This is the strangest form for me, since Ruby does not have something similar. The way I interpret it is by reading from right to left: for each x in [1,2,3], do the puts thingy left of the pipe.

With that, I conclude my first post on Reia. Now let's see if I can get Google's code prettify syntax highlighting to work.

1 comment:

phil pirj said...

Hey, Wes!

There are few odd things about reiac, that currently prevents from building ryan in a very straightforward way.
But building ryan is only required to run that yaws+reia+ryan thing. I'm not reaaly sure at the moment, will yaws be the choice, i personally think about mochiweb and inets as a target http server.
And Tony thinks that there's no need to compile reia to beam, but it's rather better to load them at runtime.
A testing framework - is still work to be done. But it's pretty simple for implementation, it just waits for block support in method calls to be implemented in Reia.
Keep in touch