Boo - gregorymorrison/euler1 GitHub Wiki

Boo is a statically-typed object-oriented language for the .NET runtime introduced in 2003. Its syntax is inspired by Python. It comes with a compiler, an interpreter, and a shell.

Time to play! Let's see if it will run any of my Python Euler1 examples? The first one only required me to change xrange to range, and declare x's type. Hooray!

#!/usr/bin/booi 
# Euler1 in Boo

def euler1(x as int):
    result = 0

    for i in range(x):
        if i%3==0 or i%5==0:
            result += i

    return result

print euler1(1000)

This next example took me a little bit to get to work. I couldn't find any functions for sum or reduce, so I had to write my own. Hmm... It also required me to figure out Boo's casting mechanism:

#!/usr/bin/booi 

def sum(ls):
    retval = 0
    for l in ls:
        retval += l cast int
    return retval

def euler1(n as int):
    i = 0
    while i < n:
        if i%3==0 or i%5==0:
            yield i
        i += 1

print sum(euler1(1000))

The next illustrates some of the differences in OOP between Python and Boo. In Boo, the class keyword self is not required. And a naming convention is enforced on member variables, which changes how getters and setters are implemented:

#!/usr/bin/booi 

class Euler1:
    [Property(Size)]
    _size as int

    [Getter(Result)]
    _result = 0

    def solve():
        for i in range(_size):
            if i%3==0 or i%5==0:
                _result += i

euler1 = Euler1(Size:1000)
euler1.solve()
print euler1.Result

I had to make substantial changes to get the next example to work. Boo doesn't do Functional very well at all:

#!/usr/bin/booi 

myMap = def(size as int): return [x for x in range(size)]
myFilter = def(ints): return [x for x in ints if x cast int%3==0 or x cast int%5==0]
def myReduce(ls):
    retval = 0
    for l in ls:
        retval += l cast int
    return retval

print myReduce(myFilter(myMap(1000)))

Boo has optional duck typing, list comprehensions, and generators. It's hard to believe that Boo wouldn't have a sum function built in  :(

#!/usr/bin/booi 

def sum(ls):
    retval = 0
    for l in ls:
        retval += l cast int
    return retval

euler1 = def(size): return sum(x for x in range(size) if x cast int%3==0 or x cast int%5==0)

print euler1(1000)

And Boo doesn't support tail recursion at all, apparently. Still, it does have the .NET libraries available. And Boo ships with a compiler, an interpreter, and an interactive shell.

I tried playing with the shell, booish.exe, but it didn't seem to handle exceptions like a REPL should; any errors would crash booish and drop me back to the prompt. Maybe you'll have better luck with it  :(

To run these examples, simply call them - they will be executed by Boo's interpreter, booi.exe.

$ euler1.boo
233168

You can also compile them using Boo's compiler, booc.exe:

$ booc euler1.boo
Boo Compiler version 0.9.4.9 (2.10.6 (mono-2-10/378032b Thu, Dec 08, 2011 21:45:06))

$ euler1
233168