Mapping a Dictionary to a Dictionary?


(Jens Alfke) #1

I may be missing something obvious, but I can’t find a library function that maps a Dictionary to another Dictionary, transforming the keys and/or values. The only method I’ve found is the regular map(), which results in an Array. Does this just not exist in the standard library?

—Jens


(Dmitri Gribenko) #2

Yes, this functionality does not exist.

Dmitri

···

On Mon, Aug 29, 2016 at 11:36 PM, Jens Alfke via swift-users <swift-users@swift.org> wrote:

I may be missing something obvious, but I can’t find a library function that maps a Dictionary to another Dictionary, transforming the keys and/or values. The only method I’ve found is the regular map(), which results in an Array. Does this just not exist in the standard library?

--
main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr@gmail.com>*/


(Zhao Xin) #3

I don''t quite understand your question. In Swift, Dictionaries are
structs. You can always use `let dict2 = dict1`. That is enough. Or did you
mean below code?

var dict1 = ...
var dict2 = ...
for (key, value) in dic1 {
    dic2.updateValue(value, forKey:key)
}

Zhaoxin

···

On Tue, Aug 30, 2016 at 4:36 AM, Jens Alfke via swift-users < swift-users@swift.org> wrote:

I may be missing something obvious, but I can’t find a library function
that maps a Dictionary to another Dictionary, transforming the keys and/or
values. The only method I’ve found is the regular map(), which results in
an Array. Does this just not exist in the standard library?

—Jens
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Jens Alfke) #4

I’m talking about a function that does for Dictionaries what map() does for Arrays: it transforms every key and value in the input Dictionary (through a caller-provided function), producing a new Dictionary.

You could use this to take a dictionary [String:String] that maps user IDs to product IDs, and produce a dictionary [User:Product].
Or you could invert a dictionary (swapping keys and values.)

—Jens

···

On Aug 29, 2016, at 2:07 PM, Zhao Xin <owenzx@gmail.com> wrote:

I don''t quite understand your question. In Swift, Dictionaries are structs. You can always use `let dict2 = dict1`.


(Lou Zell) #5

I’m talking about a function that does for Dictionaries what map() does
for Arrays: it transforms every key and value in the input Dictionary
(through a caller-provided function), producing a new Dictionary.

You could use this to take a dictionary [String:String] that maps user IDs
to product IDs, and produce a dictionary [User:Product].
Or you could invert a dictionary (swapping keys and values.)

Jens, this is untested outside of the repl, but maybe it will help:

extension Dictionary {
  func dictMap<U, V>(closure: @noescape (k: Key, v: Value) -> (U, V)) ->
[U:V] {
    var ret = [U:V]()
    for (k0, v0) in self {
      let (k1, v1) = closure(k: k0, v: v0)
      ret[k1] = v1
    }
    return ret
  }
}
let mapped = [1: 1, 2: 2].dictMap() { (k,v) in return (String(k),
String(v)) }
print(mapped)
// Prints:
// ["2": "2", "1": "1"]

Lou


(Zhao Xin) #6

Jens, I see now. I believe this is talked months ago. There is no directly
function at the moment. You can do:

var dict1:[String:String] = ...
var dict2: [User:Product] = [:]
_ = dict1.map {
    ....
    dict2.updateValue(value, forKey:key)

    return nil
}

// use dict2

It is ugly, but it works. However, some people thought this was not a good
use of map. And they make an extension of Dictionary themselves.

You can search it in [swift-user], [swift-evolution] if you can't find it
in [swift-user].

Zhaoxin

···

On Tue, Aug 30, 2016 at 5:52 AM, Jens Alfke <jens@mooseyard.com> wrote:

> On Aug 29, 2016, at 2:07 PM, Zhao Xin <owenzx@gmail.com> wrote:
>
> I don''t quite understand your question. In Swift, Dictionaries are
structs. You can always use `let dict2 = dict1`.

I’m talking about a function that does for Dictionaries what map() does
for Arrays: it transforms every key and value in the input Dictionary
(through a caller-provided function), producing a new Dictionary.

You could use this to take a dictionary [String:String] that maps user IDs
to product IDs, and produce a dictionary [User:Product].
Or you could invert a dictionary (swapping keys and values.)

—Jens


(Jens Alfke) #7

...
_ = dict1.map {

I just use a `for` loop to iterate dict1, and inside it populate dict2. Using `map` here seems wasteful since it’s building an array that’s thrown away.

    dict2.updateValue(value, forKey:key)

This is a tangent, but: Why do you use this syntax instead of the more readable `dict2[key] = value`? (I assume they’re equivalent internally.)

—Jens

···

On Aug 29, 2016, at 4:38 PM, Zhao Xin <owenzx@gmail.com> wrote:


(Zhao Xin) #8

I just thought updateValue(:,:slight_smile: is more natural than subscript function in
Dictionary.

For example, if there is no key in the dictionary, both `dict2[key] =
value` and `dict2.updateValue(value, forKey:key)` will add a new key-value
pair. However, the subscript function does it by adding something on both
side of the equation mark, which seems not natural to me. So I prefer to
use updateValue(:,:slight_smile: function instead.

Besides, updateValue(:,:slight_smile: function can use potential conversions before
Swift 3.0, which makes it more convenient.
https://lists.swift.org/pipermail/swift-users/Week-of-Mon-20160523/002021.html

Zhaoxin

···

On Tue, Aug 30, 2016 at 8:13 AM, Jens Alfke <jens@mooseyard.com> wrote:

On Aug 29, 2016, at 4:38 PM, Zhao Xin <owenzx@gmail.com> wrote:
...

_ = dict1.map {

I just use a `for` loop to iterate dict1, and inside it populate dict2.
Using `map` here seems wasteful since it’s building an array that’s thrown
away.

    dict2.updateValue(value, forKey:key)

This is a tangent, but: Why do you use this syntax instead of the more
readable `dict2[key] = value`? (I assume they’re equivalent internally.)

—Jens