forked from dojo-toulouse/elixir-koans
-
Notifications
You must be signed in to change notification settings - Fork 0
/
about_enums.exs
executable file
·293 lines (236 loc) · 6.62 KB
/
about_enums.exs
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
#!/usr/bin/env elixir
ExUnit.start
defmodule About_Enums do
use ExUnit.Case
use Koans
think "Output each element on its own line" do
list = [1, 2, 3]
Enum.each(list, fn (x) -> __? end)
end
think "Mapping over a list" do
list = [1, 2, 3]
assert Enum.map(list, fn (x) -> __? end) == [2, 3, 4]
end
think "concatenation" do
list_1 = [1, 2, 3]
list_2 = [4, 5, 6]
assert Enum.concat(list_1, list_2) == __?
end
think "Empty, or not?" do
list = [1, 2, 3]
assert Enum.empty?(list) == __?
end
think "Not empty?" do
list = []
assert Enum.empty?(list) == __?
end
think "Check if all items match" do
list = [1, 2, 3]
assert Enum.all?(list, fn (x) -> x < 4 end) == __?
end
think "Check if any items match" do
list = [1, 2, 3]
assert Enum.any?(list, fn (x) -> x < 2 end) == __?
end
think "Is it there, or not?" do
list = [:a, :b, :c]
assert Enum.member?(list, :d) == __?
end
think "What element is first?" do
list = [:a, :b, :c, :d]
assert Enum.at(list, 0) == __?
end
think "What happens if we look outside the list?" do
list = [:a, :b, :c, :d]
assert Enum.at(list, 5) == __?
end
think "at can take a default" do
list = [:a, :b, :c]
assert Enum.at(list, 5, :something) == __?
end
think "fetch is like at" do
list = [:a, :b, :c]
assert Enum.fetch(list, 0) == __?
end
think "fetch tells you if it can't find an element" do
list = [:a, :b, :c]
assert Enum.fetch(list, 4) == __?
end
think "fetch! will raise an exception if it can't find an element" do
list = [:a, :b, :c]
assert_raise __?, fn -> Enum.fetch!(list, 4) end
end
think "find the first element which matches" do
list = [1, 2, 3, 4]
assert Enum.find(list, fn (x) -> x > 2 end) == __?
end
think "what happens when find can't find?" do
list = [1, 2, 3, 4, 5]
assert Enum.find(list, fn (x) -> x > 5 end) == __?
end
think "find takes a default" do
list = [1, 2, 3]
assert Enum.find(list, 4, fn (x) -> x > 3 end) == __?
end
think "what index is this number at?" do
list = [1, 2, 3]
assert Enum.find_index(list, fn(x) -> x == 2 end) == __?
end
think "finding and manipulating a value" do
list = [1, 2, 3]
assert Enum.find_value(list, fn (x) -> rem(x, 2) == 1 end) == __?
end
think "each element with its index" do
list = [:a, :b, :c]
assert Enum.with_index(list) == __?
end
think "enums can be chunked" do
list = [1, 2, 3, 4, 5, 6]
assert Enum.chunk(list, 2) == __?
end
think "chunking can happen in steps" do
list = [1, 2, 3, 4, 5, 6]
assert Enum.chunk(list, 2, 1) == __?
end
think "chunking can have padding" do
list = [1, 2, 3, 4, 5, 6]
assert Enum.chunk(list, 3, 2, [7]) == __?
end
think "chunking by unique values" do
list = [3, 4, 5, 6, 7, 8]
assert Enum.chunk_by(list, fn (x) -> x > 5 end) == __?
end
think "dropping items" do
list = [1, 2, 3, 4]
assert Enum.drop(list, 2) == __?
end
think "dropping a lot of items" do
list = [1, 2, 3, 4]
assert Enum.drop(list, 10) == __?
end
think "dropping with negative numbers" do
list = [1, 2, 3, 4]
assert Enum.drop(list, -1) == __?
end
think "dropping while a condition is met" do
list = [1, 2, 3, 4]
assert Enum.drop_while(list, fn (x) -> x < 2 end) == __?
end
think "filtering" do
list = [1, 2, 3, 4]
assert Enum.filter(list, fn (x) -> rem(x, 2) == 1 end) == __?
end
think "filtering + mapping" do
list = [1, 2, 3, 4]
assert Enum.filter_map(list, fn (x) -> rem(x, 2) == 1 end, &(&1 * 2)) == __?
end
think "flat mapping" do
list = Enum.flat_map([{1, 3}, {4, 6}], fn({x, y}) -> x..y end)
assert list == __?
end
think "joining" do
list = [1, 2, 3]
assert Enum.join(list) == __?
end
think "joining with a separator" do
list = [1, 2, 3]
assert Enum.join(list, ",") == __?
end
think "mapping and joining" do
list = [1, 2, 3]
assert Enum.map_join(list, fn (x) -> x * 2 end) == __?
end
think "map reduce" do
list = [4, 5, 6]
assert Enum.map_reduce(list, 0, fn (x, acc) -> {x * 2, x + acc} end) == __?
end
think "zipping" do
list_1 = [1, 2, 3]
list_2 = [4, 5, 6]
assert Enum.zip(list_1, list_2) == __?
end
think "what's the maximum value in this list?" do
list = [6, 1, 5, 2, 4, 3]
assert Enum.max(list) == __?
end
think "getting the maximum by function" do
list = ["the", "longest", "word", "is", "expected"]
assert Enum.max_by(list, &String.length(&1)) == __?
end
think "what's the minimum value in this list?" do
list = [6, 1, 5, 2, 4, 3]
assert Enum.min(list) == __?
end
think "getting the minimum by function" do
list = ["the", "shortest", "word", "is", "expected"]
assert Enum.min_by(list, &String.length(&1)) == __?
end
think "partitioning" do
numbers = 1..10
{left, right} = Enum.partition(numbers, fn(x) -> rem(x, 2) == 1 end)
assert left == __?
assert right == __?
end
think "reduction" do
numbers = 1..10
result = Enum.reduce(numbers, 0, fn (x, acc) -> acc + x end)
assert result == __?
end
think "rejection" do
numbers = 1..10
result = Enum.reject(numbers, fn(x) -> rem(x, 2) == 1 end)
assert result == __?
end
think "reversal" do
numbers = 1..10
assert Enum.reverse(numbers) == __?
end
think "shuffle" do
numbers = 1..10
assert_? Enum.shuffle(numbers) == numbers
end
think "slicing" do
numbers = 1..10
assert Enum.slice(numbers, 2, 2) == __?
end
think "slicing goes too far" do
numbers = 1..10
assert Enum.slice(numbers, 2, 100) == __?
end
think "sorting" do
numbers = [1, 6, 3, 8, 4, 2, 9, 5, 7]
assert Enum.sort(numbers) == __?
end
think "unique items only, please" do
numbers = [1, 1, 2, 3, 3, 4]
assert Enum.uniq(numbers) == __?
end
think "splitting" do
numbers = [1, 2, 3, 4]
{left, right} = Enum.split(numbers, 2)
assert left == __?
assert right == __?
end
think "splitting while function is true" do
numbers = 1..10
{left, right} = Enum.split_while(numbers, fn (x) -> x < 5 end)
assert left == __?
assert right == __?
end
think "taking some items" do
numbers = 1..10
assert Enum.take(numbers, 2) == __?
end
think "taking some items the other way" do
numbers = 1..10
assert Enum.take(numbers, -2) == __?
end
think "taking every so often" do
numbers = 1..10
assert Enum.take_every(numbers, 3) == __?
end
think "taking while we can" do
numbers = 1..10
assert Enum.take_while(numbers, fn (x) -> x < 5 end) == __?
end
end