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

WRAPPING UP 146

(doto proj (.init)

(.addBuildListener logger))))

(defn property-descriptor [inst prop-name] (first

(filter #(= (name prop-name) (.getName %)) (.getPropertyDescriptors

(Introspector/getBeanInfo (class inst))))))

(defn set-property! [inst prop value]

(let [pd (property-descriptor inst prop)]

(throw-if (nil? pd) (str "No such property " prop)) (.invoke (.getWriteMethod pd) inst (into-array [value]))))

(defn set-properties! [inst prop-map]

(doseq [[k v] prop-map] (set-property! inst k v)))

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

(throw-if (nil? task) (str "No task named " name)) (doto task

(.init)

(.setProject project) (set-properties! props))

task))

4.7 Wrapping Up

Clojure unifies all kinds of collections under a single abstraction, the sequence. After more than a decade dominated by object-oriented programming, Clojure’s sequence library is the “Revenge of the Verbs.”

Clojure’s sequences are implemented using functional programming techniques: immutable data, recursive definition, and lazy realization. In the next chapter, you will see how to use these techniques directly, further expanding the power of Clojure.

Prepared exclusively for WG Custom Motorcycles

Report erratum

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