[swift-users] Recommended way to collect elements into collection-valued Dictionary?

Jens Persson jens at bitcycle.com
Thu Jun 15 17:12:27 CDT 2017


let question = """
    Please see the extension to Dictionary below.
    Is there a simpler or better way to accomplish the same thing?
    """
extension Dictionary where
    Value: RangeReplaceableCollection,
    Value: ExpressibleByArrayLiteral,
    Value.Element == Value.ArrayLiteralElement
{
    mutating func append(value: Value.Element, for key: Key) {
        if self[key]?.append(value) == nil {
            self[key] = [value]
        }
    }
}
func usageExample() {
    var characterIndexMap = [Character : [Int]]()
    for (ci, c) in question.characters.enumerated() {
        characterIndexMap.append(value: ci, for: c)
    }
    for (character, indices) in characterIndexMap
        .sorted(by: { $0.1.count > $1.1.count })
    {
        print("\(character.debugDescription) occurs at indices:", indices)
    }
}
usageExample()


/* Will print:
" " occurs at indices: [6, 10, 14, 24, 27, 38, 48, 54, 56, 64, 67, 74, 78,
81, 92, 96, 101]
"e" occurs at indices: [2, 5, 8, 9, 13, 15, 18, 40, 51, 53, 62, 69, 72, 95,
100]
"t" occurs at indices: [11, 17, 25, 31, 49, 70, 71, 79, 93, 102]
"s" occurs at indices: [4, 7, 20, 47, 57, 90, 97]
"o" occurs at indices: [22, 26, 33, 42, 65, 80, 85]
"i" occurs at indices: [21, 29, 32, 58, 89, 104]
"a" occurs at indices: [3, 35, 55, 76, 82, 98]
"h" occurs at indices: [12, 50, 91, 94, 103]
"r" occurs at indices: [36, 52, 63, 66, 73]
"l" occurs at indices: [1, 41, 61, 88]
"n" occurs at indices: [19, 23, 34, 105]
"m" occurs at indices: [59, 86, 99]
"c" occurs at indices: [30, 83, 84]
"p" occurs at indices: [60, 87]
"w" occurs at indices: [43, 75]
"y" occurs at indices: [37, 77]
"b" occurs at indices: [39, 68]
"." occurs at indices: [44]
"\n" occurs at indices: [45]
"I" occurs at indices: [46]
"x" occurs at indices: [16]
"?" occurs at indices: [107]
"g" occurs at indices: [106]
"D" occurs at indices: [28]
"P" occurs at indices: [0]
*/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170616/3f883fb4/attachment.html>


More information about the swift-users mailing list