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

Arithmetic


Clojure's Lisp syntax makes arithmetic operations easier to read and write. In languages such as C or PHP, arithmetic operations involving more than two numbers result in having to type the operator before each additional number. Looking at the following example, which would you rather write?

  • Other languages: 1 + 2 + 3 + 4

  • Clojure language: (+ 1 2 3 4)

Addition and subtraction

The and + symbols can be used to add and subtract values:

user> (+ 1 2 3)
6
user> (+ -1 -2 -3)
-6
user> (+ 1 2 -3)
0
user> (apply + (range 10))
45
user> (- 1 2 3)
-4
user> (- -1 -2 -3)
4
user> (- 1 2 -3)
2
user> (apply - (range 10))
-45

Multiplication

The star symbol (*) can be used to multiply numbers:

user> (* 1 2 3)
6
user> (* -1 -2 -3)
-6
user> (* 1 2 -3)
-6
user> (apply * (range 10))
0

Division

The result of dividing two numbers is called the quotient; thus, division in Clojure is performed with the quot form:

user> (quot 4 2)
2
user> (quot 4 (float 2))
2.0
user&gt...