-
-
Notifications
You must be signed in to change notification settings - Fork 158
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
base: main
Are you sure you want to change the base?
[Rework]: Array #704
Conversation
var oddInts = [1, 3, 5, 7, 9, 11, 13] | ||
oddInts.append(15) | ||
// oddInts is now [1, 3, 5, 7, 9, 11, 13, 15] | ||
``` |
There was a problem hiding this comment.
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
}
concepts/arrays/about.md
Outdated
// 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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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. |
concepts/arrays/about.md
Outdated
## 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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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. |
concepts/arrays/about.md
Outdated
## 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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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. |
There was a problem hiding this comment.
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:
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. |
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. |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[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] = [] | ||
``` | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An array can be checked if it's empty with [`isEmpty`][isEmpty] property. |
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) | ||
} | ||
} | ||
|
There was a problem hiding this comment.
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.
concepts/arrays/about.md
Outdated
|
||
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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. |
concepts/arrays/about.md
Outdated
|
||
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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. |
concepts/arrays/introduction.md
Outdated
|
||
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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. |
concepts/arrays/about.md
Outdated
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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. |
concepts/arrays/about.md
Outdated
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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. |
concepts/arrays/about.md
Outdated
|
||
## Add an Array to an Array | ||
|
||
Array can be added to the end of an array using the `+` operator. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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. |
No description provided.