BCut through the noise and get real results with a step-by-step approach to learning Clojure programming/b h4Key Features/h4 ulliIdeal for the Clojure beginner who is getting started for the first time /li liA step-by-step Clojure tutorial with exercises and activities that help build key skills /li liStructured to let you progress at your own pace, on your own terms /li liUse your physical print copy to redeem free access to the online interactive edition/li/ul h4Book Description/h4 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. h4What you will learn/h4 ulliLearn about Clojure fundamentals like functional programming /li liUnderstand and implement common Clojure patterns and best practices /li liExplore Clojure's testing infrastructure and the clojure.test library /li liBuild a client-server application with Clojure and ClojureScript /li liLearn how to debug and resolve errors and exceptions /li liExplore Ring - Clojure's interface and library for building web applications/li/ul h4Who this book is for/h4 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 Cover FM Copyright Table of Contents Preface Chapter 1: Hello REPL! Introduction REPL Basics Exercise 1.01: Your First Dance Exercise 1.02: Getting around in the REPL Activity 1.01: Performing Basic Operations Evaluation of Clojure Code Basic Special Forms Exercise 1.03: Working with if, do, and when Bindings Exercise 1.04: Using def and let Exercise 1.05: Creating Simple Functions with fn and defn Activity 1.02: Predicting the Atmospheric Carbon Dioxide Level Truthiness, nil, and equality Exercise 1.06: The Truth Is Simple Equality and Comparisons Exercise 1.07: Comparing Values Activity 1.03: The meditate Function v2.0 Summary Chapter 2: Data Types and Immutability Introduction Simple Data Types Strings Numbers Exercise 2.01: The Obfuscation Machine Booleans Symbols Keywords Collections Maps Exercise 2.02: Using Maps Sets Exercise 2.03: Using Sets Vectors Exercise 2.04: Using Vectors Lists Exercise 2.05: Using Lists Collection and Sequence Abstractions Exercise 2.06: Working with Nested Data Structures Activity 2.01: Creating a Simple In-Memory Database Summary Chapter 3: Functions in Depth Introduction Destructuring Exercise 3.01: Parsing Fly Vector's Data with Sequential Destructuring Exercise 3.02: Parsing MapJet Data with Associative Destructuring Advanced Call Signatures Destructuring Function Parameters Arity Overloading Variadic Functions Exercise 3.03: Multi-arity and Destructuring with Parenthmazes Higher-Order Programming First-Class Functions Partial Functions Composing Functions Exercise 3.04: High-Order Functions with Parenthmazes Multimethods Exercise 3.05: Using Multimethods Activity 3.01: Building a Distance and Cost Calculator Summary Chapter 4: Mapping and Filtering Introduction map and filter map Exercise 4.01: Working with map filter Exercise 4.02: Getting Started with filter Other Members of the filter Family – take-while and drop-while Exercise 4.03: Partitioning a Sequence with take-while and drop-while Using map and filter Together Threading Macros Using Lazy Sequences Exercise 4.04: Watching Lazy Evaluation Exercise 4.05: Creating Our Own Lazy Sequence Common Idioms and Patterns Anonymous Functions Keywords as Functions Exercise 4.06: Extracting Data from a List of Maps Sets as Predicates Filtering on a Keyword with comp and a Set Exercise 4.07: Using comp and a Set to Filter on a Keyword Returning a List Longer than the Input with mapcat Mapping with Multiple Inputs Exercise 4.08: Identifying Weather Trends Consuming Extracted Data with apply Exercise 4.09: Finding the Average Weather Temperature Activity 4.01: Using map and filter to Report Summary Information Importing a Dataset from a CSV File Exercise 4.10: Importing Data from a CSV File Real-World Laziness Exercise 4.11: Avoiding Lazy Evaluation Traps with Files Convenient CSV Parsing Exercise 4.12: Parsing CSV with semantic-csv Exercise 4.13: Querying the Data with filter Exercise 4.14: A Dedicated Query Function Exercise 4.15: Using filter to Find a Tennis Rivalry Activity 4.02: Arbitrary Tennis Rivalries Summary Chapter 5: Many to One: Reducing Introduction The Basics of reduce Exercise 5.01: Finding the Day with the Maximum Temperature Initializing reduce Partitioning with reduce Looking Back with reduce Exercise 5.02: Measuring Elevation Differences on Slopes Exercise 5.03: Winning and Losing Streaks Reducing without reduce zipmap Exercise 5.04: Creating a Lookup Table with zipmap Maps to Sequences, and Back Again group-by Exercise 5.05: Quick Summary Statistics with group-by Summarizing Tennis Scores Exercise 5.06: Complex Accumulation with reduce Introduction to Elo Exercise 5.07: Calculating Probabilities for a Single Match Exercise 5.08: Updating Player Ratings Activity 5.01: Calculating Elo Ratings for Tennis Summary Chapter 6: Recursion and Looping Introduction Clojure's Most Procedural Loop: doseq Looping Shortcuts Exercise 6.01: An Endless Stream of Groceries Recursion at Its Simplest Exercise 6.02: Partitioning Grocery Bags When to Use recur Exercise 6.03: Large-Scale Grocery Partitioning with recur What about loop? Exercise 6.04: Groceries with loop Tail Recursion Solving Complex Problems with Recursion Exercise 6.05: Europe by Train Pathfinding Exercise 6.06: The Search Function Exercise 6.07: Calculating the Costs of the Routes A Brief Introduction to HTML Activity 6.01: Generating HTML from Clojure Vectors Summary Chapter 7: Recursion II: Lazy Sequences Introduction A Simple Lazy Sequence Consuming a Sequence Exercise 7.01: Finding Inflection Points Exercise 7.02: Calculating a Running Average Lazy Consumption of Data Lazy Trees Exercise 7.03: A Tennis History Tree Exercise 7.04: A Custom take Function Knowing When to Be Lazy Exercise 7.05: Formatting the Matches Activity 7.01: Historical, Player-Centric Elo Summary Chapter 8: Namespaces, Libraries and Leiningen Introduction Namespaces Exercise 8.01: Investigating Namespaces Started by Default in REPL Exercise 8.02: Navigating Namespaces Importing Clojure Namespaces Using the refer Function Exercise 8.03: Using the refer Function to Import a Namespace Advanced Use of the refer Function Exercise 8.04: Using the :only Keyword Exercise 8.05: Using the :exclude Keyword Exercise 8.06: Using the :rename Keyword Importing Clojure Functions with require and use Exercise 8.07: Importing Clojure Functions with require and use Activity 8.01: Altering the Users List in an Application When You Want use versus When You Want require Leiningen—A Build Tool in Clojure Exercise 8.08: Creating a Leiningen Project Investigating project.clj Exercise 8.09: Executing the Application on the Command Line Exercise 8.10: Executing Application on the Command Line with arguments Activity 8.02: Summing Up Numbers Working with External Libraries Exercise 8.11: Using an External Library in a Leiningen Project Creating and Executing a jar with Leiningen Exercise 8.12: Creating a Jar File Leiningen Profiles Exercise 8.13: Adding Leiningen Profiles to a Project User-Wide Profiles Exercise 8.14: Using User-Wide Profiles Useful Clojure Libraries Activity 8.03: Building a Format-Converting Application Summary Chapter 9: Host Platform Interoperability with Java and JavaScript Introduction Using Java in Clojure Exercise 9.01: Importing a Single Java Class in Clojure Working with Time in Java Exercise 9.02: Importing Multiple Java Classes in Clojure Exercise 9.03: Macros That Help Us Use Java in Clojure Working with Java I/O Immutability in Clojure Exercise 9.04: Coffee-Ordering Application – Displaying a Menu Exercise 9.05: Coffee-Ordering Application – Saving and Loading Orders Working with Java Data Types Exercise 9.06: Java Data Types Activity 9.01: Book-Ordering Application Using JavaScript in ClojureScript Exercise 9.07: Working with JavaScript Data Types Figwheel Template Reactive Web Programming Using Rum Exercise 9.08: Investigating Figwheel and Rum Drag and Drop Exercise 9.09: JavaScript Interoperability with Drag and Drop Exceptions and Errors in Clojure Exercise 9.10: Handling Errors and Exceptions in Clojure Errors in JavaScript ClojureScript Leiningen Templates Exercise 9.11: Handling Errors in ClojureScript Activity 9.02: Creating a Support Desk Summary Chapter 10: Testing Introduction Why Testing Is Important Functional Testing Non-Functional Testing Clojure Unit Testing Exercise 10.01: Unit Testing with the clojure.test Library Using the Expectations Testing Library Exercise 10.02: Testing the Coffee Application with Expectations Unit Testing with the Midje Library Exercise 10.03: Testing the Coffee Application with Midje Property-Based Testing Exercise 10.04: Using Property-Based Testing in the Coffee-Ordering Application Activity 10.01: Writing Tests for the Coffee-Ordering Application Testing in ClojureScript Exercise 10.05: Setting Up Testing in ClojureScript Exercise 10.06: Testing ClojureScript Code Testing ClojureScript Applications with Figwheel Exercise 10.07: Tests in Figwheel Applications Exercise 10.08: Testing a ClojureScript Application Activity 10.02: Support Desk Application with Tests Summary Chapter 11: Macros Introduction What is a Macro? A Very Minimal Macro Compile Time and Run Time Runtime Parameters Syntax Quoting Exercise 11.01: The and-ors Macro Exercise 11.02: An Automatic HTML Library Exercise 11.03: Expanding the HTML Library Macros in ClojureScript Macro Hygiene Avoiding Variable Capture with Automatic Gensyms Exercise 11.04: Monitoring Functions When to Use Manual gensyms Activity 11.01: A Tennis CSV Macro Interface Design Implementation Summary Chapter 12: Concurrency Introduction Concurrency in General Automatic Parallelization with pmap Exercise 12.01: Testing Randomness Futures Exercise 12.02: A Crowdsourced Spellchecker Coordination Atoms Concept: Retries Refs and Software Transactional Memory Exercise 12.03: Stock Trading More Cohesion with refs Exercise 12.04: Keeping up with the Stock Price Agents Atoms in ClojureScript Exercise 12.05: Rock, Scissors, Paper Watchers Exercise 12.06: One, Two, Three... "Rock!" Activity 12.01: A DOM Whack-a-mole Game Summary Chapter 13: Database Interaction and the Application Layer Introduction Connecting to a Database Exercise 13.01: Establishing a Database Connection Introduction to Connection Pools Exercise 13.02: Creating a Connection Pool Creating Database Schemas Primary Keys Foreign Keys Exercise 13.03: Defining and Applying a Database Schema Managing Our Data Inserting Data Inserting Single Rows Inserting Multiple Rows Exercise 13.04: Data Insertion Querying Data Exercise 13.05: Querying Our Database Manipulating Query Return Values Exercise 13.06: Controlling Results with Custom Functions Updating and Deleting Data Exercise 13.07: Updating and Removing Existing Data Introduction to the Application Layer Exercise 13.08: Defining the Application Layer Activity 13.01: Persisting Historic Tennis Results and ELO Calculations Summary Chapter 14: HTTP with Ring Introduction HTTP, Web Servers, and REST Request and Response Exercise 14.01: Creating a Hello World Web Application Request Routing Using Compojure Exercise 14.02: Introducing Routing with Compojure Response Formats and Middleware Exercise 14.03: Response Rendering with Muuntaja Handling a Request Body Exercise 14.04: Working with a request Body Static Files Exercise 14.05: Serving Static Files Integrating with an Application Layer Accessing path and query Parameters in Compojure Exercise 14.06: Integrating with an Application Layer Activity 14.01: Exposing Historic Tennis Results and ELO Calculations via REST Summary Chapter 15: The Frontend: A ClojureScript UI Introduction Hiccup instead of HTML Getting Started with Reagent The Virtual DOM and Component Lifecycle Exercise 15.01: Creating a Reagent Application Exercise 15.02: Displaying an Image with Style Managing Component State Exercise 15.03: A Button that Modifies Its Text Components with Children Components Exercise 15.04: Creating a Grid of Images Hot Reload JavaScript Interop Exercise 15.05: Fetching Data from an HTTP Endpoint Activity 15.01: Displaying a Grid of Images from the Internet Activity 15.02: Tennis Players with Ranking Summary Appendix Index Learn how to solve problems using Clojure or ClojureScript and become a confident functional programmer with the help of engaging activities and challenging projects Key FeaturesMaster the tools and patterns of the Clojure and ClojureScript ecosystemsLearn the fundamentals of functional programming and immutabilityApply your skills practically by developing a range of scalable applicationsBook DescriptionThe Clojure Workshop is a step-by-step guide to Clojure and ClojureScript, designed to quickly get you up and running as a confident, knowledgeable developer. Because of the functional nature of the language, Clojure programming is quite different to what many developers will have experienced. As hosted languages, Clojure and ClojureScript can also be daunting for newcomers because of complexities in the tooling and the challenge of interacting with the host platforms. To help you overcome these barriers, this book adopts a practical approach. Every chapter is centered around building something. As you progress through the book, you will progressively develop the 'muscle memory' that will make you a productive Clojure programmer, and help you see the world through the concepts of functional programming. You will also gain familiarity with common idioms and patterns, as well as exposure to some of the most widely used libraries. Unlike many Clojure books, this Workshop will include significant coverage of both Clojure and ClojureScript. This makes it useful no matter your goal or preferred platform, and provides a fresh perspective on the hosted nature of the language. By the end of this book, you'll have the knowledge, skills and confidence to creatively tackle your own ambitious projects with Clojure and ClojureScript. What you will learnWrite idiomatic code with Clojure and ClojureScriptUnderstand and use common patterns and best practicesExperiment with code and interact with programs using the REPLLearn the fundamentals of functional programming and immutabilityMaster concepts including mapping, filtering, reducing and recursionStructure and build your code using namespaces and LeiningenWrite unit tests to validate application behaviorSimplify your code and improve efficiency with macrosWho this book is forThe Clojure Workshop is for anyone who is curious about functional programming and wants to get started learning Clojure or ClojureScript. Prior experience of another programming language, such as Java or JavaScript, is recommended, and will help you grasp the concepts covered in this book more easily. Table of ContentsHello REPL!Data Types and ImmutabilityFunctions in DepthMapping and FilteringMany to ReducingRecursion and LoopingRecursion Lazy SequencesNamespaces, Libraries, and LeiningenHost Platform Interop - (Java and JavaScript)TestingMacrosConcurrencyDatabase Interaction and the Application LayerHTTP with RingThe a Clojurescript UI Learn how to solve problems using Clojure or ClojureScript and become a confident functional programmer with the help of engaging activities and challenging projects Key Features Master the tools and patterns of the Clojure and ClojureScript ecosystems Learn the fundamentals of functional programming and immutability Apply your skills practically by developing a range of scalable applications Book DescriptionThe Clojure Workshop is a step-by-step guide to Clojure and ClojureScript, designed to quickly get you up and running as a confident, knowledgeable developer. Because of the functional nature of the language, Clojure programming is quite different to what many developers will have experienced. As hosted languages, Clojure and ClojureScript can also be daunting for newcomers because of complexities in the tooling and the challenge of interacting with the host platforms. To help you overcome these barriers, this book adopts a practical approach. Every chapter is centered around building something. As you progress through the book, you will progressively develop the 'muscle memory' that will make you a productive Clojure programmer, and help you see the world through the concepts of functional programming. You will also gain familiarity with common idioms and patterns, as well as exposure to some of the most widely used libraries. Unlike many Clojure books, this Workshop will include significant coverage of both Clojure and ClojureScript. This makes it useful no matter your goal or preferred platform, and provides a fresh perspective on the hosted nature of the language. By the end of this book, you'll have the knowledge, skills and confidence to creatively tackle your own ambitious projects with Clojure and ClojureScript.What you will learn Write idiomatic code with Clojure and ClojureScript Understand and use common patterns and best practices Experiment with code and interact with programs using the REPL Learn the fundamentals of functional programming and immutability Master concepts including mapping, filtering, reducing and recursion Structure and build your code using namespaces and Leiningen Write unit tests to validate application behavior Simplify your code and improve efficiency with macros Who this book is for The Clojure Workshop is for anyone who is curious about functional programming and wants to get started learning Clojure or ClojureScript. Prior experience of another programming language, such as Java or JavaScript, is recommended, and will help you grasp the concepts covered in this book more easily. ]]> Learn how to solve problems using Clojure or ClojureScript and become a confident functional programmer with the help of engaging activities and challenging projects Key Features Master the tools and patterns of the Clojure and ClojureScript ecosystems Learn the fundamentals of functional programming and immutability Apply your skills practically by developing a range of scalable applications Book Description The Clojure Workshop is a step-by-step guide to Clojure and ClojureScript, designed to quickly get you up and running as a confident, knowledgeable developer. Because of the functional nature of the language, Clojure programming is quite different to what many developers will have experienced. As hosted languages, Clojure and ClojureScript can also be daunting for newcomers because of complexities in the tooling and the challenge of interacting with the host platforms. To help you overcome these barriers, this book adopts a practical approach. Every chapter is centered around building something. As you progress through the book, you will progressively develop the 'muscle memory' that will make you a productive Clojure programmer, and help you see the world through the concepts of functional programming. You will also gain familiarity with common idioms and patterns, as well as exposure to some of the most widely used libraries. Unlike many Clojure books, this Workshop will include significant coverage of both Clojure and ClojureScript. This makes it useful no matter your goal or preferred platform, and provides a fresh perspective on the hosted nature of the language. By the end of this book, you'll have the knowledge, skills and confidence to creatively tackle your own ambitious projects with Clojure and ClojureScript. What you will learn Write idiomatic code with Clojure and ClojureScript Understand and use common patterns and best practices Experiment with code and interact with programs using the REPL Learn the fundamentals of functional programming and immutability Master concepts including mapping, filtering, reducing and recursion Structure and build your code using namespaces and Leiningen Write unit tests to validate application behavior Simplify your code and improve efficiency with macros Who this book is for The Clojure Workshop is for anyone who is curious about functional programming and wants to get started learning Clojure or ClojureScript. Prior experience of another programming language, such as Java or JavaScript, is recommended, and will help you grasp the concepts covered in this book more easily.