Book Image

Clojure for Domain-specific Languages

By : Ryan D. Kelker
Book Image

Clojure for Domain-specific Languages

By: Ryan D. Kelker

Overview of this book

<p>Clojure is a very new and rapidly growing language that runs on top of the JVM. The language being hosted on the Java platform allows for Clojure applications to use existing Java components. Although there are objects in Clojure, the language is not object oriented.</p> <p>"Clojure for Domain-specific Languages" is an example-oriented guide to building custom languages. Many of the core components of Clojure are covered to help you understand your options when making a domain-specific language. By the end of this book, you should be able to make an internal DSL. Starting with a comparison of existing DSLs, this book will move on to guide you through general programming, Clojure editing, and project management. The chapters after that are code oriented.</p> <p>"Clojure for Domain-specific Languages" tries to expose you to as much Clojure code as possible. Many of the examples are executed in a Read-Evaluate-Print-Loop environment, so the reader can also follow along on their own machine. This book uses Leiningen, but no prior knowledge of it is required.</p> <p>"Clojure for Domain-Specific Languages" aims to make you familiar with the Clojure language and help you learn the tools to make your own language.</p>
Table of Contents (19 chapters)
Clojure for Domain-specific Languages
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Destructuring


Clojure collections can be taken apart with new bindings. The bindings hold a value within the collection and can be bound in multiple ways for multiple collection types. The following is the example collection that will be used for the rest of the destructuring examples:

user> (def abc {:a 1 :b 2 :c 3})
#'user/abc
user> (keys abc)(vals abc)
(:a :c :b)
(1 3 2)
user> (:a abc)(:c abc)
1
3

Using the let form, one way of binding a key value collection is to provide: a hash-map that describes the new binding and where the value should come from.

user> (let [{value-a :a 
             value-b :b
             value-c :c} abc]
   (str value-c
        value-b
        value-a))
"321"

Non-key value collections can be bound by their position in the collection:

user> (sort (vals abc))
(1 2 3)
user> (let [[a b c] (sort (vals abc))
            ab [a b]
            bc [b c]
            ca [c a]]
         (println ab bc ca))
[1 2] [2 3] [3 1]
Nil

If the value is a collection, another...