Predicate functions

A predicate is a function that returns a true or false value and their names end with ? by convention.

  1. (odd? 1)
  1. (string? "am i a string")
  1. (int? 2.3)
  1. (int? 2.3)

Hint::clojure.core predicate functions

There are 80+ predicate functions in clojure.core

Using predicate functions as specifications

Predicate functions can be used as un-named specifications to test values conform.

Include the clojure.spec.alpha namespace to access the spec functions.

  1. (require '[clojure.spec.alpha :as spec])
  1. (spec/conform int? 42)
  1. (spec/conform seq? (range 4))

Custom predicate functions

Define custom predicate functions with defn or fn or the short form #()

Using an anonymous function

  1. (spec/conform (fn [value] (= value 42)) 42)

When the expression is quite terse, then the short form of an anonymous function is typically used. The % represents the value passed as an argument.

  1. (spec/conform #(= % 42) 42)