I would like to join two (or more) sequences that would then create a sequence of tuples. Where the first tuple would contain the first element of each sequence and the second tuple would contain the second elements, etc ... Below is an example function that takes two arrays and creates a third array of tuples. I then can use this sequence to process with map(), filter() and reduce() functions.
My example works, but is lacking in a bunch of ways. It is for arrays not for all sequences, It stops generating tuples when the first sequence runs out of elements. I would like nils to be in the tuples for the short sequences that can no longer provide elements. It is only for two arrays, I would like it to be for any number of sequences,
Partial solutions would be helpful. I'm a functional programming newbie so the proper name for this function would also be appreciated. Maybe its is already in the switfz library I just know what it is called. I chose "join" because it is roughly similar to the SQL "join" which also builds tuples (a.k.a. rows)
func join<T> (s1: Array<T>, s2: Array<T>) -> Array<(T,T)> {
var g1 = s1.generate();
var g2 = s2.generate();
var result:Array<(T,T)> = []
while let e1 = g1.next() {
if let e2 = g2.next() {
result.append((e1,e2))
}
}
return result
}
class Hamming {
class func compute(input: String, against: String) -> Int {
return join(Array(input),Array(against)).reduce(0){ return ($1.0 != $1.1) ? $0 + 1 : $0 }
}
}
Hamming.compute("abcde","abcdf") // 1