Anyway... back to challenges, nobody ever became a better programmer by simply talking.
As I mentioned I'll be posting solutions to these problems in due course; next one up is the missing integer (but I'll probably postpone anything on that until Friday, just in the event that some else cracks this for us). So please feel free to try your hand at any of these; I'm happy to assist at any time, simply PM or respond directly on this thread.
Btw I also didn't forget about @cguy's challenge -- but at this stage his follow on questions are probably too much, too complex, too soon, so we'll postpone that part until later; for now here's a quick Functional Programming rendition of the dot product of two vectors.
Description:
The zip command for those unaware; takes two input collections and merges into an array of tuple pairs, for example: [(6, 6), (5, 5), etc...], thereafter it's simple case of using reduce (fold left), to multiply the value within tuples and add it a sum total. 0 in the 'reduce(0)' command is our identity value i.e. the initial value that is proven neutral for addition; basically adding any number to 0, will always returns the number i.e. no side effect. For multiplication, the identity value is 1. $0 represents the carried forward sum total, and $1 refers to the duplex tuple created by the zip command, and in turn $1.0 references to the 1st vector value and $1.1 refers to the 2nd vector value.
Usage:
We start by creating 2 vectors: x and y -- the choice of the same value is just for simplicity sake (feel free to change this). The only restriction with dotProduct, is that both vectors must an equal value count.
And that's it, short and sweet, but missing all the follow on bits.
As I mentioned I'll be posting solutions to these problems in due course; next one up is the missing integer (but I'll probably postpone anything on that until Friday, just in the event that some else cracks this for us). So please feel free to try your hand at any of these; I'm happy to assist at any time, simply PM or respond directly on this thread.
Btw I also didn't forget about @cguy's challenge -- but at this stage his follow on questions are probably too much, too complex, too soon, so we'll postpone that part until later; for now here's a quick Functional Programming rendition of the dot product of two vectors.
Code:
import Foundation
public struct Vector{
public let coordinates: [Double]
public init(with coordinates: [Double]) {
self.coordinates = coordinates
}
}
public extension Vector {
public func dotProduct(with vector: Vector) -> Double {
assert(self.coordinates.count == vector.coordinates.count, "Aborted, coordinate mismatch: \(self.coordinates.count) vs. \(vector.coordinates.count).")
return zip(self.coordinates, vector.coordinates).reduce(0) { $0 + ($1.0 * $1.1) }
}
}
Description:
The zip command for those unaware; takes two input collections and merges into an array of tuple pairs, for example: [(6, 6), (5, 5), etc...], thereafter it's simple case of using reduce (fold left), to multiply the value within tuples and add it a sum total. 0 in the 'reduce(0)' command is our identity value i.e. the initial value that is proven neutral for addition; basically adding any number to 0, will always returns the number i.e. no side effect. For multiplication, the identity value is 1. $0 represents the carried forward sum total, and $1 refers to the duplex tuple created by the zip command, and in turn $1.0 references to the 1st vector value and $1.1 refers to the 2nd vector value.
Usage:
We start by creating 2 vectors: x and y -- the choice of the same value is just for simplicity sake (feel free to change this). The only restriction with dotProduct, is that both vectors must an equal value count.
Code:
let x = Vector(with: [6, 5, 4, 3, 2, 1])
let y = Vector(with: [6, 5, 4, 3, 2, 1])
print(x.dotProduct(with: y)) // 91.0 (6 * 6) + (5 * 5) + (4 * 4) + (3 * 3) + (2 * 2) + (1 * 1)
And that's it, short and sweet, but missing all the follow on bits.
Last edited: