nydp 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,28 @@
1
+ (chapter-start 'string-manipulation "utilities for manipulating strings")
2
+
3
+ (def string-strip (txt)
4
+ (string-replace "(^\\s+|\\s+$)" "" txt))
5
+
6
+ (def joinstr (txt . things)
7
+ ; flatten 'things into a single list (ie unnest lists)
8
+ ; convert each item to a string
9
+ ; return a single string which is the concatenation of each
10
+ ; stringified item, with given 'txt inserted in between
11
+ ; each item
12
+ (let joinables (flatten things)
13
+ (apply +
14
+ (to-string (car joinables))
15
+ (flatten (zip (map (fn (_) txt) (cdr joinables))
16
+ (map to-string (cdr joinables)))))))
17
+
18
+ (def j items
19
+ ; delegate to 'joinstr with an empty join string
20
+ ; shortcut for (joinstr "" items)
21
+ (joinstr "" items))
22
+
23
+ (def string-pieces pieces
24
+ ; string-interpolation syntax emits this form. Default implementation
25
+ ; is to delegate to 'j , but containing forms may use macros that
26
+ ; override this in order to provide specific interpolation behaviour
27
+ ; (for example, formatting numbers or stripping HTML tags)
28
+ (j pieces))
@@ -0,0 +1,5 @@
1
+ (chapter-start 'date-time "utilities for retrieving and manipulating dates and times")
2
+
3
+ (def today ()
4
+ ; return a date for the current day
5
+ (date))
@@ -0,0 +1,157 @@
1
+ (chapter-start 'list-manipulation)
2
+
3
+ (def list-length (things)
4
+ (if (no things) 0
5
+ (atom? things) 1
6
+ (+ 1 (list-length:cdr things))))
7
+
8
+ (def list-slices (things slice-size)
9
+ ; slice 'things into a list of lists each with maximum 'slice-size items
10
+ (chapter pagination)
11
+ (if (< (len things) slice-size)
12
+ (cons things nil)
13
+ (cons (firstn slice-size things)
14
+ (list-slices (nthcdr slice-size things)
15
+ slice-size))))
16
+
17
+ (def intersperse (inbetween things)
18
+ ; return a new list with 'inbetween in between every element of 'things
19
+ (if (and (pair? things) (cdr things))
20
+ (apply list (car things) inbetween
21
+ (intersperse inbetween (cdr things)))
22
+ things))
23
+
24
+ (def intersperse-splicing (inbetween things)
25
+ ; expects 'things a list of lists, joins the lists
26
+ ; placing 'inbetween in between each list.
27
+ ; For example (intersperse-splicing 'X '((a b) (c d) (e f)))
28
+ ; returns (a b X c d X e f)
29
+ (apply joinlists (intersperse (list inbetween) things)))
30
+
31
+ (def collect (f things)
32
+ ; if 'things is a list, return all the items in the list for which 'f returns non-nil
33
+ ; otherwise, return 'things if (f things) is non-nil
34
+ ; otherwise, nil
35
+ (rfnwith collector (items things)
36
+ (if (no items)
37
+ nil
38
+ (pair? items)
39
+ (if (f (car items))
40
+ (cons (car items)
41
+ (collector (cdr items)))
42
+ (collector (cdr items)))
43
+ (f items)
44
+ items)))
45
+
46
+ (def reject (f things)
47
+ ; return all the items in 'things for which 'f returns nil
48
+ (collect !f things))
49
+
50
+ (def nth (n things)
51
+ ; returns the n-th item in the list 'things
52
+ (if (eq? n 0)
53
+ (car things)
54
+ (nth (- n 1) (cdr things))))
55
+
56
+ (mac each (var things code)
57
+ ; repeatedly assigns an element of 'things to 'var,
58
+ ; and executes 'code each time
59
+ (w/uniq (xs c)
60
+ `((rfn ,c (,xs)
61
+ (if (pair? ,xs)
62
+ (do
63
+ (let ,var (car ,xs) ,code)
64
+ (,c (cdr ,xs)))))
65
+ ,things)))
66
+
67
+ (def reduce (f things)
68
+ ((rfn rd (acc list)
69
+ (if (pair? list)
70
+ (rd (f acc (car list))
71
+ (cdr list))
72
+ acc))
73
+ (car things) (cdr things)))
74
+
75
+ (def proper? (list)
76
+ ; t if this is a proper list (last cdr is nil)
77
+ ; nil otherwise (last cdr is neither cons nor nil)
78
+ (or (no list)
79
+ (and (pair? list)
80
+ (proper? (cdr list)))))
81
+
82
+ (def firstn (n things)
83
+ ; returns the first 'n items in the list 'things
84
+ (if (eq? n 0) nil
85
+ (cons (car things)
86
+ (firstn (- n 1)
87
+ (cdr things)))))
88
+
89
+ (def nthcdr (n things)
90
+ ; returns the nth cdr of the list 'things
91
+ (if (> n 0)
92
+ (nthcdr (- n 1) (cdr things))
93
+ things))
94
+
95
+ (def joinlists (things . more-thingses)
96
+ ; return a new list which is the concatenation of all the given lists
97
+ ; 'things is a list
98
+ ; 'more-thingses is a list of lists
99
+ ; call like this: (joinlists '(a b c) '(x y z) '(1 2 3))
100
+ (if things
101
+ (cons (car things)
102
+ (apply joinlists
103
+ (cdr things)
104
+ more-thingses))
105
+ more-thingses
106
+ (apply joinlists more-thingses)))
107
+
108
+ (def detect (f things)
109
+ ; if 'f is a function,
110
+ ; if 'things is a list, return the first item in the list for which 'f returns non-nil
111
+ ; otherwise, return 'things if (f things) is non-nil
112
+ ; otherwise, nil
113
+ ; if 'f is not a function, self-invoke with a function checking for equality with f
114
+ ;
115
+ ; WARNING: if the detected thing is nil, returns t instead. A return value of nil
116
+ ; means the thing was not found ; non-nil means the thing was found, including when
117
+ ; the found thing is itself nil.
118
+ (if (isa 'fn f)
119
+ (rfnwith d (items things)
120
+ (if (pair? items)
121
+ (let it (car items)
122
+ (or
123
+ (and (f it)
124
+ (or it t))
125
+ (d:cdr items)))
126
+ (f items)
127
+ items))
128
+ (detect (curry eq? f)
129
+ things)))
130
+
131
+ (def tuples (n things)
132
+ ;; split things into a list of lists each n long
133
+ (rfnwith _ (list things)
134
+ (if (no list)
135
+ nil
136
+ (cons (firstn n list) (_ (nthcdr n list))))))
137
+
138
+ (def range (start stop)
139
+ ; return a list containing the range
140
+ ; of elements starting with 'start, up
141
+ ; to but not including 'stop
142
+ (if (< start stop)
143
+ (cons start
144
+ (range (+ start 1)
145
+ stop))))
146
+
147
+ (def best (f things)
148
+ (if (no things)
149
+ nil
150
+ (let winner (car things)
151
+ (each thing (cdr things)
152
+ (if (f thing winner)
153
+ (= winner thing)))
154
+ winner)))
155
+
156
+ (def min things (best < things))
157
+ (def max things (best > things))
@@ -21,103 +21,112 @@
21
21
  (p "================================================\n")
22
22
  "~desc : total ~(just times), average ~(/ times repeats) per run"))
23
23
 
24
- (assign a 1)
25
- (assign b 1)
26
-
27
- (def bm-add-globals () (+ a b))
28
-
29
- (def bm-lc-0 () 0)
30
- (def bm-lc-1 (a) a)
31
- (def bm-lc-2 (a b) (a b))
32
- (def bm-lc-3 (a b c) (a b c))
33
- (def bm-lc-4 (a b c d) (a b c d))
34
-
35
- (def bm-lc-0R a a)
36
- (def bm-lc-1R (a . b) (apply a b))
37
- (def bm-lc-2R (a b . c) (apply a b c))
38
- (def bm-lc-3R (a b c . d) (apply a b c d))
39
- (def bm-lc-4R (a b c d . e) (apply a b c d e))
40
-
41
- (def bm-plus-0 () (+))
42
- (def bm-plus-1 (a) (+ a))
43
- (def bm-plus-2 (a b) (+ a b))
44
- (def bm-plus-3 (a b c) (+ a b c))
45
-
46
- (def bm-0-arg-times-call () (*))
47
- (def bm-1-arg-times-call () (* 23))
48
- (def bm-2-arg-times-call () (* 23 24))
49
- (def bm-3-arg-times-call () (* 23 24 25))
50
- (def bm-4-arg-times-call () (* 23 24 25 26))
51
-
52
- (def bm-plus-0-call () (bm-plus-0))
53
- (def bm-plus-1-call () (bm-plus-1 1))
54
- (def bm-plus-2-call () (bm-plus-2 1 2))
55
- (def bm-plus-3-call () (bm-plus-3 1 2 3))
56
-
57
- (def bm-0-lc-call () (bm-lc-0))
58
- (def bm-1-lc-call () (bm-lc-1 +))
59
- (def bm-2-lc-call () (bm-lc-2 + 1))
60
- (def bm-3-lc-call () (bm-lc-3 + 1 2))
61
- (def bm-4-lc-call () (bm-lc-4 + 1 2 3))
62
-
63
- (def bm-0R-lc-call () (bm-lc-0R +))
64
- (def bm-1R-lc-call () (bm-lc-1R + 1))
65
- (def bm-2R-lc-call () (bm-lc-2R + 1 2))
66
- (def bm-3R-lc-call () (bm-lc-3R + 1 2 3))
67
- (def bm-4R-lc-call () (bm-lc-4R + 1 2 3 4))
68
-
69
- (def bm-complicated-0 (a b c) (a (+ 1 b) (+ 1 c)))
70
-
71
- (def bm-complicated ()
72
- (bm-complicated-0 +
73
- (bm-complicated-0 * 3 (bm-complicated-0 + 3 6))
74
- (bm-complicated-0 - 10 (bm-complicated-0 - 13 8))))
75
-
76
- (def bm-pre-compile-test ()
77
- (for i 1 10
78
- (pre-compile (dox-src rbs))))
79
-
80
- (def bm-hash-fill
81
- (let h {}
82
- (=h.aa 1) (=h.ab 2) (=h.ac 3) (=h.ba 4) (=h.bb 5) (=h.bc 6)
83
- (=h.ca 1) (=h.cb 2) (=h.cc 3) (=h.ca 4) (=h.cb 5) (=h.cc 6)
84
- (=h.da 1) (=h.db 2) (=h.dc 3) (=h.da 4) (=h.db 5) (=h.dc 6)
85
- (=h.aa 1) (=h.ab 2) (=h.ac 3) (=h.ba 4) (=h.bb 5) (=h.bc 6)
86
- (=h.ca 1) (=h.cb 2) (=h.cc 3) (=h.ca 4) (=h.cb 5) (=h.cc 6)
87
- (=h.da 1) (=h.db 2) (=h.dc 3) (=h.da 4) (=h.db 5) (=h.dc 6)
88
- (list (list h.aa h.ab h.ac h.ba h.bb h.bc)
89
- (list h.aa h.ab h.ac h.ba h.bb h.bc)
90
- (list h.ca h.cb h.cc h.ca h.cb h.cc)
91
- (list h.da h.db h.dc h.da h.db h.dc)
92
- (list h.aa h.ab h.ac h.ba h.bb h.bc)
93
- (list h.ca h.cb h.cc h.ca h.cb h.cc)
94
- (list h.da h.db h.dc h.da h.db h.dc)
95
- (list h.da h.ab h.ac h.ba h.bb h.bc))))
24
+ ;; (assign a 1)
25
+ ;; (assign b 1)
26
+
27
+ ;; (def bm-add-globals () (+ a b))
28
+
29
+ ;; (def bm-lc-0 () 0)
30
+ ;; (def bm-lc-1 (a) a)
31
+ ;; (def bm-lc-2 (a b) (a b))
32
+ ;; (def bm-lc-3 (a b c) (a b c))
33
+ ;; (def bm-lc-4 (a b c d) (a b c d))
34
+
35
+ ;; (def bm-lc-0R a a)
36
+ ;; (def bm-lc-1R (a . b) (apply a b))
37
+ ;; (def bm-lc-2R (a b . c) (apply a b c))
38
+ ;; (def bm-lc-3R (a b c . d) (apply a b c d))
39
+ ;; (def bm-lc-4R (a b c d . e) (apply a b c d e))
40
+
41
+ ;; (def bm-plus-0 () (+))
42
+ ;; (def bm-plus-1 (a) (+ a))
43
+ ;; (def bm-plus-2 (a b) (+ a b))
44
+ ;; (def bm-plus-3 (a b c) (+ a b c))
45
+ ;; (def bm-f-1 (a) (a))
46
+ ;; (def bm-f-2 (a b) (a b))
47
+ ;; (def bm-f-3 (a b c) (a b c))
48
+
49
+ ;; (def bm-0-arg-times-call () (*))
50
+ ;; (def bm-1-arg-times-call () (* 23))
51
+ ;; (def bm-2-arg-times-call () (* 23 24))
52
+ ;; (def bm-3-arg-times-call () (* 23 24 25))
53
+ ;; (def bm-4-arg-times-call () (* 23 24 25 26))
54
+
55
+ ;; (def bm-plus-0-call () (bm-plus-0))
56
+ ;; (def bm-plus-1-call () (bm-plus-1 1))
57
+ ;; (def bm-plus-2-call () (bm-plus-2 1 2))
58
+ ;; (def bm-plus-3-call () (bm-plus-3 1 2 3))
59
+ ;; (def bm-f-1-call () (for i 0 10 (bm-f-1 +)))
60
+ ;; (def bm-f-2-call () (for i 0 10 (bm-f-2 + 2)))
61
+ ;; (def bm-f-3-call () (for i 0 10 (bm-f-3 + 2 3)))
62
+
63
+ ;; (def bm-0-lc-call () (bm-lc-0))
64
+ ;; (def bm-1-lc-call () (bm-lc-1 +))
65
+ ;; (def bm-2-lc-call () (bm-lc-2 + 1))
66
+ ;; (def bm-3-lc-call () (bm-lc-3 + 1 2))
67
+ ;; (def bm-4-lc-call () (bm-lc-4 + 1 2 3))
68
+
69
+ ;; (def bm-0R-lc-call () (bm-lc-0R +))
70
+ ;; (def bm-1R-lc-call () (bm-lc-1R + 1))
71
+ ;; (def bm-2R-lc-call () (bm-lc-2R + 1 2))
72
+ ;; (def bm-3R-lc-call () (bm-lc-3R + 1 2 3))
73
+ ;; (def bm-4R-lc-call () (bm-lc-4R + 1 2 3 4))
74
+
75
+ ;; (def bm-complicated-0 (a b c) (a (+ 1 b) (+ 1 c)))
76
+
77
+ ;; (def bm-complicated ()
78
+ ;; (bm-complicated-0 +
79
+ ;; (bm-complicated-0 * 3 (bm-complicated-0 + 3 6))
80
+ ;; (bm-complicated-0 - 10 (bm-complicated-0 - 13 8))))
81
+
82
+ ;; (def bm-pre-compile-test ()
83
+ ;; (for i 1 10
84
+ ;; (pre-compile (dox-src rbs))))
85
+
86
+ ;; (def bm-hash-fill
87
+ ;; (let h {}
88
+ ;; (=h.aa 1) (=h.ab 2) (=h.ac 3) (=h.ba 4) (=h.bb 5) (=h.bc 6)
89
+ ;; (=h.ca 1) (=h.cb 2) (=h.cc 3) (=h.ca 4) (=h.cb 5) (=h.cc 6)
90
+ ;; (=h.da 1) (=h.db 2) (=h.dc 3) (=h.da 4) (=h.db 5) (=h.dc 6)
91
+ ;; (=h.aa 1) (=h.ab 2) (=h.ac 3) (=h.ba 4) (=h.bb 5) (=h.bc 6)
92
+ ;; (=h.ca 1) (=h.cb 2) (=h.cc 3) (=h.ca 4) (=h.cb 5) (=h.cc 6)
93
+ ;; (=h.da 1) (=h.db 2) (=h.dc 3) (=h.da 4) (=h.db 5) (=h.dc 6)
94
+ ;; (list (list h.aa h.ab h.ac h.ba h.bb h.bc)
95
+ ;; (list h.aa h.ab h.ac h.ba h.bb h.bc)
96
+ ;; (list h.ca h.cb h.cc h.ca h.cb h.cc)
97
+ ;; (list h.da h.db h.dc h.da h.db h.dc)
98
+ ;; (list h.aa h.ab h.ac h.ba h.bb h.bc)
99
+ ;; (list h.ca h.cb h.cc h.ca h.cb h.cc)
100
+ ;; (list h.da h.db h.dc h.da h.db h.dc)
101
+ ;; (list h.da h.ab h.ac h.ba h.bb h.bc))))
96
102
 
97
103
  (def rbs (name)
98
104
  (let summary nil
99
- ;; (push (bm "pythag " bm-pythag 5 10) summary)
100
- ;; (push (bm "recursive " bm-complicated 5 1000) summary)
101
- (push (bm "global vars " bm-add-globals 10 50000) summary)
102
- (push (bm "0 arg times " bm-0-arg-times-call 10 50000) summary)
103
- (push (bm "1 arg times " bm-1-arg-times-call 10 50000) summary)
104
- (push (bm "2 arg times " bm-2-arg-times-call 10 50000) summary)
105
- (push (bm "SYM " bm-plus-0-call 10 50000) summary)
106
- (push (bm "SYM LEX " bm-plus-1-call 10 50000) summary)
107
- (push (bm "SYM LEX LEX " bm-plus-2-call 10 50000) summary)
108
- (push (bm "SYM LEX LEX LEX " bm-plus-3-call 10 50000) summary)
109
- ;; (push (bm "3 arg times" bm-3-arg-times-call 10 100000) summary)
110
- ;; (push (bm "4 arg times" bm-4-arg-times-call 10 100000) summary)
111
- ;; (push (bm "0R arg lexical-vars" bm-0R-lc-call 10 50000) summary)
112
- ;; (push (bm "1R arg lexical-vars" bm-1R-lc-call 10 50000) summary)
113
- ;; (push (bm "2R arg lexical-vars" bm-2R-lc-call 10 50000) summary)
114
- ;; (push (bm "3R arg lexical-vars" bm-3R-lc-call 10 50000) summary)
115
- ;; (push (bm "4R arg lexical-vars" bm-4R-lc-call 10 50000) summary)
116
- ;; (push (bm "0 arg lexical-vars " bm-0-lc-call 10 50000) summary)
117
- ;; (push (bm "1 arg lexical-vars " bm-1-lc-call 10 50000) summary)
118
- ;; (push (bm "2 arg lexical-vars " bm-2-lc-call 10 50000) summary)
119
- ;; (push (bm "3 arg lexical-vars " bm-3-lc-call 10 50000) summary)
120
- ;; (push (bm "4 arg lexical-vars " bm-4-lc-call 10 50000) summary)
121
- ;; (push (bm "hashing" bm-hash-fill 10 200000) summary)
122
- ;; (push (bm "pre-compile" bm-pre-compile-test 10 10000) summary)
105
+ ;; (push (bm "pythag " bm-pythag 5 10) summary)
106
+ ;; (push (bm "recursive " bm-complicated 5 1000) summary)
107
+ ;; (push (bm "global vars " bm-add-globals 10 50000) summary)
108
+ ;; (push (bm "0 arg times " bm-0-arg-times-call 10 50000) summary)
109
+ ;; (push (bm "1 arg times " bm-1-arg-times-call 10 50000) summary)
110
+ ;; (push (bm "2 arg times " bm-2-arg-times-call 10 50000) summary)
111
+ ;; (push (bm "SYM " bm-plus-0-call 10 50000) summary)
112
+ ;; (push (bm "SYM LEX " bm-plus-1-call 10 50000) summary)
113
+ ;; (push (bm "SYM LEX LEX " bm-plus-2-call 10 50000) summary)
114
+ ;; (push (bm "SYM LEX LEX LEX " bm-plus-3-call 10 50000) summary)
115
+ ;; (push (bm "3 arg times " bm-3-arg-times-call 10 100000) summary)
116
+ ;; (push (bm "4 arg times " bm-4-arg-times-call 10 100000) summary)
117
+ ;; (push (bm "0R arg lexical-vars " bm-0R-lc-call 10 50000) summary)
118
+ ;; (push (bm "1R arg lexical-vars " bm-1R-lc-call 10 50000) summary)
119
+ ;; (push (bm "2R arg lexical-vars " bm-2R-lc-call 10 50000) summary)
120
+ ;; (push (bm "3R arg lexical-vars " bm-3R-lc-call 10 50000) summary)
121
+ ;; (push (bm "4R arg lexical-vars " bm-4R-lc-call 10 50000) summary)
122
+ ;; (push (bm "0 arg lexical-vars " bm-0-lc-call 10 50000) summary)
123
+ ;; (push (bm "1 arg lexical-vars " bm-1-lc-call 10 50000) summary)
124
+ ;; (push (bm "2 arg lexical-vars " bm-2-lc-call 10 50000) summary)
125
+ ;; (push (bm "3 arg lexical-vars " bm-3-lc-call 10 50000) summary)
126
+ ;; (push (bm "4 arg lexical-vars " bm-4-lc-call 10 50000) summary)
127
+ ;; (push (bm "hashing " bm-hash-fill 10 200000) summary)
128
+ ;; (push (bm "pre-compile " bm-pre-compile-test 10 10000) summary)
129
+ ;; (push (bm "LEX " bm-f-1-call 10 10000) summary)
130
+ ;; (push (bm "LEX LEX " bm-f-2-call 10 10000) summary)
131
+ ;; (push (bm "LEX LEX LEX " bm-f-3-call 10 10000) summary)
123
132
  (each s summary (p name " " s))))
@@ -8,9 +8,12 @@
8
8
  (def add-hook (hook-name f)
9
9
  (hash-cons hooks hook-name f))
10
10
 
11
+ (def clear-hooks (hook-name)
12
+ (= hooks.,hook-name nil))
13
+
11
14
  (def remove-hook (hook-name f)
12
15
  (= hooks.,hook-name
13
- (select (curry !eq? f)
16
+ (collect (curry !eq? f)
14
17
  hooks.,hook-name)))
15
18
 
16
19
  (def run-hooks (hook-name . args)
@@ -37,6 +37,10 @@
37
37
  (joinlists '(a b c) '(x y z) '(1 2 3))
38
38
  (a b c x y z 1 2 3))
39
39
 
40
+ ("nil disappears"
41
+ (apply joinlists '( (a b c) (d e f) nil (j k l) ) )
42
+ (a b c d e f j k l))
43
+
40
44
  ("joins three lists without recursing"
41
45
  (joinlists '(a b c) '(x (y1 y2 y3) z) '(1 2 (3 3 3)))
42
46
  (a b c x (y1 y2 y3) z 1 2 (3 3 3))))
@@ -132,3 +136,33 @@
132
136
  ("three lists and an extra big"
133
137
  (list-slices '(a b c d e f g h i j k) 3)
134
138
  ((a b c) (d e f) (g h i) (j k))))
139
+
140
+ (examples-for assoc
141
+ ("finds nothing in an empty list"
142
+ (assoc 'foo nil)
143
+ nil)
144
+
145
+ ("finds nothing in a non-list"
146
+ (assoc 'foo 'bar)
147
+ nil)
148
+
149
+ ("finds nothing for nonexistent key"
150
+ (assoc 'z '((a b) (c d)))
151
+ nil)
152
+
153
+ ("finds value corresponding to given key"
154
+ (assoc 'c '((a b) (c d)))
155
+ (c d)))
156
+
157
+ (examples-for alref
158
+ ("finds nothing in an empty list"
159
+ (alref 'foo nil)
160
+ nil)
161
+
162
+ ("finds nothing for nonexistent key"
163
+ (alref 'z '((a b) (c d)))
164
+ nil)
165
+
166
+ ("finds value corresponding to given key"
167
+ (alref 'c '((a b) (c d)))
168
+ d))