Kevin Nelson Marshall
Other entries:
« Why should I learn Factor?

First - thanks to everyone that came out to the Factor meet up last night in NYC. It was really great to meet so many smart people and to get a chance to learn with them. There's nothing quite like the feeling of being the dumbest guy in the room -- which I clearly was last night!

We were especially lucky to have Dan in from out of town for the night. He taught us all a lot in just a little bit of time. My brain hurt after the first few minutes, in that "I understand about 1/4th of what he's saying" kind of way.

Anyway, enough about all that. Let's get back into some of my own stumbling around with Factor. This time around, I thought I would compare how to read file contents in various langauges (Java, Ruby, and Factor again)

So we need to start with some source file that we'll be reading in with each of our programs. I'm using an HTML file but really it could be any type of plain-text file (I won't get into binary files right now; remember I'm a Factor newbie so let's keep it simple!).

I'm saving my plain-text file as index.html and putting it in my "C:factor" directory. You could put this anywhere you like within your file system, just make sure you know where so you can reference it in our programs.

<html>
<head>
<title>TESTING</title>
</head>
<body>
Factor Rocks!
</body>
</html>



Now that we have a file we want to read in, let's start with our Java version. We'll call this file filetest.java

import java.io.*;
public class filetest {
public static void main(String[] args) {
try {
FileReader fr = new FileReader("C:\factor\index.html");
BufferedReader br = new BufferedReader(fr);
String curline = "";
while ((curline=br.readLine()) != null) { System.out.println(curline); }
} catch (Exception e) { }
}
}



The next step of course is to compile our java program and then execute it. We do that with the following two commands at our command line:

javac filetest.java
java filetest



Assuming we typed and saved everything right, this should have displayed the lines of our index.html file back out to the screen in our command window, which is just what we wanted. So let's take a look at the same program in Ruby next.

We'll save this next file as filetest.rb

temp = File.open("c:\factor\index.html", "r+")
temp.each do |line|
puts line
end



And now we can run the Ruby version from the command line with the following command:

ruby filetest.rb


Again, assuming we typed and saved everything correctly, we should have gotten the same result as the Java program (only with a little less typing and work). Now let's wrap up our comparison with the following Factor version.

This time we'll save the next file as filetest.factor in our c:factor
directory

USING: io io.files sequences ;
"C:\factor\index.html" <file-reader> lines [ print ] each



And finally we can run our factor program from the command line with the following command:

factor-nt -run=none "filetest.factor"



NOTE: As you can probably guess from the line above, I'm doing all of this on a windows machine this time around. Your command(s) might need to be changed a bit depending on your set up (check out the docs or FAQ for more information on running programs from the command line). Also, I feel like I should at least mention that I have both Ruby and Java installed with all the proper PATH and CLASSPATH variables set -- so I was able to execute those scripts pretty much from whatever directory I wanted. You may need to adjust all of that to meet your own specific situation.

OK so now that we've seen them all in action, let's break down the Factor version (I leave it to you to know or figure out the details/reasons of the other two versions since I'm just doing this for the purposes of learning Factor).

The first thing we needed to do as in almost all Factor programs was define our USING clause. Files are handled with the io.files vocabulary, and since we wanted to use the 'lines' word, we also had to reference the more general io vocabulary. The lines word itself accepts a stream and returns a sequence, so we also needed to reference the sequences vocabulary so that we will be able to eventually print it all out.

Next we just need to perform the actual actions we want by executing the various words. Since we want to read data in from an external file, the first word we need to execute is <file-reader>. <file-reader> takes a path which is really just a string pointing to the location of your file (like most languages the backslash is an escaping character, so we have to double up on those to make things work properly).

The <file-reader> word returns an input stream, which just so happens to be what the 'lines' word accepts. lines actually just takes a stream and builds a sequence like we mentioned above.

Finally now that we have a sequence (containing one line of the file per element of the sequence) we just need to step through and print out each element. We do this with the 'each' word which applies a 'quote' (the stuff inside the brackets) to each element of the sequence.

Somehow - that's it.

But don't take my word for it, try it all out on your own and let me know what you find or figure out!

posted by Kevin Marshall on 2008-01-13 00:00:00+00


Subscribe to my RSS feed »

BotFu feed with RSS reader

BotFu feed by Email


Search All Posts »


Kevin Marshall - Who's That?

I'm just your basic programmer. I can't spell to save my life, I'm not the greatest story teller, and I often ramble on about nothing. This blog showcases all of that!

If you're bored drop me an email at info at falicon.com or view my outdated resume.


Stalk Kevin on »

bit.ly
Delicious
Digg
Disqus
Facebook
Flickr
FriendFeed
Github
Last.fm
LinkedIn
StumbleUpon
Twitter (@falicon)

Archives by Category »

(28) Code »
(8) ColdFusion »
(15) Database »
(10) Factor »
(3) Falcons »
(321) General »
(13) JavaScript »
(18) Perl »
(17) PHP »
(20) Ruby »

Archives by Month »

(1) September 2010 »
(2) August 2010 »
(3) July 2010 »
(13) June 2010 »
(8) May 2010 »
(2) April 2010 »
(2) March 2010 »
(5) January 2010 »
(2) October 2009 »
(6) August 2009 »
(11) July 2009 »
(2) May 2009 »
(3) April 2009 »
(2) March 2009 »
(7) February 2009 »
(9) January 2009 »
(14) December 2008 »
(5) November 2008 »
(12) October 2008 »
(13) September 2008 »
(16) August 2008 »
(23) July 2008 »
(20) June 2008 »
(24) May 2008 »
(23) April 2008 »
(27) March 2008 »
(28) February 2008 »
(26) January 2008 »
(7) December 2007 »

Published Works »

Beginning Amazon's SimpleDB (Apress in dev.)
Pro Active Record (Apress 2007)
Web Services with Rails (O'Reilly 2006).

Contributed To »

Ruby Cookbook (O'Reilly 2006)
SQL Cookbook (O'Reilly 2005)
Various Reviews published in Computing Reviews

Free Code I've Created »

SimpleDB library in Python 3.0



This blog is powered by KickAssCode.