Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: Immutable value passed on reduce function

I'm trying to do the following code that transforms an array of tuples into a dictionary but I'm receiving a compile error saying:

Immutable value of type '[String : String]' only has mutating members named 'updateValue'

var array = [("key0", "value0"), ("key1", "value1")]
var initial = [String: String]()
var final = array.reduce(initial) { (dictionary, tuple) in
    dictionary.updateValue(tuple.0, forKey: tuple.1)
    return dictionary
}

Why is that if initial was declared as var? Does it have to do with @noescape on reduce's signature?

func reduce<U>(initial: U, combine: @noescape (U, T) -> U) -> U
like image 286
Raphael Oliveira Avatar asked Jun 10 '15 15:06

Raphael Oliveira


3 Answers

You can simply make the dictionary parameter mutable by preceding it with var:

var final = array.reduce(initial) { (var dictionary, tuple) in
                                     ^^^

Note however that using reduce a new dictionary is created at each iteration, making the algorithm very inefficient. You might want to consider using a traditional foreach loop

like image 176
Antonio Avatar answered Nov 10 '22 02:11

Antonio


Swift 4 has a new variant:

var array = [("key0", "value0"), ("key1", "value1")]
var initial = [String: String]()
var final = array.reduce(into: initial) { dictionary, tuple in
    dictionary[tuple.0] = tuple.1
}

Which could be expressed:

var array = [("key0", "value0"), ("key1", "value1")]
let final: [String: String] = array.reduce(into: [:]){ $0[$1.0] = $1.1 }
like image 43
mxcl Avatar answered Nov 10 '22 01:11

mxcl


On Swift 3:

var final = array.reduce(initial) { (dictionary, tuple) -> [String: String] in
    var mutableDictionary = dictionary
    //.... make changes with mutableDictionary
    return mutableDictionary
}
like image 4
Igor Avatar answered Nov 10 '22 00:11

Igor