Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

2009-01-18

Setup mods (clojure)

After much tearing of hair, many emacs restarts, many killings of *inferior-lisp*, I finally decided to give up on the pure-swank way of calling java. Up until yesterday I just had this:

  
(setq swank-clojure-jar-path "/home/joseph/src/clojure/clojure.jar")
(setq swank-clojure-extra-classpaths 
      (list "/home/joseph/clojure/clojure-contrib.jar" 
            "/home/joseph/lib/commons-httpclient-3.1.jar"))
  

Last night I reorganized everything basically following Bill Clementson's setup. Now all the classpath business goes into an environment variable and gets called by an almost empty shell script. For me, the main advantage of this setup is that it was easier to see how java was being called, so that it was easier to debug the rest of my setup.

To make a long story short, I was finally able to import org.apache.commons.httpclient this morning.

2009-01-15

Tiny baby clojure steps

Yesterday I started actually trying to do something with Clojure. Here are my very, very first impressions of what it is like to actually get your feet wet, or your fingers dirty or whatever. This can probably all be resumed with: everything is much more difficult if you don't have any experience with Java. Like me.

This is of course assuming that your system is working correctly. I only finally ended up getting slime, swank and clojure all working together correctly. It isn't that hard, or at least it doesn't seem like it once you see how things fit together. Just a couple of variables for swank, your .jars in a good spot and you are ready to go. It took me a while to realize that of course. And I made stupid mistakes like not compiling clojure-contrib yet expecting it to work. Stupid stupid mistakes.

My initial task was to try to read a file line-by-line. A very Perl-ish thing to do I guess, but it seemed to be a reasonable thing to do. Also, in the initial version of my little project (more about that later), everything started with a config file full of URLs that I was trying to read with bash.

So how do you read a file line by line in Clojure?

Well, here is what I have right now. It works.

(use '[clojure.contrib.duck-streams :only (reader)]) (with-open [r (reader "/home/joseph/localrss/rss_urls")] (doseq [line (line-seq r)] (println line)))

What is strange is that Java is always close by. Just for reading a file, you need Java objects. Here "duck-streams" are a wrapper around some Java stream things. And I had to bring the clojure-contrib .jar into my configuration for this to work. This is just something to get used to, I suppose, and probably feels perfectly natural for Java programmers.

The same seems to go for string functions. The official docs just say :

Clojure strings are Java Strings. See also Printing. user=> (map (fn [x] (.toUpperCase x)) (.split "Dasher Dancer Prancer" " ")) ("DASHER" "DANCER" "PRANCER")
Those
.toUpperCase
and
.split
s look suspiciously like macros calling Java functions, so it looks like I'll be getting chummy with those soon too.

Interestingly, I haven't seen anything about strings being vectors or sequences of some kind, as in Common Lisp. I guess all of that gets thrown away once you beat into your n00b brain that "Clojure strings are Java strings".

2008-03-24

Testing... Testing...

Now that I have started using a test suite -- this one, in fact -- my life has improved. At least I can see why PHP is not doing what I want it to.

There is something very satisfying about testing as you go. It definitely improves the modularity of your code.