Skip to content

Conform

Does a value conform to a specification?λ︎

clojure.spec.alpha/conform takes two arguments

  • a specification
  • a value to test against the specification

:clojure.spec.alpha/invalid is returned when a value does not conform to a specification.

If the value does conform to the specification, then the value is returned. This value is referred to as a conformed value.

Require the Clojure spec libraryλ︎

Set the namespace for the page and require clojure.spec.alpha library, setting the alias to spec

(ns practicalli.clojure.specifications
  (:require [clojure.spec.alpha :as spec]))

Using conformλ︎

If the value conforms to the spec, a conformed value is returned

(spec/conform odd? 101)

When a value does not conform to a spec, the value :clojure.spec.alpha/invalid is returned

(spec/conform even? 101)

(spec/conform integer? 1)

(spec/conform seq? [1 2 3])

(spec/conform seq? (range 10))

(spec/conform map? {})

(spec/conform map? (hash-map :a 1 :b 2))