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 Java Developers

Book Description

Transition smoothly from Java to the most widely used functional JVM-based language – Clojure

About This Book

  • Write apps for the multithreaded world with Clojure’s flavor of functional programming
  • Discover Clojure’s features and advantages and use them in your existing projects
  • The book is designed so that you’ll be able put to use your existing skills and software knowledge to become a more effective Clojure developer

Who This Book Is For

This book is intended for Java developers, who are looking for a way to expand their skills and understand new paradigms of programming. Whether you know a little bit about functional languages, or you are just getting started, this book will get you up and running with how to use your existing skills in Clojure and functional programming.

What You Will Learn

  • Understand the tools for the Clojure world and how they relate to Java tools and standards (like Maven)
  • Learn about immutable data structures, and what makes them feasible for everyday programming
  • Write simple multi-core programs using Clojure’s core concepts, like atoms, agents and refs
  • Understand that in Clojure, code is data, and how to take advantage of that fact by generating and manipulating code with macros
  • Learn how Clojure interacts with Java, how the class loaders work and how to use Clojure from Java or the other way around
  • Discover a new, more flexible meaning of polymorphism and understand that OOP is not the only way to get it

In Detail

We have reached a point where machines are not getting much faster, software projects need to be delivered quickly, and high quality in software is more demanding as ever.

We need to explore new ways of writing software that helps achieve those goals. Clojure offers a new possibility of writing high quality, multi-core software faster than ever, without having to leave your current platform.

Clojure for Java developers aims at unleashing the true potential of the Clojure language to use it in your projects. The book begins with the installation and setup of the Clojure environment before moving on to explore the language in-depth. Get acquainted with its various features such as functional programming, concurrency, etc. with the help of example projects. Additionally, you will also, learn how the tooling works, and how it interacts with the Java environment.

By the end of this book, you will have a firm grip on Clojure and its features, and use them effectively to write more robust programs.

Style and approach

An easy to follow, step-by-step, guide on how to start writing Clojure programs making use of all of its varied features and advantages. As this is a new language, certain new concepts are supported with theoretical section followed by simple projects to help you gain a better understanding and practice of how Clojure works.

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the code file.

Table of Contents

  1. Clojure for Java Developers
    1. Table of Contents
    2. Clojure for Java Developers
    3. Credits
    4. About the Author
    5. About the Reviewer
    6. www.PacktPub.com
      1. eBooks, discount offers, and more
        1. Why subscribe?
    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. Downloading the color images of this book
        3. Piracy
        4. Questions
    8. 1. Getting Started with Clojure
      1. Getting to know Clojure
      2. Installing Leiningen
      3. Using a REPL
        1. The nREPL protocol
        2. Hello world
        3. REPL utilities and conventions
      4. Creating a new project
        1. Project structure
        2. Creating a standalone app
      5. Using Cursive Clojure
        1. Installing Cursive Clojure
      6. Getting started with Clojure code and data
        1. Lists in Clojure
        2. Operations in Clojure
        3. Functions in Clojure
      7. Clojure's data types
        1. Scalars
        2. Collection data types
      8. Summary
    9. 2. Namespaces, Packages, and Tests
      1. Namespaces in Clojure
        1. Packages in Clojure
      2. The classpath and the classloader
        1. Back to Clojure namespaces
          1. Playing with namespaces
        2. Creating a new namespace
        3. Working with namespaces on the REPL
      3. Testing in Clojure
        1. Testing from the command line
        2. Testing in IntelliJ
      4. Summary
    10. 3. Interacting with Java
      1. Using Maven dependencies
      2. Clojure interop syntax
        1. Creating an object
        2. Calling an instance method
        3. Calling a static method or function
        4. Accessing inner classes
      3. Writing a simple image namespace
      4. Writing the tests
        1. The let statement
      5. Destructuring in Clojure
        1. Sequential destructuring
        2. Associative destructuring
      6. Exposing your code to Java
        1. Testing from Groovy
      7. Proxy and reify
      8. Summary
    11. 4. Collections and Functional Programming
      1. Basics of functional programming
      2. Persistent collections
      3. Types of collections in Clojure
      4. The sequence abstraction
      5. Specific collection types in Clojure
        1. Vectors
        2. Lists
        3. Maps
          1. Sorted maps and hash maps
          2. Common properties
        4. Sets
          1. Sorted sets and hash sets
          2. Common properties
          3. Union, difference, and intersection
        5. Applying functional programming to collections
          1. The imperative programming model
          2. The functional paradigm
          3. Functional programming and immutability
          4. Laziness
      6. Summary
    12. 5. Multimethods and Protocols
      1. Polymorphism in Java
      2. Multimethods in Clojure
        1. Keyword hierarchies
          1. isa?
          2. parents
          3. descendants
          4. underive
        2. A la carte dispatch functions
      3. Protocols in Clojure
        1. Records in Clojure
      4. Summary
    13. 6. Concurrency
      1. Using your Java knowledge
      2. The Clojure model of state and identity
      3. Promises
        1. Pulsar and lightweight threads
      4. Futures
      5. Software transactional memory and refs
      6. Atoms
      7. Agents
        1. Validators
        2. Watchers
      8. core.async
        1. Why lightweight threads?
        2. Goblocks
        3. Channels
        4. Transducers
      9. Summary
    14. 7. Macros in Clojure
      1. Lisp's foundational ideas
      2. Macros as code modification tools
        1. Modifying code in Java
        2. Modifying code in Groovy
          1. The @ToString annotation
          2. The @TupleConstructor annotation
          3. The @Slf4j annotation
      3. Writing your first macro
      4. Debugging your first macro
        1. Quote, syntax quote, and unquoting
        2. Unquote splicing
        3. gensym
      5. Macros in the real world
      6. References
      7. Summary
    15. Index