Theory: if functionλ︎
Clojure has an if function that evaluates an expresssion. If that expression is true, then the first value is returned, if false then the second argument is returned.
In pseudo-code, the if function in Clojure works as follows
In the project code an if function checks the web address by returning the value associated with :uri in the request map.
If the :url value is equal to / then the first response map with the hello message is returned.
If the :uri value is not equal to / then the second resource map with an error message is returned.
(if (= "/" (:uri request))
{:status 200
:body "<h1>Hello, Clojure World</h1>
<p>Welcome to your first Clojure app.</p>"
:headers {}}
{:status 404
:body "<h1>This is not the page you are looking for</h1>
<p>Sorry, the page you requested was not found!></p>"
:headers {}}))
Hint::Single path if functionλ︎
In the case where an if expression is defined with only one value and the expression is false, then the value
nilis returned.whenfunction is the idiomatic choice over a single pathiffunction.
Multiple expression if function with doλ︎
Each of the two possible values the if function returns can come from only evaluating one expression. For example
If you need multiple expressions they can be wrapped in the do function
The do function calls each function evaluation in turn, returning the result of the last function called.
Hint::Compojure for managing routesλ︎
The
iffunction is a very simplistic way to define routes in our web application. Compojure is a library for elegantly managing routing for our Clojure server-side applications.