英和訓練

著作権は原文に属します

(WIP) Guides - spec Guide

Getting started 始め方

The spec library specifies the structure of data, validates or destructures it, and can generate data based on the spec. specライブラリを用いると、データの構造のスペックを書くことができる。また、データを検証したり分割することができ、記述したスペックに従ってデータを自動生成することもできる。

To use spec, declare a dependency on Clojure 1.9: specを使うには、次のようにClojure 1.9への依存関係を宣言する。

[org.clojure/clojure "1.9.0"]

To start working with spec, require the clojure.spec.alpha namespace at the REPL: specを用いるには、clojure.spec.alpha名前区間をREPLでrequireする必要がある。

(require '[clojure.spec.alpha :as s])

Or include spec in your namespace: あるいは、あなたの名前空間でspecをincludeしてもいい。

(ns my.ns
  (:require [clojure.spec.alpha :as s]))

Predicates 述語

Each spec describes a set of allowed values. それぞれのスペックは、許される値らのセットを記述する。There are several ways to build specs and all of them can be composed to build more sophisticated specs. スペックを組み立てるには多くの方法があるが、そうして得られるどんなスペックも、さらに複雑なスペックの構成部品にすることができる。

Any existing Clojure function that takes a single argument and returns a truthy value is a valid predicate spec. 既存のClojure関数であって、1つの引数を取り、論理的な真偽値を返す関数は有効な述語スペックである。We can check whether a particular data value conforms to a spec using conform: 特定のデータがあるスペックに従っているかどうかは、conformを使えばチェックできる。

(s/conform even? 1000)
;;=> 1000

原文: https://clojure.org/guides/spec