Higher order functions

Higher order functions are common in Clojure and spec provides fspec to support spec’ing them.

  1. (defn value-added-tax
  2. [tax-rate]
  3. #(+ (* tax-rate %) %))

The value-added-tax function returns an anonymous function that adds the value of tax to the given value.

Define a namespace for the page and require Clojure Spec

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

Declare a function spec for value-added-tax using clojure.spec.alpha/fspec for the return value:

eval-clojure (s/fdef value-added-tax :args (spec/cat :tax-rate number?) :ret (spec/fspec :args (s/cat :value number?) :ret number?))

The :ret specification uses fspec to declare that the returning function takes and returns a number.