-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathweb-graph.rkt
65 lines (54 loc) · 1.93 KB
/
web-graph.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
#lang racket/base
(require racket/contract/base)
(provide
(contract-out
[empty-web-graph web-graph?]
[web-graph (-> web-link? ... web-graph?)]
[web-graph? (-> any/c boolean?)]))
(require racket/struct
rebellion/type/tuple
rebellion/web-link)
(module+ test
(require (submod "..")
racket/format
racket/port
racket/pretty
rackunit))
;@------------------------------------------------------------------------------
(define (property-maker descriptor)
(define name (tuple-type-name (tuple-descriptor-type descriptor)))
(define accessor (tuple-descriptor-accessor descriptor))
(define equal+hash (default-tuple-equal+hash descriptor))
(define custom-write
(make-constructor-style-printer
(λ (_) name)
(λ (this) (accessor this 0))))
(list (cons prop:equal+hash equal+hash)
(cons prop:custom-write custom-write)))
(define-tuple-type web-graph (links)
#:property-maker property-maker
#:omit-root-binding)
(define (web-graph . links)
(constructor:web-graph links))
(define empty-web-graph (web-graph))
(module+ test
(test-case "custom-write"
(define graph
(web-graph
(web-link "http://example.org" 'stylesheet "/styles.css")
(web-link "http://example.org" 'stylesheet "/fonts.css")
(web-link "http://example.org" 'search "/opensearch.xml")
(web-link "http://example.org" 'privacy-policy "/privacy-policy")))
(define (~pretty v #:columns columns)
(parameterize ([pretty-print-columns columns])
(with-output-to-string
(λ () (pretty-print v)))))
(check-equal? (~pretty graph #:columns 80)
#<<END
(web-graph
(web-link "http://example.org" 'stylesheet "/styles.css")
(web-link "http://example.org" 'stylesheet "/fonts.css")
(web-link "http://example.org" 'search "/opensearch.xml")
(web-link "http://example.org" 'privacy-policy "/privacy-policy"))
END
)))