0%

Book Description

Cut through the noise and get real results with a step-by-step approach to learning Clojure programming

Key Features

  • Ideal for the Clojure beginner who is getting started for the first time
  • A step-by-step Clojure tutorial with exercises and activities that help build key skills
  • Structured to let you progress at your own pace, on your own terms
  • Use your physical print copy to redeem free access to the online interactive edition

Book Description

You already know you want to learn Clojure, and a smarter way to learn Clojure is to learn by doing. The Clojure Workshop focuses on building up your practical skills so that you can write clean, expressive code with a language that is great for applications where concurrency and interoperability with the JVM are a priority. You'll learn from real examples that lead to real results.

Throughout The Clojure Workshop, you'll take an engaging step-by-step approach to understanding Clojure. You won't have to sit through any unnecessary theory. If you're short on time you can jump into a single exercise each day or spend an entire weekend learning about Clojure web development with Ring. It's your choice. Learning on your terms, you'll build up and reinforce key skills in a way that feels rewarding.

Every physical print copy of The Clojure Workshop unlocks access to the interactive edition. With videos detailing all exercises and activities, you'll always have a guided solution. You can also benchmark yourself against assessments, track progress, and receive content updates. You'll even earn a secure credential that you can share and verify online upon completion. It's a premium learning experience that's included with your printed copy. To redeem, follow the instructions located at the start of your Clojure book.

Fast-paced and direct, The Clojure Workshop is the ideal for Clojure beginners. You'll build and iterate on your code like a software developer , learning along the way. This process means that you'll find that your new skills stick, embedded as best practice. A solid foundation for the years ahead.

What you will learn

  • Learn about Clojure fundamentals like functional programming
  • Understand and implement common Clojure patterns and best practices
  • Explore Clojure's testing infrastructure and the clojure.test library
  • Build a client-server application with Clojure and ClojureScript
  • Learn how to debug and resolve errors and exceptions
  • Explore Ring – Clojure's interface and library for building web applications

Who this book is for

Our goal at Packt is to help you be successful, in whatever it is you choose to do. The Clojure Workshop is an ideal Clojure tutorial for the Clojure beginner who is just getting started. Pick up a Workshop today, and let Packt help you develop skills that stick with you for life.

Table of Contents

  1. Preface
    1. About the Book
      1. Audience
      2. About the Chapters
      3. Conventions
      4. Before You Begin
      5. Installing Java
      6. Installing Clojure
      7. Editors and IDEs
      8. Installing the Code Bundle
  2. 1. Hello REPL!
    1. Introduction
    2. REPL Basics
      1. Exercise 1.01: Your First Dance
      2. Exercise 1.02: Getting around in the REPL
      3. Activity 1.01: Performing Basic Operations
    3. Evaluation of Clojure Code
    4. Basic Special Forms
      1. Exercise 1.03: Working with if, do, and when
      2. Bindings
      3. Exercise 1.04: Using def and let
      4. Exercise 1.05: Creating Simple Functions with fn and defn
      5. Activity 1.02: Predicting the Atmospheric Carbon Dioxide Level
    5. Truthiness, nil, and equality
      1. Exercise 1.06: The Truth Is Simple
      2. Equality and Comparisons
      3. Exercise 1.07: Comparing Values
      4. Activity 1.03: The meditate Function v2.0
    6. Summary
  3. 2. Data Types and Immutability
    1. Introduction
    2. Simple Data Types
      1. Strings
      2. Numbers
      3. Exercise 2.01: The Obfuscation Machine
      4. Booleans
      5. Symbols
      6. Keywords
    3. Collections
      1. Maps
      2. Exercise 2.02: Using Maps
      3. Sets
      4. Exercise 2.03: Using Sets
      5. Vectors
      6. Exercise 2.04: Using Vectors
      7. Lists
      8. Exercise 2.05: Using Lists
      9. Collection and Sequence Abstractions
      10. Exercise 2.06: Working with Nested Data Structures
      11. Activity 2.01: Creating a Simple In-Memory Database
    4. Summary
  4. 3. Functions in Depth
    1. Introduction
    2. Destructuring
      1. Exercise 3.01: Parsing Fly Vector's Data with Sequential Destructuring
      2. Exercise 3.02: Parsing MapJet Data with Associative Destructuring
    3. Advanced Call Signatures
      1. Destructuring Function Parameters
      2. Arity Overloading
      3. Variadic Functions
      4. Exercise 3.03: Multi-arity and Destructuring with Parenthmazes
    4. Higher-Order Programming
      1. First-Class Functions
      2. Partial Functions
      3. Composing Functions
      4. Exercise 3.04: High-Order Functions with Parenthmazes
    5. Multimethods
      1. Exercise 3.05: Using Multimethods
      2. Activity 3.01: Building a Distance and Cost Calculator
    6. Summary
  5. 4. Mapping and Filtering
    1. Introduction
    2. map and filter
      1. map
      2. Exercise 4.01: Working with map
      3. filter
      4. Exercise 4.02: Getting Started with filter
      5. Other Members of the filter Family – take-while and drop-while
      6. Exercise 4.03: Partitioning a Sequence with take-while and drop-while
      7. Using map and filter Together
      8. Threading Macros
    3. Using Lazy Sequences
      1. Exercise 4.04: Watching Lazy Evaluation
      2. Exercise 4.05: Creating Our Own Lazy Sequence
      3. Common Idioms and Patterns
      4. Anonymous Functions
      5. Keywords as Functions
      6. Exercise 4.06: Extracting Data from a List of Maps
      7. Sets as Predicates
      8. Filtering on a Keyword with comp and a Set
      9. Exercise 4.07: Using comp and a Set to Filter on a Keyword
      10. Returning a List Longer than the Input with mapcat
      11. Mapping with Multiple Inputs
      12. Exercise 4.08: Identifying Weather Trends
      13. Consuming Extracted Data with apply
      14. Exercise 4.09: Finding the Average Weather Temperature
      15. Activity 4.01: Using map and filter to Report Summary Information
    4. Importing a Dataset from a CSV File
      1. Exercise 4.10: Importing Data from a CSV File
      2. Real-World Laziness
      3. Exercise 4.11: Avoiding Lazy Evaluation Traps with Files
      4. Convenient CSV Parsing
      5. Exercise 4.12: Parsing CSV with semantic-csv
    5. Exercise 4.13: Querying the Data with filter
      1. Exercise 4.14: A Dedicated Query Function
      2. Exercise 4.15: Using filter to Find a Tennis Rivalry
      3. Activity 4.02: Arbitrary Tennis Rivalries
    6. Summary
  6. 5. Many to One: Reducing
    1. Introduction
      1. The Basics of reduce
      2. Exercise 5.01: Finding the Day with the Maximum Temperature
    2. Initializing reduce
      1. Partitioning with reduce
      2. Looking Back with reduce
      3. Exercise 5.02: Measuring Elevation Differences on Slopes
      4. Exercise 5.03: Winning and Losing Streaks
    3. Reducing without reduce
      1. zipmap
      2. Exercise 5.04: Creating a Lookup Table with zipmap
      3. Maps to Sequences, and Back Again
      4. group-by
      5. Exercise 5.05: Quick Summary Statistics with group-by
    4. Summarizing Tennis Scores
      1. Exercise 5.06: Complex Accumulation with reduce
    5. Introduction to Elo
      1. Exercise 5.07: Calculating Probabilities for a Single Match
      2. Exercise 5.08: Updating Player Ratings
      3. Activity 5.01: Calculating Elo Ratings for Tennis
    6. Summary
  7. 6. Recursion and Looping
    1. Introduction
    2. Clojure's Most Procedural Loop: doseq
      1. Looping Shortcuts
      2. Exercise 6.01: An Endless Stream of Groceries
      3. Recursion at Its Simplest
      4. Exercise 6.02: Partitioning Grocery Bags
      5. When to Use recur
      6. Exercise 6.03: Large-Scale Grocery Partitioning with recur
      7. What about loop?
      8. Exercise 6.04: Groceries with loop
      9. Tail Recursion
    3. Solving Complex Problems with Recursion
      1. Exercise 6.05: Europe by Train
      2. Pathfinding
      3. Exercise 6.06: The Search Function
      4. Exercise 6.07: Calculating the Costs of the Routes
      5. A Brief Introduction to HTML
      6. Activity 6.01: Generating HTML from Clojure Vectors
    4. Summary
  8. 7. Recursion II: Lazy Sequences
    1. Introduction
    2. A Simple Lazy Sequence
      1. Consuming a Sequence
      2. Exercise 7.01: Finding Inflection Points
      3. Exercise 7.02: Calculating a Running Average
      4. Lazy Consumption of Data
    3. Lazy Trees
      1. Exercise 7.03: A Tennis History Tree
      2. Exercise 7.04: A Custom take Function
      3. Knowing When to Be Lazy
      4. Exercise 7.05: Formatting the Matches
      5. Activity 7.01: Historical, Player-Centric Elo
    4. Summary
  9. 8. Namespaces, Libraries and Leiningen
    1. Introduction
    2. Namespaces
      1. Exercise 8.01: Investigating Namespaces Started by Default in REPL
      2. Exercise 8.02: Navigating Namespaces
      3. Importing Clojure Namespaces Using the refer Function
      4. Exercise 8.03: Using the refer Function to Import a Namespace
      5. Advanced Use of the refer Function
      6. Exercise 8.04: Using the :only Keyword
      7. Exercise 8.05: Using the :exclude Keyword
      8. Exercise 8.06: Using the :rename Keyword
    3. Importing Clojure Functions with require and use
      1. Exercise 8.07: Importing Clojure Functions with require and use
      2. Activity 8.01: Altering the Users List in an Application
      3. When You Want use versus When You Want require
    4. Leiningen—A Build Tool in Clojure
      1. Exercise 8.08: Creating a Leiningen Project
      2. Investigating project.clj
      3. Exercise 8.09: Executing the Application on the Command Line
      4. Exercise 8.10: Executing Application on the Command Line with arguments
      5. Activity 8.02: Summing Up Numbers
    5. Working with External Libraries
      1. Exercise 8.11: Using an External Library in a Leiningen Project
      2. Creating and Executing a jar with Leiningen
      3. Exercise 8.12: Creating a Jar File
    6. Leiningen Profiles
      1. Exercise 8.13: Adding Leiningen Profiles to a Project
      2. User-Wide Profiles
      3. Exercise 8.14: Using User-Wide Profiles
      4. Useful Clojure Libraries
      5. Activity 8.03: Building a Format-Converting Application
    7. Summary
  10. 9. Host Platform Interoperability with Java and JavaScript
    1. Introduction
    2. Using Java in Clojure
      1. Exercise 9.01: Importing a Single Java Class in Clojure
      2. Working with Time in Java
      3. Exercise 9.02: Importing Multiple Java Classes in Clojure
      4. Exercise 9.03: Macros That Help Us Use Java in Clojure
    3. Working with Java I/O
      1. Immutability in Clojure
      2. Exercise 9.04: Coffee-Ordering Application – Displaying a Menu
      3. Exercise 9.05: Coffee-Ordering Application – Saving and Loading Orders
      4. Working with Java Data Types
      5. Exercise 9.06: Java Data Types
      6. Activity 9.01: Book-Ordering Application
    4. Using JavaScript in ClojureScript
      1. Exercise 9.07: Working with JavaScript Data Types
      2. Figwheel Template
    5. Reactive Web Programming Using Rum
      1. Exercise 9.08: Investigating Figwheel and Rum
      2. Drag and Drop
      3. Exercise 9.09: JavaScript Interoperability with Drag and Drop
    6. Exceptions and Errors in Clojure
      1. Exercise 9.10: Handling Errors and Exceptions in Clojure
    7. Errors in JavaScript
      1. ClojureScript Leiningen Templates
      2. Exercise 9.11: Handling Errors in ClojureScript
      3. Activity 9.02: Creating a Support Desk
    8. Summary
  11. 10. Testing
    1. Introduction
    2. Why Testing Is Important
      1. Functional Testing
      2. Non-Functional Testing
      3. Clojure Unit Testing
      4. Exercise 10.01: Unit Testing with the clojure.test Library
    3. Using the Expectations Testing Library
      1. Exercise 10.02: Testing the Coffee Application with Expectations
      2. Unit Testing with the Midje Library
      3. Exercise 10.03: Testing the Coffee Application with Midje
    4. Property-Based Testing
      1. Exercise 10.04: Using Property-Based Testing in the Coffee-Ordering Application
      2. Activity 10.01: Writing Tests for the Coffee-Ordering Application
    5. Testing in ClojureScript
      1. Exercise 10.05: Setting Up Testing in ClojureScript
      2. Exercise 10.06: Testing ClojureScript Code
      3. Testing ClojureScript Applications with Figwheel
      4. Exercise 10.07: Tests in Figwheel Applications
      5. Exercise 10.08: Testing a ClojureScript Application
      6. Activity 10.02: Support Desk Application with Tests
    6. Summary
  12. 11. Macros
    1. Introduction
    2. What is a Macro?
      1. A Very Minimal Macro
      2. Compile Time and Run Time
      3. Runtime Parameters
    3. Syntax Quoting
      1. Exercise 11.01: The and-ors Macro
      2. Exercise 11.02: An Automatic HTML Library
      3. Exercise 11.03: Expanding the HTML Library
    4. Macros in ClojureScript
    5. Macro Hygiene
      1. Avoiding Variable Capture with Automatic Gensyms
      2. Exercise 11.04: Monitoring Functions
      3. When to Use Manual gensyms
      4. Activity 11.01: A Tennis CSV Macro
      5. Interface Design
      6. Implementation
    6. Summary
  13. 12. Concurrency
    1. Introduction
    2. Concurrency in General
      1. Automatic Parallelization with pmap
      2. Exercise 12.01: Testing Randomness
    3. Futures
      1. Exercise 12.02: A Crowdsourced Spellchecker
    4. Coordination
      1. Atoms
      2. Concept: Retries
      3. Refs and Software Transactional Memory
      4. Exercise 12.03: Stock Trading
      5. More Cohesion with refs
      6. Exercise 12.04: Keeping up with the Stock Price
      7. Agents
      8. Atoms in ClojureScript
      9. Exercise 12.05: Rock, Scissors, Paper
      10. Watchers
      11. Exercise 12.06: One, Two, Three… "Rock!"
      12. Activity 12.01: A DOM Whack-a-mole Game
    5. Summary
  14. 13. Database Interaction and the Application Layer
    1. Introduction
    2. Connecting to a Database
      1. Exercise 13.01: Establishing a Database Connection
    3. Introduction to Connection Pools
      1. Exercise 13.02: Creating a Connection Pool
      2. Creating Database Schemas
      3. Primary Keys
      4. Foreign Keys
      5. Exercise 13.03: Defining and Applying a Database Schema
      6. Managing Our Data
      7. Inserting Data
      8. Inserting Single Rows
      9. Inserting Multiple Rows
      10. Exercise 13.04: Data Insertion
      11. Querying Data
      12. Exercise 13.05: Querying Our Database
      13. Manipulating Query Return Values
      14. Exercise 13.06: Controlling Results with Custom Functions
      15. Updating and Deleting Data
      16. Exercise 13.07: Updating and Removing Existing Data
    4. Introduction to the Application Layer
      1. Exercise 13.08: Defining the Application Layer
      2. Activity 13.01: Persisting Historic Tennis Results and ELO Calculations
    5. Summary
  15. 14. HTTP with Ring
    1. Introduction
    2. HTTP, Web Servers, and REST
      1. Request and Response
      2. Exercise 14.01: Creating a Hello World Web Application
      3. Request Routing
      4. Using Compojure
      5. Exercise 14.02: Introducing Routing with Compojure
      6. Response Formats and Middleware
      7. Exercise 14.03: Response Rendering with Muuntaja
      8. Handling a Request Body
      9. Exercise 14.04: Working with a request Body
    3. Static Files
      1. Exercise 14.05: Serving Static Files
      2. Integrating with an Application Layer
      3. Accessing path and query Parameters in Compojure
      4. Exercise 14.06: Integrating with an Application Layer
      5. Activity 14.01: Exposing Historic Tennis Results and ELO Calculations via REST
    4. Summary
  16. 15. The Frontend: A ClojureScript UI
    1. Introduction
    2. Hiccup instead of HTML
      1. Getting Started with Reagent
    3. The Virtual DOM and Component Lifecycle
      1. Exercise 15.01: Creating a Reagent Application
      2. Exercise 15.02: Displaying an Image with Style
      3. Managing Component State
      4. Exercise 15.03: A Button that Modifies Its Text
      5. Components with Children Components
      6. Exercise 15.04: Creating a Grid of Images
      7. Hot Reload
      8. JavaScript Interop
      9. Exercise 15.05: Fetching Data from an HTTP Endpoint
      10. Activity 15.01: Displaying a Grid of Images from the Internet
      11. Activity 15.02: Tennis Players with Ranking
    4. Summary
  17. Appendix
    1. 1. Hello REPL!
      1. Activity 1.01: Performing Basic Operations
      2. Activity 1.02: Predicting the Atmospheric Carbon Dioxide Level
      3. Activity 1.03: The meditate Function v2.0
    2. 2. Data Types and Immutability
      1. Activity 2.01: Creating a Simple In-Memory Database
    3. 3. Functions in Depth
      1. Activity 3.01: Building a Distance and Cost Calculator
    4. 4. Mapping and Filtering
      1. Activity 4.01: Using map and filter to Report Summary Information
      2. Activity 4.02: Arbitrary Tennis Rivalries
    5. 5. Many to One: Reducing
      1. Activity 5.01: Calculating Elo Ratings for Tennis
    6. 6. Recursion and Looping
      1. Activity 6.01: Generating HTML from Clojure Vectors
    7. 7. Recursion II: Lazy Sequences
      1. Activity 7.01: Historical, Player-Centric Elo
    8. 8. Namespaces, Libraries and Leiningen
      1. Activity 8.01: Altering the Users List in an Application
      2. Activity 8.02: Summing Up Numbers
      3. Activity 8.03: Building a Format-Converting Application
    9. 9. Host Platform Interoperability with Java and JavaScript
      1. Activity 9.01: Book-Ordering Application
      2. Activity 9.02: Creating a Support Desk
    10. 10. Testing
      1. Activity 10.01: Writing Tests for the Coffee-Ordering Application
      2. Activity 10.02: Support Desk Application with Tests
    11. 11. Macros
      1. Activity 11.01: A Tennis CSV Macro
    12. 12. Concurrency
      1. Activity 12.01: A DOM Whack-a-mole Game
    13. 13. Database Interaction and the Application Layer
      1. Activity 13.01: Persisting Historic Tennis Results and ELO Calculations
    14. 14. HTTP with Ring
      1. Activity 14.01: Exposing Historic Tennis Results and ELO Calculations via REST
    15. 15. The Frontend: A ClojureScript UI
      1. Activity 15.01: Displaying a Grid of Images from the Internet
      2. Activity 15.02: Tennis Players with Ranking
18.227.0.192