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
Haskell Design Patterns Rating: 0 out of 5 stars0 ratingsMastering Clojure: From Basics to Expert Proficiency Rating: 0 out of 5 stars0 ratingsData Structures and Algorithms with Go: Create efficient solutions and optimize your Go coding skills (English Edition) Rating: 0 out of 5 stars0 ratingsClojure Programming Fundamentals: A Concise Guidebook Rating: 0 out of 5 stars0 ratingsHaskell from Another Site Rating: 0 out of 5 stars0 ratingsLearning Concurrent Programming in Scala - Second Edition Rating: 0 out of 5 stars0 ratingsAdvanced Java Data Structures: Techniques and Applications for Efficient Programming Rating: 0 out of 5 stars0 ratingsProgramming Problems: A Primer for The Technical Interview Rating: 4 out of 5 stars4/5Rust Mini Reference: A Hitchhiker's Guide to the Modern Programming Languages, #5 Rating: 0 out of 5 stars0 ratingsProgramming Language Concepts: Improving your Software Development Skills Rating: 0 out of 5 stars0 ratingsAdvanced Techniques in Common LISP: Expert Insights and In-Depth Applications Rating: 0 out of 5 stars0 ratingsLearn ClojureScript: Functional programming for the web 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 ratingsClojure Web Development Essentials Rating: 0 out of 5 stars0 ratingsClojure Programming Cookbook Rating: 0 out of 5 stars0 ratingsClojure High Performance Programming, Second Edition: Become an expert at writing fast and high performant code in Clojure 1.7.0 Rating: 0 out of 5 stars0 ratingsClojure Data Structures and Algorithms Cookbook Rating: 4 out of 5 stars4/5Mastering Scala: Elegance in Code Rating: 0 out of 5 stars0 ratingsGolang for Jobseekers: Unleash the power of Go programming for career advancement (English Edition) Rating: 0 out of 5 stars0 ratingsClojure Data Analysis Cookbook - Second Edition Rating: 5 out of 5 stars5/5Clojure Reactive Programming Rating: 0 out of 5 stars0 ratingsScientific Computing with Scala Rating: 0 out of 5 stars0 ratingsLearning Elasticsearch Rating: 4 out of 5 stars4/5Elixir Cookbook Rating: 0 out of 5 stars0 ratingsGetting Started with LLVM Core Libraries Rating: 0 out of 5 stars0 ratingsProfessional Java EE Design Patterns Rating: 0 out of 5 stars0 ratingsUltimate Nuxt.js for Full-Stack Web Applications Rating: 0 out of 5 stars0 ratingsGo Programming Blueprints Rating: 0 out of 5 stars0 ratingsJulia Cookbook Rating: 0 out of 5 stars0 ratings
Programming For You
Python: Learn Python in 24 Hours Rating: 4 out of 5 stars4/5Excel : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Excel Programming: 1 Rating: 5 out of 5 stars5/5Coding All-in-One For Dummies Rating: 4 out of 5 stars4/5Learn SQL in 24 Hours Rating: 5 out of 5 stars5/5SQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5PYTHON PROGRAMMING Rating: 4 out of 5 stars4/5Python Programming : How to Code Python Fast In Just 24 Hours With 7 Simple Steps Rating: 4 out of 5 stars4/5Beginning Programming with C++ For Dummies Rating: 4 out of 5 stars4/5PYTHON: Practical Python Programming For Beginners & Experts With Hands-on Project Rating: 5 out of 5 stars5/5JavaScript All-in-One For Dummies Rating: 5 out of 5 stars5/5Microsoft Azure For Dummies Rating: 0 out of 5 stars0 ratingsAlgorithms For Dummies Rating: 4 out of 5 stars4/5iPhone 16 Pro Max User Manual: The Complete Step-By-Step Guide to Maximize your New iPhone 16 Pro Max and iOS 18 Rating: 0 out of 5 stars0 ratingsPython Data Structures and Algorithms Rating: 5 out of 5 stars5/5Godot from Zero to Proficiency (Foundations): Godot from Zero to Proficiency, #1 Rating: 5 out of 5 stars5/5Linux: Learn in 24 Hours Rating: 5 out of 5 stars5/5Learn to Code. Get a Job. The Ultimate Guide to Learning and Getting Hired as a Developer. Rating: 5 out of 5 stars5/5SQL All-in-One For Dummies Rating: 3 out of 5 stars3/5Coding All-in-One For Dummies Rating: 0 out of 5 stars0 ratings
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.
eBooks, discount offers, and morehttps://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,