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

Casting


To force a value to represent a certain data type, you'll have to cast the values as it's done in most modern languages. It's pretty straightforward like everything else in Clojure:

user> (format "%s = %d" (type \A) (int \A))
"class java.lang.Character = 65"
user> (char 0x41)
\A
user> (= 0x41 (byte 0x41) (int \A))
true

Sometimes, casting values can come at the price of affecting the performance. Notice the time elapsed on casting the value 1.0:

user> (time (short 1.0))
"Elapsed time: 0.073201 msecs"
1
user> (time (int 1.0))
"Elapsed time: 0.07077 msecs"
1
user> (time (long 1.0))
"Elapsed time: 0.070731 msecs"
1

Some languages cast string values to integers, but Clojure isn't one of them. Java interoperation methods are required to convert a string to an integer:

user> (int "123")
ClassCastException java.lang.String cannot be cast to
java.lang.Character
user> (time (Integer/parseInt "123"))
"Elapsed time: 0.145911 msecs"
123
user> (time (Integer. "123"))
"Elapsed...