Moto 0.19.0 beta 3
David Hakim <dhakim-Gkm/TONP9n1Wk0Htik3J/[email protected]> Tue, 11 Mar 2003 18:44:22 -0500
| Newsgroups | gmane.comp.lang.moto.devel,gmane.comp.lang.moto.user |
|---|---|
| Message-ID | <[email protected]> |
The third beta of Moto 0.19.0 is ready. You can download it from
http://www.projectmoto.org/moto_0_19_0_eng_c.tar.gz . Its fixes a nasty
bug that prohibited partially applied functions from being executed
correctly under the interpreter by extension methods.
Here are some examples taken from the Perl cookbook (or actually the
merd cookbook) you can run under beta 3 that demonstrate some of the
fun things you can do with hashes combined with Higher Order Functions:
$use("cstdlib")
$use("codex.util")
${
/* Some common functions we'll want */
void println(Object o) { print <String>o +"\n"; }
void outputNV(Object n,SymbolTable s) {
print <String>n +" => "+<String>s.get(<String>n)+"\n"; }
void outputNA(Object n,SymbolTable s) {
print <String>n +" => {"+join(<String[]>s.get(<String>n),",")+"} \n";
}
void fill(SymbolTable s, String[] names, Object[] values){
for(int i=0;i<length(names);i++) s.put(names[i],values[i]); }
void valueToKey(Object oldkey, SymbolTable src, SymbolTable dest) {
dest.put(<String>src.get(<String>oldkey),oldkey); }
{
print "\n# 5. Hashes\n\n";
SymbolTable age = new SymbolTable();
String[] names = {"Nat","Jules","Josh"};
Integer[] values = {new Integer(24), new Integer(25), new
Integer(17)};
// Fill the age hash
fill(age,names,values);
}
{
print "\n# 5.1. Adding an Element to a Hash\n\n";
SymbolTable food_color = new SymbolTable();
String[] names = {"Apple","Banana","Lemon","Carrot"};
String[] values = {"red", "yello","yellow","orange"};
// Fill the food_color hash
fill(food_color,names,values);
// Add a new food
food_color.put("Rasberry","pink");
// Output the keys
print "Known foods:\n";
food_color.keys().each(println);
print "\n# 5.2. Testing for the Presence of a Key in a Hash\n\n";
print food_color.contains("Banana") + "\n"; // true
print food_color.contains("Martini") + "\n"; // false
print "\n# 5.3. Deleting from a Hash\n\n";
food_color.remove("Banana");
print "\n# 5.4. Traversing a Hash\n\n";
print "\n# 5.5. Printing a Hash\n\n";
food_color.keys().each(outputNV(?,food_color));
// Or if you'd like to sort the keys first
Vector v = new Vector();
food_color.keys().each(v.add);
v.sort(<int(Object,Object)>strcmp);
v.elements().each(outputNV(?,food_color));
}
{
print "\n# 5.6. Retrieving from a Hash in Insertion Order\n\n";
SymbolTable food_color = new SymbolTable();
Vector order = new Vector();
String[] names = {"Apple","Banana","Lemon","Carrot"};
String[] values = {"red", "yello","yellow","orange"};
// Fill the food_color hash
for(int i=0;i<length(names);i++) {
food_color.put(names[i],values[i]); order.add(names[i]); }
// Retrieving from the Hash in Insertion Order
order.elements().each(outputNV(?,food_color));
}
{
print "\n# 5.7. Hashes with Multiple Values Per Key\n\n";
SymbolTable ttys = new SymbolTable();
String[] names = {"Dave","Stefano","Cory"};
String[][] values = {{"tty1",
"tty2","tty3"},{"tty4","tty5"},{"tty6"}};
// Fill the ttys hash
fill(ttys,names,values);
// Output the ttys hash sorted by username
Vector v = new Vector();
ttys.keys().each(v.add);
v.sort(<int(Object,Object)>strcmp);
v.elements().each(outputNA(?,ttys));
}
{
print "\n# 5.8. Inverting a Hash\n\n";
SymbolTable surname = new SymbolTable();
String[] names = {"Mickey","Babe"};
String[] values = {"Mantle","Ruth"};
// Fill the surname hash
fill(surname,names,values);
// namesur will be surname inverted
SymbolTable namesur = new SymbolTable();
surname.keys().each(valueToKey(?,surname,namesur));
// output namesur
namesur.keys().each(outputNV(?,namesur));
}
}$