You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/tutorials/basics/70_collections.md
+75-52Lines changed: 75 additions & 52 deletions
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,7 @@ Examples of collections that includes `Enumerable` are:
8
8
-[Hash](https://crystal-lang.org/api/Hash.html)
9
9
-[Set](https://crystal-lang.org/api/Set.html)
10
10
11
-
[Here](https://crystal-lang.org/api/Enumerable.html#direct-including-types) is a list of other types (not necessarily collections) that _direct include_`Enumerable`.
11
+
[Here](https://crystal-lang.org/api/Enumerable.html#direct-including-types) is a list of other types (not necessarily collections) that *direct include*`Enumerable`.
12
12
13
13
In this section we will review some methods implemented in `Enumerable`.
14
14
@@ -18,8 +18,10 @@ Let's start with the following two examples: given a array of `Int32` we may app
18
18
19
19
Let's write them:
20
20
21
-
**Plus1 example**
22
-
```{.crystal .crystal-play}
21
+
### Plus1 example
22
+
23
+
```crystal-play
24
+
23
25
arr = [1, 2, 3]
24
26
plus1 = [] of Int32
25
27
@@ -30,8 +32,10 @@ arr.each { |elem|
30
32
puts plus1 # => [2, 3, 4]
31
33
```
32
34
33
-
**Times2 example**
34
-
```{.crystal .crystal-play}
35
+
### Times2 example
36
+
37
+
```crystal-play
38
+
35
39
arr = [1, 2, 3]
36
40
times2 = [] of Int32
37
41
@@ -52,18 +56,22 @@ Well, we are in luck, because [Enumerable#map](https://crystal-lang.org/api/Enum
52
56
53
57
Let's re-write the examples:
54
58
55
-
**Plus1 example using Enumerable#map**
56
-
```{.crystal .crystal-play}
59
+
### Plus1 example using Enumerable#map
60
+
61
+
```crystal-play
62
+
57
63
arr = [1, 2, 3]
58
-
plus1 = arr.map { |elem| elem + 1}
64
+
plus1 = arr.map { |elem| elem + 1}
59
65
60
66
puts plus1 # => [2, 3, 4]
61
67
```
62
68
63
-
**Times2 example using Enumerable#map**
64
-
```{.crystal .crystal-play}
69
+
### Times2 example using Enumerable#map
70
+
71
+
```crystal-play
72
+
65
73
arr = [1, 2, 3]
66
-
times2 = arr.map { |elem| elem * 2}
74
+
times2 = arr.map { |elem| elem * 2}
67
75
68
76
puts times2 # => [2, 4, 6]
69
77
```
@@ -73,11 +81,12 @@ Great!
73
81
We wrote the examples using `each`, we detect the hidden pattern and we learned about `Enumerable#map`. Now let's see [Enumerable#map implementation](https://github.com/crystal-lang/crystal/blob/932f193ae/src/enumerable.cr#L905):
74
82
75
83
```crystal
76
-
def map(& : T -> U) : Array(U) forall U
77
-
ary = [] of U
78
-
each { |e| ary << yield e }
79
-
ary
80
-
end
84
+
85
+
def map(& : T -> U) : Array(U) forall U
86
+
ary = [] of U
87
+
each { |e| ary << yield e }
88
+
ary
89
+
end
81
90
```
82
91
83
92
No magic here, right? :) Just the structure we found, extracted to a method with a given name: `map`.
@@ -90,11 +99,12 @@ Related methods:
90
99
91
100
Let's move on to another two examples:
92
101
93
-
**Even example**
102
+
### Even example
94
103
95
104
In this example we keep only even numbers
96
105
97
-
```{.crystal .crystal-play}
106
+
```crystal-play
107
+
98
108
arr = [1, 2, 3, 4, 5]
99
109
even = [] of Int32
100
110
@@ -105,11 +115,12 @@ arr.each { |elem|
105
115
puts even # => [2, 4]
106
116
```
107
117
108
-
**String-length example**
118
+
### String-length example
109
119
110
120
And in this example we keep `strings` which length is more than 3.
111
121
112
-
```{.crystal .crystal-play}
122
+
```crystal-play
123
+
113
124
arr = ["Hello", "Crystal!", "ABC", "Foo"]
114
125
str3 = [] of String
115
126
@@ -128,18 +139,20 @@ As we can imagine, there is such method and it's called [Enumerable#select](http
So, we wrote the examples using `each`, we detect the hidden pattern and we learned about `Enumerable#select`. Let's see [Enumerable#select implementation](https://github.com/crystal-lang/crystal/blob/932f193ae/src/enumerable.cr#L1408):
150
163
151
164
```crystal
152
-
def select(& : T ->)
153
-
ary = [] of T
154
-
each { |e| ary << e if yield e }
155
-
ary
156
-
end
165
+
166
+
def select(& : T ->)
167
+
ary = [] of T
168
+
each { |e| ary << e if yield e }
169
+
ary
170
+
end
157
171
```
158
172
159
173
There we have the structure!
@@ -166,11 +180,12 @@ Related methods:
166
180
167
181
This next one method is not as easy as the previous ones. But, as we did before, let's start with 2 examples, then we will try to identify an structure and hopefully we will find a method already implementing what we need.
168
182
169
-
**Add all the things!**
183
+
### Add all the things
170
184
171
185
In the first example we will add all the numbers.
172
186
173
-
```{.crystal .crystal-play}
187
+
```crystal-play
188
+
174
189
arr = [1, 2, 3]
175
190
sum = 0
176
191
@@ -181,11 +196,12 @@ arr.each { |elem|
181
196
puts sum # => 6
182
197
```
183
198
184
-
**Any empty?**
199
+
### Any empty?
185
200
186
201
And in the second example we return if there is an empty string.
So, we wrote the examples using `each`, we detect the hidden pattern and we learned about `Enumerable#reduce`. Let's see [Enumerable#reduce implementation](https://github.com/crystal-lang/crystal/blob/932f193ae/src/enumerable.cr#L709) using an initial value:
227
245
228
246
```crystal
229
-
def reduce(memo)
230
-
each do |elem|
231
-
memo = yield memo, elem
232
-
end
233
-
memo
247
+
248
+
def reduce(memo)
249
+
each do |elem|
250
+
memo = yield memo, elem
234
251
end
252
+
memo
253
+
end
235
254
```
236
255
237
256
It's worth mention that `reduce` is the base for other methods. Let's see one of them related to the first example:
238
257
239
258
### Enumerable#sum
259
+
240
260
We can write our first example with [Enumerable#sum](https://crystal-lang.org/api/Enumerable.html#sum%28initial%29-instance-method) like this:
241
261
242
-
```{.crystal .crystal-play}
262
+
```crystal-play
263
+
243
264
arr = [1, 2, 3]
244
265
sum = arr.sum(0)
245
266
@@ -248,7 +269,8 @@ puts sum # => 6
248
269
249
270
We can also use it without an initial value in which case the first element would be the initial value. Is the same behaviour as in `Enumerable#reduce`.
250
271
251
-
```{.crystal .crystal-play}
272
+
```crystal-play
273
+
252
274
arr = [1, 2, 3]
253
275
puts arr.sum # => 6
254
276
```
@@ -257,27 +279,28 @@ puts arr.sum # => 6
257
279
) is the implementation:
258
280
259
281
```crystal
260
-
def sum(initial, & : T ->)
261
-
reduce(initial) { |memo, e| memo + (yield e) }
262
-
end
282
+
283
+
def sum(initial, & : T ->)
284
+
reduce(initial) { |memo, e| memo + (yield e) }
285
+
end
263
286
```
264
287
265
288
### Enumerable#any?
266
289
267
290
In case you are wondering if there is an `any?` method we can re-write the `Any empty? example` ...
268
291
269
-
**the short answer is: Yes!**
292
+
**the short answer is: Yes!** It's called [Enumerable#any?](https://crystal-lang.org/api/Enumerable.html#any%3F%28%26%3AT-%3E%29%3ABool-instance-method) and so we can write the example like this:
270
293
271
-
It's called [Enumerable#any?](https://crystal-lang.org/api/Enumerable.html#any%3F%28%26%3AT-%3E%29%3ABool-instance-method) and so we can write the example like this:
0 commit comments