-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathcore.clj
More file actions
88 lines (66 loc) · 1.75 KB
/
core.clj
File metadata and controls
88 lines (66 loc) · 1.75 KB
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
(def *flush-on-newline* true)
(def *print-readably* true)
(defmacro when [test & body]
(list 'if test (cons 'do body)))
(def list (fn [& ls] ls))
(defmacro defn [name args & body]
(list (quote def) name
(list (quote fn) args
(concat (list (quote do)) body))))
(defn apply [f args]
(lexical-eval (concat (list f) args)))
(defn newline
[]
(system-newline))
(defn flush
[]
(flush-stdout))
(defn pr [& more]
(print-string (apply str more)))
(defn prn [& more]
(apply pr more)
(newline)
(when *flush-on-newline*
(flush)
nil))
(defn print [& more]
(apply pr more))
(defn println [& more]
(apply prn more))
(defn inc [x]
(+ x 1))
(defn dec [x]
(- x 1))
(defmacro time [expr]
(list (quote let) [(quote start) (quote (System/nanoTime)) (quote ret) expr]
(quote (do
(println (str "Elapsed time: " (_slash_ (- (System/nanoTime) start) 1000000.0) " msecs"))
ret))))
(defn slurp [f & opts]
(rust-slurp f opts))
"basic operations on collections"
(defn rest [x]
(more x))
(defn next [x]
(let [result (rest x)]
(if (= '() result)
nil
result)))
(defn ffirst [x]
(first (first x)))
; proof of concept: cond
; cond requires exceptions, temporary work around
; should use special form
(defmacro throw
[exception-form]
(println (str (first exception-form)) ": " (second exception-form)))
; proof of concept: cond as expressed (almost) in Clojure
(defmacro cond
[& clauses]
(when clauses
(list 'if (first clauses)
(if (next clauses)
(second clauses)
(throw (IllegalArgumentException
"cond requires an even number of forms")))
(cons 'clojure.core/cond (next (next clauses))))))