Swift Tuple index using a variable as the index? - swift

Swift Tuple index using a variable as the index?
Anyone know if it is possible to use a variable as the index for a Swift tuple index. I wish to select and item from a tuple using a random number. I have the random number as a variable but cannot see how to use that as the index for a tuple. I have searched various places already

Make sure you've chosen the correct data structure
If you've reached a point where you need to access tuple members as if "indexed", you should probably look over your data structure to see if a tuple is really the right choice for you in this case. As MartinR mentions in a comment, perhaps an array would be a more appropriate choice, or, as mentioned by simpleBob, a dictionary.
Technically: yes, tuple members can be accessed by index-style
You can make use of runtime introspection to access the children of a mirror representation of the tuple, and choose a child value given a supplied index.
E.g., for 5-tuples where all elements are of same type:
func getMemberOfFiveTuple<T>(tuple: (T, T, T, T, T), atIndex: Int) -> T? {
let children = Mirror(reflecting: tup).children
guard case let idx = IntMax(atIndex)
where idx < children.count else { return nil }
return children[children.startIndex.advancedBy(idx)].value as? T
}
let tup = (10, 11, 12, 13, 14)
let idx = 3
if let member = getMemberOfFiveTuple(tup, atIndex: idx) {
print(member)
} // 13
Note that the type of child.value is Any, hence we need to perform an attempted type conversion back to the element type of the tuple. In case your tuple contains different-type members, the return type cannot be known at compile time, and you'd probably have to keep using type Any for the returned element.

So, what people here are saying - don't use a tuple if you don't actually need one - is correct. However, there is one caveat.
In my experience, using a Swift Array with a static size in performance critical code actually slows down your code by quite a bit. Take a look:
let clz_lookup = [4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0]
func clz(x : UInt32) -> Int {
guard x != 0 else { return 32 }
var x = x
var n = 0
if (x & 0xFFFF0000) == 0 { n = 16; x <<= 16 } else { n = 0 }
if (x & 0xFF000000) == 0 { n += 8; x <<= 8 }
if (x & 0xF0000000) == 0 { n += 4; x <<= 4 }
return n + clz_lookup[Int(x >> (32 - 4))]
}
This is a fairly straight forward piece of code that uses a cached lookup table at the end. However, if we profile this using instruments we'll see that the lookup is actually much slower than the if statements it replaces! My guess is the reason for that is probably bounds checking or something similar, plus that there is an additional pointer indirection in the implementation.
In any case, that's not good, and since the above is a function that is potentially being called a lot we want to optimise it. A simple technique for that is creating a Tuple instead of an Array, and then simply using withUnsafeMutablePointer to get a pointer directly into the Tuple (which is laid out as a contiguous, static array in memory - exactly what we want in this case!):
var clz_table = (4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0)
let clz_lookup = withUnsafeMutablePointer(to: &clz_table) { $0.withMemoryRebound(to: Int.self, capacity: 15) { $0 } }
This can be used exactly like a regular C-style array in that you can index into it and even change the values at particular indices, but there is no way to grow this array. This method of indexing should be much faster than the other solutions that are mentioning reflection, but is potentially unsafe if used wrong.

Reference: https://gist.github.com/ctreffs/785db636d68a211b25c989644b13f301
In Swift 5:
func tupleToArray<Tuple, Value>(tuple: Tuple) -> [Value] {
let tupleMirror = Mirror(reflecting: tuple)
assert(tupleMirror.displayStyle == .tuple, "Given argument is no tuple")
assert(tupleMirror.superclassMirror == nil, "Given tuple argument must not have a superclass (is: \(tupleMirror.superclassMirror!)")
assert(!tupleMirror.children.isEmpty, "Given tuple argument has no value elements")
return tupleMirror.children.compactMap { (child: Mirror.Child) -> Value? in
let valueMirror = Mirror(reflecting: child.value)
assert(valueMirror.subjectType == Value.self, "Given tuple argument's child type (\(valueMirror.subjectType)) does not reflect expected return value type (\(Value.self))")
return child.value as? Value
}
}
let sss: [String] = tupleToArray(tuple: ("📷", "🍉🍉", "✈️✈️✈️" ,"🍎🍎🍎"))
print(sss)

You can cast a tuple to a buffer only if it has an homogeneous type.
But before doing so, wonder if an array can't do the job.
var tuple: (Int, Int, Int, Int) = (0,1,2,3)
withUnsafeMutablePointer(to: &tuple) { pointer in
pointer.withMemoryRebound(to: Int.self, capacity: 4) { buffer in
buffer[3] = 0
}
}
print(tuple)
result:
(0, 1, 2, 0)

As long as tuple index is just default name, and you can provide your own names
let testTuple = (first: 1, second: 2.0)
let first: Int = testTuple.first
let second: Double = testTuple.second
you can not use variable as index. Pretty close question "can I use KVC for tuple?", and answer - you can't

Related

Ambiguous reference to member 'map' [duplicate]

I'm reading "The swift programming language 4.2". In the beginning chapter, page 23, I have this following requirement:
Rewrite the closure to return zero for all odd number
And my solution is:
let myArray: [Int] = [1, 2, 3, 4, 5]
myArray.map({ (number: Int) in
if number % 2 != 0 {
return 0
} else {
return number
}
})
But I have this following error:
Ambiguous reference to member 'map'
I really do not understand why I'm wrong, why my 'myArray' can not references to 'map' member?
Could you give me and explanation of this error and the right solution for this?
Thank you!
Make it less ambiguous by specifying the return type with as [Int]:
myArray.map({ (number: Int) in
if number % 2 != 0 {
return 0
} else {
return number
}
}) as [Int]
Or :
let result: [Int] = myArray.map({ (number: Int) in
if number % 2 != 0 {
return 0
} else {
return number
}
})
print(result) //[0, 2, 0, 4, 0]
As noted by vadian: The ambiguity comes from the fact that the generic return type in the closure cannot be inferred.
To understand how the compiler infers the return type of a closure, let's go back to the syntax of a closure :
let myClosure: returnType = { (params) -> returnType in
statements
}
Here, the type of myClosure is returnType. And it's set in two places: after :, and after ->. You could use type inference by removing the returnType from one of the two places, but not both.
So you could remove it from inside the curly braces (like in the code above) :
let result: [Int] = myArray.map({ (number: Int) in
Or just after the variable name, and specifying the return type of the closure inside the the curly braces:
let result = myArray.map({ (number: Int) -> Int in
Rather than annotating the parameter type you have to specify the return type of the closure
let myArray = [1, 2, 3, 4, 5] // You don't even need to annotate the array, the compiler knows the type.
let result = myArray.map({ number -> Int in
if number % 2 != 0 {
return 0
} else {
return number
}
})
Or with trailing closure syntax and shorthand argument names. In this case the compiler can infer everything
let result = myArray.map { $0 % 2 != 0 ? 0 : $0 }

Casting Any to Array

Here's the code I try to make work
struct A {
var x:Int = 0
}
struct B {
var y:Int = 0
}
var c: [String:Any] = [
"a":[A()],
"b":[B()]
]
for (key, value) in c {
let arr = value as! [Any]
}
It just throws exception. The runtime exception is raised when trying to cast Any to [Any].
The main thing I want to achieve is iterate through the elements of Any, if Any is array. To me it was natural to cast Any to [Any], but for some reason it doesn't work. So how can I do this in obvious thing in swift?
I saw some workarounds to cast Any to [A] or [B], but that's not my case, because the array can contain just an arbitrary struct.
You can make use of runtime introspection to inspect whether values in your dictionary are of collection type, and if so, iterate over their children (= elements, for array case), and append these to an actual array of Any, in so letting Swift know some Any values in your dictionary are actually arrays.
/* Example setup */
struct A {
var x: Int
init(_ x: Int) { self.x = x }
}
struct B {
var y: Int
init(_ y: Int) { self.y = y }
}
var c: [String:Any] = [
"a": [A(1), A(2)],
"b": [B(3)],
"c": "JustAString",
"d": A(0)
]
E.g. as follows
/* runtime introspection to extract array values from dictionary */
var commonAnyArr: [[Any]] = []
for (_, value) in c {
if case let m = Mirror(reflecting: value)
where (m.displayStyle ?? .Struct) == .Collection {
let arr = m.children.map { $0.value }
commonAnyArr.append(arr)
}
}
/* resulting array of any arrs, that Swift now recognize as actual arrays */
commonAnyArr.forEach { print($0) }
/* [B(y: 3)]
[A(x: 1), A(x: 2)] */
commonAnyArr.flatten().forEach { print($0) }
/* B(y: 3)
A(x: 1)
A(x: 2) */
Alternatively, use the runtime introspection to construct a new dictionary, containing only the key-value pairs of c where the underlying value wrapped by the Any value is in fact an array (however in the new dictionary explicitly specifying for swift that the values are arrays of Any).
/* runtime introspection to extract array values from dictionary */
var dictOfAnyArrs: [String: [Any]] = [:]
for (key, value) in c {
if case let m = Mirror(reflecting: value)
where (m.displayStyle ?? .Struct) == .Collection {
let arr = m.children.map { $0.value }
dictOfAnyArrs[key] = arr
}
}
/* "remaining" dictionary keys now only with [Arr] values */
for (key, arr) in dictOfAnyArrs {
for element in arr {
print("Do something with element \(element)")
}
print("---")
}
/* Do something with element B(y: 3)
---
Do something with element A(x: 1)
Do something with element A(x: 2)
--- */
Just note that the above could be considered somewhat "hacky" (in the eyes of Swift and its pride in static typing and runtime safety), and possibly mostly interesting more out of a technical aspect rather than to be used in actual production code (I would personally never allow anything like the above in production of my own). Perhaps if you take a step back and look at how you've reached this issue, you could re-work your code and design to not reach a point where you need to resort to runtime hacks.

Swift running sum

I'd like a function runningSum on an array of numbers a (or any ordered collection of addable things) that returns an array of the same length where each element i is the sum of all elements in A up to an including i.
Examples:
runningSum([1,1,1,1,1,1]) -> [1,2,3,4,5,6]
runningSum([2,2,2,2,2,2]) -> [2,4,6,8,10,12]
runningSum([1,0,1,0,1,0]) -> [1,1,2,2,3,3]
runningSum([0,1,0,1,0,1]) -> [0,1,1,2,2,3]
I can do this with a for loop, or whatever. Is there a more functional option? It's a little like a reduce, except that it builds a result array that has all the intermediate values.
Even more general would be to have a function that takes any sequence and provides a sequence that's the running total of the input sequence.
The general combinator you're looking for is often called scan, and can be defined (like all higher-order functions on lists) in terms of reduce:
extension Array {
func scan<T>(initial: T, _ f: (T, Element) -> T) -> [T] {
return self.reduce([initial], combine: { (listSoFar: [T], next: Element) -> [T] in
// because we seeded it with a non-empty
// list, it's easy to prove inductively
// that this unwrapping can't fail
let lastElement = listSoFar.last!
return listSoFar + [f(lastElement, next)]
})
}
}
(But I would suggest that that's not a very good implementation.)
This is a very useful general function, and it's a shame that it's not included in the standard library.
You can then generate your cumulative sum by specializing the starting value and operation:
let cumSum = els.scan(0, +)
And you can omit the zero-length case rather simply:
let cumSumTail = els.scan(0, +).dropFirst()
Swift 4
The general sequence case
Citing the OP:
Even more general would be to have a function that takes any sequence
and provides a sequence that's the running total of the input
sequence.
Consider some arbitrary sequence (conforming to Sequence), say
var seq = 1... // 1, 2, 3, ... (CountablePartialRangeFrom)
To create another sequence which is the (lazy) running sum over seq, you can make use of the global sequence(state:next:) function:
var runningSumSequence =
sequence(state: (sum: 0, it: seq.makeIterator())) { state -> Int? in
if let val = state.it.next() {
defer { state.sum += val }
return val + state.sum
}
else { return nil }
}
// Consume and print accumulated values less than 100
while let accumulatedSum = runningSumSequence.next(),
accumulatedSum < 100 { print(accumulatedSum) }
// 1 3 6 10 15 21 28 36 45 55 66 78 91
// Consume and print next
print(runningSumSequence.next() ?? -1) // 120
// ...
If we'd like (for the joy of it), we could condense the closure to sequence(state:next:) above somewhat:
var runningSumSequence =
sequence(state: (sum: 0, it: seq.makeIterator())) {
(state: inout (sum: Int, it: AnyIterator<Int>)) -> Int? in
state.it.next().map { (state.sum + $0, state.sum += $0).0 }
}
However, type inference tends to break (still some open bugs, perhaps?) for these single-line returns of sequence(state:next:), forcing us to explicitly specify the type of state, hence the gritty ... in in the closure.
Alternatively: custom sequence accumulator
protocol Accumulatable {
static func +(lhs: Self, rhs: Self) -> Self
}
extension Int : Accumulatable {}
struct AccumulateSequence<T: Sequence>: Sequence, IteratorProtocol
where T.Element: Accumulatable {
var iterator: T.Iterator
var accumulatedValue: T.Element?
init(_ sequence: T) {
self.iterator = sequence.makeIterator()
}
mutating func next() -> T.Element? {
if let val = iterator.next() {
if accumulatedValue == nil {
accumulatedValue = val
}
else { defer { accumulatedValue = accumulatedValue! + val } }
return accumulatedValue
}
return nil
}
}
var accumulator = AccumulateSequence(1...)
// Consume and print accumulated values less than 100
while let accumulatedSum = accumulator.next(),
accumulatedSum < 100 { print(accumulatedSum) }
// 1 3 6 10 15 21 28 36 45 55 66 78 91
The specific array case: using reduce(into:_:)
As of Swift 4, we can use reduce(into:_:) to accumulate the running sum into an array.
let runningSum = arr
.reduce(into: []) { $0.append(($0.last ?? 0) + $1) }
// [2, 4, 6, 8, 10, 12]
By using reduce(into:_:), the [Int] accumulator will not be copied in subsequent reduce iterations; citing the Language reference:
This method is preferred over reduce(_:_:) for efficiency when the
result is a copy-on-write type, for example an Array or a
Dictionary.
See also the implementation of reduce(into:_:), noting that the accumulator is provided as an inout parameter to the supplied closure.
However, each iteration will still result in an append(_:) call on the accumulator array; amortized O(1) averaged over many invocations, but still an arguably unnecessary overhead here as we know the final size of the accumulator.
Because arrays increase their allocated capacity using an exponential
strategy, appending a single element to an array is an O(1) operation
when averaged over many calls to the append(_:) method. When an array
has additional capacity and is not sharing its storage with another
instance, appending an element is O(1). When an array needs to
reallocate storage before appending or its storage is shared with
another copy, appending is O(n), where n is the length of the array.
Thus, knowing the final size of the accumulator, we could explicitly reserve such a capacity for it using reserveCapacity(_:) (as is done e.g. for the native implementation of map(_:))
let runningSum = arr
.reduce(into: [Int]()) { (sums, element) in
if let sum = sums.last {
sums.append(sum + element)
}
else {
sums.reserveCapacity(arr.count)
sums.append(element)
}
} // [2, 4, 6, 8, 10, 12]
For the joy of it, condensed:
let runningSum = arr
.reduce(into: []) {
$0.append(($0.last ?? ($0.reserveCapacity(arr.count), 0).1) + $1)
} // [2, 4, 6, 8, 10, 12]
Swift 3: Using enumerated() for subsequent calls to reduce
Another Swift 3 alternative (with an overhead ...) is using enumerated().map in combination with reduce within each element mapping:
func runningSum(_ arr: [Int]) -> [Int] {
return arr.enumerated().map { arr.prefix($0).reduce($1, +) }
} /* thanks #Hamish for improvement! */
let arr = [2, 2, 2, 2, 2, 2]
print(runningSum(arr)) // [2, 4, 6, 8, 10, 12]
The upside is you wont have to use an array as the collector in a single reduce (instead repeatedly calling reduce).
Just for fun: The running sum as a one-liner:
let arr = [1, 2, 3, 4]
let rs = arr.map({ () -> (Int) -> Int in var s = 0; return { (s += $0, s).1 } }())
print(rs) // [1, 3, 6, 10]
It does the same as the (updated) code in JAL's answer, in particular,
no intermediate arrays are generated.
The sum variable is captured in an immediately-evaluated closure returning the transformation.
If you just want it to work for Int, you can use this:
func runningSum(array: [Int]) -> [Int] {
return array.reduce([], combine: { (sums, element) in
return sums + [element + (sums.last ?? 0)]
})
}
If you want it to be generic over the element type, you have to do a lot of extra work declaring the various number types to conform to a custom protocol that provides a zero element, and (if you want it generic over both floating point and integer types) an addition operation, because Swift doesn't do that already. (A future version of Swift may fix this problem.)
Assuming an array of Ints, sounds like you can use map to manipulate the input:
let arr = [0,1,0,1,0,1]
var sum = 0
let val = arr.map { (sum += $0, sum).1 }
print(val) // "[0, 1, 1, 2, 2, 3]\n"
I'll keep working on a solution that doesn't use an external variable.
I thought I'd be cool to extend Sequence with a generic scan function as is suggested in the great first answer.
Given this extension, you can get the running sum of an array like this: [1,2,3].scan(0, +)
But you can also get other interesting things…
Running product: array.scan(1, *)
Running max: array.scan(Int.min, max)
Running min: array.scan(Int.max, min)
Because the implementation is a function on Sequence and returns a Sequence, you can chain it together with other sequence functions. It is efficient, having linear running time.
Here's the extension…
extension Sequence {
func scan<Result>(_ initialResult: Result, _ nextPartialResult: #escaping (Result, Self.Element) -> Result) -> ScanSequence<Self, Result> {
return ScanSequence(initialResult: initialResult, underlying: self, combine: nextPartialResult)
}
}
struct ScanSequence<Underlying: Sequence, Result>: Sequence {
let initialResult: Result
let underlying: Underlying
let combine: (Result, Underlying.Element) -> Result
typealias Iterator = ScanIterator<Underlying.Iterator, Result>
func makeIterator() -> Iterator {
return ScanIterator(previousResult: initialResult, underlying: underlying.makeIterator(), combine: combine)
}
var underestimatedCount: Int {
return underlying.underestimatedCount
}
}
struct ScanIterator<Underlying: IteratorProtocol, Result>: IteratorProtocol {
var previousResult: Result
var underlying: Underlying
let combine: (Result, Underlying.Element) -> Result
mutating func next() -> Result? {
guard let nextUnderlying = underlying.next() else {
return nil
}
previousResult = combine(previousResult, nextUnderlying)
return previousResult
}
}
One solution using reduce:
func runningSum(array: [Int]) -> [Int] {
return array.reduce([], combine: { (result: [Int], item: Int) -> [Int] in
if result.isEmpty {
return [item] //first item, just take the value
}
// otherwise take the previous value and append the new item
return result + [result.last! + item]
})
}
I'm very late to this party. The other answers have good explanations. But none of them have provided the initial result, in a generic way. This implementation is useful to me.
public extension Sequence {
/// A sequence of the partial results that `reduce` would employ.
func scan<Result>(
_ initialResult: Result,
_ nextPartialResult: #escaping (Result, Element) -> Result
) -> AnySequence<Result> {
var iterator = makeIterator()
return .init(
sequence(first: initialResult) { partialResult in
iterator.next().map {
nextPartialResult(partialResult, $0)
}
}
)
}
}
extension Sequence where Element: AdditiveArithmetic & ExpressibleByIntegerLiteral {
var runningSum: AnySequence<Element> { scan(0, +).dropFirst() }
}

Explanation of sorted(by:) in Swift

I am a little confused regarding the logic behind the sorted(by:) function in Swift (it was sort() in Swift 2). Take the code below for instance...how does the return type bool yield in the reverse ordering of the numbers?
let aBunchOfNumbers = [1,2,3,4,5,6,7,8,9,10]
let reverseSortClosure: (Int, Int) -> Bool = {
(numberOne: Int, numberTwo: Int) -> Bool in
if numberOne < numberTwo {
return true
}
return false
}
aBunchOfNumbers.sorted(by: reverseSortClosure)
(sort(_:) was renamed sorted(by:) since Swift 3)
From the docs:
https://docs.swift.org/swift-book/LanguageGuide/Closures.html#ID95
The sorted(by:) method accepts a closure that takes two arguments of the same type as the array’s contents, and returns a Bool value to say whether the first value should appear before or after the second value once the values are sorted. The sorting closure needs to return true if the first value should appear before the second value, and false otherwise.
That is, the type class for the function that sorted() accepts looks like (T, T) -> Bool, where T is the type of the inout array you want to sort. The function returns true IF the first value of the function should appear before the second value. Swift uses this function to order the list you pass in.
In Swift 2 the sort() method needs to be used as an extension of your array. aBunchOfNumbers.sort(reverseSortClosure) and if you want to reverse it you have to use >.
let aBunchOfNumbers = [1,2,3,4,5,6,7,8,9,10]
let reverseSortClosure: (Int, Int) -> Bool = {
(lhs, rhs) -> Bool in
return lhs > rhs
}
let reversed = aBunchOfNumbers.sort(reverseSortClosure) // [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
You can also simplify your code as follow:
let reversed = aBunchOfNumbers2.sort(>)
The sort method is sorting the array, so the method needs to know what value goes first, second, third, etc. The value that goes first is the value that is true, and the value which goes second is false. That is why the sort method returns in a bool. Also, your code is really complicated and not at all swifty. This does the same thing, just a lot faster.
let aBunchOfNumbers = [1,2,3,4,5,6,7,8,9,10]
aBunchOfNumbers.sort({
$0 > $1 })
Don't be worried if you're not getting closures at first. They are difficult to understand and the syntax is somewhat unique to swift.
Hope this helps
Perhaps you are learning about closures, but you don't need one here. The sort() function takes a second argument that is a function to compare two values - the function returns true if its first argument appears before the second argument.
In your case, the array contains Int - there is already a perfectly good function to compare Ints - it is >. Use it like this:
1> let result = [1,2,3,4].sort(>)
result: [Int] = 4 values {
[0] = 4
[1] = 3
[2] = 2
[3] = 1
}

Efficiently aggregating an array of non-optionals without a default value

I need to efficiently aggregate an array of non-optional values, knowing its size, having a way to get its values, but not having a default value.
Following is a rather synthetic example, resembling what I need. It won't compile, but it will give you the idea:
public func array<A>( count: Int, getValue: () -> A ) -> Array<A> {
var array = [A](count: count, repeatedValue: nil as! A)
var i = 0
while (i < count) {
array[i] = getValue()
i++
}
return array
}
Please note that the result of type Array<A?> won't do, I need non-optionals. Also note that the solution must be efficient, it must not do any extra traversals.
You can make a working function from your example code by using
the append() method to add array elements:
public func array<A>(count: Int, #noescape getValue: () -> A) -> [A] {
var array = [A]()
array.reserveCapacity(count)
for _ in 0 ..< count {
array.append(getValue())
}
return array
}
The #noescape
attribute tells the compiler that the passed closure does not outlive
the function call, this allows some performance optimizations,
compare #noescape attribute in Swift 1.2.
But it is easier to use the map() method of CollectionType:
/// Return an `Array` containing the results of mapping `transform`
/// over `self`.
///
/// - Complexity: O(N).
#warn_unused_result
public func map<T>(#noescape transform: (Self.Generator.Element) throws -> T) rethrows -> [T]
In your case:
public func array<A>(count: Int, #noescape getValue: () -> A) -> [A] {
let array = (0 ..< count).map { _ in getValue() }
return array
}
Here map() transforms each integer in the range 0 ... count-1
to an array element. The underscore in the closure indicates that
its argument (the current index) is not used.
I leave it to you to check which method is faster.
Example usage:
let a = array(10) { arc4random_uniform(10) }
print(a) // [3, 7, 9, 4, 2, 3, 1, 5, 9, 7] (Your output may be different :-)