-
Notifications
You must be signed in to change notification settings - Fork 6
/
about_enums.exs
executable file
·273 lines (220 loc) · 6.71 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
defmodule AboutEnums do
use Koans
think "Do something with each element" do
list = [__?, __?, __?]
Enum.each(list, fn (x) -> is_integer(x) end)
end
think "Mapping over a list" do
list = [1, 2, 3]
assert Enum.map(list, __?) == [2, 3, 4]
# Hint: Write a function!
end
think "Concatenation" do
list_1 = [1, 2, 3]
list_2 = [4, 5, 6]
assert Enum.concat(list_1, list_2) == __?
end
think "Is an enumerable empty?" do
assert_? Enum.empty?([1, 2, 3])
assert_? Enum.empty?([])
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 in the collection?" do
list = [:a, :b, :c]
assert Enum.member?(list, :d) == __?
end
think "Find an element at an index" 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 "It can take a default" do
list = [:a, :b, :c]
assert Enum.at(list, 5, :something) == __?
end
think "Fetching is similar to at" do
list = [:a, :b, :c]
assert Enum.fetch(list, 0) == __?
end
think "Fetching tells you if it can't find an element" do
list = [:a, :b, :c]
assert Enum.fetch(list, 4) == __?
end
think "Fetching 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 that 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 is the index of an element?" do
list = [1, 2, 3]
assert Enum.find_index(list, fn(x) -> x == 2 end) == __?
end
think "Find and manipulate a value" do
list = [1, 2, 3]
assert Enum.find_value(list, fn (x) -> rem(x, 2) == 1 end) == __?
# TODO this seems to be a really bad example
end
think "Get each element with its index" do
list = [:a, :b, :c]
assert Enum.with_index(list) == __?
end
think "Chunking elements into groups" do
list = [1, 2, 3, 4, 5, 6]
assert Enum.chunk(list, 2) == __?
end
think "Chunking elements in steps" do
list = [1, 2, 3, 4, 5, 6]
assert Enum.chunk(list, 2, 1) == __?
end
think "Chunking elements in steps with padding" do
list = [1, 2, 3, 4, 5, 6]
assert Enum.chunk(list, 3, 2, [7]) == __?
end
think "Chunking elements with a function" do
list = [3, 4, 5, 6, 7, 8]
assert Enum.chunk_by(list, fn (x) -> x > 5 end) == __?
end
think "Dropping elements" do
list = [1, 2, 3, 4]
assert Enum.drop(list, 2) == __?
assert Enum.drop(list, 10) == __?
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 and 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 into a string" 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) == __?
# TODO this example may be unecessarily difficult to grok
end
think "Zipping collections together" do
list_1 = [1, 2, 3]
list_2 = [4, 5, 6]
assert Enum.zip(list_1, list_2) == __?
# TODO would be nice to use values with more meaning. Like zipping atoms
# and values together to make a keyword list.
end
think "Find the max value in a collection" do
list = [6, 1, 5, 2, 4, 3]
assert Enum.max(list) == __?
end
think "Find the max value using a function" do
list = ["the", "longest", "word", "is", "expected"]
assert Enum.max_by(list, &String.length(&1)) == __?
end
think "Find the minimum value in a collection" do
list = [6, 1, 5, 2, 4, 3]
assert Enum.min(list) == __?
end
think "Find the minimum value using a function" do
list = ["the", "shortest", "word", "is", "expected"]
assert Enum.min_by(list, &String.length(&1)) == __?
end
defp numbers, do: 1..10
think "Partitioning" do
{left, right} = Enum.partition(numbers, fn(x) -> rem(x, 2) == 1 end)
assert left == __?
assert right == __?
end
think "Reduction" do
result = Enum.reduce(numbers, 0, fn (x, acc) -> acc + x end)
assert result == __?
# TODO this could probably be illustrated before map_reduce. Also would be
# nice to use values easier to compute in your head.
end
think "Rejection" do
result = Enum.reject(numbers, fn(x) -> rem(x, 2) == 1 end)
assert result == __?
end
think "Reversal" do
assert Enum.reverse(numbers) == __?
end
think "Shuffle" do
assert_? Enum.shuffle(numbers) == numbers
# Note: It's possible for this to fail. Does shuffle guarantee difference?
end
think "Slicing" do
assert Enum.slice(numbers, 2, 2) == __?
end
think "Slicing beyond length" do
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 elements" 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 with function" do
{left, right} = Enum.split_while(numbers, fn (x) -> x < 5 end)
assert left == __?
assert right == __?
end
think "Take some elements" do
assert Enum.take(numbers, 2) == __?
end
think "Take some elements from the end" do
assert Enum.take(numbers, -2) == __?
end
think "Take every few elements" do
assert Enum.take_every(numbers, 3) == __?
end
think "Take while function is true" do
assert Enum.take_while(numbers, fn (x) -> x < 5 end) == __?
end
end