I'm still attempting to wrap my feebile head around all that is Factor and while I'll admit that I seem to be missing out on some obvious things (like how to access data within a Vector!); I have been able to play around enough to get some pretty neat things working.
Tonight I thought I would share with you a little comparison of a basic script done in a variety of languages to handle a task I actually do quite often in my various work - screen scraping or rather reading in an HTML file from a given URL (I'll leave the parsing bit out for now so all our script examples are going to do is grab a page and show the source).
Just about every language I know of has a way of doing this simple little task and so I thought it might be interesting to show you a generic version in three different languages (Java, Ruby, and Factor).
Assuming you've got Java, Ruby, and Factor set up on your system already here goes ... First the Java version (saved in a file called geturl.java )
import java.net.*;
import java.io.*;
public class geturl {
public static void main(String[] args) {
try {
URL url = new URL("http://www.botfu.com");
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine = "";
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine); } } catch (Exception e) { } } }
Once you've got that file created and saved, drop down to your command line and compile it with the javac geturl.java command and then execute it with the java geturl command. This should dump the html source of www.botfu.com out to you in your command window.
OK so now the (much shorter) Ruby version (saved in a file called geturl.rb).
require 'net/http'
geturl = Net::HTTP.get_response(URI.parse("http://www.botfu.com"))
puts geturl.body
Once you've got that file created and saved, drop down into your command window again and run the ruby geturl.rb command and once again the HTML source of www.botfu.com should be displayed for you in your command window.
Now finally the Factor version. Start up your Factor listener (you could do this via command line as well, but since we are just doing some quick testing/demo it's much nicer in the Listener trust me). Now in the listener type in the following:
USING: http.client ; "http://www.botfu.com" http-get write
And like the previous examples you should once again get the HTML source of www.botfu.com displayed to you (this time within the listener since we ran the command from within there).
And yes kids, it's really that simple. Does one approach have an advantage over another you ask? Well that really depends on what your situation is and what you're most comfortable with ...



