Dictionary as a generic element of Array
up vote
1
down vote
favorite
I have an extension for an Array:
extension Array where Element == [String:Double] {
func values (keyOrder : [String]) -> [[Double]] {
return self.map { element in
return (0..<keyOrder.count).compactMap {element[keyOrder[$0]]}
}
}
}
It works pretty well, but only if Dictionary Key is String and Value is Double. I can imagine this function could work exactly same way for Dictionary of any types, like [AnyHashable:Any] but I have no clue how to define header, is it possible?
swift generics
add a comment |
up vote
1
down vote
favorite
I have an extension for an Array:
extension Array where Element == [String:Double] {
func values (keyOrder : [String]) -> [[Double]] {
return self.map { element in
return (0..<keyOrder.count).compactMap {element[keyOrder[$0]]}
}
}
}
It works pretty well, but only if Dictionary Key is String and Value is Double. I can imagine this function could work exactly same way for Dictionary of any types, like [AnyHashable:Any] but I have no clue how to define header, is it possible?
swift generics
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have an extension for an Array:
extension Array where Element == [String:Double] {
func values (keyOrder : [String]) -> [[Double]] {
return self.map { element in
return (0..<keyOrder.count).compactMap {element[keyOrder[$0]]}
}
}
}
It works pretty well, but only if Dictionary Key is String and Value is Double. I can imagine this function could work exactly same way for Dictionary of any types, like [AnyHashable:Any] but I have no clue how to define header, is it possible?
swift generics
I have an extension for an Array:
extension Array where Element == [String:Double] {
func values (keyOrder : [String]) -> [[Double]] {
return self.map { element in
return (0..<keyOrder.count).compactMap {element[keyOrder[$0]]}
}
}
}
It works pretty well, but only if Dictionary Key is String and Value is Double. I can imagine this function could work exactly same way for Dictionary of any types, like [AnyHashable:Any] but I have no clue how to define header, is it possible?
swift generics
swift generics
asked 14 hours ago
Łukasz
253113
253113
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
One useful trick you can use in situations like this is to move the where
clause from the extension declaration to the method declaration. This allows you to introduce new generic placeholders for the nested dictionary's Key
and Value
placeholder types:
extension Array {
func nestedValues<Key, Value>(orderedBy keys: [Key]) -> [[Value]] where Element == [Key: Value] {
return map { element in
return keys.compactMap { element[$0] }
}
}
}
Wonderful! No one-time used protocol! Kiss!
– Łukasz
10 hours ago
add a comment |
up vote
-1
down vote
Use can use value of dictionary double as an Any Type. You can try below code.
extension Array where Element == [String: Any] {
func values (keyOrder : [String]) -> [[Any]] {
return self.map { element in
return (0..<keyOrder.count).compactMap {element[keyOrder[$0]]}
}
}
}
But what to do with Key?
– Łukasz
14 hours ago
You can't set key of different type in single Dictionary. You have to explicitly casting keys into String. like int a = 10; dict["(a)"] = "Set Key as an String from Int". (map["group_id"], TransformOf<String, Int>(fromJSON: { String($0!) }, toJSON: { $0.map { Int($0)! } }))
– Anand Verma
13 hours ago
I don't understand you. I can have different Dictionaries, key must me hashable. [AnyHashable:Any]. But I don't know is it possible to define them as a type in extension header to use them inside a function to have an output same type as is defined as Element Dictionary value.
– Łukasz
13 hours ago
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
One useful trick you can use in situations like this is to move the where
clause from the extension declaration to the method declaration. This allows you to introduce new generic placeholders for the nested dictionary's Key
and Value
placeholder types:
extension Array {
func nestedValues<Key, Value>(orderedBy keys: [Key]) -> [[Value]] where Element == [Key: Value] {
return map { element in
return keys.compactMap { element[$0] }
}
}
}
Wonderful! No one-time used protocol! Kiss!
– Łukasz
10 hours ago
add a comment |
up vote
2
down vote
accepted
One useful trick you can use in situations like this is to move the where
clause from the extension declaration to the method declaration. This allows you to introduce new generic placeholders for the nested dictionary's Key
and Value
placeholder types:
extension Array {
func nestedValues<Key, Value>(orderedBy keys: [Key]) -> [[Value]] where Element == [Key: Value] {
return map { element in
return keys.compactMap { element[$0] }
}
}
}
Wonderful! No one-time used protocol! Kiss!
– Łukasz
10 hours ago
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
One useful trick you can use in situations like this is to move the where
clause from the extension declaration to the method declaration. This allows you to introduce new generic placeholders for the nested dictionary's Key
and Value
placeholder types:
extension Array {
func nestedValues<Key, Value>(orderedBy keys: [Key]) -> [[Value]] where Element == [Key: Value] {
return map { element in
return keys.compactMap { element[$0] }
}
}
}
One useful trick you can use in situations like this is to move the where
clause from the extension declaration to the method declaration. This allows you to introduce new generic placeholders for the nested dictionary's Key
and Value
placeholder types:
extension Array {
func nestedValues<Key, Value>(orderedBy keys: [Key]) -> [[Value]] where Element == [Key: Value] {
return map { element in
return keys.compactMap { element[$0] }
}
}
}
answered 10 hours ago
Hamish
43.4k797147
43.4k797147
Wonderful! No one-time used protocol! Kiss!
– Łukasz
10 hours ago
add a comment |
Wonderful! No one-time used protocol! Kiss!
– Łukasz
10 hours ago
Wonderful! No one-time used protocol! Kiss!
– Łukasz
10 hours ago
Wonderful! No one-time used protocol! Kiss!
– Łukasz
10 hours ago
add a comment |
up vote
-1
down vote
Use can use value of dictionary double as an Any Type. You can try below code.
extension Array where Element == [String: Any] {
func values (keyOrder : [String]) -> [[Any]] {
return self.map { element in
return (0..<keyOrder.count).compactMap {element[keyOrder[$0]]}
}
}
}
But what to do with Key?
– Łukasz
14 hours ago
You can't set key of different type in single Dictionary. You have to explicitly casting keys into String. like int a = 10; dict["(a)"] = "Set Key as an String from Int". (map["group_id"], TransformOf<String, Int>(fromJSON: { String($0!) }, toJSON: { $0.map { Int($0)! } }))
– Anand Verma
13 hours ago
I don't understand you. I can have different Dictionaries, key must me hashable. [AnyHashable:Any]. But I don't know is it possible to define them as a type in extension header to use them inside a function to have an output same type as is defined as Element Dictionary value.
– Łukasz
13 hours ago
add a comment |
up vote
-1
down vote
Use can use value of dictionary double as an Any Type. You can try below code.
extension Array where Element == [String: Any] {
func values (keyOrder : [String]) -> [[Any]] {
return self.map { element in
return (0..<keyOrder.count).compactMap {element[keyOrder[$0]]}
}
}
}
But what to do with Key?
– Łukasz
14 hours ago
You can't set key of different type in single Dictionary. You have to explicitly casting keys into String. like int a = 10; dict["(a)"] = "Set Key as an String from Int". (map["group_id"], TransformOf<String, Int>(fromJSON: { String($0!) }, toJSON: { $0.map { Int($0)! } }))
– Anand Verma
13 hours ago
I don't understand you. I can have different Dictionaries, key must me hashable. [AnyHashable:Any]. But I don't know is it possible to define them as a type in extension header to use them inside a function to have an output same type as is defined as Element Dictionary value.
– Łukasz
13 hours ago
add a comment |
up vote
-1
down vote
up vote
-1
down vote
Use can use value of dictionary double as an Any Type. You can try below code.
extension Array where Element == [String: Any] {
func values (keyOrder : [String]) -> [[Any]] {
return self.map { element in
return (0..<keyOrder.count).compactMap {element[keyOrder[$0]]}
}
}
}
Use can use value of dictionary double as an Any Type. You can try below code.
extension Array where Element == [String: Any] {
func values (keyOrder : [String]) -> [[Any]] {
return self.map { element in
return (0..<keyOrder.count).compactMap {element[keyOrder[$0]]}
}
}
}
answered 14 hours ago
Anand Verma
25026
25026
But what to do with Key?
– Łukasz
14 hours ago
You can't set key of different type in single Dictionary. You have to explicitly casting keys into String. like int a = 10; dict["(a)"] = "Set Key as an String from Int". (map["group_id"], TransformOf<String, Int>(fromJSON: { String($0!) }, toJSON: { $0.map { Int($0)! } }))
– Anand Verma
13 hours ago
I don't understand you. I can have different Dictionaries, key must me hashable. [AnyHashable:Any]. But I don't know is it possible to define them as a type in extension header to use them inside a function to have an output same type as is defined as Element Dictionary value.
– Łukasz
13 hours ago
add a comment |
But what to do with Key?
– Łukasz
14 hours ago
You can't set key of different type in single Dictionary. You have to explicitly casting keys into String. like int a = 10; dict["(a)"] = "Set Key as an String from Int". (map["group_id"], TransformOf<String, Int>(fromJSON: { String($0!) }, toJSON: { $0.map { Int($0)! } }))
– Anand Verma
13 hours ago
I don't understand you. I can have different Dictionaries, key must me hashable. [AnyHashable:Any]. But I don't know is it possible to define them as a type in extension header to use them inside a function to have an output same type as is defined as Element Dictionary value.
– Łukasz
13 hours ago
But what to do with Key?
– Łukasz
14 hours ago
But what to do with Key?
– Łukasz
14 hours ago
You can't set key of different type in single Dictionary. You have to explicitly casting keys into String. like int a = 10; dict["(a)"] = "Set Key as an String from Int". (map["group_id"], TransformOf<String, Int>(fromJSON: { String($0!) }, toJSON: { $0.map { Int($0)! } }))
– Anand Verma
13 hours ago
You can't set key of different type in single Dictionary. You have to explicitly casting keys into String. like int a = 10; dict["(a)"] = "Set Key as an String from Int". (map["group_id"], TransformOf<String, Int>(fromJSON: { String($0!) }, toJSON: { $0.map { Int($0)! } }))
– Anand Verma
13 hours ago
I don't understand you. I can have different Dictionaries, key must me hashable. [AnyHashable:Any]. But I don't know is it possible to define them as a type in extension header to use them inside a function to have an output same type as is defined as Element Dictionary value.
– Łukasz
13 hours ago
I don't understand you. I can have different Dictionaries, key must me hashable. [AnyHashable:Any]. But I don't know is it possible to define them as a type in extension header to use them inside a function to have an output same type as is defined as Element Dictionary value.
– Łukasz
13 hours ago
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53371946%2fdictionary-as-a-generic-element-of-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown