-
Notifications
You must be signed in to change notification settings - Fork 3
/
clojure.sls
110 lines (94 loc) · 2.77 KB
/
clojure.sls
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
#!r6rs
;; A bunch of macros stolen from /inspired by clojure
(library (ijputils clojure)
(export if-let
when-let
->
->>
doto)
(import (rnrs)
(spells match))
;; if-let & when-let are like the similar forms in clojure
(define-syntax if-let
(syntax-rules ()
((if-let (binding predicate) consequent alternative)
(let ((val predicate))
(if val
(match val [binding consequent]) ; consequent
alternative)))
((if-let (binding predicate) consequent)
(let ((val predicate))
(if val
(match val [binding consequent]) ; consequent
)))))
(define-syntax when-let
(syntax-rules ()
((when-let (binding predicate) body rest ...)
(let ((val predicate))
(when val
;; body
;; rest ...
(match val
[binding body rest ...]))))))
;; see http://clojure.github.com/clojure/clojure.core-api.html#clojure.core/-%3e
;; ->
;; macro
;; Usage: (-> x)
;; (-> x form)
;; (-> x form & more)
;; Threads the expr through the forms. Inserts x as the
;; second item in the first form, making a list of it if it is not a
;; list already. If there are more forms, inserts the first form as the
;; second item in second form, etc.
;; Added in Clojure version 1.0
(define-syntax ->
(lambda (stx)
(define (combine x form)
(syntax-case form ()
[(proc args ...)
#`(proc #,x args ...)]
[id
(identifier? #'id)
#`(id #,x)]
[else
(syntax-violation '-> "Invalid form in ->" form)]))
(syntax-case stx ()
[(-> x) #'x]
[(-> x form forms ...)
(let (;(form-list (syntax->list #'(form forms ...)))
(form-list #'(form forms ...)))
(fold-left combine #'x form-list))])))
;; ->>
;; macro
;; Usage: (->> x form)
;; (->> x form & more)
;; Threads the expr through the forms. Inserts x as the
;; last item in the first form, making a list of it if it is not a
;; list already. If there are more forms, inserts the first form as the
;; last item in second form, etc.
;; Added in Clojure version 1.1
(define-syntax ->>
(lambda (stx)
(define (combine x form)
(syntax-case form ()
[(proc args ...)
#`(proc args ... #,x)]
[id
(identifier? #'id)
#`(id #,x)]
[else
(syntax-violation '-> "Invalid form in ->" form)]))
(syntax-case stx ()
[(->> x) #'x]
[(->> x form forms ...)
(let (;(form-list (syntax->list #'(form forms ...)))
(form-list #'(form forms ...)))
(fold-left combine #'x form-list))])))
(define-syntax doto
(syntax-rules ()
((doto this (fun args ...) ...)
(let ((result this))
(fun result args ...)
...
result))))
)