8000 Merge pull request #432 from Parva9eh/patch-7 · matsoftware/swift-algorithm-club@633a40e · GitHub
[go: up one dir, main page]

Skip to content

Commit 633a40e

Browse files
authored
Merge pull request kodecocodes#432 from Parva9eh/patch-7
Revisions in README.markdown
2 parents cdea625 + d70f40e commit 633a40e

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

Array2D/README.markdown

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# Array2D
22

3-
In C and Objective-C you can write the following line,
3+
In C and Objective-C, you can write the following line,
44

55
int cookies[9][7];
66

7-
to make a 9x7 grid of cookies. This would create a two-dimensional array of 63 elements. To find the cookie at column 3, row 6, you'd write:
7+
to make a 9x7 grid of cookies. This creates a two-dimensional array of 63 elements. To find the cookie at column 3 and row 6, you can write:
88

99
myCookie = cookies[3][6];
1010

11-
Unfortunately, you can't write the above in Swift. To create a multi-dimensional array in Swift you'd have to do something like this:
11+
This statement is not acceptable in Swift. To create a multi-dimensional array in Swift, you can write:
1212

1313
```swift
1414
var cookies = [[Int]]()
@@ -21,33 +21,33 @@ for _ in 1...9 {
2121
}
2222
```
2323

24-
And then to find a cookie:
24+
Then, to find a cookie, you can write:
2525

2626
```swift
2727
let myCookie = cookies[3][6]
2828
```
2929

30-
Actually, you could create the array in a single line of code, like so:
30+
You can also create the array in a single line of code:
3131

3232
```swift
3333
var cookies = [[Int]](repeating: [Int](repeating: 0, count: 7), count: 9)
3434
```
3535

36-
but that's just ugly. To be fair, you can hide the ugliness in a helper function:
36+
This looks complicated, but can simplfy it in a helper function:
3737

3838
```swift
3939
func dim<T>(_ count: Int, _ value: T) -> [T] {
4040
return [T](repeating: value, count: count)
4141
}
4242
```
4343

44-
And then creating the array looks like this:
44+
Then, you can create the array:
4545

4646
```swift
4747
var cookies = dim(9, dim(7, 0))
4848
```
4949

50-
Swift infers that the datatype of the array should be `Int` because you specified `0` as the default value of the array elements. To use a string instead, you'd write:
50+
Swift infers that the datatype of the array must be `Int` because you specified `0` as the default value of the array elements. To use a string instead, you can write:
5151

5252
```swift
5353
var cookies = dim(9, dim(7, "yum"))
@@ -59,9 +59,9 @@ The `dim()` function makes it easy to go into even more dimensions:
5959
var threeDimensions = dim(2, dim(3, dim(4, 0)))
6060
```
6161

62-
The downside of using multi-dimensional arrays in this fashion -- actually, multiple nested arrays -- is that it's easy to lose track of what dimension represents what.
62+
The downside of using multi-dimensional arrays or multiple nested arrays in this way is to lose track of what dimension represents what.
6363

64-
So instead let's create our own type that acts like a 2-D array and that is more convenient to use. Here it is, short and sweet:
64+
Instead, you can create your own type that acts like a 2-D array which is more convenient to use:
6565

6666
```swift
6767
public struct Array2D<T> {
@@ -92,26 +92,24 @@ public struct Array2D<T> {
9292

9393
`Array2D` is a generic type, so it can hold any kind of object, not just numbers.
9494

95-
To create an instance of `Array2D` you'd write:
95+
To create an instance of `Array2D`, you can write:
9696

9797
```swift
9898
var cookies = Array2D(columns: 9, rows: 7, initialValue: 0)
9999
```
100100

101-
Thanks to the `subscript` function, you can do the following to retrieve an object from the array:
101+
By using the `subscript` function, you can retrieve an object from the array:
102102

103103
```swift
104104
let myCookie = cookies[column, row]
105105
```
106106

107-
Or change it:
107+
Or, you can change it:
108108

109109
```swift
110110
cookies[column, row] = newCookie
111111
```
112112

113-
Internally, `Array2D` uses a single one-dimensional array to store the data. The index of an object in that array is given by `(row x numberOfColumns) + column`. But as a user of `Array2D` you don't have to worry about that; you only have to think in terms of "column" and "row", and let `Array2D` figure out the details for you. That's the advantage of wrapping primitive types into a wrapper class or struct.
114-
115-
And that's all there is to it.
113+
Internally, `Array2D` uses a single one-dimensional array to store the data. The index of an object in that array is given by `(row x numberOfColumns) + column`, but as a user of `Array2D`, you only need to think in terms of "column" and "row", and the details will be done by `Array2D`. This is the advantage of wrapping primitive types into a wrapper class or struct.
116114

117115
*Written for Swift Algorithm Club by Matthijs Hollemans*

0 commit comments

Comments
 (0)
0