The Clojure Workshop
eBook - ePub

The Clojure Workshop

A New, Interactive Approach to Learning Clojure

  1. 800 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

The Clojure Workshop

A New, Interactive Approach to Learning Clojure

About this book

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.

Frequently asked questions

Yes, you can cancel anytime from the Subscription tab in your account settings on the Perlego website. Your subscription will stay active until the end of your current billing period. Learn how to cancel your subscription.
No, books cannot be downloaded as external files, such as PDFs, for use outside of Perlego. However, you can download books within the Perlego app for offline reading on mobile or tablet. Learn more here.
Perlego offers two plans: Essential and Complete
  • Essential is ideal for learners and professionals who enjoy exploring a wide range of subjects. Access the Essential Library with 800,000+ trusted titles and best-sellers across business, personal growth, and the humanities. Includes unlimited reading time and Standard Read Aloud voice.
  • Complete: Perfect for advanced learners and researchers needing full, unrestricted access. Unlock 1.4M+ books across hundreds of subjects, including academic and specialized titles. The Complete Plan also includes advanced features like Premium Read Aloud and Research Assistant.
Both plans are available with monthly, semester, or annual billing cycles.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Yes! You can use the Perlego app on both iOS or Android devices to read anytime, anywhere — even offline. Perfect for commutes or when you’re on the go.
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app.
Yes, you can access The Clojure Workshop by Joseph Fahey,Thomas Haratyk,Scott McCaughie,Yehonathan Sharvit,Konrad Szydlo in PDF and/or ePUB format, as well as other popular books in Computer Science & Application Development. We have over one million books available in our catalogue for you to explore.

Information

1. Hello REPL!

Overview
In this chapter, we explain the basics of creating Clojure programs. We start by getting you familiar with the Read Eval Print Loop (REPL), where most of the experimentation happens when writing code. The REPL also allows you to explore code and documentation by yourself, so it is an excellent place to start. After the quick dive in the REPL, we describe in more detail how to read and understand simple Lisp and Clojure code, which syntax can sometimes appear unsettling. We then explore fundamental operators and functions in Clojure, which enable you to write and run simple Clojure programs or scripts.
By the end of this chapter, you will be able to use the REPL and work with functions in Clojure.

Introduction

Have you ever ended up entangled in the "spaghetti code" of an object-oriented application? Many experienced programmers would say yes, and at some point in their journey or career would reconsider the foundation of their programs. They might look for a simpler, better alternative to object-oriented programming, and Clojure is an appealing choice. It is a functional, concise, and elegant language of the Lisp family. Its core is small, and its syntax minimal. It shines because of its simplicity, which takes a trained eye to notice and ultimately understand. Employing Clojure's more sophisticated building blocks will allow you to design and build sturdier applications.
Whether you are a seasoned programmer or a novice, hobbyist or professional, C# wizard or Haskell ninja, learning a new programming language is challenging. It is, however, a highly rewarding experience that will make you an overall better programmer. In this book, you will learn by doing and will ramp up your skills quickly.
Clojure is an excellent choice of programming language to learn today. It will allow you to work efficiently using a technology built to last. Clojure can be used to program pretty much anything: from full-blown client-server applications to simple scripts or big data processing jobs. By the end of this book, you will have written a modern web application using Clojure and ClojureScript and will have all the cards in your hand to start writing your own!

REPL Basics

Welcome to the Clojure Read Eval Print Loop (REPL), a command-line interface that we can use to interact with a running Clojure program. REPL, in the sense that it reads the user's input (where the user is you, the programmer), evaluates the input by instantly compiling and executing the code, and prints (that is, displays) the result to the user. The read-eval-print three-step process repeats over and over again (loop) until you exit the program.
The dynamism provided by the REPL allows you to discover and experiment with a tight feedback loop: your code is evaluated instantly, and you can adjust it until you get it right. Many other programming languages provide interactive shells (notably, other dynamic languages such as Ruby or Python), but in Clojure, the REPL plays an exceptional and essential role in the life of the developer. It is often integrated with the code editor and the line between editing, browsing, and executing code blurs toward a malleable development environment similar to Smalltalk. But let's start with the basics.
Throughout these exercises, you may notice some mentions of Java (for example, in the stack trace in the second exercise). This is because Clojure is implemented in Java and runs in the Java Virtual Machine (JVM). Clojure can, therefore, benefit from a mature ecosystem (a battle-tested, widely deployed execution platform and a plethora of libraries) while still being a cutting-edge technology. Clojure is designed to be a hosted language, and another implementation, called ClojureScript, allows you to execute Clojure code on any JavaScript runtime (for example, a web browser or Node.js). This hosted-language implementation choice allows for a smaller community of functional programmers to strive in an industry dominated by Java, .NET Core, and JavaScript technologies. Welcome to the Clojure party, where we're all having our cake and eating it too.

Exercise 1.01: Your First Dance

In this exercise, we will perform some basic operations in the REPL. Let's get started:
  1. Open Terminal and type clj. This will start a Clojure REPL:
    $ clj
    The output is as follows:
    Clojure 1.10.1
    user=>
    The first line is your version of Clojure, which in this example is 1.10.1. Don't worry if your version is different—the exercises we will go through together should be compatible with any version of Clojure.
    The second line displays the namespace we are currently in (user) and prompts for your input. A namespace is a group of things (such as functions) that belong together. Everything you create here will be in the user namespace by default. The user namespace can be considered your playground.
    Your REPL is ready to read.
  2. Let's try to evaluate an expression:
    user=> "Hello REPL!"
    The output is as follows:
    "Hello REPL!"
    In Clojure, literal strings are created with double quotes, "". A literal is a notation for representing a fixed value in source code.
  3. Let's see what happens if we type in multiple strings:
    user=> "Hello" "Again"
    The output is as follows:
    "Hello"
    "Again"
    We have just evaluated two expressions sequentially, and each result is printed onto separate lines.
  4. Now, let's try a bit of arithmetic, for example, 1 + 2:
    user=> 1 + 2
    The output is as follows:
    1
    #object[clojure.core$_PLUS_ 0xe8df99a "clojure.core$_PLUS_@e8df99a"]
    2
    The output is not exactly what we expected. Clojure evaluated the three components, that is, 1, +, and 2, separately. Evaluating + looks strange because the + symbol is bound to a function.
    Note
    A function is a unit of code that performs a specific task. We don't need to know more for now except that fu...

Table of contents

  1. Preface
  2. 1. Hello REPL!
  3. 2. Data Types and Immutability
  4. 3. Functions in Depth
  5. 4. Mapping and Filtering
  6. 5. Many to One: Reducing
  7. 6. Recursion and Looping
  8. 7. Recursion II: Lazy Sequences
  9. 8. Namespaces, Libraries and Leiningen
  10. 9. Host Platform Interoperability with Java and JavaScript
  11. 10. Testing
  12. 11. Macros
  13. 12. Concurrency
  14. 13. Database Interaction and the Application Layer
  15. 14. HTTP with Ring
  16. 15. The Frontend: A ClojureScript UI
  17. Appendix