aboutsummaryrefslogtreecommitdiff
path: root/test/arrays.tm
blob: de1771b7ffe27b2d5be690c3b1d5d92bdcc20e76 (plain)
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
func main():
	if yes:
		>> [:Num32]
		= [] : [Num32]

	if yes:
		>> arr := [10, 20, 30]
		= [10, 20, 30]

		>> arr[1]
		= 10
		>> arr[-1]
		= 30

		>> #arr
		= 3

		sum := 0
		for x in arr:
			sum += x
		>> sum
		= 60

		str := ""
		for i,x in arr:
			str ++= "({i},{x})"
		>> str
		= "(1,10)(2,20)(3,30)"

	if yes:
		>> arr := [10, 20] ++ [30, 40]
		= [10, 20, 30, 40]

		>> arr ++= [50, 60]
		>> arr
		= [10, 20, 30, 40, 50, 60]

		>> arr ++= 70
		>> arr
		= [10, 20, 30, 40, 50, 60, 70]

	if yes:
		>> arr := [10, 20]
		>> copy := arr
		>> arr ++= 30
		>> arr
		= [10, 20, 30]
		>> copy
		= [10, 20]

	if yes:
		>> [10*i for i in 5]
		= [10, 20, 30, 40, 50]

	>> [i*10 for i in 5]
	= [10, 20, 30, 40, 50]

	>> [i*10 for i in 5 if i mod 2 != 0]
	= [10, 30, 50]

	>> [x for x in y if x > 1 for y in [3, 4, 5] if y < 5]
	= [2, 3, 2, 3, 4]

	if yes:
		>> arr := @[10, 20]
		>> copy := arr[]
		>> arr:insert(30)
		>> arr
		= @[10, 20, 30]
		>> copy
		= [10, 20]

	if yes:
		>> arr := [10, 20, 30]
		>> arr:reversed()
		= [30, 20, 10]

	if yes:
		>> nums := [10, -20, 30]
		// Sorted function doesn't mutate original:
		>> nums:sorted()
		= [-20, 10, 30]
		>> nums
		= [10, -20, 30]
		// Sort function does mutate in place:
		>> nums:sort()
		>> nums
		= [-20, 10, 30]
		// Custom sort functions:
		>> nums:sort(func(x:&%Int,y:&%Int) x:abs() <> y:abs())
		>> nums
		= [10, -20, 30]
		>> nums:sort(func(x:&%Int,y:&%Int) y[] <> x[])
		>> nums
		= [30, 10, -20]

	>> ["A", "B", "C"]:sample(10, [1.0, 0.5, 0.0])

	if yes:
		>> heap := [Int.random(max=50) for _ in 10]
		>> heap:heapify()
		>> heap
		sorted := [:Int]
		while #heap > 0:
			sorted:insert(heap:heap_pop())
		>> sorted == sorted:sorted()
		= yes
		for _ in 10:
			heap:heap_push(Int.random(max=50))
		>> heap
		sorted = [:Int]
		while #heap > 0:
			sorted:insert(heap:heap_pop())
		>> sorted == sorted:sorted()
		= yes