Reputation: 2037
I'm learning clojure and I'd like to use it for some simple things, like I do with python.
For example I'd like to whip up a .clj file in some directory full of CSV file and munge them around a little with some CSV library, without making a whole project out of it.
In python I'd make a .py file and just import csv
at the top. Can I do that in clojure?
If not, what's the simplest way to do a little script like that?
So far everything I've found has said that I need a new lein project to do anything with libraries like that.
Upvotes: 1
Views: 95
Reputation: 4513
Take a look at inlein
, which seems to be pretty neat:
#!/usr/bin/env inlein
'{:dependencies [[org.clojure/clojure "1.8.0"]
[clojure-csv/clojure-csv "2.0.1"]]}
(require '[clojure-csv.core :as csv])
(println "Reading: " (first *command-line-args*))
(-> *command-line-args*
first
slurp
csv/parse-csv
println)
(System/exit 0)
Also it's startup time is impressive due to starting of background daemon.
Upvotes: 2
Reputation: 13185
boot
build toolYou can use boot
for that purpose. It is a build tool which allows you to write standalone scripts that execute your tasks (so it doesn't have to contain all the project structure). In the script you can specify all the dependencies you need, use other tasks or define yours. Just install boot
and write a script.
For example to create a small script to read a CSV file provided on the command line you could write a following (e.g. read-csv.boot
):
#!/usr/bin/env boot
(set-env! :dependencies '[[org.clojure/data.csv "0.1.3"]])
(require '[boot.cli :refer [defclifn]]
'[clojure.data.csv :as csv]
'[clojure.java.io :as io])
(defclifn -main
[f file FILE str "input CSV file"]
(println "Reading" file)
(with-open [in-file (io/reader file)]
(->> in-file
(csv/read-csv)
(println))))
Make it executable:
$ chmod +x read-csv.boot
And use it:
$ ./read-csv.boot -f input.csv
Reading input.csv
([1 2 3])
python
In Clojure (and Java) dependency management is a bit different than in Python. In Python libraries are installed in the system as global packages and many of them are installed by default. You also have tools like pip
that allow you to install additional packages globally. Clojure and Java usually use Maven dependencies in your projects (and don't install them and make available globally) which requires tools like build tools (e.g. maven
, ivy
, lein
or boot
) to setup your application classpath.
Upvotes: 4