Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Clojure.pdf
Скачиваний:
17
Добавлен:
09.05.2015
Размер:
12.92 Mб
Скачать

WRAPPING UP 110

In later chapters you will make these functions so much easier to use that Lancet crosses the boundary from an API to a domain-specific language.

Lancet Step 1: Ant Projects and Tasks

Download lancet/step_1_complete.clj

(ns lancet.step-1-complete

(:use clojure.contrib.except))

(def

#^{:doc "Dummy ant project to keep Ant tasks happy" } ant-project

(let [proj (org.apache.tools.ant.Project.)

logger (org.apache.tools.ant.NoBannerLogger.)] (doto logger

(.setMessageOutputLevel org.apache.tools.ant.Project/MSG_INFO) (.setOutputPrintStream System/out)

(.setErrorPrintStream System/err)) (doto proj

(.init)

(.addBuildListener logger))))

(defn instantiate-task [project name] (let [task (.createTask project name)]

(throw-if (nil? task)

IllegalArgumentException (str "No task named " name))

(doto task (.init)

(.setProject project))))

3.6 Wrapping Up

Clojure code can call directly into Java and can implement Java classes and interfaces where necessary. Do not be afraid to drop to Java when you need it. Clojure is pragmatic and does not aspire to wrap or replace Java code that already works.

One part of Java that you will use rarely is the Collections API. Clojure provides a powerful, functional, thread-safe alternative to Java collections: the sequence library. In the next chapter, you will meet Clojure’s ubiquitous sequences.

Prepared exclusively for WG Custom Motorcycles

Report erratum

this copy is (P1.0 printing, May 2009)