The zip() function takes two sequences and returns a sequence of tuples following:
output[i] = (sequence1[i], sequence2[i])
However, the sequences can potentially be of different dimensions. My question is how does the Swift language deal with this?
The docs were utterly useless.
Seems to me, there are two possibilities (in Swift):
Stop at end of the shortest
Stop at end of longest, filling with default constructor or a predefined value for shorter's element type
Swift uses the first option, the resulting sequence will have a length equal to the shorter of the two inputs.
For example:
let a: [Int] = [1, 2, 3]
let b: [Int] = [4, 5, 6, 7]
let c: [(Int, Int)] = zip(a, b) // [(1, 4), (2, 5), (3, 6)]
Apple's zip(_:_:) documentation has since been updated to answer this question:
https://developer.apple.com/documentation/swift/1541125-zip
If the two sequences passed to zip(_:_:) are different lengths, the resulting sequence is the same length as the shorter sequence. In this example, the resulting array is the same length as words:
let words = ["one", "two", "three", "four"]
let naturalNumbers = 1...Int.max
let zipped = Array(zip(words, naturalNumbers))
// zipped == [("one", 1), ("two", 2), ("three", 3), ("four", 4)]
Related
I have an array, each element of which is an array of two ints (like coordinate pairs):
`[ [1, 2], [2, 2], [11, 9], ... ]`
Elsewhere in my program there are places that need that info but as type Coord, which is defined as a tuple of (x: Int, y: Int).
What would be the best way to iterate over each element in the array and convert it to Coord tuples of (x: Int, y: Int)?
When you are sure that there exists only two objects in an internal array of main array, then you should try this
func getToupleFrom(arr: [Int]) -> (Int, Int) {
return (arr[0], arr[1])
}
Then define a global var to store the tuples, if you need them all together.
var arrayWithTuple: [(Int, Int)] = []
Your example array can have these values and can be iterated like this.
let arrayInt: [[Int]] = [[1,2], [3,4]]
for arr in arrayInt {
arrayWithTuple.append(getToupleFrom(arr: arr))
}
Try it and share results
You don't need to iterate, use map
let array = [ [1, 2], [2, 2], [11, 9]]
let tuples = array.map { ($0[0], $0[1]) }
However this will crash if the number of items in the inner arrays is less than 2.
I have tried below array example in my scala console.Declared immutable array, tried to change the array index values.Immutable should not allow modifying the values. I don't understand whats happening .can anyone explain, please.
val numbers=Array(1,2,3)
numbers(0)=5
print numbers
res1: Array[Int] = Array(5, 2, 3)
Thanks!
Declaring something as a val does not make it immutable. It merely prevents reassignment. For example, the following code would not compile:
val numbers = Array(1, 2, 3)
numbers = Array(5, 2, 3)
Notice that what changes in the above code is not something about the array object's internal state: what changes is the array that the name numbers references. At the first line, the name numbers refers to the array Array(1, 2, 3), but in the second line we try reassign the name numbers to the array Array(5, 2, 3) (which the compiler won't allow, since the name numbers is declared using val).
In contrast, the below code is allowed:
val numbers = Array(1, 2, 3)
numbers(0) = 5
In this code, the name numbers still points to the same array, but it's the internal state of the array that has changed. Using the val keyword for the name of an object cannot prevent that object's internal state from changing, the val keyword can only prevent the name from being reassigned to some other object.
You did not declare an immutable array. Array in Scala is mutable.
Where you might be confused is what exactly val vs var means. val does not make an object immutable, it makes the reference immutable so you can't reassign a different array to your variable, but you can still modify the contents since it is a mutable object.
If you want immutability you need to use val together with an immutable object like Vector or List.
Maybe your understanding of scala Array is not right.
Please pay attention to the following principle!
Arrays preserve order, can contain duplicates, and are mutable.
scala> val numbers = Array(1, 2, 3, 4, 5, 1, 2, 3, 4, 5)
numbers: Array[Int] = Array(1, 2, 3, 4, 5, 1, 2, 3, 4, 5)
scala> numbers(3) = 10
Lists preserve order, can contain duplicates, and are immutable.
scala> val numbers = List(1, 2, 3, 4, 5, 1, 2, 3, 4, 5)
numbers: List[Int] = List(1, 2, 3, 4, 5, 1, 2, 3, 4, 5)
scala> numbers(3) = 10
<console>:9: error: value update is not a member of List[Int]
numbers(3) = 10
What's the meaning of:
Separator.Iterator.Element == Self.Iterator.Element.Iterator.Element
in this (Swift standard library) swift instance method declaration?
func joined<Separator>(separator: Separator) ->
JoinedSequence<Array<Element>> where Separator : Sequence,
Separator.Iterator.Element == Self.Iterator.Element.Iterator.Element
and here is the example from Apple:
let nestedNumbers = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
let joined = nestedNumbers.joined(separator: [-1, -2])
print(Array(joined))
// Prints "[1, 2, 3, -1, -2, 4, 5, 6, -1, -2, 7, 8, 9]"
joined(separator:) is available to call on sequences of sequences:
extension Sequence where Iterator.Element : Sequence {
// ...
public func joined<Separator : Sequence>(separator: Separator) -> JoinedSequence<Self>
where Separator.Iterator.Element == Iterator.Element.Iterator.Element
}
It takes a separator: parameter, which itself must be a sequence. The constraint Separator.Iterator.Element == Iterator.Element.Iterator.Element simply means that the separator's element type must be the same as the inner element type of the sequence that joined(separator) is called on.
For example, you're calling it on a [[Int]]. It's Iterator.Element.Iterator.Element (the element type of the array's element type), is Int. Therefore the separator you use must be a sequence of Ints, which [Int] is.
Try passing in a [String] for the separator – you'll get a compiler error because String ≠ Int, and therefore doesn't meet the requirement Separator.Iterator.Element == Iterator.Element.Iterator.Element.
There is a serious lack of methods available to lists in Swift. It is really disappointing, coming from a Python background. For example, I want to remove the first element, something like this would work in Python:
mylist = mylist[1:]
How do I remove an element from a list (preferably by index, but I can do whatever method is easiest)?
Use removeAtIndex
var arr = [1, 2, 3]
arr.removeAtIndex(1)
If you want to remove a range of values, you can use removeRange:
var x = [1, 2, 3, 4, 5]
x.removeRange(1...2) // result is [1, 4, 5]
var x = [1, 2, 3, 4, 5]
x.removeRange(1..<2) // result is [1, 3, 4, 5]
Note that this method doesn't check for range bounds, so if you specify a range outside the array size, it will throw a runtime exception.
Want to know how scala arranges the data in set.
scala> val imm = Set(1, 2, 3, "four") //immutable variable
imm : scala.collection.immutable.Set[Any] = Set(1, 2, 3, four)
scala> var mu = Set(1, 2, 3, "four", 9)
mu: scala.collection.immutable.Set[Any] = Set(four, 1, 9, 2, 3)
scala> mu += "five"
scala> mu.toString
res1: String = Set(four, 1, 9, 2, 3, five)
Order remains as we insert in case of immutable Set but not in mutable Set.
Also no matter how many times I create a new set with var xyz = Set(1, 2, 3, "four", 9) the order of getting stored remains same Set(four, 1, 9, 2, 3). So it's not storing in random, there is some logic behind it which I want to know. Moreover, is there any advantage of such behavior?
Sets don't have an order. An item is in the set or it isn't. That's all you can know about a set.
If you require a certain ordering, you need to use an ordered set or possibly a sorted set.
Of course, any particular implementation of a set may or may not incidentally have an ordering which may or may not be stable across multiple calls, but that would be a purely accidental implementation detail that may change at any time, during an update of Scala or even between two calls.