-
Notifications
You must be signed in to change notification settings - Fork 0
/
GettingStarted.tiny
636 lines (498 loc) · 15.1 KB
/
GettingStarted.tiny
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
#####################################################
#
# Simple examples using the Tiny programming language
#
#####################################################
# Comments start with '#"
import 'math'
# Functions require parens around their arguments
println("Hello world, welcome to Tiny!");
# Basic arithmetic
println("2+2 is", 2+2);
println(2*(3+4)/3);
println("adding " + "strings " + "together");
# Array literals use '[' and ']'
[1, 2, 3, 4, 5];
[1, 2, [3, 4], 5]; # => nested arrays
# ranges use the range function or the .. operator
range(1,5); # => [1, 2, 3, 4, 5]
range(1,5,2); # => [1,3,5]
[1..5]; # => [1, 2, 3, 4, 5]
[1,[2..3],4]; # => [1 [2, 3], 4]
1..5..2; # => [1, 3, 5]
# objects/dictionary literals
{a : 1, b : 2, c : 3};
{a : 1, b: {c: 2, d:3}, e: 4}; # => nested dictionary
# Define a simple function to print out separator lines
fn sep ()
{
println("");
println("================================");
println("");
}
# and call it, with zero arguments
sep();
#
# Dump out the predefined functions and variables
#
println("Here is the help function 'functions()'");
functions();
functions("string") # print functions containing "string"
sep();
###############################################
#
# Simple factorial function based on reduce()
#
fn fact n -> range(1,n).reduce{x, y -> x * y}
println("fact(5) is " + fact(5));
# Apply it to a a range of numbers
range(2,20, 3).foreach{n -> println("fact of {0} is {1}", n, fact(n))}
# Functions can have type constraints and initializers
fn fact [<int>] n = 5 -> range(1,n).reduce{x, y -> x * y}
println("fact(5) is " + fact());
# Multiline functions use '{' and '}' instead of '->'
fn foo x=1 y=2 {
println("In function foo");
println("foo() = ", x+y);
}
foo()
sep();
###############################################
#
# Simple recursive fib function using the 'match' statement
#
fn fib n -> match n
| 1 -> 1
| 2 -> 1
| -> fib(n-1) + fib(n-2)
println("fibs: " + range(2,10).map{n -> fib(n)});
sep();
###############################################
#
# Compute primes using the Sieve of Eratosthenes
#
# Function to get all the primes up to the specified max
fn getPrimes max {
# Utility to mark all of the multiples of this number
fn mark num, arr, max ->
[num*2 .. max .. num]
.foreach{n -> arr[n] = false}
# for all primes, mark the multiples of that prime
fn markall arr, max ->
[2 .. max].foreach{ mindex ->
if (mindex * 2 < max && arr[mindex]) {
mark(mindex, arr, max)
}
}
primes = [0 .. max].map{true};
markall(primes, max);
[2 .. max].where{n -> primes[n]};
}
primesFound = getPrimes(100);
println("Found " + getlength(primesFound) + " primes:");
println(asstring(primesFound));
sep();
###############################################
#
# Recursively reverse a list; uses multi assignment
#
fn rev list ->
if (list) {
hd::list = list;
rev(list) + hd;
}
else {
[];
}
println("Defined rev(list) function");
list = [1, 2, 3, 4, 5, 6];
println("Reversing a list:");
println("In: " + list);
println("Out: " + rev(list));
if (rev(list) != list.reverse()) {
error("The reversed lists should be the same");
}
sep();
###############################################
#
# Towers of Hanoi
#
fn dohanoi n, to, from, using ->
if (n > 0) {
n -= 1;
dohanoi(n, using, from, to);
println("move " + from + " ==> " + to);
dohanoi(n, to, using, from);
}
disks = 4;
println("Doing tower of hanoi with " + disks + " disks");
dohanoi(disks, 3, 1, 2);
sep();
###############################################
# read the file and sum the length of each line to get total number of characters
println("Computing the number of chars in 'snake.tiny'; please wait...")
chars = readfile("./snake.tiny").map{l -> l.length}.sum();
println("Number of characters in 'snake.tiny' is {0}.", chars);
println("There are {0} functions defined in that file.",
readfile("./snake.tiny", "function").count);
sep();
###############################################
#
# recursive list summing function with pattern matching
#
fn sumlist list sumSoFar=0 ->
match list
| h::t -> sumlist(t, sumSoFar+h)
| -> sumSoFar
println("Sum of {0} is {1}", [1..10], [1..10] |> sumlist)
sep();
###############################################
#
# Print a list using pattern matching
#
fn prlist list ->
while (true) {
match list
| a::list -> println(a)
| -> break
}
prlist([1,2,4..6,[10,11],14,[0..6..2],20])
sep();
###############################################
println("Extracting words from a file");
words = readfile("snake.tiny")
.flatmap{l -> l /~ "\s+"}
.sort();
println("Total number of words in the doc is {0}", getlength(words));
println("Putting them into a hashtable to see which ones are unique.");
wordhash = {};
foreach (w in words) {wordhash[w] = true};
println("Number of unique words in the document is: {0}", keys(wordhash).count);
sep();
###############################################
# Implement a 'quicksort' with patterns
fn qsort lst ->
match lst
| [] -> []
| pivot :: tl ->
match tl.split{ it < pivot }
| smaller :: larger :: _ ->
qsort(smaller) + pivot + qsort(larger);
println("Try out the qsort function.");
20 |> getrandom |> qsort |> join ', ' |> println
println("Done qsort.")
sep();
################################################
# an alternate implementation of qsort using
# patterns and function sets
#
def qsortfs [] -> []
def qsortfs p::[] -> [p]
def qsortfs p::tail -> tail.split{it < p} ~ s::l then qsortfs(s) + p + qsortfs(l)
println("Try out the alternate qsort function.");
20 |> getrandom |> qsort |> join ', ' |> println
println("Done qsort.")
sep();
###############################################
#
# Find the sentence in a paragraph that contains a specific word.
text = "
Historically, the world of data and the world of objects
have not been well integrated. Programmers work in C# or Visual Basic
and also in SQL or XQuery. On the one side are concepts such as classes,
objects, fields, inheritance, and dotNET Framework APIs. On the other side
are tables, columns, rows, nodes, and separate languages for dealing with
them. Data types often require translation between the two worlds; there are
different standard functions. Because the object world has no notion of query, a
query can only be represented as a string without compile-time type checking or
IntelliSense support in the IDE. Transferring data from SQL tables or XML trees to
objects in memory is often tedious and error-prone.
"
println("Setting up wordsToMatch hash")
wordsToMatch = {
Historically: false
data: false
integrated: false
};
println("Defining initHash function")
fn inithash -> foreach (k in keys(wordsToMatch)) { wordsToMatch[k] = false; }
println("Words to match")
println(wordstomatch)
fn allTrue -> values(wordstomatch).reduce{x, y -> x && y }
fn setIfContained word -> if (wordsToMatch :> word) { wordsToMatch[word] = true }
# Remove commas then split the text into sentences
sentences = text -~ ',' -~ ['\s+', ' '] /~ '[.?!]+'
println("Sentences to process:")
println("------------------------")
printlist(sentences);
# now do the matching...
matched = false
sentences.
foreach{sentence ->
initHash();
split(sentence, " +").
foreach{word -> setIfContained(word)};
if (allTrue()) {
println("------------------------");
println("MATCHING SENTENCE: " + sentence);
matched = true
};
};
if (not(matched)) { error("!!!!!!!!!!!!!!!!!!!!! Matching sentence was not found!") }
sep();
#############################################################
println("Testing 'objecty' features");
# create an "object"
h = {
# value member
val : 0;
# method eith no args
incr : {this.val += 11};
# method with one arg
Add : {n -> this.val += n}
};
# print it
println(h);
# set the property then print it
h.val = 1;
println(h);
# call the increment method then print it
h.Incr();
println(h);
# call the add method then print it
h.add(10);
println(h);
println 'Done objecty features'
sep();
#################################################
#
# Euclid's gcd function
#
fn gcd (a, b) {
while (a != b) {
if (a > b) {
a = a - b
}
else {
b = b - a
}
}
a
}
println("gdc of 165 and 75 is " + gcd(165, 75))
sep();
#################################################
#
println("sin waves");
amplitude = 40;
[0 .. 6 .. 0.1]
.map{val -> math.floor(amplitude * math.sin(val)) }
.foreach{println(" " * (amplitude + it) + "*") }
sep();
#################################################
# Test out the assignment operations
println("Test out the assignment operations");
a = 5;
a += 1;
if (a != 6) { error("+= failed; a should be 6") }
a = 6;
a -= 2;
if (a != 4) { error("-= failed; a should be 4") }
a = 8;
a *= 3;
if (a != 24) { error("*= failed; a should be 24") }
a = 64;
a /= 8;;
if (a != 8) { error("/= failed; a should be 8") }
println("Done testing the assignment operations");
sep();
println("\nTesting match statement")
println("Matching on types")
fn testmatch x -> match x
| [<int>] -> "int"
| [<string>] -> "string"
| [<IDictionary>] -> "hash"
|-> "unknown"
if ((result = testmatch(1)) != "int") {
error("testmatch: got " + result + " instead of 'int'" )
}
if ((result = testmatch("abc")) != "string") {
error("testmatch: got " + result + " instead of 'string'" )
}
if ((result = testmatch({})) != "hash") {
error("testmatch: got " + result + " instead of 'hash'" )
}
if ((result = testmatch(getdate())) != "unknown") {
error("testmatch: got " + result + " instead of 'unknown'")
}
println("Matching with regex")
fn testmatch2 x -> match x
| regex('^a') -> "Eh"
| regex('^b') -> {"Bee" }
| regex('^c') -> "See"
| -> "default"
if((result = testmatch2('abc')) != 'Eh') {
error("testmatch2: got " + result + " instead of 'Eh'" )
}
if((result = testmatch2('bca')) != 'Bee') {
error("testmatch2: got " + result + " instead of 'Bee'" )
}
if((result = testmatch2('cab')) != 'See') {
error("testmatch2: got " + result + " instead of 'See'" )
}
println("Matching with regex literals")
fn testmatch2 x -> match x
| r/^a/ -> "Eh"
| r/^b/ -> {"Bee" }
| r/^c/ -> "See"
| -> "default"
if((result = testmatch2('abc')) != 'Eh') {
error("testmatch2: got " + result + " instead of 'Eh'" )
}
if((result = testmatch2('bca')) != 'Bee') {
error("testmatch2: got " + result + " instead of 'Bee'" )
}
if((result = testmatch2('cab')) != 'See') {
error("testmatch2: got " + result + " instead of 'See'" )
}
println("Testing match in a loop")
# Test match in a loop
range(1,10).foreach{
match it
| {it % 2 == 0} -> println("two " + it)
| {it % 3 == 0} -> println("three " + it)
| -> println("neither " + it)}
println("\nGetting the names of functions and variables " +
"defined in this file.\n")
time {
f = []
v = []
readfile("snake.tiny")
.foreach{
match it
| r/ *fn +([a-z0-9]+)/ -> f += matches[1]
| r/ *([a-z0-9]+) *=[^=]/ -> v += matches[1]
}
println("Functions: " + f.Distinct())
println("Variables: " + v.Distinct())
}
println("\nDone testing match statement.")
sep();
################################################
#
# Recursive length computation
#
fn len list ->
match list
| [] -> 0
| _::tail -> 1 + len(tail)
println("Length should be 10: {0}", len([1..10]))
sep();
################################################
#
# Recursive implementation of map()
#
fn myMap list f ->
match list
| hd :: tl -> f(hd) :+ myMap(tl, f)
| [] -> []
result = [1, 2, 3] |> myMap {n -> n * n}
println("Result of myMap should be [1, 4, 9]: {0}", result)
result != [1, 4, 9] then error('myMap: the results didn''t match')
# map() implemented with function sets and patterns
def mmap [] f -> []
def mmap x::xs f -> f(x) :+ mmap(xs, f)
println(mmap([1..10], {x -> x * 2}).ToString())
sep();
################################################
result = [1..100]
|> matchall('^2') # get all the numbers that start with '2'
|> sum() # sum the numbers
|> math.sqrt # get the sqrt of the sum
|> math.floor # convert it into an int.
println("Testing the pipe |> operator; should be 15: {0}", result)
sep();
################################################
#
# Various method chaining examples
#
println("Get the ten largest files in the current directory:\n")
shell('gci -file')
.sortdescending{it.length}
.take(10)
.map{n -> format("{0} {1}", asstring(n.length).padright(10), n.name)}
.print()
sep();
println("\nGet the ten processes with the largest working set:\n")
shell('gps')
.sortdescending{it.ws}
.take(10)
.map { asstring(it.ws).padright(10) + it.name }
.print()
sep()
println("Get the 10 most common words in a file")
time {
readfile('snake.tiny')
.replace('[^a-z ]+') # get rid of punctuation, etc.
.split('\s+') # split the lines into words
.SkipNullOrEmpty() # remove the empty elements
.tohash() # Turn it into a hashtable
.getenumerator() # Get the enumerator
.aslist() # and turn it into a list.
.sortdescending{it.value} # sort the list in descending frequence
.take(10) # take the top 10 most common
.foreach{println(it.key.padleft(14) + " " + it.value)}
}
println("\nAnd again with a slightly different approach using operators")
time {
(readtext('snake.tiny') -~ '[^a-z ]' /~ '\s+')
.tohash()
.getenumerator()
.aslist()
.sortdescending{ it.value } # sort the list in descending frequence
.take(10)
.outhost()
}
sep()
println("\nAnd again with a slightly different approach using the pipe ('|>') operator and functions")
time {
'snake.tiny'
|> readtext
|> replace(r/[^a-z ]+/)
|> split(r/ +/)
|> toHash()
|> sortdescending{ it.value }
|> First(10)
|> println()
}
sep()
println("\nget most common extensions in the current directory\n")
time {
shell("gci -file")
.map{it.extension}
.tohash()
.getenumerator()
.aslist()
.sortdescending{it.value}
.outhost()
}
println("\nGet the 10 highest working set processes again using the pipe operator.")
time {
shell("gps")
|> sortdescending('WS')
|> first(10)
|> printlist()
}
sep()
println("Get the 10 most common letters in 'snake.tiny' file.")
time {
readtext('snake.tiny').tochararray()
|> tohash()
|> sortdescending{it.value}
|> first(10)
|> outhost()
}
sep()
println("All done - bye bye!");
# Return a list to show that scripts can return values
[1,2,3,4]