Conditions

We begin again with our vector of characters:

(def chars (vec "42 0.5 1/2 foo-bar"))

To begin parsing this into discrete tokens like "42" and "foo-bar", we need to know what category each character is. We already made a digit? function in the past, but now we'd like a more general function that receives a character and tells us what kind it is. Let's start with this:

(def digits #{\1 \2 \3 \4 \5 \6 \7 \8 \9 \0}) (defn char-type [ch] (if (contains? digits ch) "digit" "other"))

This function returns a string because we have many possible categories that we'll want to return. Now we can run it on every character using map:

(map char-type chars)

So far it only detects digits, but we can make it return the space category for spaces by adding another if branch. Here we can check for the space character by seeing if ch is equal to \space, which is Clojure's way of representing that character:

(defn char-type [ch] (if (contains? digits ch) "digit" (if (= ch \space) "space" "other")))

Now we can run map again:

(map char-type chars)

There are a few things we should clean up before going on. First of all, the nested ifs will get really hard to read as we add more. We can improve that by using cond, which allows us to specify as many branches as we want:

(defn char-type [ch] (cond (contains? digits ch) "digit" (= ch \space) "space" true "other"))

So with cond, we just provide a condition followed by what we want it to return. The last condition is simply true because we want it to be the "catch all" condition, for when the other conditions don't match.

The second thing we should fix is our use of strings. In Clojure, strings aren't normally used for internal labels like this. Instead, there is a special data type that is well suited for it: keywords. They look like this:

(defn char-type [ch] (cond (contains? digits ch) :digit (= ch \space) :space true :other))

Keywords are like symbols, but with a colon in front. They are used anywhere in Clojure when an internal label is needed. They happen to be faster than strings for many kinds of operations, which is a nice added benefit.

(map char-type chars)
Previous: Functions and ListsNext: Hash Maps