Safari, the world’s most comprehensive technology and business learning platform.

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required

O'Reilly logo
Mastering Clojure

Book Description

Understand the philosophy of the Clojure language and dive into its inner workings to unlock its advanced features, methodologies, and constructs

About This Book

  • Learn to handle data using sequences, reducers, and transducers in Clojure

  • Explore the lesser known and more advanced features, constructs, and methodologies of the Clojure language and its ecosystem, such as asynchronous channels, actors, logic programming, and reactive programming

  • Sharpen your Clojure skills through illustrative and comprehensive examples

  • Who This Book Is For

    If you’re looking to learn more about the core libraries and dive deep into the Clojure language, then this book is ideal for you. Prior knowledge of the Clojure language is required.

    What You Will Learn

  • Maximize the impact of parallelization, functional composition, and process transformation by composing reducers and transducers

  • Process and manipulate data using sequences, reducers, and transducers in Clojure

  • Modify and add features to the Clojure language using macros

  • Explore the features of category theory and custom data sources for logic programming in Clojure

  • Orchestrate parallelism and concurrency using built-in primitives as well as community libraries in Clojure

  • Handle data with asynchronous and reactive programming methodologies and leverage it using the core.async library

  • Test your code with unit tests, specs, and type checks to write testable code

  • Troubleshoot and style your Clojure code to make it more maintainable

  • In Detail

    Clojure is a general-purpose language from the Lisp family with an emphasis on functional programming. It has some interesting concepts and features such as immutability, gradual typing, thread-safe concurrency primitives, and macro-based metaprogramming, which makes it a great choice to create modern, performant, and scalable applications.

    Mastering Clojure gives you an insight into the nitty-gritty details and more advanced features of the Clojure programming language to create more scalable, maintainable, and elegant applications. You’ll start off by learning the details of sequences, concurrency primitives, and macros. Packed with a lot of examples, you’ll get a walkthrough on orchestrating concurrency and parallelism, which will help you understand Clojure reducers, and we’ll walk through composing transducers so you know about functional composition and process transformation inside out. We also explain how reducers and transducers can be used to handle data in a more performant manner.

    Later on, we describe how Clojure also supports other programming paradigms such as pure functional programming and logic programming. Furthermore, you’ll level up your skills by taking advantage of Clojure's powerful macro system. Parallel, asynchronous, and reactive programming techniques are also described in detail.

    Lastly, we’ll show you how to test and troubleshoot your code to speed up your development cycles and allow you to deploy the code faster.

    Style and approach

    This is an easy-to-follow project-based guide that throws you directly into the excitement of Clojure code. Mastering Clojure is for anyone who is interested in expanding their knowledge of language features and advanced functional programming.

    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 If you purchased this book elsewhere, you can visit and register to have the code file.

    Table of Contents

    1. Mastering Clojure
      1. Table of Contents
      2. Mastering Clojure
      3. Credits
      4. About the Author
      5. About the Reviewer
        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. Errata
          3. Piracy
          4. Questions
      8. 1. Working with Sequences and Patterns
        1. Defining recursive functions
        2. Thinking in sequences
          1. Using the seq library
            1. Creating sequences
            2. Transforming sequences
            3. Filtering sequences
            4. Lazy sequences
          2. Using zippers
        3. Working with pattern matching
        4. Summary
      9. 2. Orchestrating Concurrency and Parallelism
        1. Managing concurrent tasks
          1. Using delays
          2. Using futures and promises
        2. Managing state
          1. Using vars
          2. Using refs
          3. Using atoms
          4. Using agents
        3. Executing tasks in parallel
          1. Controlling parallelism with thread pools
        4. Summary
      10. 3. Parallelization Using Reducers
        1. Using reduce to transform collections
          1. What's wrong with sequences?
          2. Introducing reducers
        2. Using fold to parallelize collections
        3. Processing data with reducers
        4. Summary
      11. 4. Metaprogramming with Macros
        1. Understanding the reader
        2. Reading and evaluating code
        3. Quoting and unquoting code
        4. Transforming code
          1. Expanding macros
          2. Creating macros
          3. Encapsulating patterns in macros
          4. Using reader conditionals
          5. Avoiding macros
        5. Summary
      12. 5. Composing Transducers
        1. Understanding transducers
          1. Producing results from transducers
        2. Comparing transducers and reducers
        3. Transducers in action
          1. Managing volatile references
          2. Creating transducers
        4. Summary
      13. 6. Exploring Category Theory
        1. Demystifying category theory
        2. Using monoids
        3. Using functors
        4. Using applicative functors
        5. Using monads
        6. Summary
      14. 7. Programming with Logic
        1. Diving into logic programming
          1. Solving logical relations
          2. Combining logical relations
        2. Thinking in logical relations
          1. Solving the n-queens problem
          2. Solving a Sudoku puzzle
        3. Summary
      15. 8. Leveraging Asynchronous Tasks
        1. Using channels
          1. Customizing channels
          2. Connecting channels
          3. Revisiting the dining philosophers problem
        2. Using actors
          1. Creating actors
          2. Passing messages between actors
          3. Handling errors with actors
          4. Managing state with actors
          5. Comparing processes and actors
        3. Summary
      16. 9. Reactive Programming
        1. Reactive programming with fibers and dataflow variables
        2. Using Reactive Extensions
        3. Using functional reactive programming
        4. Building reactive user interfaces
        5. Introducing Om
        6. Summary
      17. 10. Testing Your Code
        1. Writing tests
          1. Defining unit tests
          2. Using top-down testing
        2. Testing with specs
        3. Generative testing
        4. Testing with types
        5. Summary
      18. 11. Troubleshooting and Best Practices
        1. Debugging your code
          1. Using tracing
          2. Using Spyscope
        2. Logging errors in your application
        3. Thinking in Clojure
        4. Summary
      19. A. References
      20. Index