Clojure Conditionals - Understanding Flow Control Operators
Clojure offers many control flow conditionals, like if-else blocks. In this chapter, we'll study the API of a few such methods.
Get the project source code below, and follow along with the lesson material.
Download Project Source CodeTo 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.
Get unlimited access to Tinycanva: Clojure for React Developers, plus 70+ \newline books, guides and courses with the \newline Pro subscription.

[00:00 - 00:23] Closure lets you write concise code. A major factor that supports this fact is its rich standard library. So far we have studied syntax, build tools and N-repell. In this module, we will build on top of that knowledge and understand the most commonly used functions. We highly recommend that you follow along and try the examples yourself using inline eval.
[00:24 - 01:08] This is also a good time to practice some paraded commands. We will reuse the setup for first project and create a new namespace for each chapter. So start your watch process, start the node runtime and connect your editor to the repel. In this chapter, we will learn about various conditional functions. The code examples are present in the first project dot conditionals namespace. What is the "Connect this path for this namespace?" If you guessed it right, great. If you got it wrong, you might want to review the chapter on project structure again. We have seen a function already. It takes three arguments, a variable to check, an s-expression to execute if the variable is truthy and one if the variable is falsy.
[01:09 - 01:36] Empty data structures like lists, vectors and maps are truthy, whereas nil is falsy. You might find yourself in a position where you need to execute more than one s expressions in the if or else block. For example, instead of just returning the string, what if you wanted to print and return like so. This s-expression is invalid and wrapped in a command so it doesn't interfere with other examples. In cases like these, you can use a do block.
[01:37 - 01:46] Do takes an arbitrary number of s expressions and executes them in order. If not, it is similar to if except it executes the first block if the variable is falsy.
[01:47 - 02:20] The function is same as wrapping if with a not function, not inverses the balloon value of a variable. Some if statements might not care about the else clause. For example, you might want to render a component only when a variable is truthy. The when function can be used in such cases. It takes n arguments. The first argument is the Boolean expression to evaluate and the rest of the arguments are the expressions to execute. When also has a when not equivalent, which is similar to when, except it executes the block only if the test variable is falsy.
[02:21 - 02:43] In cases where when or when not do not execute, nil is returned. Quant or short for condition is similar to switch case. Quant takes even number of arguments where the arguments in even positions are the expressions to test and the arguments at odd positions are the output of those expressions. It also takes an else block which acts as a catchall.
[02:44 - 03:11] If the else is not specified and no case matches, nil is returned by the fault. If you write this function in your editor and try to execute test X 91, you might see an error saying that test X is not defined. This happens if you skip the evaluation of the definition of test X function. Your options here are to save so that the shadow watch rebuilds the process or to inline evaluate the definition of the function .
[03:12 - 03:22] Quant P is a modified version of Quant that takes a predicate function, a value and a test value. For each case, the predicate function is called on the value and test value.
[03:23 - 03:50] The conversion of test Y function would look as follows. Case is similar to Quant P equal to. In Quant P and case, the else blocks are not explicit, but the last element is considered to be else by default. In this module, we learned about various conditionals and also groomed the knowledge of N repellent paraded. Just like the chapter on paraded, think of this chapter as a reference.
[03:51 - 04:07] It will not be fair to assume that you will memorize the entire API in one setting. Try it once and refer to it as many times as you wanted. If you ever get stuck and need more examples, you might find closure docs.org website to be helpful.