nydp 0.4.1 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (118) hide show
  1. checksums.yaml +5 -5
  2. data/README.md +44 -0
  3. data/lib/lisp/core-010-precompile.nydp +13 -16
  4. data/lib/lisp/core-012-utils.nydp +21 -6
  5. data/lib/lisp/core-015-documentation.nydp +58 -24
  6. data/lib/lisp/core-017-builtin-dox.nydp +49 -42
  7. data/lib/lisp/core-020-utils.nydp +5 -5
  8. data/lib/lisp/core-030-syntax.nydp +191 -96
  9. data/lib/lisp/core-035-flow-control.nydp +41 -14
  10. data/lib/lisp/core-037-list-utils.nydp +36 -14
  11. data/lib/lisp/core-039-module.nydp +24 -0
  12. data/lib/lisp/core-040-utils.nydp +51 -23
  13. data/lib/lisp/core-041-string-utils.nydp +37 -9
  14. data/lib/lisp/core-042-date-utils.nydp +21 -1
  15. data/lib/lisp/core-043-list-utils.nydp +99 -73
  16. data/lib/lisp/core-045-dox-utils.nydp +5 -0
  17. data/lib/lisp/core-070-prefix-list.nydp +1 -1
  18. data/lib/lisp/core-080-pretty-print.nydp +57 -17
  19. data/lib/lisp/core-090-hook.nydp +35 -1
  20. data/lib/lisp/core-100-utils.nydp +110 -15
  21. data/lib/lisp/core-110-hash-utils.nydp +61 -0
  22. data/lib/lisp/core-120-settings.nydp +46 -0
  23. data/lib/lisp/core-130-validations.nydp +51 -0
  24. data/lib/lisp/{core-060-benchmarking.nydp → core-900-benchmarking.nydp} +107 -19
  25. data/lib/lisp/tests/accum-examples.nydp +28 -1
  26. data/lib/lisp/tests/aif-examples.nydp +8 -3
  27. data/lib/lisp/tests/andify-examples.nydp +7 -0
  28. data/lib/lisp/tests/at-syntax-examples.nydp +17 -0
  29. data/lib/lisp/tests/best-examples.nydp +9 -0
  30. data/lib/lisp/tests/builtin-tests.nydp +19 -0
  31. data/lib/lisp/tests/case-examples.nydp +14 -0
  32. data/lib/lisp/tests/cdr-set-examples.nydp +6 -0
  33. data/lib/lisp/tests/date-examples.nydp +56 -1
  34. data/lib/lisp/tests/destructuring-examples.nydp +46 -14
  35. data/lib/lisp/tests/detect-examples.nydp +12 -0
  36. data/lib/lisp/tests/dp-examples.nydp +24 -0
  37. data/lib/lisp/tests/each-tests.nydp +5 -0
  38. data/lib/lisp/tests/empty-examples.nydp +1 -1
  39. data/lib/lisp/tests/error-tests.nydp +4 -4
  40. data/lib/lisp/tests/explain-mac-examples.nydp +1 -1
  41. data/lib/lisp/tests/filter-forms-examples.nydp +15 -0
  42. data/lib/lisp/tests/foundation-test.nydp +12 -0
  43. data/lib/lisp/tests/hash-examples.nydp +26 -2
  44. data/lib/lisp/tests/list-grep-examples.nydp +40 -0
  45. data/lib/lisp/tests/list-tests.nydp +58 -1
  46. data/lib/lisp/tests/map-hash-examples.nydp +11 -0
  47. data/lib/lisp/tests/module-examples.nydp +10 -0
  48. data/lib/lisp/tests/multi-assign-examples.nydp +6 -0
  49. data/lib/lisp/tests/parser-tests.nydp +25 -0
  50. data/lib/lisp/tests/pretty-print-tests.nydp +17 -14
  51. data/lib/lisp/tests/set-difference-examples.nydp +8 -0
  52. data/lib/lisp/tests/set-intersection-examples.nydp +16 -0
  53. data/lib/lisp/tests/set-union-examples.nydp +8 -0
  54. data/lib/lisp/tests/settings-examples.nydp +40 -0
  55. data/lib/lisp/tests/sort-examples.nydp +8 -0
  56. data/lib/lisp/tests/string-tests.nydp +65 -1
  57. data/lib/lisp/tests/syntax-tests.nydp +5 -1
  58. data/lib/lisp/tests/to-integer-examples.nydp +16 -0
  59. data/lib/lisp/tests/validation-examples.nydp +15 -0
  60. data/lib/lisp/tests/zap-examples.nydp +12 -0
  61. data/lib/nydp.rb +13 -7
  62. data/lib/nydp/assignment.rb +10 -3
  63. data/lib/nydp/builtin.rb +1 -1
  64. data/lib/nydp/builtin/abs.rb +8 -0
  65. data/lib/nydp/builtin/cdr_set.rb +1 -6
  66. data/lib/nydp/builtin/date.rb +15 -1
  67. data/lib/nydp/builtin/error.rb +1 -1
  68. data/lib/nydp/builtin/handle_error.rb +1 -1
  69. data/lib/nydp/builtin/hash.rb +27 -45
  70. data/lib/nydp/builtin/inspect.rb +1 -1
  71. data/lib/nydp/builtin/plus.rb +10 -2
  72. data/lib/nydp/builtin/random_string.rb +2 -2
  73. data/lib/nydp/builtin/{car.rb → regexp.rb} +2 -2
  74. data/lib/nydp/builtin/ruby_wrap.rb +72 -0
  75. data/lib/nydp/builtin/string_match.rb +2 -2
  76. data/lib/nydp/builtin/string_pad_left.rb +7 -0
  77. data/lib/nydp/builtin/string_pad_right.rb +7 -0
  78. data/lib/nydp/builtin/string_replace.rb +1 -1
  79. data/lib/nydp/builtin/string_split.rb +4 -3
  80. data/lib/nydp/builtin/to_integer.rb +23 -0
  81. data/lib/nydp/builtin/to_string.rb +2 -9
  82. data/lib/nydp/builtin/type_of.rb +9 -6
  83. data/lib/nydp/closure.rb +0 -3
  84. data/lib/nydp/cond.rb +23 -1
  85. data/lib/nydp/context_symbol.rb +14 -6
  86. data/lib/nydp/core.rb +36 -28
  87. data/lib/nydp/core_ext.rb +54 -0
  88. data/lib/nydp/date.rb +37 -31
  89. data/lib/nydp/function_invocation.rb +34 -26
  90. data/lib/nydp/hash.rb +5 -6
  91. data/lib/nydp/helper.rb +41 -25
  92. data/lib/nydp/interpreted_function.rb +68 -40
  93. data/lib/nydp/literal.rb +1 -1
  94. data/lib/nydp/pair.rb +22 -5
  95. data/lib/nydp/parser.rb +11 -7
  96. data/lib/nydp/string_atom.rb +16 -22
  97. data/lib/nydp/symbol.rb +40 -27
  98. data/lib/nydp/symbol_lookup.rb +7 -7
  99. data/lib/nydp/tokeniser.rb +2 -2
  100. data/lib/nydp/truth.rb +17 -10
  101. data/lib/nydp/version.rb +1 -1
  102. data/lib/nydp/vm.rb +7 -2
  103. data/nydp.gemspec +2 -4
  104. data/spec/date_spec.rb +115 -22
  105. data/spec/embedded_spec.rb +12 -12
  106. data/spec/foreign_hash_spec.rb +14 -2
  107. data/spec/hash_non_hash_behaviour_spec.rb +7 -7
  108. data/spec/hash_spec.rb +24 -2
  109. data/spec/nydp_spec.rb +14 -2
  110. data/spec/pair_spec.rb +3 -1
  111. data/spec/parser_spec.rb +31 -20
  112. data/spec/rand_spec.rb +3 -3
  113. data/spec/spec_helper.rb +13 -1
  114. data/spec/symbol_spec.rb +31 -0
  115. data/spec/time_spec.rb +1 -1
  116. metadata +31 -38
  117. data/lib/nydp/builtin/cdr.rb +0 -7
  118. data/lib/nydp/builtin/cons.rb +0 -9
@@ -0,0 +1,51 @@
1
+ (chapter-start 'validations "utilities to record and run validation routines")
2
+
3
+ (let validations {}
4
+ (def validate/reset ()
5
+ (= validations {}))
6
+ (def validate/fns (thing context)
7
+ (hash-get validations (list (type-of thing) context)))
8
+ (def validate/fn+ (type context f)
9
+ (hash-cons validations (list type context) f)))
10
+
11
+ ;;
12
+ ;; returns a hash of error-name to list of error messages
13
+ ;;
14
+ ;; An empty return value signifies an error-free 'thing
15
+ ;;
16
+ (def validate (thing context)
17
+ (returnlet msgs {}
18
+ (let msgf λem(hash-cons msgs e m)
19
+ (eachl λv(v thing context msgf)
20
+ (validate/fns thing context)))))
21
+
22
+ ;; declare a validation routine for type 'type in context 'context
23
+ ;;
24
+ ;; 'type must be a symbol
25
+ ;; 'context must be a symbol
26
+ ;; 'body is one or more nydp expressions.
27
+ ;;
28
+ ;; 'body will be embedded in a function with access to the following variables :
29
+ ;;
30
+ ;; * the value of the 'type argument
31
+ ;; * ctx
32
+ ;; * mf
33
+ ;;
34
+ ;; 'mf ("message function") is a function that takes two arguments and is used to store
35
+ ;; the validation error message
36
+ ;; example: (mf "Last name" "Last name must not be empty")
37
+ ;;
38
+ ;; example usage:
39
+ ;;
40
+ ;; (validate/def invoice issue
41
+ ;; (if (no invoice.account)
42
+ ;; (mf "Account" "Account must be a client account"))
43
+ ;; (if (!> invoice.total 0)
44
+ ;; (mf "Amount" "Amount must be greater than zero"))
45
+ ;; (if (any? !&group invoice.invoice-items)
46
+ ;; (mf "Group" "Each line must be assigned to a group")))
47
+ ;;
48
+ ;; if your routine makes no call to 'mf then 'validate will return an empty hash, which should be
49
+ ;; interpreted as signifying that the object in question is error free in the given context.
50
+ (mac validate/def (type context . body)
51
+ `(validate/fn+ ',type ',context (fn (,type ctx mf) ,@body)))
@@ -159,23 +159,104 @@
159
159
  ;; (= h.ca v1) (= h.cb v2) (= h.cc v3) (= h.ca v4) (= h.cb v5) (= h.cc v6)
160
160
  ;; (= h.da v1) (= h.db v2) (= h.dc v3) (= h.da v4) (= h.db v5) (= h.dc v6)))
161
161
 
162
- (def bm-or-lex-lex-lex (a b c) (or a b c))
163
- (def bm-faster-or ()
164
- (bm-or-lex-lex-lex 1 2 3)
165
- (bm-or-lex-lex-lex nil 2 3))
166
-
167
- (def build-mapsum-data (count data)
168
- (if (> count 0)
169
- (build-mapsum-data (- count 1)
170
- (cons { mappit (rand 10) }
171
- data))
172
- data))
173
-
174
- (let mapsum-data (build-mapsum-data 1000)
175
- (def bm-mapsum () (mapsum &mappit mapsum-data))
176
- (def bm-mapreduce () (mapreduce &mappit + mapsum-data 0)))
177
-
178
-
162
+ ;; (def bm-acc ()
163
+ ;; (accum z
164
+ ;; (z 0) (z 1) (z 2) (z 3) (z 4) (z 5) (z 6) (z 7) (z 8) (z 9)
165
+ ;; (z 0) (z 1) (z 2) (z 3) (z 4) (z 5) (z 6) (z 7) (z 8) (z 9)
166
+ ;; (z 0) (z 1) (z 2) (z 3) (z 4) (z 5) (z 6) (z 7) (z 8) (z 9)
167
+ ;; (z 0) (z 1) (z 2) (z 3) (z 4) (z 5) (z 6) (z 7) (z 8) (z 9)
168
+ ;; (z 0) (z 1) (z 2) (z 3) (z 4) (z 5) (z 6) (z 7) (z 8) (z 9)
169
+ ;; (z 0) (z 1) (z 2) (z 3) (z 4) (z 5) (z 6) (z 7) (z 8) (z 9)
170
+ ;; (z 0) (z 1) (z 2) (z 3) (z 4) (z 5) (z 6) (z 7) (z 8) (z 9)
171
+ ;; (z 0) (z 1) (z 2) (z 3) (z 4) (z 5) (z 6) (z 7) (z 8) (z 9)
172
+ ;; (z 0) (z 1) (z 2) (z 3) (z 4) (z 5) (z 6) (z 7) (z 8) (z 9)))
173
+
174
+ ;; (def bm-facc ()
175
+ ;; (faccum z
176
+ ;; (z 0) (z 1) (z 2) (z 3) (z 4) (z 5) (z 6) (z 7) (z 8) (z 9)
177
+ ;; (z 0) (z 1) (z 2) (z 3) (z 4) (z 5) (z 6) (z 7) (z 8) (z 9)
178
+ ;; (z 0) (z 1) (z 2) (z 3) (z 4) (z 5) (z 6) (z 7) (z 8) (z 9)
179
+ ;; (z 0) (z 1) (z 2) (z 3) (z 4) (z 5) (z 6) (z 7) (z 8) (z 9)
180
+ ;; (z 0) (z 1) (z 2) (z 3) (z 4) (z 5) (z 6) (z 7) (z 8) (z 9)
181
+ ;; (z 0) (z 1) (z 2) (z 3) (z 4) (z 5) (z 6) (z 7) (z 8) (z 9)
182
+ ;; (z 0) (z 1) (z 2) (z 3) (z 4) (z 5) (z 6) (z 7) (z 8) (z 9)
183
+ ;; (z 0) (z 1) (z 2) (z 3) (z 4) (z 5) (z 6) (z 7) (z 8) (z 9)
184
+ ;; (z 0) (z 1) (z 2) (z 3) (z 4) (z 5) (z 6) (z 7) (z 8) (z 9)))
185
+
186
+ (in-private
187
+ (def bm-no-closures ()
188
+ (list λ(+ 1 2)
189
+ λ(+ 1 2)
190
+ λ(+ 1 2)
191
+ λ(+ 1 2)
192
+ λ(+ 1 2)
193
+ λ(+ 1 2)
194
+ λ(+ 1 2)
195
+ λ(+ 1 2)
196
+ λ(+ 1 2)
197
+ λ(+ 1 2)))
198
+
199
+ (def bm-cons () (cons 'a 'b))
200
+ (def bm-type-of () (type-of 'a)))
201
+
202
+
203
+
204
+ ;; ================================================
205
+ ;; Benchmark: string concatenation - 10 runs of 20000 iterations each
206
+ ;; took: 0.702480166 ms, 3.51240083e-05 ms per iteration
207
+ ;; took: 0.71368384 ms, 3.5684192e-05 ms per iteration
208
+ ;; took: 0.722608668 ms, 3.61304334e-05 ms per iteration
209
+ ;; took: 0.716350427 ms, 3.5817521349999995e-05 ms per iteration
210
+ ;; took: 0.72145049 ms, 3.60725245e-05 ms per iteration
211
+ ;; took: 0.745082221 ms, 3.725411105e-05 ms per iteration
212
+ ;; took: 0.722694129 ms, 3.613470645e-05 ms per iteration
213
+ ;; took: 0.71999777 ms, 3.59998885e-05 ms per iteration
214
+ ;; took: 0.727236822 ms, 3.63618411e-05 ms per iteration
215
+ ;; took: 0.73657519 ms, 3.68287595e-05 ms per iteration
216
+ ;; total 7.228159722999999, average 0.7228159722999999 per run
217
+ ;; ================================================
218
+ ;; string concatenation : total 7.228159722999999, average 0.7228159722999999 per run
219
+ ;; (def bm-string-concat ()
220
+ ;; (+
221
+ ;; (+ "this" "that" "another")
222
+ ;; (+ "this" "that" "another")
223
+ ;; (+ "this" "that" "another")
224
+ ;; (+ "this" "that" "another")
225
+ ;; (+ "this" "that" "another")
226
+ ;; (+ "this" "that" "another")
227
+ ;; (+ "this" "that" "another")
228
+ ;; (+ "this" "that" "another")
229
+ ;; (+ "this" "that" "another")
230
+ ;; (+ "this" "that" "another")))
231
+
232
+
233
+ ;; ================================================
234
+ ;; Benchmark: random string - 10 runs of 20000 iterations each
235
+ ;; took: 0.693267608 ms, 3.4663380399999996e-05 ms per iteration
236
+ ;; took: 0.763436112 ms, 3.8171805599999996e-05 ms per iteration
237
+ ;; took: 0.682000681 ms, 3.410003405e-05 ms per iteration
238
+ ;; took: 0.687733846 ms, 3.43866923e-05 ms per iteration
239
+ ;; took: 0.686838878 ms, 3.43419439e-05 ms per iteration
240
+ ;; took: 0.681588034 ms, 3.40794017e-05 ms per iteration
241
+ ;; took: 0.689589352 ms, 3.44794676e-05 ms per iteration
242
+ ;; took: 0.690437907 ms, 3.4521895349999996e-05 ms per iteration
243
+ ;; took: 0.683199842 ms, 3.4159992099999995e-05 ms per iteration
244
+ ;; took: 0.696863126 ms, 3.48431563e-05 ms per iteration
245
+ ;; total 6.954955385999999, average 0.6954955385999999 per run
246
+ ;; ================================================
247
+ ;; random string : total 6.954955385999999, average 0.6954955385999999 per run
248
+
249
+ ;; (def bm-random-string ()
250
+ ;; (list (random-string)
251
+ ;; (random-string)
252
+ ;; (random-string)
253
+ ;; (random-string)
254
+ ;; (random-string)
255
+ ;; (random-string)
256
+ ;; (random-string)
257
+ ;; (random-string)
258
+ ;; (random-string)
259
+ ;; (random-string)))
179
260
 
180
261
  (def bm-repeat (f n)
181
262
  ; used in benchmarking
@@ -195,10 +276,17 @@
195
276
  (p "================================================\n")
196
277
  "~desc : total ~(just times), average ~(/ times repeats) per run"))
197
278
 
279
+
280
+
198
281
  (def rbs (name)
199
282
  (let summary nil
200
- (push (bm "mapsum " bm-mapsum 10 100) summary)
201
- (push (bm "mapreduce " bm-mapreduce 10 100) summary)
283
+ ;; (push (bm "random string " bm-random-string 10 20000) summary)
284
+ ;; (push (bm "string concatenation " bm-string-concat 10 20000) summary)
285
+ ;; (push (bm "type-of " bm-type-of 10 20000) summary)
286
+ ;; (push (bm "accum " bm-acc 10 500) summary)
287
+ ;; (push (bm "accum " bm-facc 10 500) summary)
288
+ ;; (push (bm "mapsum " bm-mapsum 10 100) summary)
289
+ ;; (push (bm "mapreduce " bm-mapreduce 10 100) summary)
202
290
  ;; (push (bm "cond with OR " bm-faster-or 10 40000) summary)
203
291
  ;; (push (bm "cond with OR " bm-cond-9 10 40000) summary)
204
292
  ;; (push (bm "cond with OR " bm-cond-9 10 100000) summary)
@@ -4,4 +4,31 @@
4
4
  (a 1)
5
5
  (a 2)
6
6
  (a 3))
7
- (1 2 3)))
7
+ (1 2 3))
8
+
9
+ ("accum function returns the accumulated object in each case"
10
+ (let watcher nil
11
+ (let accumulated (accum b
12
+ (= watcher (cons (b 1) watcher))
13
+ (= watcher (cons (b 2) watcher))
14
+ (= watcher (cons (b 3) watcher)))
15
+ (list 'watcher watcher 'accumulated accumulated)))
16
+ (watcher (3 2 1)
17
+ accumulated (1 2 3))))
18
+
19
+ (examples-for accum-hash
20
+ ("accumulate the values passed to a given function under the given key, return the resulting hash"
21
+ (let h
22
+ (accum-hash hi
23
+ (hi 'a 1)
24
+ (hi 'a 2)
25
+ (hi 'b 42)
26
+ (hi 'b 43)
27
+ (hi 'a 3)
28
+ (hi 'b 44)
29
+ (hi 'c 'x)
30
+ (hi 'a 4)
31
+ (hi 'c 'y)
32
+ (hi 'c 'z))
33
+ (list h.a h.b h.c))
34
+ ((4 3 2 1) (44 43 42) (z y x))))
@@ -12,9 +12,14 @@
12
12
  "hello world")
13
13
 
14
14
  ("recurses as necessary"
15
- (explain-mac 1 '(aif (a) (b) (c) (d) (e)))
16
- (let it (a) (if it (b) (aif (c) (d) (e)))))
15
+ (explain-mac 2 '(aif (a) (b) (c) (d) (e)))
16
+ (let it (a) (if it (b) (ifv it (c) (d) (e)))))
17
+
18
+ ("assigns each successive condition to 'it"
19
+ (aif (eq? 1 2) nil
20
+ 42 (list it it))
21
+ (42 42))
17
22
 
18
23
  ("avoids unnecessary expansion"
19
- (explain-mac 1 '(aif (a) (b) (c)))
24
+ (explain-mac 2 '(aif (a) (b) (c)))
20
25
  (let it (a) (if it (b) (c)))))
@@ -0,0 +1,7 @@
1
+ (examples-for andify
2
+ ("takes a list of functions A and returns a function that applies its arguments to each f in A in turn, returning the logical AND of the set of return values"
3
+ (with (even? λn(eq? (mod n 2) 0)
4
+ triple? λn(eq? (mod n 3) 0)
5
+ big? λn(> n 10))
6
+ (select (andify even? triple? big?) (range -10 31)))
7
+ (12 18 24 30)))
@@ -0,0 +1,17 @@
1
+ (examples-for at-syntax
2
+ ("at-symbol as argument"
3
+ (let @ {}
4
+ (= @foo 1)
5
+ (= @bar 2)
6
+ (list @foo @bar))
7
+ (1 2))
8
+
9
+ ("at-syntax assignment"
10
+ (pre-compile '(= @a 42))
11
+ (hash-set @ 'a 42))
12
+
13
+ ("mixed at-syntax and dot-syntax assignment"
14
+ (pre-compile '(= @a.b 42))
15
+ (hash-set (hash-get @ 'a) 'b 42)))
16
+
17
+ ;; (let @ {} 123)
@@ -19,6 +19,15 @@
19
19
  (best > '(c g d o a p b m e g a z m))
20
20
  z)
21
21
 
22
+ ("finds object with max size"
23
+ (to-string:best (map-compare-f > &size)
24
+ (list { i 0 size 3 }
25
+ { i 1 size 1 }
26
+ { i 2 size 7 }
27
+ { i 3 size 4 }
28
+ { i 4 size 5 }))
29
+ "{i=>2, size=>7}")
30
+
22
31
  ("finds maximum of list of numbers"
23
32
  (best > '(3 5 4 7 8 2))
24
33
  8))
@@ -84,3 +84,22 @@
84
84
 
85
85
  (examples-for mod
86
86
  ("modulus for two ints" (mod 64 6) 4))
87
+
88
+ (examples-for abs
89
+ ("a positive integer" (abs 64) 64)
90
+ ("a negative integer" (abs -42) 42)
91
+ ("zero" (abs 0) 0)
92
+ ("negative zero" (abs -0) 0)
93
+ ("float zero" (abs 0.0) 0)
94
+ ("float negative zero" (abs -0.0) 0)
95
+ ("float negative" (abs -1.6) 1.6)
96
+ ("float positive" (abs 4.2) 4.2))
97
+
98
+ (examples-for inspect
99
+ ("truth" (inspect t) "t")
100
+ ("nil" (inspect nil) "nil")
101
+ ("number" (inspect 42) "42")
102
+ ("string" (inspect "hello") "\"hello\"")
103
+ ("list" (inspect '(fn (x) (this that))) "(fn (x) (this that))")
104
+ ("list with string" (inspect '(fn (x) (this "string"))) "(fn (x) (this \"string\"))")
105
+ ("list with sym" (inspect '(fn (x) (this 'quoted))) "(fn (x) (this (quote quoted)))"))
@@ -0,0 +1,14 @@
1
+ (examples-for 'case
2
+ ("expands to an if-expression"
3
+ (do (reset-uniq-counter)
4
+ (explain-mac 1
5
+ `(case eq? person.name
6
+ "conan" (greet person)
7
+ "egg" (delete person)
8
+ "bach" (play person)
9
+ else (interrogate person))))
10
+ (let caseval-1 (dot-syntax person name)
11
+ (if (eq? caseval-1 "conan") (greet person)
12
+ (eq? caseval-1 "egg") (delete person)
13
+ (eq? caseval-1 "bach") (play person)
14
+ (interrogate person)))))
@@ -0,0 +1,6 @@
1
+ (examples-for cdr-set
2
+ ("sets cdr of a cons cell"
3
+ (let things '(a b c d)
4
+ (cdr-set things '(x y z))
5
+ things)
6
+ (a x y z)))
@@ -18,6 +18,11 @@
18
18
  ("navigates to week end from sun" (let d (date 2015 11 1) (to-string d.end-of-week)) "2015-11-01" )
19
19
  ("navigates to week end" (let d (date 2015 11 6) (to-string d.end-of-week)) "2015-11-08" )
20
20
 
21
+ ("works with apply" (to-string (apply date '(2006 6 21))) "2006-06-21")
22
+ ("works with apply again" (to-string (apply date 2006 6 21 nil)) "2006-06-21")
23
+
24
+ ("parses string" (let d (date "2004-03-12") (list d.year d.month d.day)) (2004 3 12))
25
+
21
26
  ("can act as hash key"
22
27
  (with (h {} d (date 2015 11 8))
23
28
  (hash-set h d "on this day")
@@ -45,4 +50,54 @@
45
50
  ("adds days" (let d (date 2015 11 8) (to-string (+ d 1))) "2015-11-09")
46
51
  ("adds more days" (let d (date 2015 11 8) (to-string (+ d 10))) "2015-11-18")
47
52
  ("subtracts a day" (let d (date 2015 11 18) (to-string (- d 1))) "2015-11-17")
48
- ("subtracts more days" (let d (date 2015 11 18) (to-string (- d 5))) "2015-11-13"))
53
+ ("subtracts more days" (let d (date 2015 11 18) (to-string (- d 5))) "2015-11-13")
54
+
55
+ ("advances by -2 weeks" (let d (date 1965 6 8) (to-string (+ d '(-2 week)))) "1965-05-25")
56
+ ("advances by -1 week" (let d (date 1965 6 8) (to-string (+ d '(-1 week)))) "1965-06-01")
57
+ ("advances by 1 week" (let d (date 1965 6 8) (to-string (+ d '( 1 week)))) "1965-06-15")
58
+ ("advances by 2 weeks" (let d (date 1965 6 8) (to-string (+ d '( 2 week)))) "1965-06-22")
59
+
60
+ ("advances by -2 weeks" (let d (date 1965 6 8) (to-string (+ d '(-2 week)))) "1965-05-25")
61
+ ("advances by -1 week" (let d (date 1965 6 8) (to-string (+ d '(-1 week)))) "1965-06-01")
62
+ ("advances by 1 week" (let d (date 1965 6 8) (to-string (+ d '( 1 week)))) "1965-06-15")
63
+ ("advances by 2 weeks" (let d (date 1965 6 8) (to-string (+ d '( 2 week)))) "1965-06-22")
64
+
65
+ ("advances by -2 days" (let d (date 1965 6 8) (to-string (+ d '(-2 day)))) "1965-06-06")
66
+ ("advances by -1 day" (let d (date 1965 6 8) (to-string (+ d '(-1 day)))) "1965-06-07")
67
+ ("advances by 1 day" (let d (date 1965 6 8) (to-string (+ d '( 1 day)))) "1965-06-09")
68
+ ("advances by 2 days" (let d (date 1965 6 8) (to-string (+ d '( 2 day)))) "1965-06-10")
69
+
70
+ ("advances by -2 months" (let d (date 1965 6 8) (to-string (+ d '(-2 month)))) "1965-04-08")
71
+ ("advances by -1 month" (let d (date 1965 6 8) (to-string (+ d '(-1 month)))) "1965-05-08")
72
+ ("advances by 1 month" (let d (date 1965 6 8) (to-string (+ d '( 1 month)))) "1965-07-08")
73
+ ("advances by 2 months" (let d (date 1965 6 8) (to-string (+ d '( 2 month)))) "1965-08-08")
74
+
75
+ ("advances by -2 years" (let d (date 1965 6 8) (to-string (+ d '(-2 year)))) "1963-06-08")
76
+ ("advances by -1 year" (let d (date 1965 6 8) (to-string (+ d '(-1 year)))) "1964-06-08")
77
+ ("advances by 1 year" (let d (date 1965 6 8) (to-string (+ d '( 1 year)))) "1966-06-08")
78
+ ("advances by 2 years" (let d (date 1965 6 8) (to-string (+ d '( 2 year)))) "1967-06-08")
79
+
80
+ ("adapts for leap years" (let d (date 2019 12 31) (to-string (+ d '( 2 month)))) "2020-02-29")
81
+ ("jumps one year" (let d (date 2019 12 31) (to-string (+ d '( 1 year) ))) "2020-12-31")
82
+ ("jumps to 28 feb a year later" (let d (date 2019 12 31) (to-string (+ d '(14 month)))) "2021-02-28")
83
+ ("jumps to end of june" (let d (date 2019 12 31) (to-string (+ d '( 6 month)))) "2020-06-30")
84
+ ("jumps to end of july" (let d (date 2019 12 31) (to-string (+ d '( 7 month)))) "2020-07-31")
85
+ ("jumps from end feb to end feb" (let d (date 2020 02 29) (to-string (+ d '(12 month)))) "2021-02-28")
86
+
87
+ ("finds anniversary before a given date in previous year"
88
+ (to-string (anniversary/previous (date 2019 6 21) (date 1949 10 3)))
89
+ "2018-10-03")
90
+
91
+ ("finds anniversary before a given date in same year"
92
+ (to-string (anniversary/previous (date 2019 11 20) (date 1949 10 3)))
93
+ "2019-10-03")
94
+
95
+ ("finds anniversary after a given date in same year"
96
+ (to-string (anniversary/next (date 2019 6 21) (date 1949 10 3)))
97
+ "2019-10-03")
98
+
99
+
100
+ ("finds anniversary after a given date in following year"
101
+ (to-string (anniversary/next (date 2019 11 20) (date 1949 10 3)))
102
+ "2020-10-03")
103
+ )
@@ -20,25 +20,25 @@
20
20
 
21
21
  (examples-for destructure/build
22
22
  ("with no args"
23
- (destructure/build nil nil '(x))
23
+ (destructure/build nil nil '(x) (fn (a b) `(fn ,a ,@b)))
24
24
  (fn nil x))
25
25
 
26
26
  ("with one arg"
27
- (destructure/build '(a) nil '(x))
27
+ (destructure/build '(a) nil '(x) (fn (a b) `(fn ,a ,@b)))
28
28
  (fn (a) x))
29
29
 
30
30
  ("with one rest-arg"
31
- (destructure/build 'args nil '(x))
31
+ (destructure/build 'args nil '(x) (fn (a b) `(fn ,a ,@b)))
32
32
  (fn args x))
33
33
 
34
34
  ("with one destructuring arg"
35
35
  (do (reset-uniq-counter)
36
- (destructure/build '((a b)) nil '(x)))
36
+ (destructure/build '((a b)) nil '(x) (fn (a b) `(fn ,a ,@b))))
37
37
  (fn (destructure-1) (with (a (nth 0 destructure-1) b (nth 1 destructure-1)) x)))
38
38
 
39
39
  ("with complex args"
40
40
  (do (reset-uniq-counter)
41
- (destructure/build '(a (b c) (d (e f)) g . h) nil '(x)))
41
+ (destructure/build '(a (b c) (d (e f)) g . h) nil '(x) (fn (a b) `(fn ,a ,@b))))
42
42
  (fn (a destructure-1 destructure-2 g . h)
43
43
  (with (d (nth 0 destructure-2)
44
44
  (e f) (nth 1 destructure-2))
@@ -49,12 +49,23 @@
49
49
  (examples-for fun
50
50
  ("complete expansion, handles recursive destructures"
51
51
  (do (reset-uniq-counter)
52
- (pre-compile '(fun ((a (b c)) d . e) x)))
52
+ (pre-compile '(fun ((a (b c)) d . e) (do-the-thing a b c d e))))
53
53
  (fn (destructure-1 d . e)
54
54
  ((fn (a destructure-2)
55
- ((fn (b c) x)
56
- (nth 0 destructure-2)
57
- (nth 1 destructure-2))) (nth 0 destructure-1) (nth 1 destructure-1))))
55
+ ((fn (b c)
56
+ ((fn nil
57
+ (assign b (nth 0 destructure-2))
58
+ (assign c (nth 1 destructure-2))))
59
+ ((fn nil
60
+ (assign a (nth 0 destructure-1))
61
+ ((fn (destructuring-assign-3)
62
+ (assign b (car destructuring-assign-3))
63
+ (assign c (car (cdr destructuring-assign-3))))
64
+ (nth 1 destructure-1))))
65
+ (do-the-thing a b c d e))
66
+ nil))
67
+ nil)))
68
+
58
69
 
59
70
  ("nested improper arguments"
60
71
  (let (a (b c . d) e) (list "A" (list "B" "C" "D0" "D1" "D2") "E")
@@ -66,12 +77,12 @@
66
77
  (do (without-hooks 'warnings/new
67
78
  λ(on-err nil
68
79
  (pre-compile '(fun (aif (and or) . when)
69
- "ignore"))))
80
+ ignore))))
70
81
  warnings/list)
71
- ((arg-shadows-macro "arg " when " shadows macro " when " in arg list " (aif (and or) . when))
72
- (arg-shadows-macro "arg " or " shadows macro " or " in arg list " (aif (and or) . when))
73
- (arg-shadows-macro "arg " and " shadows macro " and " in arg list " (aif (and or) . when))
74
- (arg-shadows-macro "arg " aif " shadows macro " aif " in arg list " (aif (and or) . when))))
82
+ ((arg-shadows-macro "arg " when " shadows macro " when " in arg list " (aif (and or) . when) " and body " (ignore) )
83
+ (arg-shadows-macro "arg " or " shadows macro " or " in arg list " (aif (and or) . when) " and body " (ignore) )
84
+ (arg-shadows-macro "arg " and " shadows macro " and " in arg list " (aif (and or) . when) " and body " (ignore) )
85
+ (arg-shadows-macro "arg " aif " shadows macro " aif " in arg list " (aif (and or) . when) " and body " (ignore) )))
75
86
 
76
87
  ("implicit in 'let and 'with"
77
88
  (with ((a b) (list "h" "e")
@@ -79,3 +90,24 @@
79
90
  (let (g (h (i j) k)) (list "w" (list "o" (list "r" "l") "d"))
80
91
  (string-pieces a b c d e f g h i j k)))
81
92
  "hello world"))
93
+
94
+ (examples-for =
95
+ ("destructures LHS"
96
+ (let a 1
97
+ (let b 2
98
+ (let c 3
99
+ (let d 4
100
+ (let e 5
101
+ (= (a (b e) c . d) (list 'this '(that those) 'another 11 22 33))
102
+ (list a b c d e))))))
103
+ (this that another (11 22 33) those)))
104
+
105
+ (examples-for with
106
+ ("destructures its args, also allows references to earlier args"
107
+ (with ((a (b . c) d . e) '(1 (2 3 4 5) 6 7 8 9)
108
+ x a
109
+ y c
110
+ z (fn (n) (if (eq? n 1) 1 (* n (z (- n 1)))))
111
+ g (z 6))
112
+ (list a b c d e x y g))
113
+ (1 2 (3 4 5) 6 (7 8 9) 1 (3 4 5) 720)))