Reader small image

You're reading from  The Clojure Workshop

Product typeBook
Published inJan 2020
Reading LevelBeginner
PublisherPackt
ISBN-139781838825485
Edition1st Edition
Languages
Right arrow
Authors (5):
Joseph Fahey
Joseph Fahey
author image
Joseph Fahey

Joseph Fahey has been a developer for nearly two decades. He got his start in the Digital Humanities in the early 2000s. Ever since then, he has been trying to hone his skills and expand his inventory of techniques. This lead him to Common Lisp and then to Clojure when it was first introduced. As an independent developer, Joseph was able to quickly start using Clojure professionally. These days, Joseph gets to write Clojure for his day job at Empear AB.
Read more about Joseph Fahey

Thomas Haratyk
Thomas Haratyk
author image
Thomas Haratyk

Thomas Haratyk graduated from Lille University of Science and Technology and has been a professional programmer for nine years. After studying computer science and starting his career in France, he is now working as a consultant in London, helping start-ups develop their products and scale their platforms with Clojure, Ruby, and modern JavaScript.
Read more about Thomas Haratyk

Scott McCaughie
Scott McCaughie
author image
Scott McCaughie

Scott McCaughie lives near Glasgow, Scotland where he works as a senior Clojure developer for Previse, a Fintech startup aiming to solve the problem of slow payments in the B2B space. Having graduated from Heriot-Watt University, his first 6 years were spent building out Risk and PnL systems for JP Morgan. A fortuitous offer of a role learning and writing Clojure came up and he jumped at the chance. 5 years of coding later and it's the best career decision he's made. In his spare time, Scott is an avid reader, enjoys behavioral psychology and financial independence podcasts, and keeps fit by commuting by bike, running, climbing, hill walking, snowboarding. You get the picture!
Read more about Scott McCaughie

Yehonathan Sharvit
Yehonathan Sharvit
author image
Yehonathan Sharvit

Yehonathan Sharvit has been a software developer since 2001. He discovered functional programming in 2009. It has profoundly changed his view of programming and his coding style. He loves to share his discoveries and his expertise. He has been giving courses on Clojure and JavaScript since 2016. He holds a master's degree in Mathematics.
Read more about Yehonathan Sharvit

Konrad Szydlo
Konrad Szydlo
author image
Konrad Szydlo

Konrad Szydlo is a psychology and computing graduate from Bournemouth University. He has worked with Clojure for the last 8 years. Since January 2016, he has worked as a software engineer and team leader at Retailic, responsible for building a website for the biggest royalty program in Poland. Prior to this, he worked as a developer with Sky, developing e-commerce and sports applications, where he used Ruby, Java, and PHP. He is also listed in the Top 75 Datomic developers on GitHub.
Read more about Konrad Szydlo

View More author details
Right arrow

8. Namespaces, Libraries and Leiningen

Overview

In this chapter, we learn how to organize Clojure code. We start by looking at namespaces—a way to group Clojure functions together. We will see how to create our own namespaces and how to import namespaces written by others. Namespaces are building blocks of Clojure libraries. We will learn how to import and use Clojure libraries. After learning about namespaces and libraries, we investigate how to structure a Clojure project. We then look at Leiningen project template and how it helps developers to create applications.

By the end of this chapter, you will be able to use Leiningen to organize and run your projects.

Introduction

In the previous chapter, we learned about sequences in Clojure and how working with them helps us to build Clojure programs. Now that you're familiar with using Clojure to implement various pieces of functionality, it's time to become more comfortable with accomplishing the basic tasks of creating, building, testing, deploying, and running projects in Clojure and ClojureScript.

Clojure was designed to be a very practical language from the beginning. Getting things done means interacting with the outside world, building projects, using libraries, and deploying your work. As a developer, you will need to organize written code in a structure. In this chapter, you will see how namespaces can help you structure code and how build tools such as Leiningen help you put together a whole application.

In a real-world project, you won't write all the code. External dependencies are a crucial part of any project, and we'll learn here how to bring them into...

Namespaces

Namespaces are a way to organize Clojure functions; you can think of a namespace as being a directory (or file) that stores a particular group of functions. Each directory is independent of other directories; this helps to keep different groups of functions separate and gives a clear structure to your code. It also helps to avoid the confusion that can come with naming clashes.

Consider a situation where you have written a function called calculate-total, and as part of your project, you're using a library (more on libraries later in this chapter) that also contains a function called calculate-total. Although these functions have the same name, they work differently, produce slightly different outputs, and are intended to be used in different situations. When you come to use calculate-total in your code, how does the system know which calculate-function you actually want? That's where namespaces come in. The two functions will exist in different namespaces,...

Importing Clojure Functions with require and use

In the previous section, we learned how to import Clojure functions using refer. In this section, we will learn how we can import Clojure functions with require and use.

While refer allows us to literally refer to other namespaces' vars without fully qualifying them, often we need more than that. In the previous exercise, we imported a namespace and accessed vars such as fruits from it without using the namespace name as a prefix to the garden/fruits var. Often, we want to load functions from a namespace and use those functions. If we want to read the file, we need to import code from the Clojure I/O library (the library for input-output operations such as reading and writing files).

With the require function, we will load a namespace that we'll specify. This way, functions from the loaded namespace are available in our namespace for use. This is a great way to write Clojure code, reuse existing functions, and make them...

Leiningen—A Build Tool in Clojure

With namespaces, we put our functions into files and group related functions together. This helps to keep code separated into units. Consider a situation where utility functions are separated from frontend functions. This helps us navigate code and find functions. We know that frontend functions responsible for creating HTML will not be in a backend namespace responsible for connecting to the database. Build tools serve a different purpose. As the name suggests, these are tools that help us build. With them, we automate the creation of an executable application. An alternative would be to compile all the code ourselves and put it on a server. Even in applications with only a few features, we run the risk of forgetting to compile a namespace. The more namespaces there are and the more complicated an application is, the bigger the risk of making a mistake in manual code compilation. Build tools compile our code and package it into a usable form...

Working with External Libraries

Libraries are packaged programs that are ready to be used in other projects. External libraries are libraries that come from other developers. In Clojure, examples of such libraries include Ring, an HTTP library; clojure.java-time, a library for time and date manipulation; and hiccup, a library for writing HTML code using Clojure-style syntax.

Most projects will require developers to use existing code packaged as libraries. This is a good thing. We do not want to write code over and over again if the problem at hand has been already solved and someone has created a library for it that we can use.

In this section, we will use the clojure.Java-time library to display the current time.

Exercise 8.11: Using an External Library in a Leiningen Project

The aim of this exercise is to show you how to add a library to a Leiningen project and demonstrate how this library is used in code:

  1. The first thing to do is add a dependency to the time...

Leiningen Profiles

Profiles are a Leiningen tool that allows us to change the configuration of our projects. A profile is a specification that influences how a project behaves. For example, during development or testing, say that we would like to include testing frameworks in our builds but the production build does not need testing dependencies. Using profiles is a great way to separate different development setups that should be run against one code base.

Leiningen allows us to define profiles in a few places depending on our needs:

  • In the project.clj file
  • In the profiles.clj file
  • In the ~/.lein/profiles.clj file

Leiningen profiles defined in project.clj are specific to that particular project. Such profiles will not affect other projects. This allows separation between projects and the ability to customize them independently. We could have one application that uses the newest version of Clojure and requires different libraries to another application relying...

Summary

In this chapter, we learned about namespaces in Clojure. Namespaces are key Clojure concepts. We organize code into namespaces. We investigated various ways in which we can import namespaces in Clojure by using refer, require, and use. With each option to import, we learned the syntax of importing functions and when to use each type of function. We went into depth and investigated the :only, :exlude, and :rename keywords, which help us to fine-tune importing.

Then, we learned about Leiningen—a popular Clojure build tool. We created a Leiningen application and explored how Clojure projects are structured. We added dependencies on libraries. Finally, we saw how we can customize Leiningen projects using profiles. We created an application that accepted command-line arguments that were used by the application to customize the output.

In the next chapter, we will investigate host platform interop—accessing Java and JavaScript from Clojure.

...
lock icon
The rest of the chapter is locked
You have been reading a chapter from
The Clojure Workshop
Published in: Jan 2020Publisher: PacktISBN-13: 9781838825485
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (5)

author image
Joseph Fahey

Joseph Fahey has been a developer for nearly two decades. He got his start in the Digital Humanities in the early 2000s. Ever since then, he has been trying to hone his skills and expand his inventory of techniques. This lead him to Common Lisp and then to Clojure when it was first introduced. As an independent developer, Joseph was able to quickly start using Clojure professionally. These days, Joseph gets to write Clojure for his day job at Empear AB.
Read more about Joseph Fahey

author image
Thomas Haratyk

Thomas Haratyk graduated from Lille University of Science and Technology and has been a professional programmer for nine years. After studying computer science and starting his career in France, he is now working as a consultant in London, helping start-ups develop their products and scale their platforms with Clojure, Ruby, and modern JavaScript.
Read more about Thomas Haratyk

author image
Scott McCaughie

Scott McCaughie lives near Glasgow, Scotland where he works as a senior Clojure developer for Previse, a Fintech startup aiming to solve the problem of slow payments in the B2B space. Having graduated from Heriot-Watt University, his first 6 years were spent building out Risk and PnL systems for JP Morgan. A fortuitous offer of a role learning and writing Clojure came up and he jumped at the chance. 5 years of coding later and it's the best career decision he's made. In his spare time, Scott is an avid reader, enjoys behavioral psychology and financial independence podcasts, and keeps fit by commuting by bike, running, climbing, hill walking, snowboarding. You get the picture!
Read more about Scott McCaughie

author image
Yehonathan Sharvit

Yehonathan Sharvit has been a software developer since 2001. He discovered functional programming in 2009. It has profoundly changed his view of programming and his coding style. He loves to share his discoveries and his expertise. He has been giving courses on Clojure and JavaScript since 2016. He holds a master's degree in Mathematics.
Read more about Yehonathan Sharvit

author image
Konrad Szydlo

Konrad Szydlo is a psychology and computing graduate from Bournemouth University. He has worked with Clojure for the last 8 years. Since January 2016, he has worked as a software engineer and team leader at Retailic, responsible for building a website for the biggest royalty program in Poland. Prior to this, he worked as a developer with Sky, developing e-commerce and sports applications, where he used Ruby, Java, and PHP. He is also listed in the Top 75 Datomic developers on GitHub.
Read more about Konrad Szydlo