Skip to content

Collection checkers and strings

marick edited this page Feb 28, 2013 · 2 revisions

Because Clojure treats strings as a sequence of characters, the sequence checkers checkers apply. Given regular expressions, they're seldom useful.

Here are some examples:

     "abc" => (contains "bc")                ;; Same as => #"bc"
     "abc" => (contains "ac" :gaps-ok)       ;; Same as #"a.*c"
     "abc" => (contains "ba" :in-any-order)  ;; Can't do this one with regular expressions.

     "abc" => (has-suffix "bc")    ;; More expressive than `(.endsWith "abc" "bc") => truthy`

     "123" => (has every? #(Character/isDigit %))
     "11"  => (two-of \\1)

Since a regular expression on the right-hand side has the semantics of re-find, the following approves any string that contains three digits in a row:

     "123" => #"\d\d\d"

If you want to say that the string is exactly three digits, you can write this:

     "123" => (just #"\d\d\d")

When a string is used to check a collection, it is applied to each element of the collection. The following two examples mean the same thing:

     ["1" "2" "3"] => (contains   "2"  )
     ["1" "2" "3"] => (contains [ "2" ])

Strings can be compared to individual characters or collections of characters.

     "s"   => (just \s)
     "s"   => (contains \s)
     "as"  => (contains [\s \a] :in-any-order)
     "as"  => (contains  \s \a :in-any-order) ; brackets can be dropped
     "afs" => (contains [\s \a] :in-any-order :gaps-ok)

Note that the reverse is not true: you can't directly compare a collection of characters to an expected string. To do that, convert the string into a vector:

      [\a \b] => (just "ab")       ; fails
      [\a \b] => (just (vec "ab")) ; succeeds
Clone this wiki locally