Skip to content

Commit 535e427

Browse files
committed
More documentation.
1 parent 2509bd4 commit 535e427

File tree

7 files changed

+119
-4
lines changed

7 files changed

+119
-4
lines changed

doc/SUMMARY.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,9 @@
1212

1313
## Data
1414

15-
* [Data Validation](data_validation.md)
16-
* [Data Generation](data_generation.md)
15+
* [Data Validation](usage.md#data-validation)
16+
* [Data Generation](usage.md#data-generation)
17+
18+
## Misc
19+
20+
* [Usage in Babashka](babashka.md)

doc/babashka.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
As a babashka shell script:
2+
3+
```clojure
4+
#!/usr/bin/env bb
5+
6+
(require '[babashka.classpath :refer [add-classpath]]
7+
'[clojure.java.shell :refer [sh]])
8+
9+
(def deps '{:deps {minimallist {:git/url "https://github.com/green-coder/minimallist"
10+
:sha "b373bb18b8868526243735c760bdc67a88dd1e9a"}}})
11+
(def cp (:out (sh "clojure" "-Spath" "-Sdeps" (str deps))))
12+
(add-classpath cp)
13+
14+
15+
(require '[minimallist.core :as m])
16+
(require '[minimallist.helper :as h])
17+
18+
(m/valid? (h/fn int?) 1) ;=> true
19+
(m/valid? (h/fn int?) "1") ;=> false
20+
21+
;; Does not work for now.
22+
;(require '[clojure.test.check.generators :as tcg])
23+
;(require '[minimallist.generator :as mg])
24+
;(tcg/sample (mg/gen (h/fn int?)))
25+
```

doc/data_generation.md

Whitespace-only changes.

doc/data_valitation.md

Whitespace-only changes.

doc/model_builder.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,10 @@ outside of a `:condition-model` field, you need to add your own generator to the
101101

102102
```clojure
103103
;; A set of keywords.
104-
(h/set-of (fn keyword?))
104+
(h/set-of (h/fn keyword?))
105105

106106
;; Persons by name.
107-
(h/map-of (h/fn string?) (ref 'person))
107+
(h/map-of (h/fn string?) (h/ref 'person))
108108

109109
;; Sequence of numbers, either in a list or in a vector.
110110
(h/sequence-of (h/fn int?))

doc/usage.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# Usage
2+
3+
## Data Validation
4+
5+
```clojure
6+
(require '[minimallist.core :refer [valid?]])
7+
(require '[minimallist.helper :as h])
8+
9+
(valid? (h/fn string?) "Hello, world!")
10+
;=> true
11+
```
12+
13+
## Data Generation
14+
15+
```clojure
16+
(require '[minimallist.generator :as mg])
17+
(require '[clojure.test.check.generators :as tcg])
18+
19+
;; Implicit generation budget, varies with test.check's generator `size`.
20+
(tcg/sample (mg/gen (h/vector-of mg/fn-string?)))
21+
;([]
22+
; []
23+
; ["25"]
24+
; ["525" "Eq8"]
25+
; ["u" "IaDV" "JD"]
26+
; ["69y" "sk"]
27+
; ["0v" "" "M8" "rHD" "6qq0"]
28+
; ["IWd8Ma" "6yF0" "9DK" "jIK"]
29+
; ["HB6YheP" "tr3EQMj2" "j72Kw"]
30+
; ["29gL" "GM5Q" "5mM" "Ju2y0Xs" "ywn248pp"])
31+
32+
;; With an explicit generation budget of 20.
33+
(tcg/sample (mg/gen (h/vector-of mg/fn-string?)
34+
20))
35+
;(["" "" "" "" "" "" "" "" "" "" ""]
36+
; ["6" "" "B" "H" "" "" ""]
37+
; ["3" "lO" "MU" "9W" "M" "G1" ""]
38+
; ["D6" "Tw1" "Wu" "" "oS" "JW1" "N"]
39+
; ["q" "vv8" "" "3" "" "H" "" "" "" "ZfC8"]
40+
; ["7L" "42P34" "" "g7" "K22" "43" "c5WR"]
41+
; ["qYWis" "Pbk6" "02W" "0e2aTz" "L" "3" "130B"]
42+
; ["S2S" "GW3" "hFZ" "0G05wI" "a" "3HFyI"]
43+
; ["8O" "jr55e" "" "qp" "DGR" "Ma9" ""]
44+
; ["zsH" "N0" "MY49r3v" "i3F" "J0Ih6r1" "A0y" "PQlSm" "uv" "u3U" "1EVFN9u"])
45+
46+
47+
;; Recursive models
48+
(def hiccup-model
49+
(h/let ['hiccup (h/alt [:node (h/in-vector (h/cat mg/fn-keyword?
50+
(h/? (h/map))
51+
(h/* (h/not-inlined (h/ref 'hiccup)))))]
52+
[:primitive (h/alt mg/fn-nil?
53+
mg/fn-boolean?
54+
mg/fn-number?
55+
mg/fn-string?)])]
56+
(h/ref 'hiccup)))
57+
58+
;; 20 Samples using a generator with budget of 50.
59+
(-> (mg/gen hiccup-model 50)
60+
(tcg/sample 20))
61+
;([:! {} nil [:P]]
62+
; [:_D {} "" false]
63+
; [:U {} true -2.0 "" 2 0 true nil -3.0 true false]
64+
; false
65+
; [:!V {} "m" 4 "s" "V" "L" true [:Lh] "" false nil]
66+
; true
67+
; [:?- {} 1 "D" -3.5 true false "06tV" "tL" -2.0 true "r3qWdG" true]
68+
; nil
69+
; [:rqm {} [:K {}] [:I] [:*-+. {}] true]
70+
; [:W {} 8 3 "0F"]
71+
; false
72+
; [:-_3! {} [:TY8 {} false] [:-Op]]
73+
; [:a {} nil true 5 nil 4 nil]
74+
; [:EK_6 false -1.36328125 nil true "953r" -0.546875 nil "etj" false true true]
75+
; "fyaty"
76+
; 1.0
77+
; [:.?G!Y {} "5idQ" [:+-7.*w] 12 [:F] false nil [:ixj+63] nil 8]
78+
; 15
79+
; true
80+
; false)
81+
82+
83+
```

src/minimallist/generator.cljc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
(def fn-double? (-> (h/fn double?)
2828
(h/with-test-check-gen gen/double)))
2929

30+
(def fn-number? (-> (h/fn number?)
31+
(h/with-test-check-gen (gen/one-of [gen/nat gen/double]))))
32+
3033
(def fn-string? (-> (h/fn string?)
3134
(h/with-test-check-gen gen/string-alphanumeric)))
3235

0 commit comments

Comments
 (0)