在 Swift 中,我想“加入"两个序列组成一个元组序列 [英] In Swift I would like to "join" two sequences in to a sequence of tuples

查看:44
本文介绍了在 Swift 中,我想“加入"两个序列组成一个元组序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想加入两个(或更多)序列,然后创建一个元组序列.其中第一个元组将包含每个序列的第一个元素,第二个元组将包含第二个元素,等等......下面是一个示例函数,它采用两个数组并创建第三个元组数组.然后我可以使用这个序列来处理 map()、filter() 和 reduce() 函数.

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.

我的例子有效,但在很多方面都缺乏.它用于数组而不是所有序列,当第一个序列用完元素时,它停止生成元组.对于不能再提供元素的短序列,我希望 nil 位于元组中.它仅适用于两个数组,我希望它适用于任意数量的序列,

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,

部分解决方案会有所帮助.我是一个函数式编程新手,所以这个函数的正确名称也将不胜感激.也许它已经在 switfz 库中了,我只知道它叫什么.我选择了join",因为它大致类似于 SQLjoin",后者也构建元组(又名行)

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

推荐答案

已经有一个名为 Zip2 的函数:

There is already a function for that called Zip2:

var first = [0,1,2,3]
var second = ["zero", "one", "two", "three"]

Array(Zip2(first,second))
// (0, "zero"), (1, "one"), (2, "two"), (3, "three")

然而,这个函数没有用 nil 填充,它也使用传入的两个序列中最短的一个.但请注意,它不要求两个序列之间的类型匹配,并且它需要任何序列,而不仅仅是数组.

This function however does not pad with nil and it also uses the shortest of the two passed in sequences. Notice though that it does not require that the types match between the two sequences and that it takes any sequence, not just arrays.

这是我自己自定义的 Zip2WithNilPadding 实现:

Here is my own custom implementation of Zip2WithNilPadding:

struct Zip2WithNilPadding<T: SequenceType,U: SequenceType>: SequenceType {
    typealias Generator = GeneratorOf<(T.Generator.Element?, U.Generator.Element?)>

    let first: T
    let second: U

    init(_ first: T, _ second: U) {
        self.first = first
        self.second = second
    }

    func generate() -> Generator {
        var generator1: T.Generator? = first.generate()
        var generator2: U.Generator? = second.generate()

        return GeneratorOf<(T.Generator.Element?, U.Generator.Element?)>() {
            let element1 = generator1?.next()
            let element2 = generator2?.next()
            if element1 == nil && element2 == nil {
                return nil
            }
            else if element1 == nil{
                generator1 = nil
            }
            else if element2 == nil {
                generator2 = nil
            }
            return (element1, element2)
        }
    }
}

var first = [0,1,2]
var second = ["zero", "one", "two", "three", "four"]
Array(Zip2WithNilPadding(first, second))

如果您对具体实现有疑问,请告诉我,我会尽力澄清.此实现还应该帮助您创建一个采用序列数组的 Zip.不幸的是,在这种情况下,它们都必须是相同类型的序列,因为您不能拥有可变数量的泛型.

If you have questions about the specific implementation let me know and I will try to clarify. This implementation should also help you in creating a Zip that takes an array of sequences. Unfortunately in that case they would all have to be a sequence of the same type because you can't have a variable amount of generics.

这篇关于在 Swift 中,我想“加入"两个序列组成一个元组序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆