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".