1

I wrote the following code in a playground and it works fine, but it seems a little messy. Is there a more concise way of writing this in Swift?

I want a string to look like the following: 1, 12, 2, 12, 3, 12, 4, 13, 5, 13, 6, 13, 7, 14, 8, 14

I will not know how many values in each array and the arrays may not be even multiples, but I will know the relationship of 3 array1 values for each array2 value

let array1 = [1,2,3,4,5,6,7,8]  //sample 1st array
let array2 = [12,13,14]         //sample 2nd array

let relationshipInterval = 3
let remainder = (array1.count % relationshipInterval)
let multiples = (array1.count - remainder)/relationshipInterval
var string = ""
var array1Start = 0
var array1End = relationshipInterval-1
var array2Value = 0

for _ in 1...multiples {
    for array1value in array1[array1Start...array1End] {
    string += "\(array1value), "
    string += String(array2[array2Value])+", "
    }
    array1Start = array1End + 1
    array1End = array1Start + relationshipInterval - 1
    array2Value += 1
}

for array1value in array1[array1Start...array1Start+remainder-1] {
        string += "\(array1value), "
        string += String(array2[array2Value])+", "
}
print (string)  //prints 1, 12, 2, 12, 3, 12, 4, 13, 5, 13, 6, 13, 7, 14, 8, 14
1

1 Answer 1

3

I'm not sure if this covers all of the use cases, but using zip, flatMap, and joined you do this in a single step!

let array1 = [1,2,3,4,5,6,7,8]
let array2 = [12, 13, 14]

let text = zip(array1, array2.flatMap({ [$0, $0, $0] }))
    .flatMap({ [$0, $1] })
    .map({ String($0) })
    .joined(separator: ", ")

// text -> "1, 12, 2, 12, 3, 12, 4, 13, 5, 13, 6, 13, 7, 14, 8, 14"

Here's that one-liner broken into four steps so it's clearer what's happening at each stage:

// 1. create an array with each element in array2 is trippled:
let trippled = array2.flatMap({ item in
    return [item, item, item]
})
// trippled -> [12, 12, 12, 13, 13, 13, 14, 14, 14]

// 2. zip array1 with the trippled array:
let zipped = zip(array1, trippled)
// zipped -> Zip2Sequence(_sequence1: [1, 2, 3, 4, 5, 6, 7, 8], _sequence2: [12, 12, 12, 13, 13, 13, 14, 14, 14])

// 3. flatten the zipped aray
let combined = zipped.flatMap({ leftItem, rightItem in
    return [leftItem, rightItem]
})
// combined -> [1, 12, 2, 12, 3, 12, 4, 13, 5, 13, 6, 13, 7, 14, 8, 14]

// 4. tranform to a string
let text = combined.map({ item in
    return "\(item)"
}).joined(separator: ", ")
// text -> "1, 12, 2, 12, 3, 12, 4, 13, 5, 13, 6, 13, 7, 14, 8, 14"
3
  • That is perfect. I knew there had to be better way. I didn't know a lot about flatmap and am finding it makes things more concise in a lot of cases. Thanks.
    – Chris3643
    Jan 7, 2017 at 19:50
  • If I want the ratio to be, say 50:1, is there a shortened way of writing [$0,$0...] 50 times?
    – Chris3643
    Jan 7, 2017 at 20:31
  • 1
    yes! Array<Int>(repeating: $0, count: 3) is another way to achieve [$0, $0, $0] so you could use that but put in count: 50 instead.
    – MathewS
    Jan 7, 2017 at 20:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.