Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Rework]: Array #704

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

meatball133
Copy link
Member

No description provided.

var oddInts = [1, 3, 5, 7, 9, 11, 13]
oddInts.append(15)
// oddInts is now [1, 3, 5, 7, 9, 11, 13, 15]
```
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about the following way to append an item.

var x = [1]
let y = [2]
x += y
print(x)

I believe it's much easier to reason about.

An explanation why += works as expected.

struct Vector
{
    var x: Double = 0
    var y: Double = 0

    init(x: Double, y: Double)
    {
        self.x = x
        self.y = y
    }
}
func += (left: inout Vector, right: Vector)
{
  left.x += right.x
  left.y += right.y
}

// 11
## Converting an Array to a String and Back

An array ofn strings can be converted to a single string using the [`joined(separator:)`][joined] method.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
An array ofn strings can be converted to a single string using the [`joined(separator:)`][joined] method.
An array of n strings can be converted to a single string using the [`joined(separator:)`][joined] method.

## Converting an Array to a String and Back

An array ofn strings can be converted to a single string using the [`joined(separator:)`][joined] method.
The `joined(separator:)` property takes a single argument, the separator to be used between elements of the array.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The `joined(separator:)` property takes a single argument, the separator to be used between elements of the array.
The `joined(separator:)` method takes a single argument, the separator to be used between elements of the array.

## Converting an Array to a String and Back

An array ofn strings can be converted to a single string using the [`joined(separator:)`][joined] method.
The `joined(separator:)` property takes a single argument, the separator to be used between elements of the array.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The `joined(separator:)` property takes a single argument, the separator to be used between elements of the array.
The `joined(separator:)` property takes a single argument, the separator to be used between elements of the array.
> Don't forget to convert you Array to `Array<String>`, otherwise `joined(separator:)` won't work.

Here is a snippet, that shows the conversion. This might be too complicated at this early stage.

let evenInts = [2, 4, 6, 8, 10, 12]
let evenIntsString = evenInts.map({"\($0)"}).joined(separator: ", ")


Arrays are one of Swift's three primary collection types. Arrays are ordered lists of elements where the elements can be of any type, however, all elements of any given list must have the same type.
[Arrays][array] are one of Swift's three primary collection types.
Arrays are ordered lists of elements where the elements can be of any type, however, all elements of any given list must have the same type.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can add a note how this is achieved and that Generics will be introduced later. For example:

Suggested change
Arrays are ordered lists of elements where the elements can be of any type, however, all elements of any given list must have the same type.
Arrays are ordered lists of elements where the elements can be of any type, however, all elements of any given list must have the same type.
> All primary collection types are implemented using generics. This is the reason why they can work with elements from already defined types or new data types that you will define.

Comment on lines -5 to +6
To make things a bit easier she only uses the cards 1 to 10.
To make things a bit easier she only uses the cards 1 to 10 so her stack of cards can be represented by an array of numbers.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not using Strings. Each card can be represented by a String? What's the difference?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is just what the instructions say to make it easier, as you might notice this is not a new implementation of the exercise (I didn't write the exercise), more of a rewrite of the text. The idea is that the exercise stay the same.

@@ -1,93 +1,94 @@
# Instructions

As a magician-to-be, Elyse needs to practice some basics. She has a stack of cards that she wants to manipulate.
As a magician-to-be, Elyse needs to practice some basics.
She has a stack of cards that she wants to manipulate.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
She has a stack of cards that she wants to manipulate.
She has a stack of cards that she wants to manipulate. The stack of cards can be represented by an array of strings, because regular cards contains numbers ( 1 to 10) and Jack, Queen and King, which we can denote with "J", "Q" and "K". To make it more fun you can use emojis. For example: "♦️J"

```

[array]: https://developer.apple.com/documentation/swift/array
[count]: https://developer.apple.com/documentation/swift/array/count
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
[count]: https://developer.apple.com/documentation/swift/array/count
[count]: https://developer.apple.com/documentation/swift/array/count
[isEmpty]: https://developer.apple.com/documentation/swift/array/isEmpty

let emptyArray2 = Array<Int>()
let emptyArray3: [Int] = []
```

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
An array can be checked if it's empty with [`isEmpty`][isEmpty] property.

Comment on lines 7 to 23
class TaskGetCardTests: XCTestCase {
func testGetFirstCard() {
let stack = [1, 2, 3, 4, 5]
XCTAssertEqual(getCard(at: 0, from: stack), 1)
}

func testSetCard() throws {
func testGetMiddleCard() {
let stack = [1, 2, 3, 4, 5]
XCTAssertEqual(getCard(at: 2, from: stack), 3)
}

func testGetLastCard() {
let stack = [1, 2, 3, 4, 5]
XCTAssertEqual(getCard(at: 4, from: stack), 5)
}
}

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the tests should be revisited if the cards are represented as strings.

@meatball133 meatball133 added this to the Swift 6.0 milestone Dec 28, 2024

Arrays literals are written as a series of elements, each separated by commas, enclosed in square brackets. Empty arrays are just a pair of square brackets. Type names for arrays are written in one of two ways: `Array<T>` or `[T]` where `T` is the type of the elements in thee array. When creating an empty array, the type must be specified.
Arrays literals are written as a series of elements, each separated by commas, enclosed in square brackets.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Arrays literals are written as a series of elements, each separated by commas, enclosed in square brackets.
Array literals are written as a series of elements, each separated by a comma, enclosed in square brackets.


Arrays literals are written as a series of elements, each separated by commas, enclosed in square brackets. Empty arrays are just a pair of square brackets. Type names for arrays are written in one of two ways: `Array<T>` or `[T]` where `T` is the type of the elements in thee array. When creating an empty array, the type must be specified.
Arrays literals are written as a series of elements, each separated by commas, enclosed in square brackets.
Swift will infer the type of the array from the types of the elements in the array literal.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Swift will infer the type of the array from the types of the elements in the array literal.
Swift will infer the type of the array from the type of the elements in the array literal.


Arrays literals are written as a series of elements, each separated by commas, enclosed in square brackets. Empty arrays are just a pair of square brackets. Type names for arrays are written in one of two ways: `Array<T>` or `[T]` where `T` is the type of the elements in the array. When creating an empty array, the type must be specified.
Arrays literals are written as a series of elements, each separated by commas, enclosed in square brackets.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Arrays literals are written as a series of elements, each separated by commas, enclosed in square brackets.
Array literals are written as a series of elements, each separated by a comma, enclosed in square brackets.


Arrays literals are written as a series of elements, each separated by commas, enclosed in square brackets. Empty arrays are just a pair of square brackets. Type names for arrays are written in one of two ways: `Array<T>` or `[T]` where `T` is the type of the elements in the array. When creating an empty array, the type must be specified.
Arrays literals are written as a series of elements, each separated by commas, enclosed in square brackets.
Swift will infer the type of the array from the types of the elements in the array literal.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Swift will infer the type of the array from the types of the elements in the array literal.
Swift will infer the type of the array from the type of the elements in the array literal.


Elements of an array can be modified by assigning a new value to the element at a given index.
The index of an element is an `Int` and starts with `0` for the first (leftmost) element.
If the index is outside the valid range of indices, a runtime error will occur and the program will crash.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
If the index is outside the valid range of indices, a runtime error will occur and the program will crash.
If the given index is outside the valid range of indices for the array, a runtime error will occur and the program will crash.


Arrays literals are written as a series of elements, each separated by commas, enclosed in square brackets. Empty arrays are just a pair of square brackets. Type names for arrays are written in one of two ways: `Array<T>` or `[T]` where `T` is the type of the elements in the array. When creating an empty array, the type must be specified.
Arrays literals are written as a series of elements, each separated by commas, enclosed in square brackets.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Arrays literals are written as a series of elements, each separated by commas, enclosed in square brackets.
Array literals are written as a series of elements, each separated by a comma, enclosed in square brackets.

Elements can be deleted from an array using the [`remove(at:)`][remove] method.
The `remove(at:)` method takes a single argument, the index of the element to be removed from the array.
The index of an element is an `Int` and starts with `0` for the first (leftmost) element.
If the index is outside the valid range of indices, a runtime error will occur and the program will crash.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
If the index is outside the valid range of indices, a runtime error will occur and the program will crash.
If the given index is outside the valid range of indices for the array, a runtime error will occur and the program will crash.

The elements of an array can be stepped through one at a time using a for-in loop. This type of loop takes each element of the array, in order, and binds the element to a specified name for further processing inside the loop body. For example, to print out all of the odd integers in an array one can write:
Elements of an array can be modified by assigning a new value to the element at a given index.
The index of an element is an `Int` and starts with `0` for the first (leftmost) element.
If the index is outside the valid range of indices, a runtime error will occur and the program will crash.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
If the index is outside the valid range of indices, a runtime error will occur and the program will crash.
If the given index is outside the valid range of indices for the array, a runtime error will occur and the program will crash.


Elements of an array can be accessed individually by supplying the index of the element inside square brackets following the array.
The index of an element is an `Int` and starts with `0` for the first (leftmost) element.
If the index is outside the valid range of indices, a runtime error will occur and the program will crash.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
If the index is outside the valid range of indices, a runtime error will occur and the program will crash.
If the given index is outside the valid range of indices for the array, a runtime error will occur and the program will crash.


## Add an Array to an Array

Array can be added to the end of an array using the `+` operator.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Array can be added to the end of an array using the `+` operator.
An array can be added to the end of another array using the `+` operator.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants