aboutsummaryrefslogtreecommitdiff
path: root/README.org
blob: cb6f2a80a328d6ae327652c3760b3210dc3182b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
* ~petty-types~

A tiny library that provides

+ ~type-specifier-p~ a predicate that checks whether its argument is a
  valid type specifier
+ ~(list-of type &optional len)~ DEFTYPE for a LIST of elements with TYPE
+ ~(vector-of type &optional len)~ DEFTYPE for a VECTOR of elements with TYPE
+ ~(tuple &rest types)~ DEFTYPE for a list of exactly TYPES
+ ~(table from to)~ DEFTYPE for a hashtable 

  Here are some examples:

#+begin_src lisp

> (typep (list 1 2 3 4) '(list-of real))
T
> (typep (list 1 2 3 4 3 2 1) '(list-of real))
T
> (typep (list 1 2 3 4) '(list-of real 4))
T
> (typep (list 1 2 #C(0 1)) '(list-of real))
NIL
> (typep (list 1 2 3 4) '(list-of real 5))
NIL
> (typep "aaabbb" '(vector-of (member #\a #\b)))
T
> (typep "aacabbb" '(vector-of (member #\a #\b)))
NIL
> (typep "aaabbb" '(vector-of (member #\a #\b) 6))
T
> (typep "aaaabbb" '(vector-of (member #\a #\b) 6))
NIL
> (typep "4aabbb" '(vector-of (member #\a #\b) 6))
NIL
> (typep "aabbba" '(vector-of (member #\a #\b) 6))
T

;; THIS IS ESPECIALLY ANNOYING:
> (type-of "abab")
(SIMPLE-ARRAY CHARACTER (4))
> (typep "abab" '(simple-array (member #\a #\b) (4)))
NIL

;; but as above, VECTOR-OF works.
> (typep "abab" '(vector-of (member #\a #\b) 4))
T

;; here is an example of TUPLE

> (typep '(1 #\x nil :foo) '(tuple fixnum character null keyword))
T

> (typep '(1 #\x nil xxx) '(tuple fixnum character null keyword))
NIL   ; Because XXX is not a KEYWORD


#+end_src