我如何在功能上将对象数组转换为不同的结构

时间:2018-05-24 09:36:49

标签: arrays swift functional-programming

我有一系列字体,每个字体都有一个familyName和一个fontName。

我想将它们转换为表单中的元组数组(familyName:String,fontNames:[String])。

我觉得应该有一种简单的功能性方法来做到这一点,但无法解决这个问题。我最接近的是对reduce的两次调用:先进入字典然后进入数组。

let dictionary = fonts.reduce(into [String : [String]]() ) { result, font in
    let array = result[font.fontFamily] ?? []
    result[fontFamily] = array + [font.fontName]
}

let array = dictionary(into: [(String, [String])]() ) { result, element in
    result.append( (element.key, element.value.sorted()) )
}.sorted { $0.0 < $1.0 }

我还在元组数组中对元组数组和fontNames数组进行排序。

有没有办法可以避开中间词典?

非常感谢。

更新 我创建了一个游乐场来向sanjaykmwt展示他们的建议结果:

struct Font {
    let family: String
    let name: String
}

let fonts = [
    Font(family: "ABC", name: "abc"),
    Font(family: "ABC", name: "def"),
    Font(family: "ABC", name: "ghi"),
    Font(family: "XYZ", name: "xyz"),
    Font(family: "XYZ", name: "uvw")
]

let sortedFamily = fonts.sorted(by: { (lhs, rhs) -> Bool in
    return lhs.family < rhs.family
})

let dict = sortedFamily.map({["family":$0.family,
                              "fonts":$0.name]})

print("dict: \(dict)")

输出:

dict: [["family": "ABC", "fonts": "abc"], ["family": "ABC", "fonts": "def"], ["family": "ABC", "fonts": "ghi"], ["family": "XYZ", "fonts": "xyz"], ["family": "XYZ", "fonts": "uvw"]]

3 个答案:

答案 0 :(得分:2)

如果您的Fonts数组fontFamilyfontName

您可以grouping然后map

 // Array Of Fonts Example

        let array = [Font.init(fontFamily: "Cago", fontName: "AA"),
                                     Font.init(fontFamily: "Cago", fontName: "CCCC"),
                                     Font.init(fontFamily: "Mango", fontName: "AAsss"),
                                      Font.init(fontFamily: "Mango", fontName: "mngoo")]

         // Grouping

        let groupedByFamilayName = Dictionary.init(grouping: array) {$0.fontFamily}

        // Map
        let arrayOfTuple =  groupedByFamilayName.map { (key,array) -> (String,[String]) in
                            return (key,array.map({$0.fontName}))
                        }

     print(arrayOfTuple)

答案 1 :(得分:2)

扩大(或承包!)关于Abdelahad Darwish的回答......

let tuples = Dictionary(grouping: fonts) { $0.family }
    .map { (familyName: $0.key, fontNames: $0.value.map { $0.name }) }

print(tuples)
[(familyName: "XYZ", fontNames: ["xyz", "uvw"]), (familyName: "ABC", fontNames: ["abc", "def", "ghi"])]

答案 2 :(得分:-2)

    let sortedFamily = fonts.sorted(by: { (lhs, rhs) -> Bool in
        return lhs.family < rhs.family
    })

    let dict = sortedFamily.map({["family":$0.family,"fonts":$0.fonts.sorted()]})

尝试打印字典,您将获得一切排序

如果你想要更短,它可以是:

  let dict = fonts.sorted(by: { (lhs, rhs) -> Bool in
        return lhs.family < rhs.family
    }).map({["family":$0.family,"fonts":$0.fonts.sorted()]})