Clojure Basics - Syntax and Native Data Types

Let's get our hands dirty with native data types and common operations.

Project Source Code

Get the project source code below, and follow along with the lesson material.

Download Project Source Code

To set up the project on your local machine, please follow the directions provided in the README.md file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.

This lesson preview is part of the Tinycanva: Clojure for React Developers course and can be unlocked immediately with a \newline Pro subscription or a single-time purchase. Already have access to this course? Log in here.

This video is available to students only
Unlock This Course

Get unlimited access to Tinycanva: Clojure for React Developers, plus 70+ \newline books, guides and courses with the \newline Pro subscription.

Thumbnail for the \newline course Tinycanva: Clojure for React Developers
  • [00:00 - 00:13] Closure is considered to be an expressive language and one of the reasons behind this is its syntax and native data types. If you don't come from a list background, you might find it hard to read closure code at first.

    [00:14 - 00:22] But we are in luck today because closure has just one syntactical rule. Elements inside parentheses are elements of a list.

    [00:23 - 00:40] The first element of the list is the function or the operation that you want to execute and all other elements are the arguments to this operation. This operation can be a function, this operation can be a macro or this operation can be something else that we will see later.

    [00:41 - 00:52] All you need to remember is that the first element of the list is the operation and all other elements are arguments to that operation. Let's start with the basic data types.

    [00:53 - 01:03] I highly recommend that you start the repel and follow along. We have numbers, we have floats and we have ratios for non-terminating decimals .

    [01:04 - 01:16] Strings are defined using double quotes and blue lanes can be true or false. The string function can be used to cast a variable to string and also to conc atenate two strings together.

    [01:17 - 01:29] The type function can be used to check the type of any variable. You would observe that the type of one is Java long number and the type of a ratio is closure long ratio.

    [01:30 - 01:40] This tells us that numbers in closure are just Java numbers whereas ratios are closure specific. Closure has no support for in-fix operations.

    [01:41 - 01:53] If you try to use an operator between two operands like 1+2 and try to execute it in the repel, you will get an error. All algebraic operations are defined as functions.

    [01:54 - 02:03] The plus function can be called with two or more arguments. Similarly, the product function can also be called using two or more arguments.

    [02:04 - 02:17] Closure course ships with all the algebraic functions that you would expect like question, remainder and all others. Inc or increment can be used to add one to any number.

    [02:18 - 02:30] Similarly, dick or decrement can be used to remove one from any number. Algebraic comparisons are also available as functions and all comparisons can take two or more arguments.

    [02:31 - 02:44] In fact, the comparison operator and most algebraic operators can work with just one argument. Closure code is organized into namespaces.

    [02:45 - 02:53] You can think of a namespace as an address for every file. It is similar to JavaScript module or PHP namespaces.

    [02:54 - 03:05] The thing to note here is that when you start a repel, you are in the user namespace by default. We can change this namespace and we will get to this later.

    [03:06 - 03:16] The death macro can be used to define variables in a namespace. Here we defined the age variable in the user namespace.

    [03:17 - 03:31] The literal age is now a symbol. A symbol is a string that points to another value. Like age points to the value 20. A concept similar to symbols is keywords.

    [03:32 - 03:40] Where symbols point to another value, keywords point to themselves. Keywords can be defined using a colon prefix.

    [03:41 - 03:48] And you can define namespace qualified keywords using double colons. Keywords and closure are very similar to keywords in Ruby.

    [03:49 - 04:06] And they are particularly suited to define keys for maps, enumerations, or variables that would never change like roles in a web based application. Collections and closure is an abstract type that implements the i sequence interface.

    [04:07 - 04:22] All compound data structures like lists, vectors, maps, and sets are collections. Collections have a common set of functions that apply across data structures and some data structure specific functions.

    [04:23 - 04:34] And all the operations that manipulate the collection in any way are immutable. Closure has no support for mutating a data structure after it is defined.

    [04:35 - 04:42] List enclosure is an implementation of the linked list algorithm. Link lists are optimized for traversal.

    [04:43 - 05:10] A list can be defined using the list operation with any number of arguments or using the literal form with a single code followed by parenthesis. We need the single code because if we try to define a list without it, closure will consider the list to be a function called where the first element is the operation and all other elements are the arguments to that operation.

    [05:11 - 05:23] In our case, the first element is not a valid operation. If you try to execute this code, you will get an error saying that one does not implement the iFn interface.

    [05:24 - 05:34] Which in plain words means that one is not a function and hence cannot be called. There are many functions that you can use to operate on a list.

    [05:35 - 05:51] We will try a few of them in this screencast, but I highly recommend that you read the lesson and test all the functions listed. The list predicate, i.e. a list with the question mark, can be used to figure out if a variable is a list.

    [05:52 - 06:03] Conventionally, functions ending with the question mark are considered to be predicates. The count function can be used to count the number of elements in a list.

    [06:04 - 06:17] Now count is a sequence function, which means that it will work for lists for vectors, for maps and even for sets. The conge function can be used to append an element at the top of the list.

    [06:18 - 06:32] Let's append 9 to the list 1, 3. The conge function is immutable, which means that the original list was never modified, but a new copy was created with the extra element 9.

    [06:33 - 06:49] In terms of implementation, a view was created on the original list and the elements were not copied to a new physical location. But from the point of view of a developer, this operation was immutable.

    [06:50 - 07:04] Vectors are conceptually similar to lists, but in terms of implementation, they are an array list and not a linked list. An array list is a list where each element has an index.

    [07:05 - 07:17] This makes vectors efficient for lookups. All these sequence functions like count and conge will work with vectors because vectors also implement the ISC quince interface.

    [07:18 - 07:34] To define a new vector, we can use the vector function or we can use the literal form and just use square brackets. And as you have guessed, there is also a predicate function to figure out if a variable is vector.

    [07:35 - 07:50] Vectors implement the I-F-N interface as well, which means that vectors are callable as functions. They accept an argument, which is the index that you want to look up and return the value of the element at that index.

    [07:51 - 08:00] If the index is out of bounds, an exception is raised. They should give you more insight into how closure is built up as a language.

    [08:01 - 08:11] For something to be a sequence, it needs to implement the ISC qu interface. For something to be a function, it needs to implement the I-F-N interface.

    [08:12 - 08:23] Maps are a collection of key value pairs and can be defined using the literal form of curly brackets. The keys in the map are generally a keyword.

    [08:24 - 08:43] Maps also implement the I-S-E-Q interface, which means that all sequence functions like count and conge will work for maps. ASOC, or short for associate, is a map-specific function that adds one or more key value pairs to a map.

    [08:44 - 09:00] Similarly, D-SOC removes one or more key value pairs from the map. The function keys and vowels can be used to get all the keys or all the values of a map as a list, and the function get can be used to look up certain keys in the map.

    [09:01 - 09:22] By default, the maps are not sorted, but it is possible to create sort orders, depending on the keys or values or a combination. Sets are unordered collection of elements that can be defined using the literal form hash followed by curly brackets, or also using the operation set.

    [09:23 - 09:37] There are many set specific functions, like Union Intersection Difference, which are defined in the closure set namespace. So, in order to access them, you need to require the closure set namespace.

    [09:38 - 09:52] All other functions that we have used so far are a part of the closure core namespace, which is required by default. We can use the required macro to require the set specific functions.

    [09:53 - 10:05] Don't worry if this syntax doesn't make sense. We'll study this in more detail later in this course. After you have required these functions, you can execute them just like other functions.

    [10:06 - 10:17] Custom functions can be defined using the Defend Macro. The Defend Macro accepts a function name, a vector of arguments, and the function body.

    [10:18 - 10:28] There is no need to specify return values explicitly, because the last element of the list is returned by default. Comments can be defined using a semicolon.

    [10:29 - 10:44] The convention is to use two semicolons for a fullline command and one semicol on for an inline comment. We can also use the comment macro to define multiline comments that contain code.

    [10:45 - 10:58] This is generally called a rich comment, and we'll study about this later in this course. Commas are read as white spaces. You don't really need to add them, but you can if you wish.

    [10:59 - 11:14] This brings us to the concept of homo-econicity. In C-family languages, like JavaScript, the syntax used for defining data structures is remarkably different from the syntax used for defining functions.

    [11:15 - 11:31] In closure, the same literal forms are used to define data as well as code. On the left you see a list of three numbers, a vector of two elements, and a function definition, which is a combination of list and vectors.

    [11:32 - 11:47] And on the right hand side, you can see JavaScript code, where the syntax for defining data is different from the syntax for defining functions. In conclusion, closure affords us all the data types that we'd expect from a language.

    [11:48 - 12:02] Some data types like numbers and strings are just host data types, whereas some others like ratios are closure-specific data types. We also learned that collections are abstract and are built around the i- sequence protocol.

    [12:03 - 12:18] We then studied the homo-iconic nature of closure to conclude that code is data and data is code. Before you proceed to the next chapter, I recommend that you read the lesson text and practice all the functions mentioned in there.

    [12:19 - 12:34] This chapter just scratches the surface of the extent of closure's API. Closure.org is a good community resource to learn more about the API and functions available in the closure core.