スクリプティング

clj.batを準備

@echo off
setlocal enabledelayedexpansion

set CLOJURE_HOME=d:\usr\clojure
set CLOJURE_HOME_VERSION=clojure-1.4.0

set CLASSPATH=%CLASSPATH%;.;.\src;.\classes
for %%i in (%CLOJURE_HOME%\%CLOJURE_HOME_VERSION%\*.jar) do set CLASSPATH=!CLASSPATH!;%%i
for %%i in (%CLOJURE_HOME%\lib\*.jar) do set CLASSPATH=!CLASSPATH!;%%i

REM set CLASSPATH
java -client -cp "%CLASSPATH%" clojure.main %*

endlocal

お試しスクリプト

(ns coba.ken
  (:use [clojure.java.io :only (reader)])
  (:import (java.io BufferedReader))
  (:import (java.util.regex Pattern)))

(defn read-lines
  "From clojure.contrib.io.
  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))))

(defn re-split
  "From clojure.contrib.str-utils.
  Splits the string on instances of 'pattern'.  Returns a sequence of
  strings.  Optional 'limit' argument is the maximum number of
  splits.  Like Perl's 'split'."
  ([^Pattern pattern string] (seq (. pattern (split string))))
  ([^Pattern pattern string limit] (seq (. pattern (split string limit)))))

(defn- -nth [list index]
  "Like nth but avoid ArrayIndexOutOfBoundsException."
  (if (> (count list) index)
    (nth list index)
    nil))

; expects *command-line-args* as filenames.
; open each file and do something.
(doseq [filename *command-line-args*]
  (doseq [line (read-lines filename)]
    (let [fields (re-split #"\s." line)]
      (println (-nth  fields 0)))))

実行方法

clj print.clj ファイル名...

・・・なんですけど、clojure.contribの便利な関数の扱いが
最近どうなってるのかぜんぜんわからない。

フィルター作るのに使いたいこともあり、
^:dynamicつけろとかのWarningも抑止したい。