在斯威夫特,我想QUOT&;&加盟QUOT;在对元组序列的两个序列 [英] In Swift I would like to "join" two sequences in to a sequence of tuples

查看:85
本文介绍了在斯威夫特,我想QUOT&;&加盟QUOT;在对元组序列的两个序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想加入两个(或更多)的序列那么这将创建一个元组的序列。其中第一元组将包含各序列和第二元组将包含第二元件等的第一个元素...下面是一个例子函数,接受两个阵列,并创建一个元组的第三阵列。那么我可以用这个序列与地图(),滤波器()和减少()函数来处理。

我的例子作品,但缺乏在一大堆方法。它是用于阵列不是对所有的序列,它停止时第一序列用完元件产生元组。我想尼尔斯是在元组的短序列,可以不再提供元素。这只是两个数组,我想它是任何数目的序列,

部分解决方案将是有益的。我是一个函数式编程新手,所以这个功能的正确名称也将是AP preciated。也许它已经在switfz库我只知道它叫什么。我选择了加盟,因为它是大致相同的SQL加盟,这也是建立元组(又名行)

  FUNC加入< T> (S1:数组< T&GT ;, S2:数组< T>) -  GT;阵列≤(T,T)> {变种G1 = s1.generate();
变种G2 = s2.generate();VAR结果:数组≤(T,T)> = []而让E1 = g1.next(){    如果让E2 = g2.next(){
        result.append((E1,E2))
    }
}
返回结果
}
海明类{
    类FUNC计算(输入:字符串,反对:字符串) - GT; INT {
        返回联接(阵列(输入),阵列(反对))。降低(0){回报率(1.0 $!= $ 1.1)? $ 0 + 1:$ 0}
    }
}Hamming.compute(ABCDE,ABCDF)// 1


解决方案

有已经是调用一个函数 ZIP2

  VAR第一= [0,1,2,3]
VAR秒= [零,一,二,三]阵列(ZIP2(一,二))
//(0,零),(1,1),(2,二),(3,三化)

此功能但是不垫为零,它也采用了最短的两个序列中过去了。注意,虽然它并不需要该类型的两个序列之间匹配,并且,它需要的任何序列,而不是仅仅阵列

下面是Zip2WithNilPadding我自己的定义实现:

 结构Zip2WithNilPadding< T:序列类型,U:序列类型计算值:序列类型{
    typealias发电机= GeneratorOf≤(?T.Generator.Element ?, U.Generator.Element)GT;    让第一:T已
    让第二:U    的init(_第一:T,_第二:U){
        self.first =第一
        self.second =第二
    }    FUNC产生() - GT;发电机 {
        无功发生器1:T.Generator? = first.generate()
        VAR generator2:U.Generator? = second.generate()        返回GeneratorOf≤(T.Generator.Element ?, U.Generator.Element?)>(){
            让中element1 =发生器1?。接下来()
            让我们在element2 = generator2?。接下来()
            如果中element1 ==零和放大器;&安培; element2的== {为零
                零回报
            }
            否则,如果中element1 == {为零
                发生器1 =零
            }
            否则,如果在element2 == {为零
                generator2 =零
            }
            收益率(中element1,element2的)
        }
    }
}VAR第一= [0,1,2]
VAR秒= [零,一,二,三,四]
阵列(Zip2WithNilPadding(一,二))

如果您对具体的实施问题,让我知道,我会尽力澄清。这种实现也应该帮助你创建ZIP的花费序列的数组。不幸的是在这种情况下,他们将所有必须是同一类型的序列,因为不能有泛型的可变量

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

解决方案

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")

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.

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))

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.

这篇关于在斯威夫特,我想QUOT&;&加盟QUOT;在对元组序列的两个序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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