Taking a page out of the python book , I like to include a statement
at the end that checks to see if the program is being run as a
command-line program and then respond accordingly.

As an example,

(if (command-line?)
    (your-awesome-function (read-integer (first *command-line-args*))))

Might appear at the end of my file.

command-line? and read-integer are listed below
(ns coderloop.utils
  (:use [clojure.contrib [duck-streams :only [file-str read-lines]]])
  (:use [clojure [string :only [trim blank? split]]]))

(defn read-integer [file]
  (Integer/parseInt
   (trim (slurp (file-str file)))))

(defn command-line? []
  (.isAbsolute (java.io.File. *file*)))

This has the advantage of also working when the namespace is being run
on the command-line with no arguments,
but not when the namespace is being used or required by another
namespace. read-integer is just a simple example for how to read a
single integer from a file.

You can then actually run your program by making a shell script with
something like
#!/bin/bash
java -Xmn500M -Xms2000M -Xmx2000M -server -cp ./lib/*:./src
clojure.main your-namespace-file.clj  $@


A little known fact is that the above can actually be embedded into a
clojure source file with the following trick:

Make this the first line of your clojure namespace to make it executable:

":";exec java -verbose:gc -Xmn500M -Xms2000M -Xmx2000M -server -cp
"your-classpath" clojure.main $0 $*;

The trick is putting the ":"; at the beginning and then calling exec.
To clojure this looks like a literal string followed by a comment.  To
bash it looks like a no-op followed by an invocation of java on the
file itself, and no lines after the first ever get executed. You can
make whatever class structure you cant and then just symlink the
clojure file to be an executable file at the base of the directory.
You can still always go the shell script route if you don't like the
embedding trick. This technique is common with emacs-lisp to make
things executable.

Hope that helps.

sincerely,
--Robert McIntyre


On Sun, Jan 16, 2011 at 9:56 PM, Stuart Campbell <stu...@harto.org> wrote:
> On 17 January 2011 13:48, John Svazic <jsva...@gmail.com> wrote:
>>
>> Benny already answered, but here's the common block that I'm using for
>> my Clojure submissions:
>>
>> (import '(java.io BufferedReader FileReader))
>> (defn process-file [file-name]
>>  (let [rdr (BufferedReader. (FileReader. file-name))]
>>    (line-seq rdr)))
>>
>> (defn my-func [col]
>>  ; Do something interesting
>> )
>>
>> (println (my-func (process-file (first *command-line-args*))))
>>
>> Basically I read the lines of the file into a sequence, then process
>> that sequence in my function.  Since I'm only dealing with the first
>> parameter passed to my script, a (first *command-line-args*) call is
>> equivalent to args[0] in other languages like Java.
>
> If you include clojure.contrib.io, you could use read-lines:
>
> (defn read-lines
>   "Like clojure.core/line-seq but opens f with reader.  Automatically
>   closes the reader AFTER YOU CONSUME THE ENTIRE SEQUENCE."
>   [f]
>   (let [read-line (fn this [^BufferedReader rdr]
>                     (lazy-seq
>                      (if-let [line (.readLine rdr)]
>                        (cons line (this rdr))
>                        (.close rdr))))]
>     (read-line (reader f))))
>
> Regards,
> Stuart
>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@googlegroups.com
> Note that posts from new members are moderated - please be patient with your
> first post.
> To unsubscribe from this group, send email to
> clojure+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to