Skip to content

Commit 8862271

Browse files
committed
Fix style
1 parent 17ffbed commit 8862271

File tree

1 file changed

+75
-52
lines changed

1 file changed

+75
-52
lines changed

docs/tutorials/basics/70_collections.md

Lines changed: 75 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Examples of collections that includes `Enumerable` are:
88
- [Hash](https://crystal-lang.org/api/Hash.html)
99
- [Set](https://crystal-lang.org/api/Set.html)
1010

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`.
1212

1313
In this section we will review some methods implemented in `Enumerable`.
1414

@@ -18,8 +18,10 @@ Let's start with the following two examples: given a array of `Int32` we may app
1818

1919
Let's write them:
2020

21-
**Plus1 example**
22-
```{.crystal .crystal-play}
21+
### Plus1 example
22+
23+
```crystal-play
24+
2325
arr = [1, 2, 3]
2426
plus1 = [] of Int32
2527
@@ -30,8 +32,10 @@ arr.each { |elem|
3032
puts plus1 # => [2, 3, 4]
3133
```
3234

33-
**Times2 example**
34-
```{.crystal .crystal-play}
35+
### Times2 example
36+
37+
```crystal-play
38+
3539
arr = [1, 2, 3]
3640
times2 = [] of Int32
3741
@@ -52,18 +56,22 @@ Well, we are in luck, because [Enumerable#map](https://crystal-lang.org/api/Enum
5256

5357
Let's re-write the examples:
5458

55-
**Plus1 example using Enumerable#map**
56-
```{.crystal .crystal-play}
59+
### Plus1 example using Enumerable#map
60+
61+
```crystal-play
62+
5763
arr = [1, 2, 3]
58-
plus1 = arr.map { |elem| elem + 1}
64+
plus1 = arr.map { |elem| elem + 1 }
5965
6066
puts plus1 # => [2, 3, 4]
6167
```
6268

63-
**Times2 example using Enumerable#map**
64-
```{.crystal .crystal-play}
69+
### Times2 example using Enumerable#map
70+
71+
```crystal-play
72+
6573
arr = [1, 2, 3]
66-
times2 = arr.map { |elem| elem * 2}
74+
times2 = arr.map { |elem| elem * 2 }
6775
6876
puts times2 # => [2, 4, 6]
6977
```
@@ -73,11 +81,12 @@ Great!
7381
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):
7482

7583
```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
8190
```
8291

8392
No magic here, right? :) Just the structure we found, extracted to a method with a given name: `map`.
@@ -90,11 +99,12 @@ Related methods:
9099

91100
Let's move on to another two examples:
92101

93-
**Even example**
102+
### Even example
94103

95104
In this example we keep only even numbers
96105

97-
```{.crystal .crystal-play}
106+
```crystal-play
107+
98108
arr = [1, 2, 3, 4, 5]
99109
even = [] of Int32
100110
@@ -105,11 +115,12 @@ arr.each { |elem|
105115
puts even # => [2, 4]
106116
```
107117

108-
**String-length example**
118+
### String-length example
109119

110120
And in this example we keep `strings` which length is more than 3.
111121

112-
```{.crystal .crystal-play}
122+
```crystal-play
123+
113124
arr = ["Hello", "Crystal!", "ABC", "Foo"]
114125
str3 = [] of String
115126
@@ -128,18 +139,20 @@ As we can imagine, there is such method and it's called [Enumerable#select](http
128139

129140
Let's re-write the examples:
130141

131-
**Even example using Enumerable#select**
142+
### Even example using Enumerable#select
143+
144+
```crystal-play
132145
133-
```{.crystal .crystal-play}
134146
arr = [1, 2, 3, 4, 5]
135147
even = arr.select { |elem| elem.even? }
136148
137149
puts even # => [2, 4]
138150
```
139151

140-
**String-length example using Enumerable#select**
152+
### String-length example using Enumerable#select
153+
154+
```crystal-play
141155
142-
```{.crystal .crystal-play}
143156
arr = ["Hello", "Crystal!", "ABC", "Foo"]
144157
str3 = arr.select { |elem| elem.size > 3 }
145158
@@ -149,11 +162,12 @@ puts str3 # => ["Hello", "Crystal!"]
149162
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):
150163

151164
```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
157171
```
158172

159173
There we have the structure!
@@ -166,11 +180,12 @@ Related methods:
166180

167181
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.
168182

169-
**Add all the things!**
183+
### Add all the things
170184

171185
In the first example we will add all the numbers.
172186

173-
```{.crystal .crystal-play}
187+
```crystal-play
188+
174189
arr = [1, 2, 3]
175190
sum = 0
176191
@@ -181,11 +196,12 @@ arr.each { |elem|
181196
puts sum # => 6
182197
```
183198

184-
**Any empty?**
199+
### Any empty?
185200

186201
And in the second example we return if there is an empty string.
187202

188-
```{.crystal .crystal-play}
203+
```crystal-play
204+
189205
arr = ["Crystal", "A", "language", "for", "humans", "and", "computers"]
190206
empty_str = false
191207
@@ -205,41 +221,46 @@ Drumroll please ... the method is called [Enumerable#reduce](https://crystal-lan
205221

206222
Let's re-write the examples:
207223

208-
**Add all the things! using Enumerable#reduce**
224+
### Add all the things! using Enumerable#reduce
225+
226+
```crystal-play
209227
210-
```{.crystal .crystal-play}
211228
arr = [1, 2, 3]
212229
sum = arr.reduce(0) { |accum, elem| accum + elem }
213230
214231
puts sum # => 6
215232
```
216233

217-
**Any empty? using Enumerable#reduce**
234+
### Any empty? using Enumerable#reduce
235+
236+
```crystal-play
218237
219-
```{.crystal .crystal-play}
220238
arr = ["Crystal", "A", "language", "for", "humans", "and", "computers"]
221-
empty_str = arr.reduce(false) { |accum, elem| accum || elem.empty?}
239+
empty_str = arr.reduce(false) { |accum, elem| accum || elem.empty? }
222240
223241
puts empty_str # => false
224242
```
225243

226244
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:
227245

228246
```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
234251
end
252+
memo
253+
end
235254
```
236255

237256
It's worth mention that `reduce` is the base for other methods. Let's see one of them related to the first example:
238257

239258
### Enumerable#sum
259+
240260
We can write our first example with [Enumerable#sum](https://crystal-lang.org/api/Enumerable.html#sum%28initial%29-instance-method) like this:
241261

242-
```{.crystal .crystal-play}
262+
```crystal-play
263+
243264
arr = [1, 2, 3]
244265
sum = arr.sum(0)
245266
@@ -248,7 +269,8 @@ puts sum # => 6
248269

249270
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`.
250271

251-
```{.crystal .crystal-play}
272+
```crystal-play
273+
252274
arr = [1, 2, 3]
253275
puts arr.sum # => 6
254276
```
@@ -257,27 +279,28 @@ puts arr.sum # => 6
257279
) is the implementation:
258280

259281
```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
263286
```
264287

265288
### Enumerable#any?
266289

267290
In case you are wondering if there is an `any?` method we can re-write the `Any empty? example` ...
268291

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:
270293

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:
294+
```crystal-play
272295
273-
```{.crystal .crystal-play}
274296
arr = ["Crystal", "A", "language", "for", "humans", "and", "computers"]
275297
puts arr.any? { |str| str.empty? } # => false
276298
```
277299

278300
Even more, we have a more concise way of writing it:
279301

280-
```{.crystal .crystal-play}
302+
```crystal-play
303+
281304
arr = ["Crystal", "A", "language", "for", "humans", "and", "computers"]
282305
puts arr.any? &.empty? # => false
283306
```

0 commit comments

Comments
 (0)