Josh Paradroid
Josh Paradroid

Reputation: 1414

How may I functionally transform array of objects into a different structure

I've got an array of fonts which each have a familyName and a fontName.

I would like to transform them into an array of tuples in the form (familyName: String, fontNames: [String]).

I feel like there should be an easy functional way to do this, but can't work it out. The closest I've got is two calls to reduce: First into a dictionary and then into an array.

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 }

I'm also sorting the array of tuples and the array of fontNames in the array of tuples.

Is there a way I can avoid the intermediary dictionary?

Many thanks.

Update I created a playground to show sanjaykmwt the results of their suggestions:

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

Output:

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

Upvotes: 1

Views: 83

Answers (3)

Ashley Mills
Ashley Mills

Reputation: 53121

Expanding (or contracting!) on Abdelahad Darwish's answer…

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

Upvotes: 2

sanjaykmwt
sanjaykmwt

Reputation: 605

    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()]})

try and print the dict you will get everything sorted

if you want even shorter it can be:

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

Upvotes: -2

Abdelahad Darwish
Abdelahad Darwish

Reputation: 6067

if You have an array of Fonts with fontFamily, fontName

you can make grouping then 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)

Upvotes: 2

Related Questions