-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.scm
159 lines (144 loc) · 4.86 KB
/
utils.scm
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
(library
(melt utils)
(export get-absolute-path
flatten
decompose-path-name
compose-path-name
identity
hash-ref
assq-ref
make-alist
alist->hash-table
alist-delete
alist?
alist-cons
until)
(import (scheme)
(melt structure))
(define (make-alist keys values)
(map cons keys values))
(define (flatten x)
(cond ((null? x) '())
((pair? x) (append (flatten (car x)) (flatten (cdr x))))
(else (list x))))
;; just like echo, return what it accept!!
(define identity
(lambda (obj)
obj))
;; return the absolute path of the file
(define (get-absolute-path file-name)
(if (string? file-name)
(if (path-absolute? file-name)
file-name
(string-append (current-directory) "/" file-name))
(error file-name "must be string!")))
;; decompose a string
;; example
;; "/usr/lib/share" ==> ("usr" "lib" "share")
(define (decompose-path-name path-name)
(define (generate-list path)
(if (eq? path "")
'()
(if (eq? "" (path-first path))
(cons (path-rest path)
'())
(cons (path-first path)
(generate-list (path-rest path))))))
(if (string? path-name)
(if (eq? "" path-name)
'()
(let ((name (if (path-absolute? path-name)
(path-rest path-name)
path-name)))
(generate-list name)))
(error path-name "must be string!")))
;; define string join
(define (string-join str-list seperator command)
(define (join-seperator str seperator command)
(cond
[(eq? command 'prefix)
(string-append seperator str)]
[(eq? command 'suffix)
(string-append str seperator)]))
(define (verify-string str-list)
(if (eq? str-list '())
#f
(if (string? (car str-list))
(if (eq? '() (cdr str-list))
#t
(verify-string (cdr str-list)))
#f)))
(cond
[(atom? str-list)
(if (eq? '() str-list)
(error str-list "Empty list!")
(error str-list "Must be a list!"))]
[(verify-string str-list)
(if (or (string? seperator)
(char? seperator))
(let ((sep (if (char? seperator)
(string seperator)
seperator)))
(cond
[(or (eq? command 'prefix)
(eq? command 'suffix))
(let* ((number (length str-list))
(new-list (map join-seperator str-list (make-list number sep) (make-list number command))))
(apply string-append new-list))]
[(eq? command 'middle)
(let* ((number (- (length str-list) 1))
(new-list (map join-seperator (cdr str-list) (make-list number sep) (make-list number 'suffix))))
(apply string-append (cons (car str-list)
new-list)))]
[else (error command "isn't a proper command!")]))
(error seperator "isn't a string or char!"))]))
;; components is a list of strings
;; like ("hello" "nice" "good") => /hello/nice/good
(define (compose-path-name str-list)
(string-join str-list "/" 'prefix))
;; until the test is satified, end the iterate
(define-syntax until
(syntax-rules ()
[(_ test forms ...)
(do ()
(test)
forms ...)]))
(define alist->hash-table
(lambda (alist)
(let ((ht (make-eqv-hashtable)))
(do ((iterate-alist alist (cdr iterate-alist))
(pair-list (car alist) (car iterate-alist)))
((eq? iterate-alist '()) ht)
(hashtable-set! ht
(car pair-list)
(cdr pair-list))))))
(define hash-ref
(case-lambda
[(hashtable key)
(hashtable-ref hashtable key #f)]
[(hashtable key default)
(hashtable-ref hashtable key default)]))
;; return the left alist which dosn't contain the symbal
(define alist-delete
(lambda (symbal alist)
(if (assq symbal alist)
(remove (assq symbal alist) alist)
(error symbal "symbol is not in alist! : in alist-delete utils.scm \n"))))
(define assq-ref
(lambda (symbol alist)
(cdr (assq symbol alist))))
(define alist-cons
(lambda (key obj alist)
(cons (cons key obj)
alist)))
;; take '() as an alist
(define (alist? arg)
(call/cc
(lambda (cc)
(if (and (atom? arg) (not (null? arg)))
(cc #f))
(do ((arg-list arg (cdr arg-list)))
((null? arg-list) #t)
(if (not (pair? (car arg-list)))
(cc #f))))))
)