Montreal Motorcycle Show P1
Posted on February 26, 2007
I just saw all the photos I made during the Montreal Motorcycle Show and I think I managed to make at least one great one. Behold! The greatest Ducati admirer!
Filed Under Uncategorized | Leave a Comment
Prolog code
Posted on February 23, 2007
Example of how to draw NxM board in Prolog. Usefull when you need to show the state of a chess board or for games like wolves and sheep. Code from Simon Colton’s tutorial page.
127 cell(A,B) :- 128 L = [1,2,3,4,5,6,7,8], 129 member(A,L), 130 member(B,L). 131 132 draw_board(Board) :- 133 write(‘ 12345678\n‘), 134 write(‘ +——–+‘), 135 findall(_, 136 (cell(A,B), draw_newline(A,B), draw_cell(A,B,Board),draw_endline(B)), 137 _), 138 write(‘\n +——–+\n\n‘). 139 140 draw_newline(A,1) :- 141 write(‘\n‘), 142 write(A), 143 write(‘|‘). 144 draw_newline(_,X) :- 145 X=\=1. 146 147 148 draw_cell(A,B,[Wolves, _]) :- 149 member(A/B,Wolves), 150 write(‘w‘), !. 151 draw_cell(A,B,[_, Sheep]) :- 152 member(A/B,Sheep), 153 write(‘r‘), !. 154 draw_cell(_,_,[_, _]) :- 155 write(‘ ‘). 156 157 158 draw_endline(8) :- 159 write(‘|‘). 160 draw_endline(X) :- 161 X=\=8. 162 Pass the state as a parameter draw_board([[1/1,1/3,1/5,1/7],[2/4]]). and it will display the board: 12345678 +——–+ 1|w w w w | 2| r | 3| | 4| | 5| | 6| | 7| | 8| | +——–+
Filed Under Uncategorized | 2 Comments
Deus ex machina
Posted on February 22, 2007
I know computers don’t have emotions. And especially they can not hate or dislike. But why this particular computer puts my IP address in /etc/hosts.deny? And it’s not like it does so in some systematic way… No. I can login twenty times and then Bam! FSCK you!
Filed Under Uncategorized | Leave a Comment
Could suggest something…
Posted on February 13, 2007
Filed Under Uncategorized | Leave a Comment
Faith Based Programming
Posted on February 11, 2007
What is “Faith Based Programming“? It is simply an extension of accepting God’s plan for my life as it applies to my job as a software engineer. It is putting my complete faith in God that what I program into the machine.
Every morning, before starting, I speak the Programmer’s Prayer: “Lord: I begin today on your Great Work. Guide my hand, and bring forth the Code your Will desires. Give me your Strength to keep my Faith, and your Sensory Deprivation to ignore temptation right before my eyes. Goto Lord.”
And with this prayer looping constantly through my heard, I am prepared to begin my day in Glory.
Filed Under Uncategorized | Leave a Comment
DOM Processing
Posted on February 9, 2007
Suppose that we need to process an XML document:
<?xml version=”1.0″?> <purchaseOrder orderDate=”19991020″> <shipTo country=”US”> <name>Alice Smith</name> <street>123 Maple Street</street> <city>Mill Valley</city> <state>CA</state> <zip>90952</zip> </shipTo> <billTo country=”US”> <name>Robert Smith</name> <street>8 Oak Avenue</street> <city>Old Town</city> <state>PA</state> <zip>95819</zip> </billTo> <comment>Hurry, my lawn is going wild!</comment> <items> <item partNum=”872AA”> <productName>Lawnmower</productName> <quantity>1</quantity> <USPrice>148.95</USPrice> <comment>Confirm this is electricWe need to collect the partNum attributes and compute the sum of the prices in the USPrice elements. In Java we can do this for example using DOM:import org.w3c.dom.Document; import org.w3c.dom.Element; ... //Retrieve the Document object DocumentBuilder fact = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document po = fact.parse(new File("po.xml")); Element root = po.getDocumentElement(); //Retrieve all partNums and compute the grand total for the purchase order double total = 0; NodeList children = root.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node node = children.item(i); //Find the items child element if ("items".equals(node.getLocalName())) { NodeList itemList = node.getChildNodes(); for (int j = 0; j < itemList.getLength(); j++) { Node item = itemList.item(j); //Get the partNum attribute value NamedNodeMap attrs = item.getAttributes(); System.out.println("partNum:" + attrs.getNamedItem("partNum")); //Find the USPrice child element NodeList itemChildren = item.getChildNodes(); for (int k = 0; k < itemChildren.getLength(); k++) { Node child = itemChildren.item(k); if ("USPrice".equals(child.getLocalName()) { total += Double.valueOf(child.getNodeValue()).doubleValue(); } } } } } System.out.println("Grand total = " + total);We can do the same thing in Scala, but in a much simplier way:
import scala.xml.XML;
val doc = XML.loadFile("po.xml");
var total = 0;
for(val z <- doc \\ "item";
val y <- z \ "USPrice") {
Console.println("partnum: " + z \ "@partNum");
total = total + Double.valueOf(y.text)
}
Console.println("Grand total " + total);And according to the documentation, Scala implementation uses half the the memory of DOM model.
Example is from Scalable Programming Abstractions for XML Services.
Filed Under Uncategorized | 1 Comment
Scala, first example.
Posted on February 8, 2007
Here is an example of code in Scala. Scala is a functional/OO hybrid language for JVM and .NET virtual machines.
I started playing with it today.object RSSDump extends Application { val url = "http://www.digg.com/rss/index.xml" val elem = scala.xml.XML.load(url) val items = elem \\ "item" for(val item <- items) { val title = (item \ "title").text Console.println(title) } }Some things to note:
object RSSDump is a singleton object, it has a state.val is a constant value, it can not change. we can do
var i: int i = i + 1 // increment variableif we have to.
Scala functions can have any names, such as \ \\ ! !?, so we and not limited to overloading +-=.
Scala has XML processing build in, elem \\ “item”. More about it in the next post.
Filed Under Uncategorized | Leave a Comment
Octopussy
Posted on February 8, 2007
Octopuses are characterized by their eight arms…
Octopussies, well, let’s just say they are different.Filed Under Uncategorized | Leave a Comment
Ruby pretents to be VB
Posted on February 2, 2007
In REXML elements have index 1, unless they are children of an element, then they start from 0.
Ruby is not fun, I must be doing something wrong.
Filed Under Uncategorized | Leave a Comment
Ruby
Posted on February 1, 2007
Bludy hell, I did not write a single line of ruby code yet, and I hate it already.
- FreeRide does not want to execute ruby interpreter if it is installed on desktop.
- FreeRide freezes when I try to execute the debugger.
- Why RSS parser is in the core library is beyond my understanding but whatever.
- FreeRise crashes when I try to manualy enter the path to ruby’s bin directory.
- Documentation http://www.ruby-doc.org/core/classes/RSS/Parser.html
says that that RSS::Parser has class method parse. How am I supposed to access parsed data or configure what to parse?- Freeride does not display what I print to stdout. Fuck freeride. Lets’ try SciTE.
- Ok, so to find what method returns I have to run puts result.class
- http://www.ruby-doc.org/core/classes/RSS/Rss.html where the fsck is method documentation? Why people praise ruby, and yet core library is not docummented?
….
20 minutes later: it is not a bad language at all, but some things just suck.
Filed Under Uncategorized | Leave a Comment
