Clojure Fundamentals - Part 1

Learn functional programming with Clojure.
Course info
Rating
(189)
Level
Intermediate
Updated
Jul 13, 2012
Duration
3h 8m
Table of contents
Overview and the REPL
Functions
Namespaces
Collections
Destructuring
Sequences
Flow Control
Description
Course info
Rating
(189)
Level
Intermediate
Updated
Jul 13, 2012
Duration
3h 8m
Description

Clojure is a functional programming language for the Java Virtual Machine. This course explores Clojure starting with the basics, with only a Java or C# programming background assumed.

About the author
About the author

Alan is a developer at Relevance, where he specializes in web applications. He is a frequent contributor to the Clojure language, and has also contributed to several popular Clojure libraries. At Relevance, he's built Clojure software for a range of clients, in domains ranging from online gaming to network security.

Section Introduction Transcripts
Section Introduction Transcripts

Overview and the REPL
Hi, I'm Alan Dipert from Relevance, and welcome to the Clojure Class. Clojure is a high level dynamic functional programming language. It's also a lisp dialect. Clojure was designed to run on top of existing platforms, like the Java Virtual Machine, the dot net CLR and JavaScript engines. There are Clojure compilers for each of these platforms, but this course will focus on Clojure for the JVM, which is the most popular. However, what you learn will be generally applicable to Clojure on the other platforms as well. Clojure provides tools for managing state and can make programming for multiple course easier. It was also designed to be performant, which sets it apart from other dynamic languages. My hope with the course is to give you the student the knowledge and tools you need to use Clojure successfully on real projects and in product settings. Learning and using Clojure has been an absolute joy for me, and I hope that you have fun learning and using Clojure too. In this module, we'll cover Clojure's design rational, its general capabilities, and we'll introduce Clojure syntax and the Clojure REPL. The Clojure REPL is a kind of command line for Clojure that we'll use throughout the rest of the course to demonstrate Clojure code examples.

Functions
Hi, I'm Alan Dipert and in this module, we're going to talk about functions. Clojure is a functional programming language and as such, functions are a big part of programming and Clojure. And one way to think about them is the building block or the fundamental abstraction that Clojure programs tend to be built upon. In Clojure, functions are first class which means that a function object is treated like any other value and can be passed to other functions for instance, the same way a value might. Clojure also has support for higher ordered functions which are functions that could take functions as arguments. That's simply an implication of functions being first class. It's common in Clojure code to have pure functions. These are functions that do not have side effects in are closer to being the functions you might recognize from math than they are to same methods from programming languages you might be familiar with. We'll investigate what all these means and along the way, we'll take look at apply which is a way to apply a collection of data to a function as if it were an argument list and we'll also take a look at let which is a way in Clojure to create lexical scopes. Finally, we'll take a look at what method invocation and Clojure looks like and why it's different than function invocation. ( Pause )

Namespaces
Hello, I'm Alan Dipert and this is the names and namespaces module. Modularization is an important part of writing extensible, reusable, and understandable programs. Namespaces are a modularization tool in Clojure. They allow us to divide named things like functions or pieces of data into namespaces. A single Clojure program may have multiple namespaces. The division of names into namespaces makes naming easier because we have to worry less about the possibility of collisions. So, let's learn about Clojure's namespace system and the tools Clojure provides for working with namespaces.

Collections
Clojure comes with a rift (phonetic) set of collection data structures. Beyond the list, Clojure has literal support for maps or dictionaries, sets and vectors which are similar to arrays. Every closure collection is immutable, which means it cannot be updated in place. Instead of mutating collections, Functions and Clojure tend to take them as arguments and then return new collections without affecting the original. The immutability of Clojure data is part of what makes concurrent programming in Clojure easier. In this module, we'll learn about the syntax and characteristics of Clojure's collection data structures. We'll also explore the API Clojure provides for working with these collections. The computer scientist Alan Perlis once said, "It is better to have 100 functions operate on one data structure than have 10 functions operate on 10 data structures. " His description of what is better is similar to the way Clojure encourages programmers to orient many functions around several data structures. For now, you have to trust Mr. Perlis, but hopefully, after this module and with a little more Clojure under your belt, you'll feel the same way.

Destructuring
Clojure is definitely a functional programing language but all it's function are oriented around data so it may also be considered data oriented. We saw on the previous module that Clojure provides a rich set of collections and a rich set of functions for working with those collections. These functions make working with different kinds of data easier. In addition to the functions we saw, Clojure eases data work by providing a way to pull apart compound data structures concisely. This mechanism known as destructuring is a kind of mini-language embedded in Clojure that is incredibly useful. In this module, we'll learn about destructuring and use it to pull apart sequential and associative collections so that we can work with them in pieces.

Sequences
The ideas of order and sequentiality are useful in programing. Traditionally we use things like loops, arrays and strings to represent and convey these ideas. Clojure provides the sequence obstruction to consolidate or use of this ideas in our programs. This consolidation makes it possible for there to exist a large library of functions for working with sequential things. In this module, we look at the sequence obstruction and its associated API. We'll also see some code examples that leverage Clojure sequences.

Flow Control
Flow control is the control over which code in a program is executed. Flow control constructs are an important part of any programming language because we often need to do or not do certain things depending on some condition. Clojure provides a variety of flow control operators and a simple model for what is true and what is false. In this module, we'll learn Clojure simple Boolean semantics and apply them to flow control operators.