-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsample.crn
208 lines (178 loc) · 5.29 KB
/
sample.crn
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
;;;
; MIT License
;
; Copyright (c) 2020 Pablo Blanco Celdrán
;
; Permission is hereby granted, free of charge, to any person obtaining a copy
; of this software and associated documentation files (the "Software"), to deal
; in the Software without restriction, including without limitation the rights
; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
; copies of the Software, and to permit persons to whom the Software is
; furnished to do so, subject to the following conditions:
;
; The above copyright notice and this permission notice shall be included in all
; copies or substantial portions of the Software.
;
; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
; SOFTWARE.
;;;
(module sample
[:import lib2 :from "samples/lib"])
; Example of import:
(import lib :from "samples/lib")
; Import can be destructured if using a binding list.
(import [ sum ] :from "samples/lib")
; Compose functions (Not the same as thread-thru!)
(let [a (fn [x] (* x 2))
b (fn [x] (+ x 5))]
(let [ab (>>= a b)]
(assert (= (b (a 3)) (ab 3))))
(def ab
(>>= a b)))
(println! "(b (a 3)) = " (ab 3))
(println! "Hello world!")
(println! "oh-man ->" (object/get "oh-man" lib))
(println! "(sum 2 2) ->" (sum 2 2))
; Simple matching:
(let [val :b]
(println! "when " val " = "
(when val
:a "Is A!"
:b "Is B!"
:c "Is C!"
_ "Unexpected case.")))
; Complex matching when using the list literal
(let [val ["a" 5 {:yes "you"}]]
(println! "Example: "
(when val
["a" _ _] "Was A!"
[_ 5 _] "Was 5!"
_ "No match...")))
; Repeated _ cases will be merged into a single else branch, but a warning will
; be produced also.
(when 1
:a (println! "a")
_ (println! "_")
_ (println! "_")
_ (println! "_"))
; Other uses of the list literal: as a Tuple type.
(let [tpl ["a" :b 5]]
(let [[a b c] tpl] ; Ease of destructure in let bindings, for example.
(println! tpl)
(println! "a: " (:0 tpl)) ; Table-like element access
(println! "a: " a)
(println! "b: " b)
(println! "c: " c)))
; Tail recursion fibbonacci
(def fib
(fn [n a? b?]
(let [a (or? a? 0)
b (or? b? 1)]
(if (= n 0) a
(if (= n 1) b
(#' (- n 1) b (+ a b)))))))
; Using it like:
(let [n 9]
(println! (str "fib(" n ") = " (fib 9))))
; For loop, for side-effects (Sometimes you need 'em).
; Numeric for like this one is optimized away as a normal for
(for [i (range 0 10)]
(println! (str "i = " i)))
; Other sources of iterable components are transformed to pairs:
(for [v [1 2 3 4]]
(println! (str "v = " v)))
; If you put two names, the for will always run in pairs:
; Being the second the "index" or the "key" depending on what you're iterating.
(for [v i (range 10 20)]
(println! (str i " = " v)))
; For security reasons, externs are considered impure by default.
(declare myFunc)
(declare :pure mySum)
; Impure functions:
(let [state (atom 0)]
(defn! print-state []
(println! "State: " (atom/get state)))
(defn! count []
(atom/reset! state
(+ (atom/get state) 1))))
(print-state)
(count)
(print-state)
(count)
(print-state)
(let [file (file/open "samples/meta.lua" "w")]
(if (some? file)
(do
(file/write file "print('Hello there!')")
(file/close file))
(println! "Could not open file for writing!")))
(def tester true)
(if tester
(+ 2 2)
(println! "nooo")
(- 1 1))
(if (= tester true)
(println! "Simple dimple"))
; Comparison operators
(def simple-eq (= 1 2))
(def complex-eq (= 1 2 3 4))
(def complex-lt (> 1 2 3 4))
(def complex-gt (< 1 2 3 4))
(def complex-gteq (>= 1 2 3 4))
; Logic operators
(def all-or (or true false false true))
(def all-and (and true false false true))
; "not" has 1-arity, so the excess of arguments produces error.
(def simple-not (not true))
; Other stuff
(def more-things
{ :nor (nor true false true)
:nand (nand true false true)
:xor (xor true false) ; Plain old XOR
:xor-nary (xor true false true false true false) ; Xor is n-ary in fact.
})
(println! "nor =" (:nor more-things)) ; Shorthand!
(println! "nand =" (table/get :nand more-things)) ; Same thing!
(println! "xor =" (:xor more-things))
(println! "xor-nary =" (:xor-nary more-things))
(defn double [x]
(* x 2))
; Lists
(def doubles
(list/map [1 2 3 4] double))
(def triples
(list/map [1 2 3 4]
(fn [x]
(* x 3))))
(if tester
(+ 2 3)
(* 5 5))
; Do blocks!
(if tester
(do
(println! "Thing one")
(println! "Thing two..."))
(println! "This is part of the 'else'"))
(defn something [] unit)
; Somewhat threading macros
(println! "Result:"
(-> 2
(+ 4)
(* 8)))
(<- 2
(+ 4)
(* 8)
(println! "Result:"))
; Try catch!
(try (something)
(catch [err]
(println! "Error! " err)))
; For sake of optimization, instead of reducing functions, arithmetics are
; expanded to their binary operator counterparts.
(println! "Arithmetic expansion! "
(+ (- 2 5 6) 1 2 3 4 5 (* 1 2 6 8)))