Clojure for Java Developers
By Díaz Eduardo
()
About this ebook
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.
Related to Clojure for Java Developers
Related ebooks
Learn ClojureScript: Functional programming for the web Rating: 0 out of 5 stars0 ratingsGo Programming Cookbook: Over 75+ recipes to program microservices, networking, database and APIs using Golang Rating: 0 out of 5 stars0 ratingsJAVA 9.0 To 13.0 New Features: Learn, Implement and Migrate to New Version of Java. Rating: 0 out of 5 stars0 ratingsKotlin Quick Start Guide: Core features to get you ready for developing applications Rating: 0 out of 5 stars0 ratingsJakarta EE for Java Developers: Build Cloud-Native and Enterprise Applications Using a High-Performance Enterprise Java Platform Rating: 0 out of 5 stars0 ratingsBuilding Kotlin Applications: A comprehensive guide for Android, Web, and Server-Side Development (English Edition) Rating: 0 out of 5 stars0 ratingsJava 8 to 21: Explore and work with the cutting-edge features of Java 21 (English Edition) Rating: 0 out of 5 stars0 ratingsJulia High Performance Rating: 4 out of 5 stars4/5Clojure Web Development Essentials Rating: 0 out of 5 stars0 ratingsClojure High Performance Programming - Second Edition Rating: 0 out of 5 stars0 ratingsLearning Concurrent Programming in Scala Rating: 0 out of 5 stars0 ratingsThe Clojure Workshop: Use functional programming to build data-centric applications with Clojure and ClojureScript Rating: 0 out of 5 stars0 ratingsElixir Cookbook Rating: 0 out of 5 stars0 ratingsMastering Clojure Rating: 0 out of 5 stars0 ratingsErlang and OTP in Action Rating: 0 out of 5 stars0 ratingsClojure Programming Cookbook Rating: 0 out of 5 stars0 ratingsSchematron: A language for validating XML Rating: 0 out of 5 stars0 ratingsAspectJ in Action: Enterprise AOP with Spring Applications Rating: 0 out of 5 stars0 ratingsFunctional Programming in Scala Rating: 4 out of 5 stars4/5Code Beneath the Surface: Mastering Assembly Programming Rating: 0 out of 5 stars0 ratingsPractical Rust 1.x Cookbook Rating: 0 out of 5 stars0 ratingsCommon Lisp A Complete Guide Rating: 1 out of 5 stars1/5Scientific Computing with Scala Rating: 0 out of 5 stars0 ratingsSpring Batch in Action Rating: 0 out of 5 stars0 ratingsNeo4j in Action Rating: 0 out of 5 stars0 ratingsThe Way to Go: A Thorough Introduction to the Go Programming Language Rating: 2 out of 5 stars2/5Haskell Design Patterns Rating: 0 out of 5 stars0 ratingsClojure Reactive Programming Rating: 0 out of 5 stars0 ratings
Programming For You
Learn Data Warehousing in 24 Hours Rating: 0 out of 5 stars0 ratingsGrokking Algorithms: An illustrated guide for programmers and other curious people Rating: 4 out of 5 stars4/5SQL All-in-One For Dummies Rating: 3 out of 5 stars3/5SQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5Python: Learn Python in 24 Hours Rating: 4 out of 5 stars4/5Learn JavaScript in 24 Hours Rating: 3 out of 5 stars3/5HTML, CSS, & JavaScript All-in-One For Dummies Rating: 0 out of 5 stars0 ratingsLearn Algorithmic Trading: Build and deploy algorithmic trading systems and strategies using Python and advanced data analysis Rating: 0 out of 5 stars0 ratingsJavascript For Beginners: Your Guide For Learning Javascript Programming in 24 Hours Rating: 3 out of 5 stars3/5Python for Finance Cookbook: Over 50 recipes for applying modern Python libraries to financial data analysis Rating: 0 out of 5 stars0 ratingsMastering C# and .NET Framework Rating: 5 out of 5 stars5/5ASP.NET Core in Action, Third Edition Rating: 0 out of 5 stars0 ratingsReact Deep Dive Rating: 5 out of 5 stars5/5The Creative Programmer Rating: 0 out of 5 stars0 ratingsNarrative Design for Indies: Getting Started Rating: 4 out of 5 stars4/5Accelerated DevOps with AI, ML & RPA: Non-Programmer’s Guide to AIOPS & MLOPS Rating: 5 out of 5 stars5/5Python for Beginners: A Crash Course to Learn Python Programming in 1 Week Rating: 0 out of 5 stars0 ratingsGrokking Deep Reinforcement Learning Rating: 5 out of 5 stars5/5Grokking Simplicity: Taming complex software with functional thinking Rating: 4 out of 5 stars4/5Python Data Structures and Algorithms Rating: 5 out of 5 stars5/5Computational Thinking: A beginner's guide to problem-solving and programming Rating: 4 out of 5 stars4/5CSS in Depth Rating: 2 out of 5 stars2/5Ethical Hacking Rating: 4 out of 5 stars4/5
Reviews for Clojure for Java Developers
0 ratings0 reviews
Book preview
Clojure for Java Developers - Díaz Eduardo
Table of Contents
Clojure for Java Developers
Credits
About the Author
About the Reviewer
www.PacktPub.com
eBooks, discount offers, and more
Why subscribe?
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Downloading the color images of this book
Piracy
Questions
1. Getting Started with Clojure
Getting to know Clojure
Installing Leiningen
Using a REPL
The nREPL protocol
Hello world
REPL utilities and conventions
Creating a new project
Project structure
Creating a standalone app
Using Cursive Clojure
Installing Cursive Clojure
Getting started with Clojure code and data
Lists in Clojure
Operations in Clojure
Functions in Clojure
Clojure's data types
Scalars
Collection data types
Summary
2. Namespaces, Packages, and Tests
Namespaces in Clojure
Packages in Clojure
The classpath and the classloader
Back to Clojure namespaces
Playing with namespaces
Creating a new namespace
Working with namespaces on the REPL
Testing in Clojure
Testing from the command line
Testing in IntelliJ
Summary
3. Interacting with Java
Using Maven dependencies
Clojure interop syntax
Creating an object
Calling an instance method
Calling a static method or function
Accessing inner classes
Writing a simple image namespace
Writing the tests
The let statement
Destructuring in Clojure
Sequential destructuring
Associative destructuring
Exposing your code to Java
Testing from Groovy
Proxy and reify
Summary
4. Collections and Functional Programming
Basics of functional programming
Persistent collections
Types of collections in Clojure
The sequence abstraction
Specific collection types in Clojure
Vectors
Lists
Maps
Sorted maps and hash maps
Common properties
Sets
Sorted sets and hash sets
Common properties
Union, difference, and intersection
Applying functional programming to collections
The imperative programming model
The functional paradigm
Functional programming and immutability
Laziness
Summary
5. Multimethods and Protocols
Polymorphism in Java
Multimethods in Clojure
Keyword hierarchies
isa?
parents
descendants
underive
A la carte dispatch functions
Protocols in Clojure
Records in Clojure
Summary
6. Concurrency
Using your Java knowledge
The Clojure model of state and identity
Promises
Pulsar and lightweight threads
Futures
Software transactional memory and refs
Atoms
Agents
Validators
Watchers
core.async
Why lightweight threads?
Goblocks
Channels
Transducers
Summary
7. Macros in Clojure
Lisp's foundational ideas
Macros as code modification tools
Modifying code in Java
Modifying code in Groovy
The @ToString annotation
The @TupleConstructor annotation
The @Slf4j annotation
Writing your first macro
Debugging your first macro
Quote, syntax quote, and unquoting
Unquote splicing
gensym
Macros in the real world
References
Summary
Index
Clojure for Java Developers
Clojure for Java Developers
Copyright © 2016 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: February 2016
Production reference: 1190216
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78528-150-1
www.packtpub.com
Credits
Author
Eduardo Díaz
Reviewers
Ning Sun
Nate West
Commissioning Editor
Kunal Parikh
Acquisition Editor
Usha Iyer
Content Development Editors
Neeshma Ramakrishnan
Kajal Thapar
Technical Editor
Saurabh Malhotra
Copy Editor
Sneha Singh
Project Coordinator
Shweta H. Birwatkar
Proofreaders
Safis Editing
Indexer
Hemangini Bari
Production Coordinator
Shantanu N. Zagade
Cover Work
Shantanu N. Zagade
About the Author
Eduardo Díaz is a developer with a strong background in the Java language. He has a passion for functional programming and new programming paradigms. His work includes full stack development, systems design, and high volume real time data processing.
He has worked on every technology related problem you can imagine, as a consultant solving anything related to Java, UNIX, C, or any other strange problem you might have had.
As a developer, he has been working for around 10 years on Java, Python, Scala, Clojure, in the media, bank, and primarily communications industries.
He is currently working at Grupo Expansion, a media company, where he helps design and implement a new content delivery platform aiming to empower content editors and encourage developers to find new ways to use data.
First of all, I would like to thank Neeshma Ramakrishnan and Kajal Thapar at Packt Publishing, they are the best editors, they have an incredible amount of patience and without them, there would be nothing even close to a book.
I would also like to thank my colleagues and friends at Grupo Expansion, Lucasian Labs, and Javanes; they have helped me grow in every way imaginable. Thanks for everything!
Last but not the least, thanks to my family for always keeping up with me, cheering me up, believing in me and helping in everything I do, even when it means not seeing me for weeks or months. I truly value your support!
About the Reviewer
Ning Sun is a software engineer currently working for a China-based startup, LeanCloud, providing one-stop backend as a service
for mobile apps. Being a startup engineer, he has to solve various kinds of problems and play different kinds of roles; however, he has always been an enthusiast for open source technology. He contributes to several open source projects and has also learned a lot from them.
At LeanCloud, he built a messaging system that supports tens of millions of clients per day. The system is fully powered by Clojure and its ecosystem. He has been an early member of Clojure Chinese community since 2011. He has delivered talks at several meetups and has been very active in the mailing list and open source projects. Ning created Clojure RPC library Slacker
, which is widely used at LeanCloud and several other companies.
He worked on https://Delicious.com in 2013, which is known as one of the most important websites in the early Web 2.0 era.
He has been a reviewer of two books about Solr at Packt Publishing. Also, he was the reviewer of Programming Clojure, Chinese edition.
You can always find him on Github.com/sunng87 and Twitter.com/Sunng.
www.PacktPub.com
eBooks, discount offers, and more
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.
https://www2.packtpub.com/books/subscription/packtlib
Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read Packt's entire library of books.
Why subscribe?
Fully searchable across every book published by Packt
Copy and paste, print, and bookmark content
On demand and accessible via a web browser
Preface
In the last few years, we have seen a widespread tendency to create new languages for JVM. There are all sorts of new languages with different paradigms and different ways of working.
Clojure is one of those languages, one that we believe is worth learning.
Over the course of this book, you will learn about Clojure and how opinionated it is. You will learn why immutable objects are not only possible, but it is a good idea to use them.
You will learn about functional programming and see how it fits the concept of immutable programs.
You will understand the very powerful idea of representing your code as a data structure of the same language.
It is important to note that we will build all this knowledge on top of what you already know; this book assumes that you understand the Java language and a bit of how it works. It assumes that you understand how to create classes and objects, how to call methods, and also a bit about the JVM. We will find similarities and differences from the Java language that you already know, so you can understand how the Clojure world works.
It is said that a great programmer is not the one who knows a lot of different programming languages, it is someone who knows different programming paradigms.
Clojure brings ideas from Lisp and functional programming, which are different to what you are used to. This book will help you understand the power behind these ideas and why a language so old still exists and has a place.
Learning Lisp is one of the great pleasures of life, I hope you enjoy it!
What this book covers
Chapter 1, Getting Started with Clojure, is your first step with Clojure, from how to install an interpreter, how to use the IntelliJ plugin Cursive Clojure, and how to start writing your first lines of code in Clojure.
Chapter 2, Namespaces, Packages, and Tests, deals with how every other language needs a way to organize our code. We do it with namespaces, and we start learning by doing little experiments and by comparing with the Java packages we already know.
Chapter 3, Interacting with Java, discusses the fact that one of the most useful features of Clojure is that it can be hosted on top of other platforms. Java is probably one of the most common platforms, and here we learn how to interact with Java code and libraries and how to expose our Clojure code to Java.
Chapter 4, Collections and Functional Programming, tells us that functional programming and immutable data structures (or collections) are fundamental to programming in the Clojure language; here we understand how we can write meaningful programs using immutable data structures and how functional programming is ideal for that.
Chapter 5, Multimethods and Protocols,