Debian Codegraph @

examples/prism-arturo.html

raw symbols SCIP
1 <h2>Full example</h2>
2 <pre><code>; this is a comment
3 ; this is another comment
4
5 ;---------------------------------
6 ; VARIABLES & VALUES
7 ;---------------------------------
8
9 ; numbers
10 a1: 2
11 a2: 3.14
12 a3: to :complex [1 2.0] ; 1.0+2.0i
13
14 ; strings
15 c1: "this is a string"
16 c2: {
17 this is a multiline string
18 that is indentation-agnostic
19 }
20 c3: {:
21 this is
22 a verbatim
23 multiline string
24 which will remain exactly
25 as the original
26 :}
27
28 ; characters
29 ch: `c`
30
31 ; blocks/arrays
32 d: [1 2 3]
33
34 ; dictionaries
35 e: #[
36 name: "John"
37 surname: "Doe"
38 age: 34
39 likes: [pizza spaghetti]
40 ]
41
42 ; yes, functions are values too
43 f: function [x][
44 2 * x
45 ]
46
47 ; colors - right, you can directly define them as well!
48 g1: #red
49 g2: #0077BF
50
51 ; dates
52 h: now ; 2021-05-03T17:10:48+02:00
53
54 ; logical values
55 i1: true
56 i2: false
57 i3: maybe
58
59 ;---------------------------------
60 ; BASIC OPERATORS
61 ;---------------------------------
62
63 ; simple arithmetic
64 1 + 1 ; =&gt; 2
65 8 - 1 ; =&gt; 7
66 4.2 - 1.1 ; =&gt; 3.1
67 10 * 2 ; =&gt; 20
68 35 / 4 ; =&gt; 8
69 35 // 4 ; =&gt; 8.75
70 2 ^ 5 ; =&gt; 32
71 5 % 3 ; =&gt; 2
72
73 ; bitwise operators
74 and 3 5 ; =&gt; 1
75 or 3 5 ; =&gt; 7
76 xor 3 5 ; =&gt; 6
77
78 ; pre-defined constants
79 pi ; =&gt; 3.141592653589793
80 epsilon ; =&gt; 2.718281828459045
81 null ; =&gt; null
82 true ; =&gt; true
83 false ; =&gt; false
84
85 ;---------------------------------
86 ; COMPARISON OPERATORS
87 ;---------------------------------
88
89 ; equality
90 1 = 1 ; =&gt; true
91 2 = 1 ; =&gt; false
92
93 ; inequality
94 1 &lt;&gt; 1 ; =&gt; false
95 2 &lt;&gt; 1 ; =&gt; true
96
97 ; more comparisons
98 1 &lt; 10 ; =&gt; true
99 1 =&lt; 10 ; =&gt; true
100 10 =&lt; 10 ; =&gt; true
101 1 &gt; 10 ; =&gt; false
102 1 &gt;= 10 ; =&gt; false
103 11 &gt;= 10 ; =&gt; true
104
105 ;---------------------------------
106 ; CONDITIONALS
107 ;---------------------------------
108
109 ; logical operators
110 and? true true ; =&gt; true
111 and? true false ; =&gt; false
112 or? true false ; =&gt; true
113 or? false false ; =&gt; false
114
115 and? [1=2][2&lt;3] ; =&gt; false
116 ; (the second block will not be evaluated)
117
118 ; simple if statements
119 if 2 &gt; 1 [ print "yes!"] ; yes!
120 if 3 &lt;&gt; 2 -&gt; print "true!" ; true!
121
122 ; if/else statements
123 if? 2 &gt; 3 -&gt; print "2 is greater than 3"
124 else -&gt; print "2 is not greater than 3" ; 2 is not greater than 3
125
126 ; switch statements
127 switch 2 &gt; 3 -&gt; print "2 is greater than 3"
128 -&gt; print "2 is not greater than 3" ; 2 is not greater than 3
129
130 a: (2 &gt; 3)["yes"]["no"] ; a: "no"
131 a: (2 &gt; 3)? -&gt; "yes" -&gt; "no" ; a: "no" (exactly the same as above)
132
133 ; case/when statements
134 case [1]
135 when? [&gt;2] -&gt; print "1 is greater than 2. what?!"
136 when? [&lt;0] -&gt; print "1 is less than 0. nope..."
137 else -&gt; print "here we are!" ; here we are!
138
139 ;---------------------------------
140 ; LOOPS
141 ;---------------------------------
142
143 ; with `loop`
144 arr: [1 4 5 3]
145 loop arr 'x [
146 print ["x =" x]
147 ]
148 ; x = 1
149 ; x = 4
150 ; x = 5
151 ; x = 3
152
153 ; with loop and custom index
154 loop.with:'i arr 'x [
155 print ["item at position" i "=&gt;" x]
156 ]
157 ; item at position 0 =&gt; 1
158 ; item at position 1 =&gt; 4
159 ; item at position 2 =&gt; 5
160 ; item at position 3 =&gt; 3
161
162 ; using ranges
163 loop 1..3 'x -&gt; ; since it's a single statement
164 print x ; there's no need for [block] notation
165 ; we can wrap it up using the `-&gt;` syntactic sugar
166
167 loop `a`..`c` 'ch -&gt;
168 print ch
169 ; a
170 ; b
171 ; c
172
173 ; picking multiple items
174 loop 1..10 [x y] -&gt;
175 print ["x =" x ", y =" y]
176 ; x = 1 , y = 2
177 ; x = 3 , y = 4
178 ; x = 5 , y = 6
179 ; x = 7 , y = 8
180 ; x = 9 , y = 10
181
182 ; looping through a dictionary
183 dict: #[name: "John", surname: "Doe", age: 34]
184 loop dict [key value][
185 print [key "-&gt;" value]
186 ]
187 ; name -&gt; John
188 ; surname -&gt; Doe
189 ; age -&gt; 34
190
191 ; while loops
192 i: new 0
193 while [i&lt;3][
194 print ["i =" i]
195 inc 'i
196 ]
197 ; i = 0
198 ; i = 1
199 ; i = 2
200
201 ;---------------------------------
202 ; STRINGS
203 ;---------------------------------
204
205 ; case
206 a: "tHis Is a stRinG"
207 print upper a ; THIS IS A STRING
208 print lower a ; this is a string
209 print capitalize a ; THis Is a stRinG
210
211 ; concatenation
212 a: "Hello " ++ "World!" ; a: "Hello World!"
213
214 ; strings as an array
215 split "hello" ; =&gt; [h e l l o]
216 split.words "hello world" ; =&gt; [hello world]
217
218 print first "hello" ; h
219 print last "hello" ; o
220
221 ; conversion
222 to :string 123 ; =&gt; "123"
223 to :integer "123" ; =&gt; 123
224
225 ; joining strings together
226 join ["hello" "world"] ; =&gt; "helloworld"
227 join.with:"-" ["hello" "world"] ; =&gt; "hello-world"
228
229 ; string interpolation
230 x: 2
231 print ~"x = |x|" ; x = 2
232
233 ; interpolation with `print`
234 print ["x =" x] ; x = 2
235 ; (`print` works by calculating the given block
236 ; and joining the different values as strings
237 ; with a single space between them)
238
239 ; templates
240 print render.template {
241 &lt;||= switch x=2 [ ||&gt;
242 Yes, x = 2
243 &lt;||][||&gt;
244 No, x is not 2
245 &lt;||]||&gt;
246 } ; Yes, x = 2
247
248 ; matching
249 prefix? "hello" "he" ; =&gt; true
250 suffix? "hello" "he" ; =&gt; false
251
252 contains? "hello" "ll" ; =&gt; true
253 contains? "hello" "he" ; =&gt; true
254 contains? "hello" "x" ; =&gt; false
255
256 in? "ll" "hello" ; =&gt; true
257 in? "x" "hello" ; =&gt; false
258
259 ;---------------------------------
260 ; BLOCKS
261 ;---------------------------------
262
263 ; calculate a block
264 arr: [1 1+1 1+1+1]
265 @arr ; =&gt; [1 2 3]
266
267 ; execute a block
268 sth: [print "Hello world"] ; this is perfectly valid,
269 ; could contain *anything*
270 ; and will not be executed...
271
272 do sth ; Hello world
273 ; (...until we tell it to)
274
275 ; array indexing
276 arr: ["zero" "one" "two" "three"]
277 print first arr ; zero
278 print arr\0 ; zero
279 print last arr ; three
280 print arr\3 ; three
281
282 x: 2
283 print get arr x ; two
284 print arr\[x] ; two
285
286 ; setting an array element
287 arr\0: "nada"
288 set arr 2 "dos"
289 print arr ; nada one dos three
290
291 ; adding elements to an array
292 arr: new []
293 'arr ++ "one"
294 'arr ++ "two"
295 print arr ; one two
296
297 ; remove elements from an array
298 arr: new ["one" "two" "three" "four"]
299 'arr -- "two" ; arr: ["one" "three" "four"]
300 remove 'arr .index 0 ; arr: ["three" "four"]
301
302 ; getting the size of an array
303 arr: ["one" 2 "three" 4]
304 print size arr ; 4
305
306 ; getting a slice of an array
307 print slice ["one" "two" "three" "four"] 0 1 ; one two
308
309 ; check if array contains a specific element
310 print contains? arr "one" ; true
311 print contains? arr "five" ; false
312
313 ; sorting array
314 arr: [1 5 3 2 4]
315 sort arr ; =&gt; [1 2 3 4 5]
316 sort.descending arr ; =&gt; [5 4 3 2 1]
317
318 ; mapping values
319 map 1..10 [x][2*x] ; =&gt; [2 4 6 8 10 12 14 16 18 20]
320 map 1..10 'x -&gt; 2*x ; same as above
321 map 1..10 =&gt; [2*&] ; same as above
322 map 1..10 =&gt; [2*] ; same as above
323
324 ; selecting/filtering array values
325 select 1..10 [x][odd? x] ; =&gt; [1 3 5 7 9]
326 select 1..10 =&gt; odd? ; same as above
327
328 filter 1..10 =&gt; odd? ; =&gt; [2 4 6 8 10]
329 ; (now, we leave out all odd numbers -
330 ; while select keeps them)
331
332 ; misc operations
333 arr: ["one" 2 "three" 4]
334 reverse arr ; =&gt; [4 "three" 2 "one"]
335 shuffle arr ; =&gt; [2 4 "three" "one"]
336 unique [1 2 3 2 3 1] ; =&gt; [1 2 3]
337 permutate [1 2 3] ; =&gt; [[1 2 3] [1 3 2] [3 1 2] [2 1 3] [2 3 1] [3 2 1]]
338 take 1..10 3 ; =&gt; [1 2 3]
339 repeat [1 2] 3 ; =&gt; [1 2 1 2 1 2]
340
341 ;---------------------------------
342 ; FUNCTIONS
343 ;---------------------------------
344
345 ; declaring a function
346 f: function [x][ 2*x ]
347 f: function [x]-&gt; 2*x ; same as above
348 f: $[x]-&gt;2*x ; same as above (only using the `$` alias
349 ; for the `function`... function)
350
351 ; calling a function
352 f 10 ; =&gt; 20
353
354 ; returning a value
355 g: function [x][
356 if x &lt; 2 -&gt; return 0
357
358 res: 0
359 loop 0..x 'z [
360 res: res + z
361 ]
362 return res
363 ]
364
365 ;---------------------------------
366 ; CUSTOM TYPES
367 ;---------------------------------
368
369 ; defining a custom type
370 define :person [ ; define a new custom type "Person"
371 name ; with fields: name, surname, age
372 surname
373 age
374 ][
375 ; with custom post-construction initializer
376 init: [
377 this\name: capitalize this\name
378 ]
379
380 ; custom print function
381 print: [
382 render "NAME: |this\name|, SURNAME: |this\surname|, AGE: |this\age|"
383 ]
384
385 ; custom comparison operator
386 compare: 'age
387 ]
388
389 ; create a method for our custom type
390 sayHello: function [this][
391 ensure -&gt; is? :person this
392
393 print ["Hello" this\name]
394 ]
395
396 ; create new objects of our custom type
397 a: to :person ["John" "Doe" 34] ; let's create 2 "Person"s
398 b: to :person ["jane" "Doe" 33] ; and another one
399
400 ; call pseudo-inner method
401 sayHello a ; Hello John
402 sayHello b ; Hello Jane
403
404 ; access object fields
405 print ["The first person's name is:" a\name] ; The first person's name is: John
406 print ["The second person's name is:" b\name] ; The second person's name is: Jane
407
408 ; changing object fields
409 a\name: "Bob"
410 sayHello a ; Hello Bob
411
412 ; verifying object type
413 print type a ; :person
414 print is? :person a ; true
415
416 ; printing objects
417 print a ; NAME: John, SURNAME: Doe, AGE: 34
418
419 ; sorting user objects (using custom comparator)
420 sort @[a b] ; Jane..., John...
421 sort.descending @[a b] ; John..., Jane...</code></pre>