aboutsummaryrefslogtreecommitdiff
path: root/test/lists.tm
blob: fef23f5d9f4a997dc87ca79eba37da2f9de7811a (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
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
func main()
	do
		>> nums : [Num32] = []
		= []

	do
		>> nums : [Num32]
		= []

	do
		>> list := [10, 20, 30]
		= [10, 20, 30]

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

		>> list.length
		= 3

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

		str := ""
		for i,x in list
			str ++= "($i,$x)"
		>> str
		= "(1,10)(2,20)(3,30)"

	do
		>> list := [10, 20] ++ [30, 40]
		= [10, 20, 30, 40]

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

	do
		>> list := [10, 20]
		>> copy := list
		>> list ++= [30]
		>> list
		= [10, 20, 30]
		>> copy
		= [10, 20]

	do
		>> [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]

	do
		>> list := @[10, 20]
		>> copy := list[]
		>> list.insert(30)
		>> list
		= @[10, 20, 30]
		>> copy
		= [10, 20]

		>> list[1] = 999
		>> list
		= @[999, 20, 30]

	do
		>> list := &[10, 20, 30]
		>> reversed := list.reversed()
		= [30, 20, 10]
		# Ensure the copy-on-write behavior triggers:
		>> list[1] = 999
		>> reversed
		= [30, 20, 10]

	do
		>> 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,y:&Int) x.abs() <> y.abs())
		>> nums
		= @[10, -20, 30]
		>> nums.sort(func(x,y:&Int) y[] <> x[])
		>> nums
		= @[30, 10, -20]

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

	do
		>> heap := @[(i * 1337) mod 37 for i in 10]
		>> heap.heapify()
		>> heap
		heap_order : @[Int]
		repeat
			heap_order.insert(heap.heap_pop() or stop)
		assert heap_order[] == heap_order.sorted()
		heap_order[] = []
		for i in 10
			heap.heap_push((i*13337) mod 37)
		>> heap
		repeat
			heap_order.insert(heap.heap_pop() or stop)
		assert heap_order[] == heap_order.sorted()

	do
		>> [i*10 for i in 5].from(3)
		= [30, 40, 50]
		>> [i*10 for i in 5].to(3)
		= [10, 20, 30]
		>> [i*10 for i in 5].to(-2)
		= [10, 20, 30, 40]
		>> [i*10 for i in 5].from(-2)
		= [40, 50]

		>> [i*10 for i in 5].by(2)
		= [10, 30, 50]
		>> [i*10 for i in 5].by(-1)
		= [50, 40, 30, 20, 10]

		>> [10, 20, 30, 40].by(2)
		= [10, 30]
		>> [10, 20, 30, 40].by(-2)
		= [40, 20]

		>> [i*10 for i in 10].by(2).by(2)
		= [10, 50, 90]

		>> [i*10 for i in 10].by(2).by(-1)
		= [90, 70, 50, 30, 10]

		# Test iterating over list.from() and list.to()
		xs := ["A", "B", "C", "D"]
		for i,x in xs.to(-2)
			for y in xs.from(i+1)
				say("$(x)$(y)")

	do
		>> nums := @[-7, -4, -1, 2, 5]
		>> nums.sort()
		>> [nums.binary_search(i) for i in nums[]]
		= [1, 2, 3, 4, 5]
		>> nums.sort(func(a,b:&Int) a.abs() <> b.abs())
		>> [nums.binary_search(i, func(a,b:&Int) a.abs() <> b.abs()) for i in nums[]]
		= [1, 2, 3, 4, 5]

	>> ["a", "b", "c"].find("b")
	= 2?
	>> ["a", "b", "c"].find("XXX")
	= none

	>> [10, 20].where(func(i:&Int) i.is_prime())
	= none
	>> [4, 5, 6].where(func(i:&Int) i.is_prime())
	= 2?

	do
		>> nums := &[10, 20, 30, 40, 50]
		>> nums.pop()
		= 50?
		>> nums
		= &[10, 20, 30, 40]
		>> nums.pop(2)
		= 20?
		>> nums
		= &[10, 30, 40]
		>> nums.clear()
		>> nums
		= &[]
		>> nums.pop()
		= none