O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Clojure for Domain-specific Languages

Book Description

Enhance your existing Clojure know-how with this example-packed tutorial on building custom languages. It will help you unlock the potential of Clojure in a way you probably never thought possible.

  • Explore DSL concepts from existing Clojure DSLs and libraries
  • Bring Clojure into your Java applications as Clojure can be hosted on a Java platform
  • A tutorial-based guide to develop custom domain-specific languages

In Detail

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.

"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.

"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.

"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.

Table of Contents

  1. Clojure for Domain-specific Languages
    1. Table of Contents
    2. Clojure for Domain-specific Languages
    3. Credits
    4. About the Author
    5. About the Reviewers
    6. www.PacktPub.com
      1. Support files, eBooks, discount offers and more
        1. Why Subscribe?
        2. Free Access for Packt account holders
    7. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Errata
        3. Piracy
        4. Questions
    8. 1. An Overview of Domain-specific Languages with Clojure
      1. Domain-specific languages (DSL)
        1. Limited scope
        2. Syntax
        3. Using a DSL
        4. Popular DSLs
        5. A contract between language and domain
        6. The language of trust
      2. Internal versus External DSLs
        1. External DSLs
        2. Internal DSLs
        3. Clojure libraries
        4. The characteristics of a Clojure library
        5. The current state of Clojure libraries
      3. Database domains
      4. The HTML domain
        1. Formative
        2. Hiccup
        3. Mustache
        4. Clostache
      5. The ECMA/JavaScript domain
        1. ClojureScript
        2. Comparing ClojureScript and JavaScript
      6. The Audio domain
        1. Music-as-data
        2. Overtone
      7. Image domains
      8. Summary
    9. 2. Design Concepts with Clojure
      1. Every function is a little program
        1. A pure function
        2. Floor-to-roof development
      2. Each function only does one thing
      3. Patterns for success
        1. DRY
        2. KISS
        3. YAGNI
        4. Writing Clojure
        5. Spacing and alignment
        6. Syntax
        7. Name conventions
        8. Collection types
      4. Summary
    10. 3. Clojure Editing and Project Creation
      1. The origin of Emacs and its usage
      2. Installing and setting up Emacs24
      3. Setting up Emacs
      4. Creating and editing CLJ files in Emacs
      5. Running a Clojure REPL inside Emacs
        1. The nrepl.el Emacs extension
          1. Testing the setup
      6. Leiningen and project management
      7. Installing Leiningen and starting a project
      8. Including Clojure or Java libraries in your project
        1. Compiling your project to a Java JAR
      9. Leiningen
      10. Summary
    11. 4. Features, Functions, and Macros
      1. Namespaces
      2. Java inside Clojure
      3. Immutability
      4. Dynamic objects
      5. Metadata
      6. Lazy sequences
      7. Destructuring
      8. Functions and arity
      9. Anonymous functions
      10. Macros
      11. Summary
    12. 5. Collections and Sequencing
      1. Collections
        1. Collections by example
      2. Vectors
        1. Vectors by example
      3. Lists
        1. Lists by example
      4. Maps
        1. Maps by example
      5. Sets
        1. Sets by example
      6. Sequences
        1. Sequences by example
      7. :let, :while, and :when
      8. Summary
    13. 6. Assignment and Concurrency
      1. Variables
      2. Transients
      3. Atoms
      4. Agents
      5. Refs
      6. Futures
      7. Promises
      8. Summary
    14. 7. Flow Control, Error Handling, and Math
      1. Flow control
      2. Object comparison
      3. Casting
      4. Error handling
      5. Arithmetic
        1. Addition and subtraction
        2. Multiplication
        3. Division
        4. Remainder and modulus
        5. Increment and decrement
        6. Greatest and least values
        7. Equality
      6. Summary
    15. 8. Methods for Abstraction
      1. Creating and constructing classes
        1. Creating interfaces and implementing them with deftype
        2. Using records, protocols, and type extensions
      2. Overriding methods with reify and proxy
        1. Working with reify
        2. Implementing interface methods with proxy
      3. Custom symbol definitions with macros
        1. Definitions using records
        2. Making definitions using proxy
        3. Making definitions using deftype
      4. Multimethod polymorphism
        1. Creating the Bottle and Customer classes
        2. Testing the customer-drink methods
      5. Relationships with hierarchies
        1. Resolving parent relationship conflicts
      6. Assertion testing with metadata
      7. Input constraints with :pre
      8. Output constraints with :post
      9. Summary
    16. 9. An Example Twitter DSL
      1. Creating Java-based abstractions
        1. Making Java objects easier to manipulate
        2. Retrieving values in a better way
      2. Examples of our Twitter DSL
        1. The Retweet bot
        2. Creating an event notifier
      3. Reading the OAuth configuration
        1. Twitter account registration and application keys
        2. Adding required dependencies
        3. Creating the project and API configuration
        4. Reading the Twitter configuration
      4. Making our most important macro
        1. Building the deftwitter macro
        2. Building the twitter-> macro
      5. Handling search queries
        1. Adding the tdsl.search namespace
        2. Search macros and functions
      6. Handling tweets
        1. Adding the tdsl.tweet namespace
        2. Tweet macros and functions
      7. Adding user-related features
        1. Adding the tdsl.user namespace
        2. User macros and functions
        3. User details and multimethods
        4. Adding logging features
      8. Summary
    17. 10. Unit Testing
      1. Exploring the clojure.test framework
        1. Testing tdsl.core
        2. Using the is macro
        3. Using the are macro
        4. Developing the final test
      2. The expectations framework
        1. Using the expect macro
        2. Search testing
      3. The midje framework
        1. Using the fact macro
      4. The speclj framework
        1. Using the describe, it, should, and should= macros
        2. Using the should-contain macro
      5. Summary
    18. 11. Clojure DSLs inside Java
      1. Making a Java-callable Clojure class
        1. Class naming
      2. Data hiding
        1. AOT – the ahead-of-time compilation
      3. Java-wrapping your Clojure
      4. Summary
    19. A. References
      1. Chapter 1: An Overview of Domain-specific Languages with Clojure
      2. Chapter 2: Design Concepts with Clojure
      3. Chapter 3: Clojure Editing and Project Creation
      4. Chapter 4: Features, Functions, and Macros
      5. Chapter 5: Collections and Sequencing
      6. Chapter 6: Assignment and Concurrency
      7. Chapter 7: Flow Control, Error Handling, and Math
      8. Chapter 8: Methods for Abstraction
      9. Chapter 9: An Example Twitter DSL
      10. Chapter 10: Unit Testing
      11. Chapter 11: Clojure DSLs inside Java
    20. Index