This repository has been archived by the owner on Jul 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rark.rkt
1265 lines (1000 loc) · 29.3 KB
/
rark.rkt
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#lang planet kogir/rark
;; TODO: Kill this before release
(displayln "loading rark.rkt...")
(provide (all-defined-out))
;; I implemented square brackets in the reader as follows:
;; [stuff ...] => (square-brackets stuff ...)
;; This idea was stolen from multiple of the arc spinoffs on the arc forum
;(mac square-brackets (body ...)
;; Let's make ~ ssyntax extensible:
;(mac complement-ssyntax (body ...)
;; do - mapped to racket (begin ...) (performance)
;; safeset - mostly integrated into def and mac. Now more of a noisy =
;; def - sucked into core (required)
(def caar (xs) (car (car xs)))
(def cadr (xs) (car (cdr xs)))
(def cddr (xs) (cdr (cdr xs)))
;; no - sucked into core (performance)
(def acons (x) (is (type x) 'cons))
(def atom (x) (no (acons x)))
(def copylist (xs)
(if (no xs)
nil
(cons (car xs) (copylist (cdr xs)))))
;; TODO: map this to Racket's (list)?
;; No more need to copy since rest args are proper lists
(def list args args)
(def idfn (x) x)
;; map1 - sucked into core (performance)
(def pair (xs (o f list))
(if (no xs)
nil
(no (cdr xs))
(list (list (car xs)))
(cons (f (car xs) (cadr xs))
(pair (cddr xs) f))))
;; mac - sucked into core (required)
;; and - Sucked into core (performance, nil handling)
(def assoc (key al)
(if (atom al)
nil
(and (acons (car al)) (is (caar al) key))
(car al)
(assoc key (cdr al))))
(def alref (al key) (cadr (assoc key al)))
;; with - sucked into core (performance)
;; let - sucked into core (performance)
;; withs - sucked into core (performance)
(def join args
(if (no args)
nil
(let a (car args)
(if (no a)
(apply join (cdr args))
(cons (car a) (apply join (cdr a) (cdr args)))))))
;; rfn - no longer needed, since afn is now hygenic :)
;; afn - sucked into core (performance + hygene)
;; reader expands x:y:z into (compose x y z), ~x into (no x)
;; compose - Sucked into core (performance + ssyntax support)
;; complement - sucked into core (performance)
(def rev (xs)
((afn (xs acc)
(if (no xs)
acc
(self (cdr xs) (cons (car xs) acc))))
xs nil))
(def isnt (x y) (no (is x y)))
;; w/uniq - killed for now - see how far hygiene can get us
;; or - sucked into core (performance, nil handling)
(def alist (x) (or (no x) (is (type x) 'cons)))
(mac in (x choice ...)
(or (is x choice) ...))
(def iso (x y)
(or (is x y)
(and (acons x)
(acons y)
(iso (car x) (car y))
(iso (cdr x) (cdr y)))))
(mac when (test body ...)
(if test (do body ...)))
(mac unless (test body ...)
(if (no test) (do body ...)))
(mac while (test body ...)
((afn () (when test body ... (self)))))
(def empty (seq)
(or (no seq)
(and (or (is (type seq) 'string) (is (type seq) 'table))
(is (len seq) 0))))
(def reclist (f xs)
(and xs (or (f xs) (reclist f (cdr xs)))))
(def recstring (test s (o start 0))
((afn (i)
(and (< i (len s))
(or (test i)
(self (+ i 1)))))
start))
(def testify (x)
(if (isa x 'fn) x [is _ x]))
(def some (test seq)
(let f (testify test)
(if (alist seq)
(reclist f:car seq)
(recstring f:seq seq))))
(def all (test seq)
(~some (complement (testify test)) seq))
(def mem (test seq)
(let f (testify test)
(reclist [if (f:car _) _] seq)))
(def find (test seq)
(let f (testify test)
(if (alist seq)
(reclist [if (f:car _) (car _)] seq)
(recstring [if (f:seq _) (seq _)] seq))))
(def isa (x y) (is (type x) y))
;; TODO: Add hack to allow (mappend (table) '(1 2 3 4))
(def map (f . seqs)
(if (some [isa _ 'string] seqs)
(withs (n (apply min (map len seqs))
new (newstring n))
((afn (i)
(if (is i n)
new
(do (sref new (apply f (map [_ i] seqs)) i)
(self (+ i 1)))))
0))
(no (cdr seqs))
(map1 f (car seqs))
((afn (seqs)
(if (some no seqs)
nil
(cons (apply f (map1 car seqs))
(self (map1 cdr seqs)))))
seqs)))
(def mappend (f . args)
(apply + nil (apply map f args)))
(def firstn (n xs)
(if (no n) xs
(and (> n 0) xs) (cons (car xs) (firstn (- n 1) (cdr xs)))
nil))
(def nthcdr (n xs)
(if (no n) xs
(> n 0) (nthcdr (- n 1) (cdr xs))
xs))
(def tuples (xs (o n 2))
(if (no xs)
nil
(cons (firstn n xs)
(tuples (nthcdr n xs) n))))
(def caris (x val)
(and (acons x) (is (car x) val)))
(def warn (msg . args)
(disp (+ "Warning: " msg ". "))
(map [do (write _) (disp " ")] args)
(disp #\newline))
(mac atomic (body ...)
(atomic-invoke (fn () body ...)))
(mac atlet (args ...)
(atomic (let args ...)))
(mac atwith (args ...)
(atomic (with args ...)))
(mac atwiths (args ...)
(atomic (withs args ...)))
(def treewise (f base tree)
(if (atom tree)
(base tree)
(f (treewise f base (car tree))
(treewise f base (cdr tree)))))
;; do1 - mapped to racket begin0 (performance)
;; TODO: Find a way to push this back into arc.arc
;; expand=, expand=list, and = sucked into core (performance, required)
;; TODO: See if I can eliminate sets here using a different construction
(mac loop (start test update body ...)
(do start
((afn ()
(if test
(do body ...
update
(self)))))))
;; TODO: Consider using Racket's for construct with in-range
(mac for (v init max body ...)
(with (v nil i init stop (+ max 1))
(loop (assign v i) (< v stop) (assign v (+ v 1))
body ...)))
(mac down (v init min body ...)
(with (v nil i init stop (- min 1))
(loop (assign v i) (> v stop) (assign v (- v 1))
body ...)))
(mac repeat (n body ...)
(for reps 1 n body ...))
;; TODO: Use Racket's for-each to skip creating the list
;; TODO: Special case strings
(mac each (var expr body ...)
(with (seq expr
mapfn (fn (var) body ...))
(if (alist seq)
(each1 mapfn seq)
(isa seq 'table)
; TODO: You can do better
(maptable (fn var body ...) seq)
(for iter 0 (- (len seq) 1)
(mapfn (seq iter)))))
(void))
(def cut (seq start (o end))
(let end (if (no end) (len seq)
(< end 0) (+ (len seq) end)
end)
(if (isa seq 'string)
(let s2 (newstring (- end start))
(for i 0 (- end start 1)
(= (s2 i) (seq (+ start i))))
s2)
(firstn (- end start) (nthcdr start seq)))))
(mac whilet (var test body ...)
((afn (var)
(when var
body ...
(self test)))
test))
(def last (xs)
(if (cdr xs)
(last (cdr xs))
(car xs)))
(def rem (test seq)
(let f (testify test)
(if (alist seq)
((afn (s)
(if (no s) nil
(f (car s)) (self (cdr s))
(cons (car s) (self (cdr s)))))
seq)
(coerce (rem test (coerce seq 'cons)) 'string))))
(def keep (test seq)
(rem (complement (testify test)) seq))
(def trues (f xs)
(and xs
(let fx (f (car xs))
(if fx
(cons fx (trues f (cdr xs)))
(trues f (cdr xs))))))
;; caselet - sucked into core (argument pairing, performance)
;; case - sucked into core (argument pairing, performance)
(mac push (x place)
(atomic
(= place (cons x place))
place))
(mac swap (place1 place2)
(atwiths (p1 place1
p2 place2)
(= place1 p2)
(= place2 p1)))
(mac rotate (a c ...)
(shift (c ... a) (a c ...)))
(mac shift ((from0 from ...) (to0 to ...))
(let tmp from0
(= to from) ...
(= to0 tmp)))
(mac pop (place)
(atwiths (val place)
(do1 (car val)
(= place (cdr val)))))
(def adjoin (x xs (o test iso))
(if (some [test x _] xs)
xs
(cons x xs)))
(mac pushnew (x place args ...)
(atomic
(= place (adjoin x place args ...))))
(mac pull (test place)
(atomic
(= place (rem test place))))
(mac togglemem (x place args ...)
(atwiths (val place
tmp x)
(= place (if (mem tmp val)
(rem tmp val)
(adjoin tmp val args ...)))))
;; TODO: move atomic within = and lost it here
(mac ++ (place (o i 1))
(if (isa 'place 'sym)
(= place (+ place i))
(atomic
(= place (+ place i)))))
;; TODO: Ditto
(mac -- (place (o i 1))
(if (isa 'place 'sym)
(= place (- place i))
(atomic
(= place (- place i)))))
;; TODO: Check this with PG
(mac zap (op place args ...)
(atomic
(= place (op place args ...))))
(def pr args
(each1 disp args)
(car args))
(def prt args
(each1 [if _ (disp _)] args)
(car args))
(def prn args
(do1 (apply pr args)
(writec #\newline)))
(mac wipe (arg ...)
(do (= arg nil) ...))
(mac set (arg ...)
(do (= arg t) ...))
(mac iflet (var expr then rest ...)
(let temp expr
(if temp
(let var temp then)
rest ...)))
(mac whenlet (var expr body ...)
(iflet var expr (do body ...)))
;; aif - sucked into core (hygiene)
(mac awhen (expr body ...)
(aif expr (do body ...)))
;; aand - sucked into core (hygiene)
(mac accum (accfn body ...)
(withs (acc nil accfn [push _ acc])
body ...
(rev acc)))
(mac drain (expr (o eof nil))
(with (acc nil done nil)
(while (no done)
(let res expr
(if (is res eof)
(= done t)
(push res acc))))
(rev acc)))
(mac whiler (var expr endval body ...)
(withs (var nil test (testify endval))
(while (no (test (= var expr)))
body ...)))
;; macex - No longer needed, Racket does the expansion for us
(def consif (x y) (if x (cons x y) y))
(def string args
(apply + "" (map [coerce _ 'string] args)))
(def flat x
((afn (x acc)
(if (no x) acc
(atom x) (cons x acc)
(self (car x) (self (cdr x) acc))))
x nil))
(mac check (x test (o alt))
(let temp x
(if (test temp) temp alt)))
(def pos (test seq (o start 0))
(let f (testify test)
(if (alist seq)
((afn (seq n)
(if (no seq)
nil
(f (car seq))
n
(self (cdr seq) (+ n 1))))
(nthcdr start seq)
start)
(recstring [if (f (seq _)) _] seq start))))
;; even - Sucked into core (performance)
;; odd - Sucked into core (performance)
(mac after (x ys ...)
(protect (fn () x) (fn () ys ...)))
(mac io-expander (f var name body ...)
(let var (f name)
(after (do body ...) (close var))))
(mac w/infile (var name body ...)
(io-expander infile var name body ...))
(mac w/outfile (var name body ...)
(io-expander outfile var name body ...))
(mac w/instring (var str body ...)
(io-expander instring var str body ...))
(mac w/socket (var port body ...)
(io-expander open-socket var port body ...))
(mac w/outstring (var body ...)
(let var (outstring) body ...))
(mac w/appendfile (var name body ...)
(let var (outfile name 'append)
(after (do body ...) (close var))))
(mac w/stdout (str body ...)
(call-w/stdout str (fn () body ...)))
(mac w/stdin (str body ...)
(call-w/stdin str (fn () body ...)))
(mac tostring (body ...)
(w/outstring temp
(w/stdout temp body ...)
(inside temp)))
(mac fromstring (str body ...)
(w/instring temp str
(w/stdin temp body ...)))
(def readstring1 (s (o eof nil)) (w/instring i s (read i eof)))
(def read ((o x (stdin)) (o eof nil))
(if (isa x 'string) (readstring1 x eof) (sread x eof)))
(def readfile (name) (w/infile s name (drain (read s))))
(def readfile1 (name) (w/infile s name (read s)))
(def readall (src (o eof nil))
((afn (i)
(let x (read i eof)
(if (is x eof)
nil
(cons x (self i)))))
(if (isa src 'string) (instring src) src)))
(def allchars (str)
(tostring (whiler c (readc str nil) no
(writec c))))
(def filechars (name)
(w/infile s name (allchars s)))
(let counter 0
(def writefile (val file)
(let tmpfile (+ file ".tmp." (atomic (++ counter)))
(w/outfile o tmpfile (write val o))
(mvfile tmpfile file))
val))
(def sym (x) (coerce x 'sym))
(def int (x (o b 10)) (coerce x 'int b))
;; TODO: Check performance - using eval is a hack
(mac rand-choice (expr ...)
(let choices (list 'expr ...)
(eval (choices (rand (len choices))))))
(mac n-of (n expr)
(let acc nil
(repeat n (push expr acc))
(rev acc)))
;; TODO: Use OpenSSL for this to make it cross platform
(let str (infile "/dev/urandom")
(def rand-string (n)
(let c "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
(with (nc 62 s (newstring n) i 0)
(while (< i n)
(let x (readb str)
(unless (> x 247)
(= (s i) (c (mod x nc)))
(++ i))))
s))))
;; TODO: Is this as inefficient as it looks? for uses the equivalent of list-ref
(mac forlen (var s body ...)
(for var 0 (- (len s) 1) body ...))
(def best (f seq)
(if (no seq)
nil
(let wins (car seq)
(each elt (cdr seq)
(if (f elt wins) (= wins elt)))
wins)))
(def max args (best > args))
(def min args (best < args))
(def most (f seq)
(unless (no seq)
(withs (wins (car seq) topscore (f wins))
(each elt (cdr seq)
(let score (f elt)
(if (> score topscore) (= wins elt topscore score))))
wins)))
(def insert-sorted (test elt seq)
(if (no seq)
(list elt)
(test elt (car seq))
(cons elt seq)
(cons (car seq) (insert-sorted test elt (cdr seq)))))
(mac insort (test elt seq)
(zap [insert-sorted test elt _] seq))
(def reinsert-sorted (test elt seq)
(if (no seq)
(list elt)
(is elt (car seq))
(reinsert-sorted test elt (cdr seq))
(test elt (car seq))
(cons elt (rem elt seq))
(cons (car seq) (reinsert-sorted test elt (cdr seq)))))
(mac insortnew (test elt seq)
(zap [reinsert-sorted test elt _] seq))
(def memo (f)
(with (cache (table) nilcache (table))
(fn args
(or (cache args)
(and (no (nilcache args))
(aif (apply f args)
(= (cache args) it)
(do (set (nilcache args))
nil)))))))
(mac defmemo (name parms body ...)
(safeset name (memo (fn parms body ...))))
;; TODO: suck into core (performance)
(def <= args
(or (no args)
(no (cdr args))
(and (no (> (car args) (cadr args)))
(apply <= (cdr args)))))
;; TODO: Suck into core (performance)
(def >= args
(or (no args)
(no (cdr args))
(and (no (< (car args) (cadr args)))
(apply >= (cdr args)))))
(def whitec (c)
(in c #\space #\newline #\tab #\return))
(def nonwhite (c) (no (whitec c)))
(def letter (c) (or (<= #\a c #\z) (<= #\A c #\Z)))
(def digit (c) (<= #\0 c #\9))
(def alphadig (c) (or (letter c) (digit c)))
(def punc (c)
(in c #\. #\, #\; #\: #\! #\?))
(def readline ((o str (stdin)))
(awhen (readc str)
(tostring
(writec it)
(whiler c (readc str) [in _ nil #\newline]
(writec c)))))
(mac summing (sumfn body ...)
(let c 0
(let sumfn (fn (arg) (if arg (++ c)))
body ...)
c))
(def sum (f xs)
(let n 0
(each x xs (++ n (f x)))
n))
(def carif (x) (if (atom x) x (car x)))
(def prall (elts (o init "") (o sep ", "))
(when elts
(pr init (car elts))
(map [pr sep _] (cdr elts))
elts))
(def prs args
(prall args "" #\space))
(def tree-subst (old new tree)
(if (is tree old)
new
(atom tree)
tree
(cons (tree-subst old new (car tree))
(tree-subst old new (cdr tree)))))
(def ontree (f tree)
(f tree)
(unless (atom tree)
(ontree f (car tree))
(ontree f (cdr tree))))
(def dotted (x)
(if (atom x)
nil
(and (cdr x) (or (atom (cdr x))
(dotted (cdr x))))))
(def fill-table (table data)
(each arg (pair data) (with (k (car arg) v (cdr arg))
(= (table k) v)))
table)
(def keys (h)
(accum a (each (k v) h (a k))))
(def vals (h)
(accum a (each (k v) h (a v))))
(def tablist (h)
(let z nil
(maptable (fn (k v) (= z (cons (list k v) z))) h)
z))
(def listtab (al)
(let h (table)
(map (fn ((k v)) (= (h k) v))
al)
h))
;; TODO: Find a syntax to bring these back to Arc
;; obj - sucked into core (arg grouping)
(mac w/table (var body ...)
(let var (table) body ... var))
(def load-table (file (o eof) (o multiple))
(w/infile i file (read-table i eof multiple)))
(def read-table ((o i (stdin)) (o eof) (o multiple))
(if multiple
(w/table h
(whiler e (read i eof) eof
(= (h (car e)) (cadr e))))
(let e (read i eof)
(if (alist e) (listtab e) e))))
(def load-tables (file)
(w/infile i file
(let eof (uniq)
(drain (read-table i eof) eof))))
(def save-table (h file)
(writefile (tablist h) file))
(def write-table (h (o o (stdout)))
(write (tablist h) o))
(def copy (x . args)
(let x2 (case (type x)
sym x
cons (copylist x) ; (apply (fn args args) x)
string (let new (newstring (len x))
(forlen i x
(= (new i) (x i)))
new)
table (let new (table)
(each (k v) x
(= (new k) v))
new)
(err "Can't copy " x))
(map (fn ((k v)) (= (x2 k) v))
(pair args))
x2))
(def abs (n)
(if (< n 0) (- n) n))
(def round (n)
(withs (base (trunc n) rem (abs (- n base)))
(if (> rem 1/2) ((if (> n 0) + -) base 1)
(< rem 1/2) base
(odd base) ((if (> n 0) + -) base 1)
base)))
(def roundup (n)
(withs (base (trunc n) rem (abs (- n base)))
(if (>= rem 1/2)
((if (> n 0) + -) base 1)
base)))
(def nearest (n quantum)
(* (roundup (/ n quantum)) quantum))
(def avg (ns) (/ (apply + ns) (len ns)))
(def med (ns (o test >))
((sort test ns) (round (/ (len ns) 2))))
(def sort (test seq)
(if (alist seq)
(mergesort test (copylist seq))
(coerce (mergesort test (coerce seq 'cons)) (type seq))))
(def mergesort (less? lst)
(with (n (len lst))
(if (<= n 1) lst
; ; check if the list is already sorted
; ; (which can be a common case, eg, directory lists).
; (let loop ([last (car lst)] [next (cdr lst)])
; (or (null? next)
; (and (not (less? (car next) last))
; (loop (car next) (cdr next)))))
; lst
((afn (n)
(if (> n 2)
; needs to evaluate L->R
(withs (j (/ (if (even n) n (- n 1)) 2) ; faster than round
a (self j)
b (self (- n j)))
(merge less? a b))
; the following case just inlines the length 2 case,
; it can be removed (and use the above case for n>1)
; and the code still works, except a little slower
(is n 2)
(with (x (car lst) y (cadr lst) p lst)
(= lst (cddr lst))
(when (less? y x) (scar p y) (scar (cdr p) x))
(scdr (cdr p) nil)
p)
(is n 1)
(with (p lst)
(= lst (cdr lst))
(scdr p nil)
p)
nil))
n))))
(def merge (less? x y)
(if (no x) y
(no y) x
(let lup (afn (r x y r-x?) ; r-x? for optimization -- is r connected to x?
(if (less? (car y) (car x))
(do (if r-x? (scdr r y))
(if (cdr y) (self y x (cdr y) nil) (scdr y x)))
; (car x) <= (car y)
(do (if (no r-x?) (scdr r x))
(if (cdr x) (self x (cdr x) y t) (scdr x y)))))
(if (less? (car y) (car x))
(do (if (cdr y) (lup y x (cdr y) nil) (scdr y x))
y)
; (car x) <= (car y)
(do (if (cdr x) (lup x (cdr x) y t) (scdr x y))
x)))))
(def bestn (n f seq)
(firstn n (sort f seq)))
(def split (seq pos)
(list (cut seq 0 pos) (cut seq pos)))
(mac time (expr)
(let t1 (msec)
(do1 expr
(let t2 (msec)
(prn "time: " (- t2 t1) " msec.")))))
(mac etime (expr)
(let t1 (msec)
(do1 expr
(let t2 (msec)
(disp (string "time: " (- t2 t1) " msec.\n") (stderr))))))
(mac ttime (tag expr)
(let t1 (msec)
(do1 expr
(let t2 (msec)
(disp (string tag " time: " (- t2 t1) " msec.\n")
(stderr))))))
(mac jtime (expr)
(do1 'ok (time expr)))
(mac time10 (expr)
(time (repeat 10 expr)))
(def union (f xs ys)
(+ xs (rem (fn (y) (some [f _ y] xs))
ys)))
;; TODO: find a way to move these back out
;; deftem - sucked into functions :(
;; addtem - sucked into functions :(
(def inst (tem . args)
(let x (eq-table)
(each (k v) (if (acons tem) tem (templates* tem))
(unless (no v) (= (x k) (v))))
(each (k v) (pair args)
(= (x k) v))
x))
(def temread (tem (o str (stdin)))
(templatize tem (read str)))
(def templatize (tem raw)
(with (x (inst tem) fields (if (acons tem) tem (templates* tem)))
(each (k v) raw
(when (assoc k fields)
(= (x k) v)))
x))
(def temload (tem file)
(w/infile i file (temread tem i)))
(def temloadall (tem file)
(map (fn (pairs) (templatize tem pairs))
(w/infile in file (readall in))))
(def number (n) (in (type n) 'int 'num))
(def since (t1) (- (seconds) t1))
(def minutes-since (t1) (/ (since t1) 60))
(def hours-since (t1) (/ (since t1) 3600))
(def days-since (t1) (/ (since t1) 86400))
(def cache (timef valf)
(with (cached nil gentime nil)
(fn ()
(unless (and cached (< (since gentime) (timef)))
(= cached (valf)
gentime (seconds)))
cached)))
(mac defcache (name lasts body ...)
(safeset name (cache (fn () lasts)
(fn () body ...))))
(mac errsafe (expr)
(on-err (fn (c) nil)
(fn () expr)))
(def saferead (arg) (errsafe (read arg)))
(def safe-load-table (filename)
(or (errsafe (load-table filename))
(table)))
(def date ((o s (seconds)))
(rev (nthcdr 3 (timedate s))))
(def datestring ((o s (seconds)))
(let (y m d) (date s)
(string y "-" (if (< m 10) "0") m "-" (if (< d 10) "0") d)))
(def count (test x)
(with (n 0 testf (testify test))
(each elt x
(if (testf elt) (++ n)))
n))
(def ellipsize (str (o limit 80))
(if (<= (len str) limit)
str
(+ (cut str 0 limit) "...")))
(def rand-elt (seq)
(seq (rand (len seq))))
(mac until (test body ...)
(while (no test) body ...))
(def before (x y seq (o i 0))
(with (xp (pos x seq i) yp (pos y seq i))
(and xp (or (no yp) (< xp yp)))))
(def orf fns
(fn args
((afn (fs)
(and fs (or (apply (car fs) args) (self (cdr fs)))))
fns)))
(def andf fns
(fn args
((afn (fs)
(if (no fs) t
(no (cdr fs)) (apply (car fs) args)
(and (apply (car fs) args) (self (cdr fs)))))
fns)))
(def atend (i s)
(> i (- (len s) 2)))
(def multiple (x y)
(is 0 (mod x y)))
(mac nor (args ...) (no (or args ...)))
(def compare (comparer scorer)
(fn (x y) (comparer (scorer x) (scorer y))))
(def only (f)
(fn args (if (car args) (apply f args))))
(mac conswhen (f x y)
(with (tf f tx x)
(if (tf tx) (cons tx y) y)))
(def retrieve (n f xs)
(if (no n) (keep f xs)
(or (<= n 0) (no xs)) nil
(f (car xs)) (cons (car xs) (retrieve (- n 1) f (cdr xs)))
(retrieve n f (cdr xs))))