Boo is new, well at least relatively new. If you are familiar with Python and C#, you can feel right at home. Most of Boo is written in C# by a Python devotee. Boo has static typing, but also mimics dynamic typing by inference. This way you are not in a complete straight-jacket. It runs in the NET or MONO environment, can be interpreted or compiled and a Python-like shell is available too. Boo is open source, developed enough to allow for serious experimentation. For those of you who have used the SharpDevelop IDE for C# there is a Boo plug-in. This code snippet will compile to a 10k executable file and can be distributed along with a 64k Boo.Lang.dll (runtime DLL) to other .NET or Mono computers. A utility to convert Boo to C#, or C# to Boo has been published.
A Taste of Boo
ddanbe commented: Thanks for sharing! +14
# Just a taste of Boo ...
# Boo is a language that can be interpreted (with Booi.exe) or compiled
# (with Booc.exe), there is even a shell similar to the Python shell (Booish.exe).
# Boo works in the NET or MONO environment on Windows or Unix systems.
# You may notice the Python like syntax and some concepts borrowed from C#, Perl
# and Ruby. It is easy to translate Python to Boo, but there are differences!
# Boo is open source and you can download it from: http://boo.codehaus.org/
# You do need the Microsoft NET or open source MONO environment on your machine.
# Most any programmer's editor will do, or download Booxw.exe from the above site.
# The Boo plugin for the SharpDevelop IDE works well too.
# tested with BOO_0.7.0 vegaseat 09nov2005
/*
one line comments start with a Python # or a C //
you are looking at an example of a multiline comment
*/
# any imports have to be done first ...
import System.IO // for Directory
# next come all classes and functions ...
# (at least at this point in time)
# the indented lines are part of the function
# Boo uses the Python indented statement block
# the function's argument types have to be declared
# function return types can be inferred
def convertFahrenheit2Celsius(fahrenheit as double):
"""this would be the document string for the function"""
celcius = 0.555 * (fahrenheit - 32)
return celcius
# simple stuff first ...
print "Show a result the Python way ..."
print "Simple math like 12345679*63 =", 12345679*63
print "\n... or using Boo string interpolation ..."
print "Simple math like 12345679*63 = ${12345679*63}"
# print just an empty line
print
print "Display numbers 0 to 9 ..."
# the indentation makes the print statement part of the loop
for number in range(10):
print number
print
print "Display numbers 0 to 9 on one line ..."
# print is a macro of System.Console.WriteLine() and adds a newline
# use System.Console.Write() to stay on the same line
# (using a comma to prevent the newline like in Python gives an error with Boo)
for number in range(10):
//print number,
System.Console.Write(number)
print; print
# just in case you think Boo has only for loops
# there is a while loop too
print "Count from 10 to 15, skip 13 ..."
k = 10
while k <= 15:
unless k == 13:
print k
k++
# print 50 dashes
print "-"*50
# a little more complex this time
# Boo does have its roots in the C# language
# so we are using C# format specifiers in the output
# (the Python C-like format specifier % does not work!)
print "Square root of integers 0 to 9 formatted as a float with 4 decimals:"
for value in range(10):
squareRoot = System.Math.Sqrt(value)
System.Console.WriteLine("sqrt({0:D1}) = {1:F4}", value, squareRoot)
print
print "A not so typical for loop:"
for food in "spam", "eggs", "cumquats":
print "I love", food
print
# strings can be enclosed in " or '
# (only " allows string interpolation)
animal = "hippopotamus"
print "this is the string = ", animal
print "length of string = ", len(animal)
# a short intro to string slicing
# a little cryptic at first blush, but very powerful
# [begin : < end] note: end is exclusive
# defaults are begin = 0, end = length
# Python also has step, which is not implemented yet in Boo ...
print "exclude first 3 char = ", animal[3: ]
print "exclude last 4 char = ", animal[:-4]
print "reverse the string:"
for c in reversed(animal):
System.Console.Write(c)
print; print
print "reverse first name and last name:"
name = "Ferdinand Porsche"
# Boo has built-in regex support, \s is space
firstname, lastname = @/\s/.Split(name)
print lastname, firstname
# or you can do it the no-regex way ...
firstname, lastname = name.Split() # default is whitespace
print lastname, firstname
print
# Boo has lists similar to Python
list1 = List(range(8))
print list1.GetType() # Boo.Lang.List
print list1 # [0, 1, 2, 3, 4, 5, 6, 7]
print "number of items in list = ", len(list1)
# lists can be sliced too ...
print "exclude first 3 items in list = ", list1[3:]
print "first 3 items in list = ", list1[:3]
print "exclude last 2 items in list = ", list1[:-2]
# create a list of all items above value 3 ...
# (do behaves like a function)
list2 = list1.Collect() do (item as int):
return item > 3
print "list1 = ", list1
print "all items > 3 = ", list2
# add to the list with Add or Push ...
list1.Add("any")
list1.Push("type")
# add another list ...
list1.Extend(["can", "be added", 3.1415])
print "added more items:", list1
# index is zero based
print "'any' is at index", list1.IndexOf('any')
# find 'can' ...
if 'can' in list1:
print "'can' has been found"
# or ...
print "indeed 'can' has been found" if 'can' in list1
# use list comprehension ...
list3 = [k for k in list1 if k isa string]
print "just the string items: ", list3 # [any, type, can, be added]
# join the strings in list3 ...
str1 = list3.Join(" ")
print "join the strings: ", str1 # any type can be added
print
# Boo has arrays that are similar to Python tuples
arr1 = array('ABCDE')
print arr1 # System.Object[]
print arr1.GetType() # System.Object[]
print List(arr1) # [A, B, C, D, E]
print arr1[1] # B
if arr1[1] isa string:
print arr1[1]
else:
print arr1[1].GetType() # System.Char
for item in arr1:
print item, cast(int, item) # prints char and ASCII number eg. A 65
print
print "Test boolean results ..."
print "Is 3 > 5? Result =", 3 > 5 # False, yes False and not false as documented
print "Is 3 < 5? Result =", 3 < 5 # True
print
/*
Duck typing was coined by Dave Thomas in the Ruby community:
"if it walks like a duck, and talks like a duck, then it is a duck".
This is a way to fake being a dynamic language like Python.
Useful, if you don't exactly know the type returned by an external call.
*/
d as duck // d accepts different types
d = 21 // sets d to an integer
print d
d += 12 // it can do everything an integer does
print d
d = "hypocrite" // sets d to a string
print d
d = d.ToUpper() // now it can do everything a string does
print d
print
# show files of a given extension from a given folder
ftype = "*.boo"
folder = "./" // current folder
//folder = "D:/Boo/BooTest/" // or specified folder
// print "Show string the Python way ..."
// print "All " + ftype + " files in folder " + folder + " :"
// ... or the Boo way ...
print "All ${ftype} files in folder ${folder} :"
for fname in Directory.GetFiles(folder, ftype):
print fname
print
# let's use the function we defined at the beginning
# (make sure you define/create the function before you call it)
print "A Fahrenheit to Celcius table:"
# range is from -40 to < 220 in steps of 10
for tf in range(-40, 220, 10):
System.Console.WriteLine("{0:F1}F = {1:F1}C", tf, convertFahrenheit2Celsius(tf))
print
print "A more limited table:"
# another variation of the for loop ...
for tf in -40,0,32,98.6:
System.Console.WriteLine("{0:F1}F = {1:F1}C", tf, convertFahrenheit2Celsius(tf))
prompt("Press enter to exit ...") // optional console wait
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.